Changeset 177:6bfa783ac0b4

Show
Ignore:
Timestamp:
03/06/08 20:24:13 (6 months ago)
Author:
Frank Benoit <benoit@tionex.de>
branch:
default
Message:

Sync with dwt-linux

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • dwt/dwthelper/utils.d

    r98 r177  
    1414import tango.text.Unicode; 
    1515import tango.text.convert.Utf; 
     16import tango.core.Exception; 
    1617import tango.stdc.stdlib : exit; 
     18 
     19import tango.util.log.Trace; 
    1720 
    1821void implMissing( char[] file, uint line ){ 
     
    4952alias ArrayWrapperT!(char[])  ArrayWrapperString2; 
    5053 
    51 dchar getFirstCodepoint( char[] str ){ 
    52     foreach( dchar d; str ){ 
    53         return d; 
    54     } 
    55 
     54int codepointIndexToIndex( char[] str, int cpIndex ){ 
     55    int cps = cpIndex; 
     56    int res = 0; 
     57    while( cps > 0 ){ 
     58        cps--; 
     59        if( str[res] < 0x80 ){ 
     60            res+=1; 
     61        } 
     62        else if( str[res] < 0xE0 ){ 
     63            res+=2; 
     64        } 
     65        else if( str[res] & 0xF0 ){ 
     66            res+=3; 
     67        } 
     68        else{ 
     69            res+=4; 
     70        } 
     71    } 
     72    return res; 
     73
     74int indexToCodepointIndex( char[] str, int index ){ 
     75    int i = 0; 
     76    int res = 0; 
     77    while( i < index ){ 
     78        if( str[i] < 0x80 ){ 
     79            i+=1; 
     80        } 
     81        else if( str[i] < 0xE0 ){ 
     82            i+=2; 
     83        } 
     84        else if( str[i] & 0xF0 ){ 
     85            i+=3; 
     86        } 
     87        else{ 
     88            i+=4; 
     89        } 
     90        res++; 
     91    } 
     92    return res; 
     93
     94 
     95char[] firstCodePointStr( char[] str, out int consumed ){ 
     96    dchar[1] buf; 
     97    uint ate; 
     98    dchar[] res = str.toString32( buf, &ate ); 
     99    consumed = ate; 
     100    return str[ 0 .. ate ]; 
     101
     102 
     103dchar firstCodePoint( char[] str ){ 
     104    int dummy; 
     105    return firstCodePoint( str, dummy ); 
     106
     107dchar firstCodePoint( char[] str, out int consumed ){ 
     108    dchar[1] buf; 
     109    uint ate; 
     110    dchar[] res = str.toString32( buf, &ate ); 
     111    consumed = ate; 
     112    if( ate is 0 || res.length is 0 ){ 
     113        Trace.formatln( "dwthelper.utils {}: str.length={} str={:X2}", __LINE__, str.length, cast(ubyte[])str ); 
     114    } 
     115    assert( ate > 0 ); 
     116    assert( res.length is 1 ); 
     117    return res[0]; 
     118
     119 
     120char[] dcharToString( dchar key ){ 
     121    dchar[1] buf; 
     122    buf[0] = key; 
     123    return tango.text.convert.Utf.toString( buf ); 
     124
     125 
     126int codepointCount( char[] str ){ 
     127    scope dchar[] buf = new dchar[]( str.length ); 
     128    uint ate; 
     129    dchar[] res = tango.text.convert.Utf.toString32( str, buf, &ate ); 
     130    assert( ate is str.length ); 
     131    return res.length; 
     132
     133 
     134alias tango.text.convert.Utf.toString16 toString16; 
     135alias tango.text.convert.Utf.toString toString; 
     136 
     137int getRelativeCodePointOffset( char[] str, int startIndex, int searchRelCp ){ 
     138    int ignore; 
     139    int i = startIndex; 
     140    if( searchRelCp > 0 ){ 
     141        while( searchRelCp !is 0 ){ 
     142 
     143            if( ( i < str.length ) 
     144                && ( str[i] & 0x80 ) is 0x00 ) 
     145            { 
     146                i+=1; 
     147            } 
     148            else if( ( i+1 < str.length ) 
     149                && (( str[i+1] & 0xC0 ) is 0x80 ) 
     150                && (( str[i  ] & 0xE0 ) is 0xC0 )) 
     151            { 
     152                i+=2; 
     153            } 
     154            else if( ( i+2 < str.length ) 
     155                && (( str[i+2] & 0xC0 ) is 0x80 ) 
     156                && (( str[i+1] & 0xC0 ) is 0x80 ) 
     157                && (( str[i  ] & 0xF0 ) is 0xE0 )) 
     158            { 
     159                i+=3; 
     160            } 
     161            else if(( i+3 < str.length ) 
     162                && (( str[i+3] & 0xC0 ) is 0x80 ) 
     163                && (( str[i+2] & 0xC0 ) is 0x80 ) 
     164                && (( str[i+1] & 0xC0 ) is 0x80 ) 
     165                && (( str[i  ] & 0xF8 ) is 0xF0 )) 
     166            { 
     167                i+=4; 
     168            } 
     169            else{ 
     170                tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i ); 
     171            } 
     172            searchRelCp--; 
     173        } 
     174    } 
     175    else if( searchRelCp < 0 ){ 
     176        while( searchRelCp !is 0 ){ 
     177            do{ 
     178                i--; 
     179                if( i < 0 ){ 
     180                    Trace.formatln( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp ); 
     181                    tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i ); 
     182                } 
     183            } while(( str[i] & 0xC0 ) is 0x80 ); 
     184            searchRelCp++; 
     185        } 
     186    } 
     187    return i - startIndex; 
     188
     189dchar getRelativeCodePoint( char[] str, int startIndex, int searchRelCp, out int relIndex ){ 
     190    relIndex = getRelativeCodePointOffset( str, startIndex, searchRelCp ); 
     191    int ignore; 
     192    return firstCodePoint( str[ startIndex+relIndex .. $ ], ignore ); 
     193
     194 
     195int utf8AdjustOffset( char[] str, int offset ){ 
     196    if( str.length <= offset || offset <= 0 ){ 
     197        return offset; 
     198    } 
     199    while(( str[offset] & 0xC0 ) is 0x80 ){ 
     200        offset--; 
     201    } 
     202    return offset; 
     203
     204 
     205dchar CharacterFirstToLower( char[] str ){ 
     206    int consumed; 
     207    return CharacterFirstToLower( str, consumed ); 
     208
     209dchar CharacterFirstToLower( char[] str, out int consumed ){ 
     210    dchar[1] buf; 
     211    buf[0] = firstCodePoint( str, consumed ); 
     212    dchar[] r = tango.text.Unicode.toLower( buf ); 
     213    return r[0]; 
     214
     215 
    56216dchar CharacterToLower( dchar c ){ 
    57217    dchar[] r = tango.text.Unicode.toLower( [c] ); 
     
    91251} 
    92252 
     253public int lastIndexOf(char[] str, char ch){ 
     254    return lastIndexOf( str, ch, str.length ); 
     255} 
     256public int lastIndexOf(char[] str, char ch, int formIndex){ 
     257    int res = tango.text.Util.locatePrior( str, ch, formIndex ); 
     258    if( res is str.length ) res = -1; 
     259    return res; 
     260} 
     261 
    93262public char[] replace( char[] str, char from, char to ){ 
    94263    return tango.text.Util.replace( str.dup, from, to ); 
     
    130299} 
    131300 
     301public bool equals( char[] src, char[] other ){ 
     302    return src == other; 
     303} 
     304 
     305public bool equalsIgnoreCase( char[] src, char[] other ){ 
     306    return tango.text.Unicode.toFold(src) == tango.text.Unicode.toFold(other); 
     307} 
     308 
    132309public bool startsWith( char[] src, char[] pattern ){ 
    133310    if( src.length < pattern.length ){ 
     
    138315public char[] toLowerCase( char[] src ){ 
    139316    return tango.text.Unicode.toLower( src ); 
     317} 
     318 
     319public hash_t toHash( char[] src ){ 
     320    return typeid(char[]).getHash(&src); 
    140321} 
    141322