Mercurial > projects > dwt-linux
comparison dwt/dwthelper/utils.d @ 307:9995e4a9d2ee
sync with dwt-win
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 21 Aug 2008 17:34:04 +0200 |
parents | 1e93ff891d85 |
children | 7ca3f26319f1 9f01a0643e26 |
comparison
equal
deleted
inserted
replaced
306:9764f08379f2 | 307:9995e4a9d2ee |
---|---|
195 return tango.text.convert.Integer.toString(i); | 195 return tango.text.convert.Integer.toString(i); |
196 } | 196 } |
197 | 197 |
198 public static int parseInt( String s, int radix ){ | 198 public static int parseInt( String s, int radix ){ |
199 try{ | 199 try{ |
200 return tango.text.convert.Integer.parse( s, cast(uint)radix ); | 200 return tango.text.convert.Integer.toLong( s, radix ); |
201 } | 201 } |
202 catch( IllegalArgumentException e ){ | 202 catch( IllegalArgumentException e ){ |
203 throw new NumberFormatException( e ); | 203 throw new NumberFormatException( e ); |
204 } | 204 } |
205 } | 205 } |
206 | 206 |
207 public static int parseInt( String s ){ | 207 public static int parseInt( String s ){ |
208 try{ | 208 try{ |
209 return tango.text.convert.Integer.parse( s ); | 209 return tango.text.convert.Integer.toLong( s ); |
210 } | 210 } |
211 catch( IllegalArgumentException e ){ | 211 catch( IllegalArgumentException e ){ |
212 throw new NumberFormatException( e ); | 212 throw new NumberFormatException( e ); |
213 } | 213 } |
214 } | 214 } |
370 res+=4; | 370 res+=4; |
371 } | 371 } |
372 } | 372 } |
373 return res; | 373 return res; |
374 } | 374 } |
375 | |
376 /++ | |
377 + | |
378 +/ | |
375 int indexToCodepointIndex( String str, int index ){ | 379 int indexToCodepointIndex( String str, int index ){ |
376 if( index < 0 ) return index; | 380 if( index < 0 ) return index; |
377 int i = 0; | 381 int i = 0; |
378 int res = 0; | 382 int res = 0; |
379 while( i < index ){ | 383 while( i < index ){ |
395 res++; | 399 res++; |
396 } | 400 } |
397 return res; | 401 return res; |
398 } | 402 } |
399 | 403 |
404 /++ | |
405 + Get that String, that contains the next codepoint of a String. | |
406 +/ | |
400 String firstCodePointStr( String str, out int consumed ){ | 407 String firstCodePointStr( String str, out int consumed ){ |
401 dchar[1] buf; | 408 dchar[1] buf; |
402 uint ate; | 409 uint ate; |
403 dchar[] res = str.toString32( buf, &ate ); | 410 dchar[] res = str.toString32( buf, &ate ); |
404 consumed = ate; | 411 consumed = ate; |
405 return str[ 0 .. ate ]; | 412 return str[ 0 .. ate ]; |
406 } | 413 } |
407 | 414 |
415 /++ | |
416 + Get first codepoint of a String. If an offset is needed, simply use a slice: | |
417 + --- | |
418 + dchar res = str[ offset .. $ ].firstCodePoint(); | |
419 + --- | |
420 +/ | |
408 dchar firstCodePoint( String str ){ | 421 dchar firstCodePoint( String str ){ |
409 int dummy; | 422 int dummy; |
410 return firstCodePoint( str, dummy ); | 423 return firstCodePoint( str, dummy ); |
411 } | 424 } |
412 dchar firstCodePoint( String str, out int consumed ){ | 425 dchar firstCodePoint( String str, out int consumed ){ |
419 } | 432 } |
420 assert( ate > 0 ); | 433 assert( ate > 0 ); |
421 assert( res.length is 1 ); | 434 assert( res.length is 1 ); |
422 return res[0]; | 435 return res[0]; |
423 } | 436 } |
437 dchar firstCodePoint( wchar[] str, out int consumed ){ | |
438 dchar[1] buf; | |
439 uint ate; | |
440 dchar[] res = str.toString32( buf, &ate ); | |
441 consumed = ate; | |
442 if( ate is 0 || res.length is 0 ){ | |
443 Trace.formatln( "dwthelper.utils {}: str.length={} str={:X2}", __LINE__, str.length, cast(ubyte[])str ); | |
444 } | |
445 assert( ate > 0 ); | |
446 assert( res.length is 1 ); | |
447 return res[0]; | |
448 } | |
424 | 449 |
425 String dcharToString( dchar key ){ | 450 String dcharToString( dchar key ){ |
426 dchar[1] buf; | 451 dchar[1] buf; |
427 buf[0] = key; | 452 buf[0] = key; |
428 return tango.text.convert.Utf.toString( buf ); | 453 return tango.text.convert.Utf.toString( buf ); |
438 | 463 |
439 alias tango.text.convert.Utf.toString16 toString16; | 464 alias tango.text.convert.Utf.toString16 toString16; |
440 alias tango.text.convert.Utf.toString toString; | 465 alias tango.text.convert.Utf.toString toString; |
441 | 466 |
442 int getRelativeCodePointOffset( String str, int startIndex, int searchRelCp ){ | 467 int getRelativeCodePointOffset( String str, int startIndex, int searchRelCp ){ |
468 return getAbsoluteCodePointOffset( str, startIndex, searchRelCp ) - startIndex; | |
469 } | |
470 int getAbsoluteCodePointOffset( String str, int startIndex, int searchRelCp ){ | |
443 int ignore; | 471 int ignore; |
444 int i = startIndex; | 472 int i = startIndex; |
445 if( searchRelCp > 0 ){ | 473 if( searchRelCp > 0 ){ |
446 while( searchRelCp !is 0 ){ | 474 while( searchRelCp !is 0 ){ |
447 | 475 |
481 else if( searchRelCp < 0 ){ | 509 else if( searchRelCp < 0 ){ |
482 while( searchRelCp !is 0 ){ | 510 while( searchRelCp !is 0 ){ |
483 do{ | 511 do{ |
484 i--; | 512 i--; |
485 if( i < 0 ){ | 513 if( i < 0 ){ |
486 return -1; | 514 return startIndex-1; |
487 //Trace.formatln( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp ); | 515 //Trace.formatln( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp ); |
488 //tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i ); | 516 //tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i ); |
489 } | 517 } |
490 } while(( str[i] & 0xC0 ) is 0x80 ); | 518 } while(( str[i] & 0xC0 ) is 0x80 ); |
491 searchRelCp++; | 519 searchRelCp++; |
492 } | 520 } |
493 } | 521 } |
494 return i - startIndex; | 522 return i; |
523 } | |
524 int getAbsoluteCodePointOffset( wchar[] str, int startIndex, int searchRelCp ){ | |
525 int ignore; | |
526 int i = startIndex; | |
527 if( searchRelCp > 0 ){ | |
528 while( searchRelCp !is 0 ){ | |
529 | |
530 if( ( i < str.length ) | |
531 && ( str[i] & 0xD800 ) !is 0xD800 ) | |
532 { | |
533 i+=1; | |
534 } | |
535 else if( ( i+1 < str.length ) | |
536 && (( str[i+1] & 0xDC00 ) is 0xDC00 ) | |
537 && (( str[i ] & 0xDC00 ) is 0xD800 )) | |
538 { | |
539 i+=2; | |
540 } | |
541 else{ | |
542 Trace.formatln( "invalid utf8 characters: {:X2}", cast(ubyte[]) str ); | |
543 tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i ); | |
544 } | |
545 searchRelCp--; | |
546 } | |
547 } | |
548 else if( searchRelCp < 0 ){ | |
549 while( searchRelCp !is 0 ){ | |
550 do{ | |
551 i--; | |
552 if( i < 0 ){ | |
553 return startIndex-1; | |
554 //Trace.formatln( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp ); | |
555 //tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i ); | |
556 } | |
557 } while(( str[i] & 0xDC00 ) is 0xDC00 ); | |
558 searchRelCp++; | |
559 } | |
560 } | |
561 return i; | |
495 } | 562 } |
496 dchar getRelativeCodePoint( String str, int startIndex, int searchRelCp, out int relIndex ){ | 563 dchar getRelativeCodePoint( String str, int startIndex, int searchRelCp, out int relIndex ){ |
497 relIndex = getRelativeCodePointOffset( str, startIndex, searchRelCp ); | 564 relIndex = getRelativeCodePointOffset( str, startIndex, searchRelCp ); |
498 int ignore; | 565 int ignore; |
499 return firstCodePoint( str[ startIndex+relIndex .. $ ], ignore ); | 566 return firstCodePoint( str[ startIndex+relIndex .. $ ], ignore ); |