comparison dwt/dwthelper/utils.d @ 361:4bffbf81e2d6

redirect direct prints to DwtLogger
author Frank Benoit <benoit@tionex.de>
date Fri, 20 Mar 2009 21:00:06 +0100
parents 8ebacc5c07dc
children 5f6d9bb33a53
comparison
equal deleted inserted replaced
360:ee1dd551f5b1 361:4bffbf81e2d6
8 public import Math = tango.math.Math; 8 public import Math = tango.math.Math;
9 9
10 public import tango.core.Exception : IllegalArgumentException, IOException; 10 public import tango.core.Exception : IllegalArgumentException, IOException;
11 11
12 import tango.io.Stdout; 12 import tango.io.Stdout;
13 version(TANGOSVN) { 13 import tango.io.stream.Format;
14 import tango.io.stream.Format;
15 alias FormatOutput Print;
16 }
17 else
18 import tango.io.Print;
19
20 static import tango.stdc.stringz; 14 static import tango.stdc.stringz;
21 static import tango.text.Util; 15 static import tango.text.Util;
22 static import tango.text.Text; 16 static import tango.text.Text;
23 static import tango.text.Ascii; 17 static import tango.text.Ascii;
24 import tango.text.Unicode; 18 import tango.text.Unicode;
25 import tango.text.convert.Utf; 19 import tango.text.convert.Utf;
26 import tango.core.Exception; 20 import tango.core.Exception;
27 import tango.stdc.stdlib : exit; 21 import tango.stdc.stdlib : exit;
28 22
29 import tango.util.log.Trace; 23 import tango.util.log.Log;
30 import tango.text.UnicodeData; 24 import tango.text.UnicodeData;
25
26 Logger getDwtLogger(){
27 return Log.lookup( "dwt" );
28 }
31 29
32 alias char[] String; 30 alias char[] String;
33 alias tango.text.Text.Text!(char) StringBuffer; 31 alias tango.text.Text.Text!(char) StringBuffer;
34 32
35 alias ArrayBoundsException ArrayIndexOutOfBoundsException; 33 alias ArrayBoundsException ArrayIndexOutOfBoundsException;
36 34
37 void implMissing( String file, uint line ){ 35 void implMissing( String file, uint line ){
38 Stderr.formatln( "implementation missing in file {} line {}", file, line ); 36 getDwtLogger().fatal( "implementation missing in file {} line {}", file, line );
39 Stderr.formatln( "exiting ..." ); 37 getDwtLogger().fatal( "exiting ..." );
40 exit(1); 38 exit(1);
41 } 39 }
42 40
43 abstract class ArrayWrapper{ 41 abstract class ArrayWrapper{
44 } 42 }
56 } 54 }
57 return false; 55 return false;
58 } 56 }
59 public override hash_t toHash(){ 57 public override hash_t toHash(){
60 return (typeid(T[])).getHash(&array); 58 return (typeid(T[])).getHash(&array);
59 }
60 static if( is( T == char )){
61 public override char[] toString(){
62 return array;
63 }
61 } 64 }
62 } 65 }
63 66
64 class ValueWrapperT(T) : ValueWrapper { 67 class ValueWrapperT(T) : ValueWrapper {
65 public T value; 68 public T value;
447 dchar[1] buf; 450 dchar[1] buf;
448 uint ate; 451 uint ate;
449 dchar[] res = str.toString32( buf, &ate ); 452 dchar[] res = str.toString32( buf, &ate );
450 consumed = ate; 453 consumed = ate;
451 if( ate is 0 || res.length is 0 ){ 454 if( ate is 0 || res.length is 0 ){
452 Trace.formatln( "dwthelper.utils {}: str.length={} str={:X2}", __LINE__, str.length, cast(ubyte[])str ); 455 getDwtLogger().trace( "dwthelper.utils {}: str.length={} str={:X2}", __LINE__, str.length, cast(ubyte[])str );
453 } 456 }
454 assert( ate > 0 ); 457 assert( ate > 0 );
455 assert( res.length is 1 ); 458 assert( res.length is 1 );
456 return res[0]; 459 return res[0];
457 } 460 }
459 dchar[1] buf; 462 dchar[1] buf;
460 uint ate; 463 uint ate;
461 dchar[] res = str.toString32( buf, &ate ); 464 dchar[] res = str.toString32( buf, &ate );
462 consumed = ate; 465 consumed = ate;
463 if( ate is 0 || res.length is 0 ){ 466 if( ate is 0 || res.length is 0 ){
464 Trace.formatln( "dwthelper.utils {}: str.length={} str={:X2}", __LINE__, str.length, cast(ubyte[])str ); 467 getDwtLogger().trace( "dwthelper.utils {}: str.length={} str={:X2}", __LINE__, str.length, cast(ubyte[])str );
465 } 468 }
466 assert( ate > 0 ); 469 assert( ate > 0 );
467 assert( res.length is 1 ); 470 assert( res.length is 1 );
468 return res[0]; 471 return res[0];
469 } 472 }
483 } 486 }
484 487
485 alias tango.text.convert.Utf.toString16 toString16; 488 alias tango.text.convert.Utf.toString16 toString16;
486 alias tango.text.convert.Utf.toString toString; 489 alias tango.text.convert.Utf.toString toString;
487 490
491 int toAbsoluteCodePointStartOffset( String str, int index ){
492 //getDwtLogger().trace( "str={}, str.length={}, index={}", str, str.length, index );
493 //Trace.memory( str );
494 if( str.length is index ){
495 return index;
496 }
497 if( ( str[index] & 0x80 ) is 0x00 ) {
498 return index;
499 }
500 else{
501 int steps = 0;
502 while(( str[index] & 0xC0 ) is 0x80 ){
503 index--;
504 steps++;
505 if( steps > 3 || index < 0 ){
506 break;
507 }
508 }
509 if((( str[index] & 0xE0 ) is 0xC0) && ( steps <= 1 )){
510 // ok
511 }
512 else if((( str[index] & 0xF0 ) is 0xE0) && ( steps <= 2 )){
513 // ok
514 }
515 else if((( str[index] & 0xF8 ) is 0xF0) && ( steps <= 3 )){
516 // ok
517 }
518 else{
519 tango.text.convert.Utf.onUnicodeError( "invalid utf8 input to toAbsoluteCodePointStartOffset" );
520 }
521 return index;
522 }
523 }
488 int getRelativeCodePointOffset( String str, int startIndex, int searchRelCp ){ 524 int getRelativeCodePointOffset( String str, int startIndex, int searchRelCp ){
489 return getAbsoluteCodePointOffset( str, startIndex, searchRelCp ) - startIndex; 525 return getAbsoluteCodePointOffset( str, startIndex, searchRelCp ) - startIndex;
490 } 526 }
491 int getAbsoluteCodePointOffset( String str, int startIndex, int searchRelCp ){ 527 int getAbsoluteCodePointOffset( String str, int startIndex, int searchRelCp ){
528
529 //getDwtLogger().trace( "str={}, str.length={}, startIndex={}, searchRelCp={}", str, str.length, startIndex, searchRelCp );
530 //Trace.memory( str );
531
492 int ignore; 532 int ignore;
493 int i = startIndex; 533 int i = startIndex;
494 if( searchRelCp > 0 ){ 534 if( searchRelCp > 0 ){
495 while( searchRelCp !is 0 ){ 535 while( searchRelCp !is 0 ){
496 536
497 if( ( i < str.length ) 537 if( ( i < str.length )
498 && ( str[i] & 0x80 ) is 0x00 ) 538 && (( str[i] & 0x80 ) is 0x00 ))
499 { 539 {
500 i+=1; 540 i+=1;
501 } 541 }
502 else if( ( i+1 < str.length ) 542 else if( ( i+1 < str.length )
503 && (( str[i+1] & 0xC0 ) is 0x80 ) 543 && (( str[i+1] & 0xC0 ) is 0x80 )
519 && (( str[i ] & 0xF8 ) is 0xF0 )) 559 && (( str[i ] & 0xF8 ) is 0xF0 ))
520 { 560 {
521 i+=4; 561 i+=4;
522 } 562 }
523 else{ 563 else{
524 Trace.formatln( "invalid utf8 characters: {:X2}", cast(ubyte[]) str ); 564 getDwtLogger().trace( "getAbsoluteCodePointOffset invalid utf8 characters: {:X2}", cast(ubyte[]) str );
525 tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i ); 565 tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i );
526 } 566 }
527 searchRelCp--; 567 searchRelCp--;
528 } 568 }
529 } 569 }
531 while( searchRelCp !is 0 ){ 571 while( searchRelCp !is 0 ){
532 do{ 572 do{
533 i--; 573 i--;
534 if( i < 0 ){ 574 if( i < 0 ){
535 return startIndex-1; 575 return startIndex-1;
536 //Trace.formatln( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp );
537 //tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i );
538 } 576 }
539 } while(( str[i] & 0xC0 ) is 0x80 ); 577 } while(( str[i] & 0xC0 ) is 0x80 );
540 searchRelCp++; 578 searchRelCp++;
541 } 579 }
542 } 580 }
558 && (( str[i ] & 0xDC00 ) is 0xD800 )) 596 && (( str[i ] & 0xDC00 ) is 0xD800 ))
559 { 597 {
560 i+=2; 598 i+=2;
561 } 599 }
562 else{ 600 else{
563 Trace.formatln( "invalid utf8 characters: {:X2}", cast(ubyte[]) str ); 601 getDwtLogger().trace( "invalid utf16 characters: {:X2}", cast(ubyte[]) str );
564 tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i ); 602 tango.text.convert.Utf.onUnicodeError( "invalid utf16 input", i );
565 } 603 }
566 searchRelCp--; 604 searchRelCp--;
567 } 605 }
568 } 606 }
569 else if( searchRelCp < 0 ){ 607 else if( searchRelCp < 0 ){
570 while( searchRelCp !is 0 ){ 608 while( searchRelCp !is 0 ){
571 do{ 609 do{
572 i--; 610 i--;
573 if( i < 0 ){ 611 if( i < 0 ){
574 return startIndex-1; 612 return startIndex-1;
575 //Trace.formatln( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp ); 613 //getDwtLogger().trace( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp );
576 //tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i ); 614 //tango.text.convert.Utf.onUnicodeError( "invalid utf16 input", i );
577 } 615 }
578 } while(( str[i] & 0xDC00 ) is 0xDC00 ); 616 } while(( str[i] & 0xDC00 ) is 0xDC00 );
579 searchRelCp++; 617 searchRelCp++;
580 } 618 }
581 } 619 }
620 int tries = 4; 658 int tries = 4;
621 while(( str[res] & 0xC0 ) is 0x80 ){ 659 while(( str[res] & 0xC0 ) is 0x80 ){
622 res--; 660 res--;
623 assert( tries-- > 0 ); 661 assert( tries-- > 0 );
624 } 662 }
625 Trace.formatln( "utf8OffsetDecr {}->{}", offset, res ); 663 getDwtLogger().trace( "utf8OffsetDecr {}->{}", offset, res );
626 Trace.memory( str ); 664 //Trace.memory( str );
627 return res; 665 return res;
628 } 666 }
629 667
630 class Character { 668 class Character {
631 public static bool isUpperCase( dchar c ){ 669 public static bool isUpperCase( dchar c ){
1038 } 1076 }
1039 1077
1040 void ExceptionPrintStackTrace( Exception e ){ 1078 void ExceptionPrintStackTrace( Exception e ){
1041 ExceptionPrintStackTrace( e, Stderr ); 1079 ExceptionPrintStackTrace( e, Stderr );
1042 } 1080 }
1043 void ExceptionPrintStackTrace( Exception e, Print!(char) print ){ 1081 void ExceptionPrintStackTrace( Exception e, FormatOutput!(char) print ){
1044 Exception exception = e; 1082 Exception exception = e;
1045 while( exception !is null ){ 1083 while( exception !is null ){
1046 print.formatln( "Exception in {}({}): {}", exception.file, exception.line, exception.msg ); 1084 print.formatln( "Exception in {}({}): {}", exception.file, exception.line, exception.msg );
1047 if( exception.info !is null ){ 1085 if( exception.info !is null ){
1048 foreach( msg; exception.info ){ 1086 foreach( msg; exception.info ){
1258 auto e = new Exception( null ); 1296 auto e = new Exception( null );
1259 int idx = 0; 1297 int idx = 0;
1260 const start = 3; 1298 const start = 3;
1261 foreach( msg; e.info ){ 1299 foreach( msg; e.info ){
1262 if( idx >= start && idx < start+deepth ) { 1300 if( idx >= start && idx < start+deepth ) {
1263 Trace.formatln( "{}: {}", prefix, msg ); 1301 getDwtLogger().trace( "{}: {}", prefix, msg );
1264 } 1302 }
1265 idx++; 1303 idx++;
1266 } 1304 }
1267 } 1305 }
1268 1306