diff dwt/dwthelper/utils.d @ 197:184ab53b7785

Changes and fixes for jface
author Frank Benoit <benoit@tionex.de>
date Thu, 10 Apr 2008 11:19:49 +0200
parents 3afcd4ddcf90
children 3d5dbb27dec2
line wrap: on
line diff
--- a/dwt/dwthelper/utils.d	Mon Apr 07 13:08:57 2008 +0200
+++ b/dwt/dwthelper/utils.d	Thu Apr 10 11:19:49 2008 +0200
@@ -9,14 +9,31 @@
 public import tango.core.Exception : IllegalArgumentException, IOException;
 
 import tango.io.Stdout;
+import tango.io.Print;
 import tango.stdc.stringz;
 static import tango.text.Util;
+static import tango.text.Text;
 import tango.text.Unicode;
 import tango.text.convert.Utf;
 import tango.core.Exception;
 import tango.stdc.stdlib : exit;
 
 import tango.util.log.Trace;
+import tango.text.UnicodeData;
+static import tango.util.collection.model.Seq;
+// static import tango.util.collection.ArraySeq;
+// static import tango.util.collection.LinkSeq;
+// static import tango.util.collection.model.Map;
+// static import tango.util.collection.HashMap;
+//
+// alias tango.util.collection.model.Seq.Seq!(Object) List;
+// alias tango.util.collection.ArraySeq.ArraySeq!(Object) ArrayList;
+// alias tango.util.collection.LinkSeq.LinkSeq!(Object) LinkList;
+// alias tango.util.collection.model.Map.Map!(Object,Object) Map;
+// alias tango.util.collection.HashMap.HashMap!(Object,Object) HashMap;
+
+alias char[] String;
+alias tango.text.Text.Text!(char) StringBuffer;
 
 void implMissing( char[] file, uint line ){
     Stderr.formatln( "implementation missing in file {} line {}", file, line );
@@ -41,17 +58,264 @@
     public this( T data ){
         value = data;
     }
+    public int opEquals( T other ){
+        return value == other;
+    }
+    public int opEquals( Object other ){
+        if( auto o = cast(ValueWrapperT!(T))other ){
+            return value == o.value;
+        }
+        return false;
+    }
+}
+
+class Boolean : ValueWrapperT!(bool) {
+    public static Boolean TRUE;
+    public static Boolean FALSE;
+    public this( bool v ){
+        super(v);
+    }
+
+    alias ValueWrapperT!(bool).opEquals opEquals;
+    public int opEquals( int other ){
+        return value == ( other !is 0 );
+    }
+    public int opEquals( Object other ){
+        if( auto o = cast(Boolean)other ){
+            return value == o.value;
+        }
+        return false;
+    }
+    public bool booleanValue(){
+        return value;
+    }
 }
 
