comparison dwt/dwthelper/utils.d @ 311:77fa7f3ab37e

Additions for jface.text
author Frank Benoit <benoit@tionex.de>
date Mon, 08 Sep 2008 01:33:00 +0200
parents 7ca3f26319f1
children 0f7ac29ac726
comparison
equal deleted inserted replaced
310:7ca3f26319f1 311:77fa7f3ab37e
12 import tango.io.Stdout; 12 import tango.io.Stdout;
13 import tango.io.Print; 13 import tango.io.Print;
14 static import tango.stdc.stringz; 14 static import tango.stdc.stringz;
15 static import tango.text.Util; 15 static import tango.text.Util;
16 static import tango.text.Text; 16 static import tango.text.Text;
17 static import tango.text.Ascii;
17 import tango.text.Unicode; 18 import tango.text.Unicode;
18 import tango.text.convert.Utf; 19 import tango.text.convert.Utf;
19 import tango.core.Exception; 20 import tango.core.Exception;
20 import tango.stdc.stdlib : exit; 21 import tango.stdc.stdlib : exit;
21 22
22 import tango.util.log.Trace; 23 import tango.util.log.Trace;
23 import tango.text.UnicodeData; 24 import tango.text.UnicodeData;
24 static import tango.util.collection.model.Seq;
25 25
26 alias char[] String; 26 alias char[] String;
27 alias tango.text.Text.Text!(char) StringBuffer; 27 alias tango.text.Text.Text!(char) StringBuffer;
28
29 alias ArrayBoundsException ArrayIndexOutOfBoundsException;
28 30
29 void implMissing( String file, uint line ){ 31 void implMissing( String file, uint line ){
30 Stderr.formatln( "implementation missing in file {} line {}", file, line ); 32 Stderr.formatln( "implementation missing in file {} line {}", file, line );
31 Stderr.formatln( "exiting ..." ); 33 Stderr.formatln( "exiting ..." );
32 exit(1); 34 exit(1);
123 return FALSE; 125 return FALSE;
124 } 126 }
125 public static Boolean valueOf( bool b ){ 127 public static Boolean valueOf( bool b ){
126 return b ? TRUE : FALSE; 128 return b ? TRUE : FALSE;
127 } 129 }
130 public static bool getBoolean(String name){
131 return tango.text.Ascii.icompare(System.getProperty(name, "false"), "true" ) is 0;
132 }
128 } 133 }
129 134
130 alias Boolean ValueWrapperBool; 135 alias Boolean ValueWrapperBool;
131 136
132 137
144 } 149 }
145 } 150 }
146 this( byte value ){ 151 this( byte value ){
147 super( value ); 152 super( value );
148 } 153 }
154
155 public static String toString( byte i ){
156 return tango.text.convert.Integer.toString(i);
157 }
158
149 } 159 }
150 alias Byte ValueWrapperByte; 160 alias Byte ValueWrapperByte;
151 161
152 162
153 class Integer : ValueWrapperT!(int) { 163 class Integer : ValueWrapperT!(int) {
276 } 286 }
277 public static String toString( double value ){ 287 public static String toString( double value ){
278 implMissing( __FILE__, __LINE__ ); 288 implMissing( __FILE__, __LINE__ );
279 return null; 289 return null;
280 } 290 }
291 public static double parseDouble(String s){
292 implMissing( __FILE__, __LINE__ );
293 return 0.0;
294 }
281 } 295 }
282 296
283 class Float : ValueWrapperT!(float) { 297 class Float : ValueWrapperT!(float) {
284 298
285 public static float POSITIVE_INFINITY = (1.0f / 0.0f); 299 public static float POSITIVE_INFINITY = (1.0f / 0.0f);
559 searchRelCp++; 573 searchRelCp++;
560 } 574 }
561 } 575 }
562 return i; 576 return i;
563 } 577 }
578 dchar getRelativeCodePoint( String str, int startIndex, int searchRelCp ){
579 int dummy;
580 return getRelativeCodePoint( str, startIndex, dummy );
581 }
564 dchar getRelativeCodePoint( String str, int startIndex, int searchRelCp, out int relIndex ){ 582 dchar getRelativeCodePoint( String str, int startIndex, int searchRelCp, out int relIndex ){
565 relIndex = getRelativeCodePointOffset( str, startIndex, searchRelCp ); 583 relIndex = getRelativeCodePointOffset( str, startIndex, searchRelCp );
566 int ignore; 584 int ignore;
567 return firstCodePoint( str[ startIndex+relIndex .. $ ], ignore ); 585 return firstCodePoint( str[ startIndex+relIndex .. $ ], ignore );
568 } 586 }
601 Trace.formatln( "utf8OffsetDecr {}->{}", offset, res ); 619 Trace.formatln( "utf8OffsetDecr {}->{}", offset, res );
602 Trace.memory( str ); 620 Trace.memory( str );
603 return res; 621 return res;
604 } 622 }
605 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 }
606 bool CharacterIsDefined( dchar ch ){ 702 bool CharacterIsDefined( dchar ch ){
607 return (ch in tango.text.UnicodeData.unicodeData) !is null; 703 return (ch in tango.text.UnicodeData.unicodeData) !is null;
608 } 704 }
609 dchar CharacterFirstToLower( String str ){ 705 dchar CharacterFirstToLower( String str ){
610 int consumed; 706 int consumed;
614 dchar[1] buf; 710 dchar[1] buf;
615 buf[0] = firstCodePoint( str, consumed ); 711 buf[0] = firstCodePoint( str, consumed );
616 dchar[] r = tango.text.Unicode.toLower( buf ); 712 dchar[] r = tango.text.Unicode.toLower( buf );
617 return r[0]; 713 return r[0];
618 } 714 }
619 715 int length( String str ){
716 return str.length;
717 }
620 dchar CharacterToLower( dchar c ){ 718 dchar CharacterToLower( dchar c ){
621 dchar[] r = tango.text.Unicode.toLower( [c] ); 719 dchar[] r = tango.text.Unicode.toLower( [c] );
622 return r[0]; 720 return r[0];
623 } 721 }
624 dchar CharacterToUpper( dchar c ){ 722 dchar CharacterToUpper( dchar c ){
634 bool CharacterIsLetter( dchar c ){ 732 bool CharacterIsLetter( dchar c ){
635 return tango.text.Unicode.isLetter( c ); 733 return tango.text.Unicode.isLetter( c );
636 } 734 }
637 public String toUpperCase( String str ){ 735 public String toUpperCase( String str ){
638 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;
639 } 742 }
640 743
641 public int indexOf( String str, char searched ){ 744 public int indexOf( String str, char searched ){
642 int res = tango.text.Util.locate( str, searched ); 745 int res = tango.text.Util.locate( str, searched );
643 if( res is str.length ) res = -1; 746 if( res is str.length ) res = -1;
675 int res = tango.text.Util.locatePatternPrior( str, ch, start ); 778 int res = tango.text.Util.locatePatternPrior( str, ch, start );
676 if( res is str.length ) res = -1; 779 if( res is str.length ) res = -1;
677 return res; 780 return res;
678 } 781 }
679 782
783 public String replaceAll( String str, String regex, String replacement ){
784 implMissing(__FILE__,__LINE__);
785 return null;
786 }
680 public String replace( String str, char from, char to ){ 787 public String replace( String str, char from, char to ){
681 return tango.text.Util.replace( str.dup, from, to ); 788 return tango.text.Util.replace( str.dup, from, to );
682 } 789 }
683 790
684 public String substring( String str, int start ){ 791 public String substring( String str, int start ){
703 810
704 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){
705 dst[ dstBegin .. dstBegin + srcEnd - srcBegin ] = src[ srcBegin .. srcEnd ]; 812 dst[ dstBegin .. dstBegin + srcEnd - srcBegin ] = src[ srcBegin .. srcEnd ];
706 } 813 }
707 814
708 public wchar[] toCharArray( String str ){ 815 public char[] toCharArray( String str ){
709 return toString16( str ); 816 return str;
710 } 817 }
711 818
712 public bool endsWith( String src, String pattern ){ 819 public bool endsWith( String src, String pattern ){
713 if( src.length < pattern.length ){ 820 if( src.length < pattern.length ){
714 return false; 821 return false;
860 class ParseException : Exception { 967 class ParseException : Exception {
861 this( String e = null ){ 968 this( String e = null ){
862 super(e); 969 super(e);
863 } 970 }
864 } 971 }
972 class ClassCastException : Exception {
973 this( String e = null ){
974 super(e);
975 }
976 }
865 977
866 interface Cloneable{ 978 interface Cloneable{
867 } 979 }
868 980
869 interface Comparable { 981 interface Comparable {
925 } 1037 }
926 exception = exception.next; 1038 exception = exception.next;
927 } 1039 }
928 } 1040 }
929 1041
930 interface Reader{ 1042 class Reader{
1043 protected Object lock;
1044 protected this(){
1045 implMissing(__FILE__,__LINE__);
1046 }
1047 protected this(Object lock){
1048 implMissing(__FILE__,__LINE__);
1049 }
1050 abstract void close();
1051 void mark(int readAheadLimit){
1052 implMissing(__FILE__,__LINE__);
1053 }
1054 bool markSupported(){
1055 implMissing(__FILE__,__LINE__);
1056 return false;
1057 }
1058 int read(){
1059 implMissing(__FILE__,__LINE__);
1060 return 0;
1061 }
1062 int read(char[] cbuf){
1063 implMissing(__FILE__,__LINE__);
1064 return 0;
1065 }
1066 abstract int read(char[] cbuf, int off, int len);
1067 bool ready(){
1068 implMissing(__FILE__,__LINE__);
1069 return false;
1070 }
1071 void reset(){
1072 implMissing(__FILE__,__LINE__);
1073 }
1074 long skip(long n){
1075 implMissing(__FILE__,__LINE__);
1076 return 0;
1077 }
931 } 1078 }
932 interface Writer{ 1079 interface Writer{
933 } 1080 }
934 1081
935 1082
1043 } 1190 }
1044 } 1191 }
1045 return true; 1192 return true;
1046 } 1193 }
1047 1194
1048 int SeqIndexOf(T)( tango.util.collection.model.Seq.Seq!(T) s, T src ){ 1195 /+int SeqIndexOf(T)( tango.util.collection.model.Seq.Seq!(T) s, T src ){
1049 int idx; 1196 int idx;
1050 foreach( e; s ){ 1197 foreach( e; s ){
1051 if( e == src ){ 1198 if( e == src ){
1052 return idx; 1199 return idx;
1053 } 1200 }
1054 idx++; 1201 idx++;
1055 } 1202 }
1056 return -1; 1203 return -1;
1057 } 1204 }+/
1205
1058 int arrayIndexOf(T)( T[] arr, T v ){ 1206 int arrayIndexOf(T)( T[] arr, T v ){
1059 int res = -1; 1207 int res = -1;
1060 int idx = 0; 1208 int idx = 0;
1061 foreach( p; arr ){ 1209 foreach( p; arr ){
1062 if( p == v){ 1210 if( p == v){
1066 idx++; 1214 idx++;
1067 } 1215 }
1068 return res; 1216 return res;
1069 } 1217 }
1070 1218
1071 int seqIndexOf( tango.util.collection.model.Seq.Seq!(Object) seq, Object v ){ 1219 // int seqIndexOf( tango.util.collection.model.Seq.Seq!(Object) seq, Object v ){
1072 int res = -1; 1220 // int res = -1;
1073 int idx = 0; 1221 // int idx = 0;
1074 foreach( p; seq ){ 1222 // foreach( p; seq ){
1075 if( p == v){ 1223 // if( p == v){
1076 res = idx; 1224 // res = idx;
1077 break; 1225 // break;
1078 } 1226 // }
1079 idx++; 1227 // idx++;
1080 } 1228 // }
1081 return res; 1229 // return res;
1082 } 1230 // }
1083 1231
1084 void PrintStackTrace( int deepth = 100, String prefix = "trc" ){ 1232 void PrintStackTrace( int deepth = 100, String prefix = "trc" ){
1085 auto e = new Exception( null ); 1233 auto e = new Exception( null );
1086 int idx = 0; 1234 int idx = 0;
1087 const start = 3; 1235 const start = 3;
1108 template getImportData(String name ){ 1256 template getImportData(String name ){
1109 const ImportData getImportData = ImportData( import(name), name ); 1257 const ImportData getImportData = ImportData( import(name), name );
1110 } 1258 }
1111 1259
1112 interface CharSequence { 1260 interface CharSequence {
1113 wchar charAt(int index); 1261 char charAt(int index);
1114 int length(); 1262 int length();
1115 CharSequence subSequence(int start, int end); 1263 CharSequence subSequence(int start, int end);
1116 String toString(); 1264 String toString();
1117 } 1265 }
1118 1266
1267 class StringCharSequence : CharSequence {
1268 private String str;
1269 this( String str ){
1270 this.str = str;
1271 }
1272 char charAt(int index){
1273 return str[index];
1274 }
1275 int length(){
1276 return str.length;
1277 }
1278 CharSequence subSequence(int start, int end){
1279 return new StringCharSequence( str[ start .. end ]);
1280 }
1281 String toString(){
1282 return str;
1283 }
1284 }
1285
1286