Changeset 277:f18872e0f232
- 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
| r273 |
r277 |
|
| 341 | 341 | } |
|---|
| 342 | 342 | int indexToCodepointIndex( String str, int index ){ |
|---|
| | 343 | if( index < 0 ) return index; |
|---|
| 343 | 344 | int i = 0; |
|---|
| 344 | 345 | int res = 0; |
|---|
| 345 | 346 | while( i < index ){ |
|---|
| | 347 | if( i >= str.length ){ |
|---|
| | 348 | break; |
|---|
| | 349 | } |
|---|
| 346 | 350 | if( str[i] < 0x80 ){ |
|---|
| 347 | 351 | i+=1; |
|---|
| … | … | |
| 436 | 440 | } |
|---|
| 437 | 441 | else{ |
|---|
| 438 | | Trace.formatln( "invalid utf8 characters: {:X2}", cast(ubyte[]) str ); |
|---|
| | 442 | Trace.formatln( "invalid utf8 characters: {:X2}", cast(ubyte[]) str ); |
|---|
| 439 | 443 | tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i ); |
|---|
| 440 | 444 | } |
|---|
| … | … | |
| 447 | 451 | i--; |
|---|
| 448 | 452 | 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 ); |
|---|
| 451 | 456 | } |
|---|
| 452 | 457 | } while(( str[i] & 0xC0 ) is 0x80 ); |
|---|
| … | … | |
| 470 | 475 | } |
|---|
| 471 | 476 | return offset; |
|---|
| | 477 | } |
|---|
| | 478 | int 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 | } |
|---|
| | 490 | int 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; |
|---|
| 472 | 503 | } |
|---|
| 473 | 504 | |
|---|