-alias ValueWrapperT!(bool)    ValueWrapperBool;
-alias ValueWrapperT!(int)     ValueWrapperInt;
-alias ValueWrapperT!(long)    ValueWrapperLong;
+alias Boolean    ValueWrapperBool;
+
+
+class Byte : ValueWrapperT!(byte) {
+    public static byte parseByte( char[] s ){
+        try{
+            int res = tango.text.convert.Integer.parse( s );
+            if( res < byte.min || res > byte.max ){
+                throw new NumberFormatException( "out of range" );
+            }
+            return res;
+        }
+        catch( IllegalArgumentException e ){
+            throw new NumberFormatException( e );
+        }
+    }
+    this( byte value ){
+        super( value );
+    }
+}
+alias Byte ValueWrapperByte;
+
+
+class Integer : ValueWrapperT!(int) {
+
+    public static int MIN_VALUE = 0x80000000;
+    public static int MAX_VALUE = 0x7fffffff;
+    public static int SIZE = 32;
+
+    public this ( int value ){
+        super( value );
+    }
+
+    public this ( char[] s ){
+            implMissing( __FILE__, __LINE__ );
+            super(0);
+    }
+
+    public static char[] toString( int i, int radix ){
+        switch( radix ){
+        case 2:
+            return toBinaryString(i);
+        case 8:
+            return toOctalString(i);
+        case 10:
+            return toString(i);
+        case 16:
+            return toHexString(i);
+        default:
+            implMissing( __FILE__, __LINE__ );
+            return null;
+        }
+    }
+
+    public static char[] toHexString( int i ){
+        return tango.text.convert.Integer.toString(i, tango.text.convert.Integer.Style.Hex );
+    }
+
+    public static char[] toOctalString( int i ){
+        return tango.text.convert.Integer.toString(i, tango.text.convert.Integer.Style.Octal );
+    }
+
+    public static char[] toBinaryString( int i ){
+        return tango.text.convert.Integer.toString(i, tango.text.convert.Integer.Style.Binary );
+    }
+
+    public static char[] toString( int i ){
+        return tango.text.convert.Integer.toString(i);
+    }
+
+    public static int parseInt( char[] s, int radix ){
+        try{
+            return tango.text.convert.Integer.parse( s, cast(uint)radix );
+        }
+        catch( IllegalArgumentException e ){
+            throw new NumberFormatException( e );
+        }
+    }
+
+    public static int parseInt( char[] s ){
+        try{
+            return tango.text.convert.Integer.parse( s );
+        }
+        catch( IllegalArgumentException e ){
+            throw new NumberFormatException( e );
+        }
+    }
+
+    public static Integer valueOf( char[] s, int radix ){
+        implMissing( __FILE__, __LINE__ );
+        return null;
+    }
+
+    public static Integer valueOf( char[] s ){
+        implMissing( __FILE__, __LINE__ );
+        return null;
+    }
+
+    public static Integer valueOf( int i ){
+        implMissing( __FILE__, __LINE__ );
+        return null;
+    }
+
+    public byte byteValue(){
+        return cast(byte)value;
+    }
+
+    public short shortValue(){
+        return cast(short)value;
+    }
+
+    public int intValue(){
+        return value;
+    }
+
+    public long longValue(){
+        return cast(long)value;
+    }
+
+    public float floatValue(){
+        return cast(float)value;
+    }
+
+    public double doubleValue(){
+        return cast(double)value;
+    }
+
+    public override  hash_t toHash(){
+        return intValue();
+    }
+
+    public override char[] toString(){
+        return tango.text.convert.Integer.toString( value );
+    }
+}
+alias Integer ValueWrapperInt;
+
+class Double : ValueWrapperT!(double) {
+    this( double value ){
+        super(value);
+    }
+    this( char[] str ){
+        implMissing( __FILE__, __LINE__ );
+        super(0.0);
+    }
+    public double doubleValue(){
+        return value;
+    }
+    public static char[] toString( double value ){
+        implMissing( __FILE__, __LINE__ );
+        return null;
+    }
+}
+
+class Float : ValueWrapperT!(float) {
+
+    public static float POSITIVE_INFINITY = (1.0f / 0.0f);
+    public static float NEGATIVE_INFINITY = ((-1.0f) / 0.0f);
+    public static float NaN = (0.0f / 0.0f);
+    public static float MAX_VALUE = 3.4028235e+38f;
+    public static float MIN_VALUE = 1.4e-45f;
+    public static int SIZE = 32;
+
+    this( float value ){
+        super(value);
+    }
+    this( char[] str ){
+        implMissing( __FILE__, __LINE__ );
+        super(0.0);
+    }
+    public float floatValue(){
+        return value;
+    }
+    public static char[] toString( float value ){
+        implMissing( __FILE__, __LINE__ );
+        return null;
+    }
+    public static float parseFloat( char[] s ){
+        try{
+            return tango.text.convert.Float.toFloat( s );
+        }
+        catch( IllegalArgumentException e ){
+            throw new NumberFormatException( e );
+        }
+    }
+
+}
+class Long : ValueWrapperT!(long) {
+    this( long value ){
+        super(value);
+    }
+    this( char[] str ){
+        implMissing( __FILE__, __LINE__ );
+        super(0);
+    }
+    public long longValue(){
+        return value;
+    }
+    public static long parseLong(char[] s){
+        implMissing( __FILE__, __LINE__ );
+        return 0;
+    }
+    public static char[] toString( double value ){
+        implMissing( __FILE__, __LINE__ );
+        return null;
+    }
+}
+alias Long ValueWrapperLong;
+
+
+// alias ValueWrapperT!(int)     ValueWrapperInt;
+
 alias ArrayWrapperT!(byte)    ArrayWrapperByte;
 alias ArrayWrapperT!(int)     ArrayWrapperInt;
 alias ArrayWrapperT!(Object)  ArrayWrapperObject;
 alias ArrayWrapperT!(char)    ArrayWrapperString;
 alias ArrayWrapperT!(char[])  ArrayWrapperString2;
 
