Changeset 277:f18872e0f232

Show
Ignore:
Timestamp:
08/04/08 18:54:11 (5 months ago)
Author:
Frank Benoit <benoit@tionex.de>
branch:
default
Message:

unicode utils

Files:

Legend:

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

    r273 r277  
    341341} 
    342342int indexToCodepointIndex( String str, int index ){ 
     343    if( index < 0 ) return index; 
    343344    int i = 0; 
    344345    int res = 0; 
    345346    while( i < index ){ 
     347        if( i >= str.length ){ 
     348            break; 
     349        } 
    346350        if( str[i] < 0x80 ){ 
    347351            i+=1; 
     
    436440            } 
    437441            else{ 
    438                 Trace.formatln( "invalid utf8 characters: {:X2}", cast(ubyte[]) str ); 
     442                Trace.formatln( "invalid utf8 characters: {:X2}", cast(ubyte[]) str ); 
    439443                tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i ); 
    440444            } 
     
    447451                i--; 
    448452                if( i < 0 ){ 
    449                     Trace.formatln( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp ); 
    450                     tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i ); 
     453                    return -1; 
     454                    //Trace.formatln( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp ); 
     455                    //tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i ); 
    451456                } 
    452457            } while(( str[i] & 0xC0 ) is 0x80 ); 
     
    470475    } 
    471476    return offset; 
     477} 
     478int utf8OffsetIncr( String str, int offset ){ 
     479    int res = offset +1; 
     480    if( str.length <= res || res <= 0 ){ 
     481        return res; 
     482    } 
     483    int tries = 4; 
     484    while(( str[res] & 0xC0 ) is 0x80 ){ 
     485        res++; 
     486        assert( tries-- > 0 ); 
     487    } 
     488    return res; 
     489} 
     490int utf8OffsetDecr( String str, int offset ){ 
     491    int res = offset-1; 
     492    if( str.length <= res || res <= 0 ){ 
     493        return res; 
     494    } 
     495    int tries = 4; 
     496    while(( str[res] & 0xC0 ) is 0x80 ){ 
     497        res--; 
     498        assert( tries-- > 0 ); 
     499    } 
     500    Trace.formatln( "utf8OffsetDecr {}->{}", offset, res ); 
     501    Trace.memory( str ); 
     502    return res; 
    472503} 
    473504