comparison 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
comparison
equal deleted inserted replaced
196:1e7701c27c03 197:184ab53b7785
7 public import Math = tango.math.Math; 7 public import Math = tango.math.Math;
8 8
9 public import tango.core.Exception : IllegalArgumentException, IOException; 9 public import tango.core.Exception : IllegalArgumentException, IOException;
10 10
11 import tango.io.Stdout; 11 import tango.io.Stdout;
12 import tango.io.Print;
12 import tango.stdc.stringz; 13 import tango.stdc.stringz;
13 static import tango.text.Util; 14 static import tango.text.Util;
15 static import tango.text.Text;
14 import tango.text.Unicode; 16 import tango.text.Unicode;
15 import tango.text.convert.Utf; 17 import tango.text.convert.Utf;
16 import tango.core.Exception; 18 import tango.core.Exception;
17 import tango.stdc.stdlib : exit; 19 import tango.stdc.stdlib : exit;
18 20
19 import tango.util.log.Trace; 21 import tango.util.log.Trace;
22 import tango.text.UnicodeData;
23 static import tango.util.collection.model.Seq;
24 // static import tango.util.collection.ArraySeq;
25 // static import tango.util.collection.LinkSeq;
26 // static import tango.util.collection.model.Map;
27 // static import tango.util.collection.HashMap;
28 //
29 // alias tango.util.collection.model.Seq.Seq!(Object) List;
30 // alias tango.util.collection.ArraySeq.ArraySeq!(Object) ArrayList;
31 // alias tango.util.collection.LinkSeq.LinkSeq!(Object) LinkList;
32 // alias tango.util.collection.model.Map.Map!(Object,Object) Map;
33 // alias tango.util.collection.HashMap.HashMap!(Object,Object) HashMap;
34
35 alias char[] String;
36 alias tango.text.Text.Text!(char) StringBuffer;
20 37
21 void implMissing( char[] file, uint line ){ 38 void implMissing( char[] file, uint line ){
22 Stderr.formatln( "implementation missing in file {} line {}", file, line ); 39 Stderr.formatln( "implementation missing in file {} line {}", file, line );
23 Stderr.formatln( "exiting ..." ); 40 Stderr.formatln( "exiting ..." );
24 exit(1); 41 exit(1);
39 class ValueWrapperT(T) : ValueWrapper { 56 class ValueWrapperT(T) : ValueWrapper {
40 public T value; 57 public T value;
41 public this( T data ){ 58 public this( T data ){
42 value = data; 59 value = data;
43 } 60 }
44 } 61 public int opEquals( T other ){
45 62 return value == other;
46 alias ValueWrapperT!(bool) ValueWrapperBool; 63 }
47 alias ValueWrapperT!(int) ValueWrapperInt; 64 public int opEquals( Object other ){
48 alias ValueWrapperT!(long) ValueWrapperLong; 65 if( auto o = cast(ValueWrapperT!(T))other ){
66 return value == o.value;
67 }
68 return false;
69 }
70 }
71
72 class Boolean : ValueWrapperT!(bool) {
73 public static Boolean TRUE;
74 public static Boolean FALSE;
75 public this( bool v ){
76 super(v);
77 }
78
79 alias ValueWrapperT!(bool).opEquals opEquals;
80 public int opEquals( int other ){
81 return value == ( other !is 0 );
82 }
83 public int opEquals( Object other ){
84 if( auto o = cast(Boolean)other ){
85 return value == o.value;
86 }
87 return false;
88 }
89 public bool booleanValue(){
90 return value;
91 }
92 }
93
94 alias Boolean ValueWrapperBool;
95
96
97 class Byte : ValueWrapperT!(byte) {
98 public static byte parseByte( char[] s ){
99 try{
100 int res = tango.text.convert.Integer.parse( s );
101 if( res < byte.min || res > byte.max ){
102 throw new NumberFormatException( "out of range" );
103 }
104 return res;
105 }
106 catch( IllegalArgumentException e ){
107 throw new NumberFormatException( e );
108 }
109 }
110 this( byte value ){
111 super( value );
112 }
113 }
114 alias Byte ValueWrapperByte;
115
116
117 class Integer : ValueWrapperT!(int) {
118
119 public static int MIN_VALUE = 0x80000000;
120 public static int MAX_VALUE = 0x7fffffff;
121 public static int SIZE = 32;
122
123 public this ( int value ){
124 super( value );
125 }
126
127 public this ( char[] s ){
128 implMissing( __FILE__, __LINE__ );
129 super(0);
130 }
131
132 public static char[] toString( int i, int radix ){
133 switch( radix ){
134 case 2:
135 return toBinaryString(i);
136 case 8:
137 return toOctalString(i);
138 case 10:
139 return toString(i);
140 case 16:
141 return toHexString(i);
142 default:
143 implMissing( __FILE__, __LINE__ );
144 return null;
145 }
146 }
147
148 public static char[] toHexString( int i ){
149 return tango.text.convert.Integer.toString(i, tango.text.convert.Integer.Style.Hex );
150 }
151
152 public static char[] toOctalString( int i ){
153 return tango.text.convert.Integer.toString(i, tango.text.convert.Integer.Style.Octal );
154 }
155
156 public static char[] toBinaryString( int i ){
157 return tango.text.convert.Integer.toString(i, tango.text.convert.Integer.Style.Binary );
158 }
159
160 public static char[] toString( int i ){
161 return tango.text.convert.Integer.toString(i);
162 }
163
164 public static int parseInt( char[] s, int radix ){
165 try{
166 return tango.text.convert.Integer.parse( s, cast(uint)radix );
167 }
168 catch( IllegalArgumentException e ){
169 throw new NumberFormatException( e );
170 }
171 }
172
173 public static int parseInt( char[] s ){
174 try{
175 return tango.text.convert.Integer.parse( s );
176 }
177 catch( IllegalArgumentException e ){
178 throw new NumberFormatException( e );
179 }
180 }
181
182 public static Integer valueOf( char[] s, int radix ){
183 implMissing( __FILE__, __LINE__ );
184 return null;
185 }
186
187 public static Integer valueOf( char[] s ){
188 implMissing( __FILE__, __LINE__ );
189 return null;
190 }
191
192 public static Integer valueOf( int i ){
193 implMissing( __FILE__, __LINE__ );
194 return null;
195 }
196
197 public byte byteValue(){
198 return cast(byte)value;
199 }
200
201 public short shortValue(){
202 return cast(short)value;
203 }
204
205 public int intValue(){
206 return value;
207 }
208
209 public long longValue(){
210 return cast(long)value;
211 }
212
213 public float floatValue(){
214 return cast(float)value;
215 }
216
217 public double doubleValue(){
218 return cast(double)value;
219 }
220
221 public override hash_t toHash(){
222 return intValue();
223 }
224
225 public override char[] toString(){
226 return tango.text.convert.Integer.toString( value );
227 }
228 }
229 alias Integer ValueWrapperInt;
230
231 class Double : ValueWrapperT!(double) {
232 this( double value ){
233 super(value);
234 }
235 this( char[] str ){
236 implMissing( __FILE__, __LINE__ );
237 super(0.0);
238 }
239 public double doubleValue(){
240 return value;
241 }
242 public static char[] toString( double value ){
243 implMissing( __FILE__, __LINE__ );
244 return null;
245 }
246 }
247
248 class Float : ValueWrapperT!(float) {
249
250 public static float POSITIVE_INFINITY = (1.0f / 0.0f);
251 public static float NEGATIVE_INFINITY = ((-1.0f) / 0.0f);
252 public static float NaN = (0.0f / 0.0f);
253 public static float MAX_VALUE = 3.4028235e+38f;
254 public static float MIN_VALUE = 1.4e-45f;
255 public static int SIZE = 32;
256
257 this( float value ){
258 super(value);
259 }
260 this( char[] str ){
261 implMissing( __FILE__, __LINE__ );
262 super(0.0);
263 }
264 public float floatValue(){
265 return value;
266 }
267 public static char[] toString( float value ){
268 implMissing( __FILE__, __LINE__ );
269 return null;
270 }
271 public static float parseFloat( char[] s ){
272 try{
273 return tango.text.convert.Float.toFloat( s );
274 }
275 catch( IllegalArgumentException e ){
276 throw new NumberFormatException( e );
277 }
278 }
279
280 }
281 class Long : ValueWrapperT!(long) {
282 this( long value ){
283 super(value);
284 }
285 this( char[] str ){
286 implMissing( __FILE__, __LINE__ );
287 super(0);
288 }
289 public long longValue(){
290 return value;
291 }
292 public static long parseLong(char[] s){
293 implMissing( __FILE__, __LINE__ );
294 return 0;
295 }
296 public static char[] toString( double value ){
297 implMissing( __FILE__, __LINE__ );
298 return null;
299 }
300 }
301 alias Long ValueWrapperLong;
302
303
304 // alias ValueWrapperT!(int) ValueWrapperInt;
305
49 alias ArrayWrapperT!(byte) ArrayWrapperByte; 306 alias ArrayWrapperT!(byte) ArrayWrapperByte;
50 alias ArrayWrapperT!(int) ArrayWrapperInt; 307 alias ArrayWrapperT!(int) ArrayWrapperInt;
51 alias ArrayWrapperT!(Object) ArrayWrapperObject; 308 alias ArrayWrapperT!(Object) ArrayWrapperObject;
52 alias ArrayWrapperT!(char) ArrayWrapperString; 309 alias ArrayWrapperT!(char) ArrayWrapperString;
53 alias ArrayWrapperT!(char[]) ArrayWrapperString2; 310 alias ArrayWrapperT!(char[]) ArrayWrapperString2;
54 311
312 Object[] StringArrayToObjectArray( String[] strs ){
313 Object[] res = new Object[strs.length];
314 foreach( idx, str; strs ){
315 res[idx] = new ArrayWrapperString(str);
316 }
317 return res;
318 }
55 int codepointIndexToIndex( char[] str, int cpIndex ){ 319 int codepointIndexToIndex( char[] str, int cpIndex ){
56 int cps = cpIndex; 320 int cps = cpIndex;
57 int res = 0; 321 int res = 0;
58 while( cps > 0 ){ 322 while( cps > 0 ){
59 cps--; 323 cps--;
201 offset--; 465 offset--;
202 } 466 }
203 return offset; 467 return offset;
204 } 468 }
205 469
470 bool CharacterIsDefined( dchar ch ){
471 return (ch in tango.text.UnicodeData.unicodeData) !is null;
472 }
206 dchar CharacterFirstToLower( char[] str ){ 473 dchar CharacterFirstToLower( char[] str ){
207 int consumed; 474 int consumed;
208 return CharacterFirstToLower( str, consumed ); 475 return CharacterFirstToLower( str, consumed );
209 } 476 }
210 dchar CharacterFirstToLower( char[] str, out int consumed ){ 477 dchar CharacterFirstToLower( char[] str, out int consumed ){
226 return tango.text.Unicode.isWhitespace( c ); 493 return tango.text.Unicode.isWhitespace( c );
227 } 494 }
228 bool CharacterIsDigit( dchar c ){ 495 bool CharacterIsDigit( dchar c ){
229 return tango.text.Unicode.isDigit( c ); 496 return tango.text.Unicode.isDigit( c );
230 } 497 }
498 bool CharacterIsLetter( dchar c ){
499 return tango.text.Unicode.isLetter( c );
500 }
501 public char[] toUpperCase( char[] str ){
502 return tango.text.Unicode.toUpper( str );
503 }
231 504
232 public int indexOf( char[] str, char searched ){ 505 public int indexOf( char[] str, char searched ){
233 int res = tango.text.Util.locate( str, searched ); 506 int res = tango.text.Util.locate( str, searched );
234 if( res is str.length ) res = -1; 507 if( res is str.length ) res = -1;
235 return res; 508 return res;
257 public int lastIndexOf(char[] str, char ch, int formIndex){ 530 public int lastIndexOf(char[] str, char ch, int formIndex){
258 int res = tango.text.Util.locatePrior( str, ch, formIndex ); 531 int res = tango.text.Util.locatePrior( str, ch, formIndex );
259 if( res is str.length ) res = -1; 532 if( res is str.length ) res = -1;
260 return res; 533 return res;
261 } 534 }
535 public int lastIndexOf(char[] str, char[] ch ){
536 return lastIndexOf( str, ch, str.length );
537 }
538 public int lastIndexOf(char[] str, char[] ch, int start ){
539 int res = tango.text.Util.locatePatternPrior( str, ch, start );
540 if( res is str.length ) res = -1;
541 return res;
542 }
262 543
263 public char[] replace( char[] str, char from, char to ){ 544 public char[] replace( char[] str, char from, char to ){
264 return tango.text.Util.replace( str.dup, from, to ); 545 return tango.text.Util.replace( str.dup, from, to );
265 } 546 }
266 547
311 if( src.length < pattern.length ){ 592 if( src.length < pattern.length ){
312 return false; 593 return false;
313 } 594 }
314 return src[ 0 .. pattern.length ] == pattern; 595 return src[ 0 .. pattern.length ] == pattern;
315 } 596 }
597
316 public char[] toLowerCase( char[] src ){ 598 public char[] toLowerCase( char[] src ){
317 return tango.text.Unicode.toLower( src ); 599 return tango.text.Unicode.toLower( src );
318 } 600 }
319 601
320 public hash_t toHash( char[] src ){ 602 public hash_t toHash( char[] src ){
321 return typeid(char[]).getHash(&src); 603 return typeid(char[]).getHash(&src);
322 } 604 }
323 605
324 public char[] trim( char[] str ){ 606 public char[] trim( char[] str ){
325 return tango.text.Util.trim( str ).dup; 607 return tango.text.Util.trim( str ).dup;
608 }
609 public char[] intern( char[] str ){
610 return str;
326 } 611 }
327 612
328 public char* toStringzValidPtr( char[] src ){ 613 public char* toStringzValidPtr( char[] src ){
329 if( src ){ 614 if( src ){
330 return src.toStringz(); 615 return src.toStringz();
344 tango.text.convert.Integer.Style.Signed, 629 tango.text.convert.Integer.Style.Signed,
345 prefix ? tango.text.convert.Integer.Flags.Prefix : tango.text.convert.Integer.Flags.None 630 prefix ? tango.text.convert.Integer.Flags.Prefix : tango.text.convert.Integer.Flags.None
346 ); 631 );
347 } 632 }
348 633
634 class RuntimeException : Exception {
635 this( char[] e = null){
636 super(e);
637 }
638 this( Exception e ){
639 super(e.toString);
640 }
641 }
642 class IndexOutOfBoundsException : Exception {
643 this( char[] e = null){
644 super(e);
645 }
646 }
647
648 class UnsupportedOperationException : RuntimeException {
649 this( char[] e = null){
650 super(e);
651 }
652 this( Exception e ){
653 super(e.toString);
654 }
655 }
349 class NumberFormatException : IllegalArgumentException { 656 class NumberFormatException : IllegalArgumentException {
350 this( char[] e ){ 657 this( char[] e ){
351 super(e); 658 super(e);
352 } 659 }
353 this( Exception e ){ 660 this( Exception e ){
354 super(e.toString); 661 super(e.toString);
662 }
663 }
664 class NullPointerException : Exception {
665 this( char[] e = null ){
666 super(e);
667 }
668 this( Exception e ){
669 super(e.toString);
670 }
671 }
672 class IllegalStateException : Exception {
673 this( char[] e = null ){
674 super(e);
675 }
676 this( Exception e ){
677 super(e.toString);
678 }
679 }
680 class InterruptedException : Exception {
681 this( char[] e = null ){
682 super(e);
683 }
684 this( Exception e ){
685 super(e.toString);
686 }
687 }
688 class InvocationTargetException : Exception {
689 Exception cause;
690 this( Exception e = null, char[] msg = null ){
691 super(msg);
692 cause = e;
693 }
694
695 alias getCause getTargetException;
696 Exception getCause(){
697 return cause;
698 }
699 }
700 class MissingResourceException : Exception {
701 char[] classname;
702 char[] key;
703 this( char[] msg, char[] classname, char[] key ){
704 super(msg);
705 this.classname = classname;
706 this.key = key;
707 }
708 }
709 class ParseException : Exception {
710 this( char[] e = null ){
711 super(e);
712 }
713 }
714
715 interface Cloneable{
716 }
717
718 interface Comparable {
719 int compareTo(Object o);
720 }
721 interface Comparator {
722 int compare(Object o1, Object o2);
723 }
724 interface EventListener{
725 }
726
727 class EventObject {
728 protected Object source;
729
730 public this(Object source) {
731 if (source is null)
732 throw new IllegalArgumentException( "null arg" );
733 this.source = source;
734 }
735
736 public Object getSource() {
737 return source;
738 }
739
740 public override char[] toString() {
741 return this.classinfo.name ~ "[source=" ~ source.toString() ~ "]";
355 } 742 }
356 } 743 }
357 744
358 private struct GCStats { 745 private struct GCStats {
359 size_t poolsize; // total size of pool 746 size_t poolsize; // total size of pool
368 GCStats s = gc_stats(); 755 GCStats s = gc_stats();
369 return s.poolsize; 756 return s.poolsize;
370 } 757 }
371 758
372 759
373 760 void ExceptionPrintStackTrace( Exception e ){
374 761 ExceptionPrintStackTrace( e, Stderr );
762 }
763 void ExceptionPrintStackTrace( Exception e, Print!(char) print ){
764 print.formatln( "Exception in {}({}): {}", e.file, e.line, e.msg );
765 }
766
767 interface Reader{
768 }
769 interface Writer{
770 }
771
772
773 class Collator : Comparator {
774 public static Collator getInstance(){
775 implMissing( __FILE__, __LINE__ );
776 return null;
777 }
778 private this(){
779 }
780 int compare(Object o1, Object o2){
781 implMissing( __FILE__, __LINE__ );
782 return 0;
783 }
784 }
785
786 interface Enumeration {
787 public bool hasMoreElements();
788 public Object nextElement();
789 }
790
791
792 template arraycast(T) {
793 T[] arraycast(U) (U[] u) {
794 static if (
795 (is (T == interface ) && is (U == interface )) ||
796 (is (T == class ) && is (U == class ))) {
797 return(cast(T[])u);
798 }
799 else {
800 int l = u.length;
801 T[] res;
802 res.length = l;
803 for (int i = 0; i < l; i++) {
804 res[i] = cast(T)u[i];
805 }
806 return(res);
807 }
808 }
809 }
810
811 char[] stringcast( Object o ){
812 if( auto str = cast(ArrayWrapperString) o ){
813 return str.array;
814 }
815 return null;
816 }
817 char[][] stringcast( Object[] objs ){
818 char[][] res = new char[][](objs.length);
819 foreach( idx, obj; objs ){
820 res[idx] = stringcast(obj);
821 }
822 return res;
823 }
824 ArrayWrapperString stringcast( char[] str ){
825 return new ArrayWrapperString( str );
826 }
827 ArrayWrapperString[] stringcast( char[][] strs ){
828 ArrayWrapperString[] res = new ArrayWrapperString[ strs.length ];
829 foreach( idx, str; strs ){
830 res[idx] = stringcast(str);
831 }
832 return res;
833 }
834
835
836 bool ArrayEquals(T)( T[] a, T[] b ){
837 if( a.length !is b.length ){
838 return false;
839 }
840 for( int i = 0; i < a.length; i++ ){
841 static if( is( T==class) || is(T==interface)){
842 if( a[i] !is null && b[i] !is null ){
843 if( a[i] != b[i] ){
844 return false;
845 }
846 }
847 else if( a[i] is null && b[i] is null ){
848 }
849 else{
850 return false;
851 }
852 }
853 else{
854 if( a[i] != b[i] ){
855 return false;
856 }
857 }
858 }
859 }
860
861 class Arrays{
862 public static bool equals(Object[] a, Object[] b){
863 if( a.length !is b.length ){
864 return false;
865 }
866 for( int i = 0; i < a.length; i++ ){
867 if( a[i] is null && b[i] is null ){
868 continue;
869 }
870 if( a[i] !is null && b[i] !is null && a[i] == b[i] ){
871 continue;
872 }
873 return false;
874 }
875 return true;
876 }
877 }
878
879 int SeqIndexOf(T)( tango.util.collection.model.Seq.Seq!(T) s, T src ){
880 int idx;
881 foreach( e; s ){
882 if( e == src ){
883 return idx;
884 }
885 idx++;
886 }
887 return -1;
888 }
889 int arrayIndexOf(T)( T[] arr, T v ){
890 int res = -1;
891 int idx = 0;
892 foreach( p; arr ){
893 if( p == v){
894 res = idx;
895 break;
896 }
897 idx++;
898 }
899 return res;
900 }
901
902 int seqIndexOf( tango.util.collection.model.Seq.Seq!(Object) seq, Object v ){
903 int res = -1;
904 int idx = 0;
905 foreach( p; seq ){
906 if( p == v){
907 res = idx;
908 break;
909 }
910 idx++;
911 }
912 return res;
913 }
914
915 void PrintStackTrace(){
916 try{
917 throw new Exception( null );
918 }
919 catch( Exception e ){
920 foreach( msg; e.info ){
921 Trace.formatln( "trc: {}", msg );
922 }
923 }
924 }
925
926 struct ImportData{
927 void[] data;
928 char[] name;
929
930 public static ImportData opCall( void[] data, char[] name ){
931 ImportData res;
932 res.data = data;
933 res.name = name;
934 return res;
935 }
936 }
937
938 template getImportData(char[] name ){
939 const ImportData getImportData = ImportData( import(name), name );
940 }