+Object[] StringArrayToObjectArray( String[] strs ){
+    Object[] res = new Object[strs.length];
+    foreach( idx, str; strs ){
+        res[idx] = new ArrayWrapperString(str);
+    }
+    return res;
+}
 int codepointIndexToIndex( char[] str, int cpIndex ){
     int cps = cpIndex;
     int res = 0;
@@ -203,6 +467,9 @@
     return offset;
 }
 
+bool CharacterIsDefined( dchar ch ){
+    return (ch in tango.text.UnicodeData.unicodeData) !is null;
+}
 dchar CharacterFirstToLower( char[] str ){
     int consumed;
     return CharacterFirstToLower( str, consumed );
@@ -228,6 +495,12 @@
 bool CharacterIsDigit( dchar c ){
     return tango.text.Unicode.isDigit( c );
 }
+bool CharacterIsLetter( dchar c ){
+    return tango.text.Unicode.isLetter( c );
+}
+public char[] toUpperCase( char[] str ){
+    return tango.text.Unicode.toUpper( str );
+}
 
 public int indexOf( char[] str, char searched ){
     int res = tango.text.Util.locate( str, searched );
@@ -259,6 +532,14 @@
     if( res is str.length ) res = -1;
     return res;
 }
+public int lastIndexOf(char[] str, char[] ch ){
+    return lastIndexOf( str, ch, str.length );
+}
+public int lastIndexOf(char[] str, char[] ch, int start ){
+    int res = tango.text.Util.locatePatternPrior( str, ch, start );
+    if( res is str.length ) res = -1;
+    return res;
+}
 
 public char[] replace( char[] str, char from, char to ){
     return tango.text.Util.replace( str.dup, from, to );
@@ -313,6 +594,7 @@
     }
     return src[ 0 .. pattern.length ] == pattern;
 }
+
 public char[] toLowerCase( char[] src ){
     return tango.text.Unicode.toLower( src );
 }
@@ -324,6 +606,9 @@
 public char[] trim( char[] str ){
     return tango.text.Util.trim( str ).dup;
 }
+public char[] intern( char[] str ){
+    return str;
+}
 
 public char* toStringzValidPtr( char[] src ){
     if( src ){
@@ -346,6 +631,28 @@
             );
 }
 
+class RuntimeException : Exception {
+    this( char[] e = null){
+        super(e);
+    }
+    this( Exception e ){
+        super(e.toString);
+    }
+}
+class IndexOutOfBoundsException : Exception {
+    this( char[] e = null){
+        super(e);
+    }
+}
+
+class UnsupportedOperationException : RuntimeException {
+    this( char[] e = null){
+        super(e);
+    }
+    this( Exception e ){
+        super(e.toString);
+    }
+}
 class NumberFormatException : IllegalArgumentException {
     this( char[] e ){
         super(e);
@@ -354,6 +661,86 @@
         super(e.toString);
     }
 }
+class NullPointerException : Exception {
+    this( char[] e = null ){
+        super(e);
+    }
+    this( Exception e ){
+        super(e.toString);
+    }
+}
+class IllegalStateException : Exception {
+    this( char[] e = null ){
+        super(e);
+    }
+    this( Exception e ){
+        super(e.toString);
+    }
+}
+class InterruptedException : Exception {
+    this( char[] e = null ){
+        super(e);
+    }
+    this( Exception e ){
+        super(e.toString);
+    }
+}
+class InvocationTargetException : Exception {
+    Exception cause;
+    this( Exception e = null, char[] msg = null ){
+        super(msg);
+        cause = e;
+    }
+
+    alias getCause getTargetException;
+    Exception getCause(){
+        return cause;
+    }
+}
+class MissingResourceException : Exception {
+    char[] classname;
+    char[] key;
+    this( char[] msg, char[] classname, char[] key ){
+        super(msg);
+        this.classname = classname;
+        this.key = key;
+    }
+}
+class ParseException : Exception {
+    this( char[] e = null ){
+        super(e);
+    }
+}
+
+interface Cloneable{
+}
+
+interface Comparable {
+    int compareTo(Object o);
+}
+interface Comparator {
+    int compare(Object o1, Object o2);
+}
+interface EventListener{
+}
+
+class EventObject {
+    protected Object source;
+
+    public this(Object source) {
+        if (source is null)
+        throw new IllegalArgumentException( "null arg" );
+        this.source = source;
+    }
+
+    public Object getSource() {
+        return source;
+    }
+
+    public override char[] toString() {
+        return this.classinfo.name ~ "[source=" ~ source.toString() ~ "]";
+    }
+}
 
 private struct GCStats {
     size_t poolsize;        // total size of pool
@@ -370,5 +757,184 @@
 }
 
 
