Show
Ignore:
Timestamp:
08/25/07 22:49:09 (15 months ago)
Author:
krobillard
Message:

UTF-8 strings can now be read.
ur_makeString() now takes UCell argument.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/thune/thread_safe/encoding.c

    r447 r453  
    11/*============================================================================ 
    22    Urlan Interpreter 
    3     Copyright (C) 2005-2006  Karl Robillard 
     3    Copyright (C) 2005-2007  Karl Robillard 
    44 
    55    This library is free software; you can redistribute it and/or 
     
    3131   Returns number of characters copied. 
    3232*/ 
    33 int copyUtf16ToAscii( char* dest, const uint16_t* src, int len ) 
     33int copyUcs2ToUtf8( char* dest, const uint16_t* src, int len ) 
     34{ 
     35    // TODO: Prevent overflow of dest. 
     36    const char* start; 
     37    const uint16_t* end; 
     38    uint16_t c; 
     39 
     40    start = dest; 
     41    end = src + len; 
     42 
     43    while( src != end ) 
     44    { 
     45        c = *src++; 
     46        if( c > 127 ) 
     47        { 
     48            if( c > 0x07ff ) 
     49            { 
     50                *dest++ = 0xE0 | (c >> 12); 
     51                *dest++ = 0x80 | ((c >> 6) & 0x3f); 
     52                c = 0x80 | (c & 0x3f); 
     53            } 
     54            else 
     55            { 
     56                *dest++ = 0xC0 | (c >> 6); 
     57                c = 0x80 | (c & 0x3f); 
     58            } 
     59        } 
     60        *dest++ = (char) c; 
     61    } 
     62    return dest - start; 
     63} 
     64 
     65 
     66#if 0 
     67int copyUcs2ToAscii( char* dest, const uint16_t* src, int len ) 
    3468{ 
    3569    const uint16_t* end; 
     
    4781    return len; 
    4882} 
     83#endif 
    4984 
    5085 
    51 #ifndef UR_CONFIG_UDS 
    5286/* 
    5387   Returns number of characters copied. 
     
    87121                    case UR_ENC_UTF16: 
    88122                        count >>= 1; 
    89                         strN = ur_makeBinary( count, &bin );  
    90                         bin->used = copyUtf16ToAscii( bin->ptr.c, 
    91                                                       (uint16_t*) cpA, count ); 
     123                        strN = ur_makeBinary( count * 2, &bin );  
     124                        bin->used = copyUcs2ToUtf8( bin->ptr.c, 
     125                                                    (uint16_t*) cpA, count ); 
    92126                        enc = UR_ENC_ASCII; 
    93127                        goto set_result; 
     
    156190    } 
    157191} 
    158 #endif 
    159192 
    160193