Changeset 525 for trunk/thune/series.c

Show
Ignore:
Timestamp:
05/20/08 21:19:55 (7 months ago)
Author:
krobillard
Message:

Change can now poke a big-endian number into a binary.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/thune/series.c

    r522 r525  
    10911091 
    10921092 
    1093 /* 
    1094    Append n to binary in big-endian byte format. 
    1095 */ 
    1096 static void _appendAsBytes( UThread* ut, UCell* bcell, 
    1097                             int32_t n, int byteCount ) 
    1098 { 
    1099     uint8_t* bp; 
    1100     UBinary* bin = ur_bin( bcell ); 
    1101  
    1102     ur_arrayReserve( bin, 1, bin->used + byteCount ); 
    1103     bp = bin->ptr.b + bin->used; 
    1104     bin->used += byteCount; 
    1105  
     1093static void _intToBytes( int n, uint8_t* bp, int byteCount ) 
     1094{ 
    11061095    switch( byteCount ) 
    11071096    { 
     
    11251114            break; 
    11261115    } 
     1116} 
     1117 
     1118 
     1119/* 
     1120   Append n to binary in big-endian byte format. 
     1121*/ 
     1122static void _appendAsBytes( UThread* ut, UCell* bcell, 
     1123                            int32_t n, int byteCount ) 
     1124{ 
     1125    UBinary* bin = ur_bin( bcell ); 
     1126 
     1127    ur_arrayReserve( bin, 1, bin->used + byteCount ); 
     1128    _intToBytes( n, bin->ptr.b + bin->used, byteCount ); 
     1129    bin->used += byteCount; 
     1130 
    11271131} 
    11281132 
     
    25122516*/ 
    25132517static UIndex _changeBytes( UBinary* dst, UIndex di, 
    2514                             UBinary* src, UIndex si, int part ) 
    2515 { 
    2516     int slen = src->used - si; 
     2518                            char* src, int slen, int part ) 
     2519{ 
    25172520    if( slen > 0 ) 
    25182521    { 
     
    25402543 
    25412544        // TODO: Handle overwritting self when dst is src. 
    2542         memCpy( dst->ptr.c + di, 
    2543                 src->ptr.c + si, slen ); 
     2545        assert( di <= dst->used ); 
     2546        memCpy( dst->ptr.c + di, src, slen ); 
    25442547 
    25452548        return newIndex; 
     
    25712574        case UT_STRING: 
    25722575        { 
    2573             UBinary* s1; 
    2574  
    2575             if( ur_is(tos, UT_STRING) ) 
    2576             { 
    2577                 if( ur_encCharSize(tos) != 1 ) 
     2576            UBinary* s1 = ur_bin(a1); 
     2577 
     2578            switch( ur_type(tos) ) 
     2579            { 
     2580                case UT_CHAR: 
     2581                case UT_INT: 
     2582                { 
     2583                    uint8_t bytes[ 4 ]; 
     2584 
     2585                    if( part < 1 ) 
     2586                        part = 1; 
     2587                    else if( part > 4 ) 
     2588                        goto type_err; 
     2589 
     2590                    _intToBytes( ur_int(tos), bytes, part ); 
     2591                    a1->series.it = _changeBytes( s1, a1->series.it, 
     2592                                                  (char*) bytes, part, 0 ); 
     2593                } 
     2594                    break; 
     2595 
     2596                case UT_STRING: 
     2597                    if( ur_encCharSize(tos) != 1 ) 
     2598                        goto type_err; 
     2599                    // Fall through... 
     2600 
     2601                case UT_BINARY: 
     2602                { 
     2603                    UBinary* s2 = ur_bin(tos); 
     2604                    a1->series.it = _changeBytes( s1, a1->series.it, 
     2605                                        s2->ptr.c + tos->series.it, 
     2606                                        ur_sliceEnd(tos, s2) - tos->series.it, 
     2607                                        part ); 
     2608                } 
     2609                    break; 
     2610 
     2611                default: 
    25782612                    goto type_err; 
    25792613            } 
    2580             else if( ! ur_is(tos, UT_BINARY) ) 
    2581             { 
    2582                 goto type_err; 
    2583             } 
    2584  
    2585             s1 = ur_bin(a1); 
    2586             assert( a1->series.it <= s1->used ); 
    2587  
    2588             if( ur_is(tos, UT_CHAR) ) 
    2589             { 
    2590                 if( a1->series.it == s1->used ) 
    2591                 { 
    2592                     ur_arrayReserve( s1, sizeof(char), 1 ); 
    2593                     ++s1->used; 
    2594                 } 
    2595                 s1->ptr.b[ a1->series.it ] = ur_char(tos); 
    2596                 a1->series.it += 1; 
    2597             } 
    2598             else 
    2599             { 
    2600                 a1->series.it = _changeBytes( s1, a1->series.it, 
    2601                                               ur_bin(tos), tos->series.it, 
    2602                                               part ); 
    2603             } 
     2614 
    26042615            UR_S_DROP; 
    26052616        }