Mercurial > projects > dwt-win
comparison dwt/dwthelper/utils.d @ 305:a401002c3a1f
Sync with dwt-linux
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 08 Sep 2008 01:43:08 +0200 |
parents | 708cbfe418e7 |
children | 745001b1a52c |
comparison
equal
deleted
inserted
replaced
304:4e8e11ccfa60 | 305:a401002c3a1f |
---|---|
2 * Authors: Frank Benoit <keinfarbton@googlemail.com> | 2 * Authors: Frank Benoit <keinfarbton@googlemail.com> |
3 */ | 3 */ |
4 module dwt.dwthelper.utils; | 4 module dwt.dwthelper.utils; |
5 | 5 |
6 public import dwt.dwthelper.System; | 6 public import dwt.dwthelper.System; |
7 public import dwt.dwthelper.Runnable; | |
7 public import Math = tango.math.Math; | 8 public import Math = tango.math.Math; |
8 | 9 |
9 public import tango.core.Exception : IllegalArgumentException, IOException; | 10 public import tango.core.Exception : IllegalArgumentException, IOException; |
10 | 11 |
11 import tango.io.Stdout; | 12 import tango.io.Stdout; |
12 import tango.io.Print; | 13 import tango.io.Print; |
13 static import tango.stdc.stringz; | 14 static import tango.stdc.stringz; |
14 static import tango.text.Util; | 15 static import tango.text.Util; |
15 static import tango.text.Text; | 16 static import tango.text.Text; |
17 static import tango.text.Ascii; | |
16 import tango.text.Unicode; | 18 import tango.text.Unicode; |
17 import tango.text.convert.Utf; | 19 import tango.text.convert.Utf; |
18 import tango.core.Exception; | 20 import tango.core.Exception; |
19 import tango.stdc.stdlib : exit; | 21 import tango.stdc.stdlib : exit; |
20 | 22 |
21 import tango.util.log.Trace; | 23 import tango.util.log.Trace; |
22 import tango.text.UnicodeData; | 24 import tango.text.UnicodeData; |
23 static import tango.util.collection.model.Seq; | |
24 | 25 |
25 alias char[] String; | 26 alias char[] String; |
26 alias tango.text.Text.Text!(char) StringBuffer; | 27 alias tango.text.Text.Text!(char) StringBuffer; |
28 | |
29 alias ArrayBoundsException ArrayIndexOutOfBoundsException; | |
27 | 30 |
28 void implMissing( String file, uint line ){ | 31 void implMissing( String file, uint line ){ |
29 Stderr.formatln( "implementation missing in file {} line {}", file, line ); | 32 Stderr.formatln( "implementation missing in file {} line {}", file, line ); |
30 Stderr.formatln( "exiting ..." ); | 33 Stderr.formatln( "exiting ..." ); |
31 exit(1); | 34 exit(1); |
122 return FALSE; | 125 return FALSE; |
123 } | 126 } |
124 public static Boolean valueOf( bool b ){ | 127 public static Boolean valueOf( bool b ){ |
125 return b ? TRUE : FALSE; | 128 return b ? TRUE : FALSE; |
126 } | 129 } |
130 public static bool getBoolean(String name){ | |
131 return tango.text.Ascii.icompare(System.getProperty(name, "false"), "true" ) is 0; | |
132 } | |
127 } | 133 } |
128 | 134 |
129 alias Boolean ValueWrapperBool; | 135 alias Boolean ValueWrapperBool; |
130 | 136 |
131 | 137 |
143 } | 149 } |
144 } | 150 } |
145 this( byte value ){ | 151 this( byte value ){ |
146 super( value ); | 152 super( value ); |
147 } | 153 } |
154 | |
155 public static String toString( byte i ){ | |
156 return tango.text.convert.Integer.toString(i); | |
157 } | |
158 | |
148 } | 159 } |
149 alias Byte ValueWrapperByte; | 160 alias Byte ValueWrapperByte; |
150 | 161 |
151 | 162 |
152 class Integer : ValueWrapperT!(int) { | 163 class Integer : ValueWrapperT!(int) { |
275 } | 286 } |
276 public static String toString( double value ){ | 287 public static String toString( double value ){ |
277 implMissing( __FILE__, __LINE__ ); | 288 implMissing( __FILE__, __LINE__ ); |
278 return null; | 289 return null; |
279 } | 290 } |
291 public static double parseDouble(String s){ | |
292 implMissing( __FILE__, __LINE__ ); | |
293 return 0.0; | |
294 } | |
280 } | 295 } |
281 | 296 |
282 class Float : ValueWrapperT!(float) { | 297 class Float : ValueWrapperT!(float) { |
283 | 298 |
284 public static float POSITIVE_INFINITY = (1.0f / 0.0f); | 299 public static float POSITIVE_INFINITY = (1.0f / 0.0f); |
558 searchRelCp++; | 573 searchRelCp++; |
559 } | 574 } |
560 } | 575 } |
561 return i; | 576 return i; |
562 } | 577 } |
578 dchar getRelativeCodePoint( String str, int startIndex, int searchRelCp ){ | |
579 int dummy; | |
580 return getRelativeCodePoint( str, startIndex, dummy ); | |
581 } | |
563 dchar getRelativeCodePoint( String str, int startIndex, int searchRelCp, out int relIndex ){ | 582 dchar getRelativeCodePoint( String str, int startIndex, int searchRelCp, out int relIndex ){ |
564 relIndex = getRelativeCodePointOffset( str, startIndex, searchRelCp ); | 583 relIndex = getRelativeCodePointOffset( str, startIndex, searchRelCp ); |
565 int ignore; | 584 int ignore; |
566 return firstCodePoint( str[ startIndex+relIndex .. $ ], ignore ); | 585 return firstCodePoint( str[ startIndex+relIndex .. $ ], ignore ); |
567 } | 586 } |
600 Trace.formatln( "utf8OffsetDecr {}->{}", offset, res ); | 619 Trace.formatln( "utf8OffsetDecr {}->{}", offset, res ); |
601 Trace.memory( str ); | 620 Trace.memory( str ); |
602 return res; | 621 return res; |
603 } | 622 } |
604 | 623 |
624 class Character { | |
625 public static bool isUpperCase( dchar c ){ | |
626 implMissing( __FILE__, __LINE__); | |
627 return false; | |
628 } | |
629 public static dchar toUpperCase( dchar c ){ | |
630 dchar[] r = tango.text.Unicode.toUpper( [c] ); | |
631 return r[0]; | |
632 } | |
633 public static dchar toLowerCase( dchar c ){ | |
634 dchar[] r = tango.text.Unicode.toLower( [c] ); | |
635 return r[0]; | |
636 } | |
637 public static bool isWhitespace( dchar c ){ | |
638 return tango.text.Unicode.isWhitespace( c ); | |
639 } | |
640 public static bool isDigit( dchar c ){ | |
641 return tango.text.Unicode.isDigit( c ); | |
642 } | |
643 public static bool isLetterOrDigit( dchar c ){ | |
644 return isDigit(c) || isLetter(c); | |
645 } | |
646 public static bool isUnicodeIdentifierPart(char ch){ | |
647 implMissing( __FILE__, __LINE__); | |
648 return false; | |
649 } | |
650 public static bool isUnicodeIdentifierStart(char ch){ | |
651 implMissing( __FILE__, __LINE__); | |
652 return false; | |
653 } | |
654 public static bool isIdentifierIgnorable(char ch){ | |
655 implMissing( __FILE__, __LINE__); | |
656 return false; | |
657 } | |
658 public static bool isJavaIdentifierPart(char ch){ | |
659 implMissing( __FILE__, __LINE__); | |
660 return false; | |
661 } | |
662 | |
663 this( char c ){ | |
664 // must be correct for container storage | |
665 implMissing( __FILE__, __LINE__); | |
666 } | |
667 } | |
668 | |
669 String new_String( String cont, int offset, int len ){ | |
670 return cont[ offset .. offset+len ].dup; | |
671 } | |
672 String new_String( String cont ){ | |
673 return cont.dup; | |
674 } | |
675 String String_valueOf( bool v ){ | |
676 return v ? "true" : "false"; | |
677 } | |
678 String String_valueOf( int v ){ | |
679 return tango.text.convert.Integer.toString(v); | |
680 } | |
681 String String_valueOf( long v ){ | |
682 return tango.text.convert.Integer.toString(v); | |
683 } | |
684 String String_valueOf( float v ){ | |
685 return tango.text.convert.Float.toString(v); | |
686 } | |
687 String String_valueOf( double v ){ | |
688 return tango.text.convert.Float.toString(v); | |
689 } | |
690 String String_valueOf( dchar v ){ | |
691 return dcharToString(v); | |
692 } | |
693 String String_valueOf( char[] v ){ | |
694 return v.dup; | |
695 } | |
696 String String_valueOf( char[] v, int offset, int len ){ | |
697 return v[ offset .. offset+len ].dup; | |
698 } | |
699 String String_valueOf( Object v ){ | |
700 return v is null ? "null" : v.toString(); | |
701 } | |
605 bool CharacterIsDefined( dchar ch ){ | 702 bool CharacterIsDefined( dchar ch ){ |
606 return (ch in tango.text.UnicodeData.unicodeData) !is null; | 703 return (ch in tango.text.UnicodeData.unicodeData) !is null; |
607 } | 704 } |
608 dchar CharacterFirstToLower( String str ){ | 705 dchar CharacterFirstToLower( String str ){ |
609 int consumed; | 706 int consumed; |
613 dchar[1] buf; | 710 dchar[1] buf; |
614 buf[0] = firstCodePoint( str, consumed ); | 711 buf[0] = firstCodePoint( str, consumed ); |
615 dchar[] r = tango.text.Unicode.toLower( buf ); | 712 dchar[] r = tango.text.Unicode.toLower( buf ); |
616 return r[0]; | 713 return r[0]; |
617 } | 714 } |
618 | 715 int length( String str ){ |
716 return str.length; | |
717 } | |
619 dchar CharacterToLower( dchar c ){ | 718 dchar CharacterToLower( dchar c ){ |
620 dchar[] r = tango.text.Unicode.toLower( [c] ); | 719 dchar[] r = tango.text.Unicode.toLower( [c] ); |
621 return r[0]; | 720 return r[0]; |
622 } | 721 } |
623 dchar CharacterToUpper( dchar c ){ | 722 dchar CharacterToUpper( dchar c ){ |
633 bool CharacterIsLetter( dchar c ){ | 732 bool CharacterIsLetter( dchar c ){ |
634 return tango.text.Unicode.isLetter( c ); | 733 return tango.text.Unicode.isLetter( c ); |
635 } | 734 } |
636 public String toUpperCase( String str ){ | 735 public String toUpperCase( String str ){ |
637 return tango.text.Unicode.toUpper( str ); | 736 return tango.text.Unicode.toUpper( str ); |
737 } | |
738 | |
739 public String replaceFirst( String str, String regex, String replacement ){ | |
740 implMissing(__FILE__,__LINE__); | |
741 return str; | |
638 } | 742 } |
639 | 743 |
640 public int indexOf( String str, char searched ){ | 744 public int indexOf( String str, char searched ){ |
641 int res = tango.text.Util.locate( str, searched ); | 745 int res = tango.text.Util.locate( str, searched ); |
642 if( res is str.length ) res = -1; | 746 if( res is str.length ) res = -1; |
674 int res = tango.text.Util.locatePatternPrior( str, ch, start ); | 778 int res = tango.text.Util.locatePatternPrior( str, ch, start ); |
675 if( res is str.length ) res = -1; | 779 if( res is str.length ) res = -1; |
676 return res; | 780 return res; |
677 } | 781 } |
678 | 782 |
783 public String replaceAll( String str, String regex, String replacement ){ | |
784 implMissing(__FILE__,__LINE__); | |
785 return null; | |
786 } | |
679 public String replace( String str, char from, char to ){ | 787 public String replace( String str, char from, char to ){ |
680 return tango.text.Util.replace( str.dup, from, to ); | 788 return tango.text.Util.replace( str.dup, from, to ); |
681 } | 789 } |
682 | 790 |
683 public String substring( String str, int start ){ | 791 public String substring( String str, int start ){ |
702 | 810 |
703 public void getChars( String src, int srcBegin, int srcEnd, String dst, int dstBegin){ | 811 public void getChars( String src, int srcBegin, int srcEnd, String dst, int dstBegin){ |
704 dst[ dstBegin .. dstBegin + srcEnd - srcBegin ] = src[ srcBegin .. srcEnd ]; | 812 dst[ dstBegin .. dstBegin + srcEnd - srcBegin ] = src[ srcBegin .. srcEnd ]; |
705 } | 813 } |
706 | 814 |
707 public wchar[] toCharArray( String str ){ | 815 public char[] toCharArray( String str ){ |
708 return toString16( str ); | 816 return str; |
709 } | 817 } |
710 | 818 |
711 public bool endsWith( String src, String pattern ){ | 819 public bool endsWith( String src, String pattern ){ |
712 if( src.length < pattern.length ){ | 820 if( src.length < pattern.length ){ |
713 return false; | 821 return false; |
750 } | 858 } |
751 public String intern( String str ){ | 859 public String intern( String str ){ |
752 return str; | 860 return str; |
753 } | 861 } |
754 | 862 |
863 /++ | |
864 + This is like tango.stdc.stringz.toStringz, but in case of an empty input string, | |
865 + this function returns a pointer to a null value instead of a null ptr. | |
866 +/ | |
755 public char* toStringzValidPtr( String src ){ | 867 public char* toStringzValidPtr( String src ){ |
756 if( src ){ | 868 if( src ){ |
757 return src.toStringz(); | 869 return src.toStringz(); |
758 } | 870 } |
759 else{ | 871 else{ |
859 class ParseException : Exception { | 971 class ParseException : Exception { |
860 this( String e = null ){ | 972 this( String e = null ){ |
861 super(e); | 973 super(e); |
862 } | 974 } |
863 } | 975 } |
976 class ClassCastException : Exception { | |
977 this( String e = null ){ | |
978 super(e); | |
979 } | |
980 } | |
864 | 981 |
865 interface Cloneable{ | 982 interface Cloneable{ |
866 } | 983 } |
867 | 984 |
868 interface Comparable { | 985 interface Comparable { |
924 } | 1041 } |
925 exception = exception.next; | 1042 exception = exception.next; |
926 } | 1043 } |
927 } | 1044 } |
928 | 1045 |
929 interface Reader{ | 1046 class Reader{ |
1047 protected Object lock; | |
1048 protected this(){ | |
1049 implMissing(__FILE__,__LINE__); | |
1050 } | |
1051 protected this(Object lock){ | |
1052 implMissing(__FILE__,__LINE__); | |
1053 } | |
1054 abstract void close(); | |
1055 void mark(int readAheadLimit){ | |
1056 implMissing(__FILE__,__LINE__); | |
1057 } | |
1058 bool markSupported(){ | |
1059 implMissing(__FILE__,__LINE__); | |
1060 return false; | |
1061 } | |
1062 int read(){ | |
1063 implMissing(__FILE__,__LINE__); | |
1064 return 0; | |
1065 } | |
1066 int read(char[] cbuf){ | |
1067 implMissing(__FILE__,__LINE__); | |
1068 return 0; | |
1069 } | |
1070 abstract int read(char[] cbuf, int off, int len); | |
1071 bool ready(){ | |
1072 implMissing(__FILE__,__LINE__); | |
1073 return false; | |
1074 } | |
1075 void reset(){ | |
1076 implMissing(__FILE__,__LINE__); | |
1077 } | |
1078 long skip(long n){ | |
1079 implMissing(__FILE__,__LINE__); | |
1080 return 0; | |
1081 } | |
930 } | 1082 } |
931 interface Writer{ | 1083 interface Writer{ |
932 } | 1084 } |
933 | 1085 |
934 | 1086 |
1042 } | 1194 } |
1043 } | 1195 } |
1044 return true; | 1196 return true; |
1045 } | 1197 } |
1046 | 1198 |
1047 int SeqIndexOf(T)( tango.util.collection.model.Seq.Seq!(T) s, T src ){ | 1199 /+int SeqIndexOf(T)( tango.util.collection.model.Seq.Seq!(T) s, T src ){ |
1048 int idx; | 1200 int idx; |
1049 foreach( e; s ){ | 1201 foreach( e; s ){ |
1050 if( e == src ){ | 1202 if( e == src ){ |
1051 return idx; | 1203 return idx; |
1052 } | 1204 } |
1053 idx++; | 1205 idx++; |
1054 } | 1206 } |
1055 return -1; | 1207 return -1; |
1056 } | 1208 }+/ |
1209 | |
1057 int arrayIndexOf(T)( T[] arr, T v ){ | 1210 int arrayIndexOf(T)( T[] arr, T v ){ |
1058 int res = -1; | 1211 int res = -1; |
1059 int idx = 0; | 1212 int idx = 0; |
1060 foreach( p; arr ){ | 1213 foreach( p; arr ){ |
1061 if( p == v){ | 1214 if( p == v){ |
1065 idx++; | 1218 idx++; |
1066 } | 1219 } |
1067 return res; | 1220 return res; |
1068 } | 1221 } |
1069 | 1222 |
1070 int seqIndexOf( tango.util.collection.model.Seq.Seq!(Object) seq, Object v ){ | 1223 // int seqIndexOf( tango.util.collection.model.Seq.Seq!(Object) seq, Object v ){ |
1071 int res = -1; | 1224 // int res = -1; |
1072 int idx = 0; | 1225 // int idx = 0; |
1073 foreach( p; seq ){ | 1226 // foreach( p; seq ){ |
1074 if( p == v){ | 1227 // if( p == v){ |
1075 res = idx; | 1228 // res = idx; |
1076 break; | 1229 // break; |
1077 } | 1230 // } |
1078 idx++; | 1231 // idx++; |
1079 } | 1232 // } |
1080 return res; | 1233 // return res; |
1081 } | 1234 // } |
1082 | 1235 |
1083 void PrintStackTrace( int deepth = 100, String prefix = "trc" ){ | 1236 void PrintStackTrace( int deepth = 100, String prefix = "trc" ){ |
1084 auto e = new Exception( null ); | 1237 auto e = new Exception( null ); |
1085 int idx = 0; | 1238 int idx = 0; |
1086 const start = 3; | 1239 const start = 3; |
1106 | 1259 |
1107 template getImportData(String name ){ | 1260 template getImportData(String name ){ |
1108 const ImportData getImportData = ImportData( import(name), name ); | 1261 const ImportData getImportData = ImportData( import(name), name ); |
1109 } | 1262 } |
1110 | 1263 |
1264 interface CharSequence { | |
1265 char charAt(int index); | |
1266 int length(); | |
1267 CharSequence subSequence(int start, int end); | |
1268 String toString(); | |
1269 } | |
1270 | |
1271 class StringCharSequence : CharSequence { | |
1272 private String str; | |
1273 this( String str ){ | |
1274 this.str = str; | |
1275 } | |
1276 char charAt(int index){ | |
1277 return str[index]; | |
1278 } | |
1279 int length(){ | |
1280 return str.length; | |
1281 } | |
1282 CharSequence subSequence(int start, int end){ | |
1283 return new StringCharSequence( str[ start .. end ]); | |
1284 } | |
1285 String toString(){ | |
1286 return str; | |
1287 } | |
1288 } | |
1289 | |
1290 |