diff 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
line wrap: on
line diff
--- a/dwt/dwthelper/utils.d	Fri Mar 20 20:30:32 2009 +0100
+++ b/dwt/dwthelper/utils.d	Fri Mar 20 21:00:06 2009 +0100
@@ -10,13 +10,7 @@
 public import tango.core.Exception : IllegalArgumentException, IOException;
 
 import tango.io.Stdout;
-version(TANGOSVN) {
-    import tango.io.stream.Format;
-    alias FormatOutput Print;
-}
-else
-    import tango.io.Print;
-
+import tango.io.stream.Format;
 static import tango.stdc.stringz;
 static import tango.text.Util;
 static import tango.text.Text;
@@ -26,17 +20,21 @@
 import tango.core.Exception;
 import tango.stdc.stdlib : exit;
 
-import tango.util.log.Trace;
+import tango.util.log.Log;
 import tango.text.UnicodeData;
 
+Logger getDwtLogger(){
+    return Log.lookup( "dwt" );
+}
+
 alias char[] String;
 alias tango.text.Text.Text!(char) StringBuffer;
 
 alias ArrayBoundsException ArrayIndexOutOfBoundsException;
 
 void implMissing( String file, uint line ){
-    Stderr.formatln( "implementation missing in file {} line {}", file, line );
-    Stderr.formatln( "exiting ..." );
+    getDwtLogger().fatal( "implementation missing in file {} line {}", file, line );
+    getDwtLogger().fatal( "exiting ..." );
     exit(1);
 }
 
@@ -59,6 +57,11 @@
     public override hash_t toHash(){
         return (typeid(T[])).getHash(&array);
     }
+    static if( is( T == char )){
+        public override char[] toString(){
+            return array;
+        }
+    }
 }
 
 class ValueWrapperT(T) : ValueWrapper {
@@ -449,7 +452,7 @@
     dchar[] res = str.toString32( buf, &ate );
     consumed = ate;
     if( ate is 0 || res.length is 0 ){
-        Trace.formatln( "dwthelper.utils {}: str.length={} str={:X2}", __LINE__, str.length, cast(ubyte[])str );
+        getDwtLogger().trace( "dwthelper.utils {}: str.length={} str={:X2}", __LINE__, str.length, cast(ubyte[])str );
     }
     assert( ate > 0 );
     assert( res.length is 1 );
@@ -461,7 +464,7 @@
     dchar[] res = str.toString32( buf, &ate );
     consumed = ate;
     if( ate is 0 || res.length is 0 ){
-        Trace.formatln( "dwthelper.utils {}: str.length={} str={:X2}", __LINE__, str.length, cast(ubyte[])str );
+        getDwtLogger().trace( "dwthelper.utils {}: str.length={} str={:X2}", __LINE__, str.length, cast(ubyte[])str );
     }
     assert( ate > 0 );
     assert( res.length is 1 );
@@ -485,17 +488,54 @@
 alias tango.text.convert.Utf.toString16 toString16;
 alias tango.text.convert.Utf.toString toString;
 
+int toAbsoluteCodePointStartOffset( String str, int index ){
+    //getDwtLogger().trace( "str={}, str.length={}, index={}", str, str.length, index );
+    //Trace.memory( str );
+    if( str.length is index ){
+        return index;
+    }
+    if( ( str[index] & 0x80 ) is 0x00 ) {
+        return index;
+    }
+    else{
+        int steps = 0;
+        while(( str[index] & 0xC0 ) is 0x80 ){
+            index--;
+            steps++;
+            if( steps > 3 || index < 0 ){
+                break;
+            }
+        }
+        if((( str[index] & 0xE0 ) is 0xC0) && ( steps <= 1 )){
+            // ok
+        }
+        else if((( str[index] & 0xF0 ) is 0xE0) && ( steps <= 2 )){
+            // ok
+        }
+        else if((( str[index] & 0xF8 ) is 0xF0) && ( steps <= 3 )){
+            // ok
+        }
+        else{
+            tango.text.convert.Utf.onUnicodeError( "invalid utf8 input to toAbsoluteCodePointStartOffset" );
+        }
+        return index;
+    }
+}
 int getRelativeCodePointOffset( String str, int startIndex, int searchRelCp ){
     return getAbsoluteCodePointOffset( str, startIndex, searchRelCp ) - startIndex;
 }
 int getAbsoluteCodePointOffset( String str, int startIndex, int searchRelCp ){
+
+    //getDwtLogger().trace( "str={}, str.length={}, startIndex={}, searchRelCp={}", str, str.length, startIndex, searchRelCp );
+    //Trace.memory( str );
+
     int ignore;
     int i = startIndex;
     if( searchRelCp > 0 ){
         while( searchRelCp !is 0 ){
 
             if( ( i < str.length )
-                && ( str[i] & 0x80 ) is 0x00 )
+            && (( str[i] & 0x80 ) is 0x00 ))
             {
                 i+=1;
             }
@@ -521,7 +561,7 @@
                 i+=4;
             }
             else{
-                Trace.formatln( "invalid utf8 characters:  {:X2}", cast(ubyte[]) str );
+                getDwtLogger().trace( "getAbsoluteCodePointOffset invalid utf8 characters:  {:X2}", cast(ubyte[]) str );
                 tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i );
             }
             searchRelCp--;
@@ -533,8 +573,6 @@
                 i--;
                 if( i < 0 ){
                     return startIndex-1;
-                    //Trace.formatln( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp );
-                    //tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i );
                 }
             } while(( str[i] & 0xC0 ) is 0x80 );
             searchRelCp++;
@@ -560,8 +598,8 @@
                 i+=2;
             }
             else{
-                Trace.formatln( "invalid utf8 characters:  {:X2}", cast(ubyte[]) str );
-                tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i );
+                getDwtLogger().trace( "invalid utf16 characters:  {:X2}", cast(ubyte[]) str );
+                tango.text.convert.Utf.onUnicodeError( "invalid utf16 input", i );
             }
             searchRelCp--;
         }
@@ -572,8 +610,8 @@
                 i--;
                 if( i < 0 ){
                     return startIndex-1;
-                    //Trace.formatln( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp );
-                    //tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i );
+                    //getDwtLogger().trace( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp );
+                    //tango.text.convert.Utf.onUnicodeError( "invalid utf16 input", i );
                 }
             } while(( str[i] & 0xDC00 ) is 0xDC00 );
             searchRelCp++;
@@ -622,8 +660,8 @@
         res--;
         assert( tries-- > 0 );
     }
-    Trace.formatln( "utf8OffsetDecr {}->{}", offset, res );
-    Trace.memory( str );
+    getDwtLogger().trace( "utf8OffsetDecr {}->{}", offset, res );
+    //Trace.memory( str );
     return res;
 }
 
@@ -1040,7 +1078,7 @@
 void ExceptionPrintStackTrace( Exception e ){
     ExceptionPrintStackTrace( e, Stderr );
 }
-void ExceptionPrintStackTrace( Exception e, Print!(char) print ){
+void ExceptionPrintStackTrace( Exception e, FormatOutput!(char) print ){
     Exception exception = e;
     while( exception !is null ){
         print.formatln( "Exception in {}({}): {}", exception.file, exception.line, exception.msg );
@@ -1260,7 +1298,7 @@
     const start = 3;
     foreach( msg; e.info ){
         if( idx >= start && idx < start+deepth ) {
-            Trace.formatln( "{}: {}", prefix, msg );
+            getDwtLogger().trace( "{}: {}", prefix, msg );
         }
         idx++;
     }