+void ExceptionPrintStackTrace( Exception e ){
+    ExceptionPrintStackTrace( e, Stderr );
+}
+void ExceptionPrintStackTrace( Exception e, Print!(char) print ){
+    print.formatln( "Exception in {}({}): {}", e.file, e.line, e.msg );
+}
+
+interface Reader{
+}
+interface Writer{
+}
+
+
+class Collator : Comparator {
+    public static Collator getInstance(){
+        implMissing( __FILE__, __LINE__ );
+        return null;
+    }
+    private this(){
+    }
+    int compare(Object o1, Object o2){
+        implMissing( __FILE__, __LINE__ );
+        return 0;
+    }
+}
+
+interface Enumeration {
+    public bool hasMoreElements();
+    public Object nextElement();
+}
+
+
+template arraycast(T) {
+    T[] arraycast(U) (U[] u) {
+        static if (
+            (is (T == interface ) && is (U == interface )) ||
+            (is (T == class ) && is (U == class ))) {
+            return(cast(T[])u);
+        }
+        else {
+            int l = u.length;
+            T[] res;
+            res.length = l;
+            for (int i = 0; i < l; i++) {
+                res[i] = cast(T)u[i];
+            }
+            return(res);
+        }
+    }
+}
+
+char[] stringcast( Object o ){
+    if( auto str = cast(ArrayWrapperString) o ){
+        return str.array;
+    }
+    return null;
+}
+char[][] stringcast( Object[] objs ){
+    char[][] res = new char[][](objs.length);
+    foreach( idx, obj; objs ){
+        res[idx] = stringcast(obj);
+    }
+    return res;
+}
+ArrayWrapperString stringcast( char[] str ){
+    return new ArrayWrapperString( str );
+}
+ArrayWrapperString[] stringcast( char[][] strs ){
+    ArrayWrapperString[] res = new ArrayWrapperString[ strs.length ];
+    foreach( idx, str; strs ){
+        res[idx] = stringcast(str);
+    }
+    return res;
+}
 
 
+bool ArrayEquals(T)( T[] a, T[] b ){
+    if( a.length !is b.length ){
+        return false;
+    }
+    for( int i = 0; i < a.length; i++ ){
+        static if( is( T==class) || is(T==interface)){
+            if( a[i] !is null && b[i] !is null ){
+                if( a[i] != b[i] ){
+                    return false;
+                }
+            }
+            else if( a[i] is null && b[i] is null ){
+            }
+            else{
+                return false;
+            }
+        }
+        else{
+            if( a[i] != b[i] ){
+                return false;
+            }
+        }
+    }
+}
+
+class Arrays{
+    public static bool equals(Object[] a, Object[] b){
+        if( a.length !is b.length ){
+            return false;
+        }
+        for( int i = 0; i < a.length; i++ ){
+            if( a[i] is null && b[i] is null ){
+                continue;
+            }
+            if( a[i] !is null && b[i] !is null && a[i] == b[i] ){
+                continue;
+            }
+            return false;
+        }
+        return true;
+    }
+}
+
+int SeqIndexOf(T)( tango.util.collection.model.Seq.Seq!(T) s, T src ){
+    int idx;
+    foreach( e; s ){
+        if( e == src ){
+            return idx;
+        }
+        idx++;
+    }
+    return -1;
+}
+int arrayIndexOf(T)( T[] arr, T v ){
+    int res = -1;
+    int idx = 0;
+    foreach( p; arr ){
+        if( p == v){
+            res = idx;
+            break;
+        }
+        idx++;
+    }
+    return res;
+}
+
+int seqIndexOf( tango.util.collection.model.Seq.Seq!(Object) seq, Object v ){
+    int res = -1;
+    int idx = 0;
+    foreach( p; seq ){
+        if( p == v){
+            res = idx;
+            break;
+        }
+        idx++;
+    }
+    return res;
+}
+
+void PrintStackTrace(){
+    try{
+        throw new Exception( null );
+    }
+    catch( Exception e ){
+        foreach( msg; e.info ){
+            Trace.formatln( "trc: {}", msg );
+        }
+    }
+}
+
+struct ImportData{
+    void[] data;
+    char[] name;
+
+    public static ImportData opCall( void[] data, char[] name ){
+        ImportData res;
+        res.data = data;
+        res.name = name;
+        return res;
+    }
+}
+
+template getImportData(char[] name ){
+    const ImportData getImportData = ImportData( import(name), name );
+}
\ No newline at end of file