Mercurial > projects > dwt-win
annotate dwt/dwthelper/utils.d @ 187:bcdc37794717
Reverted changes from previous wrong reverting changes:
See: -r026ce3b05f57, -r4d06074bb1af, -rc4643827733c
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 10 Mar 2008 17:08:22 +0100 |
parents | c4643827733c |
children | 72ef824d4983 |
rev | line source |
---|---|
22
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
1 /** |
0 | 2 * Authors: Frank Benoit <keinfarbton@googlemail.com> |
3 */ | |
4 module dwt.dwthelper.utils; | |
5 | |
6 public import dwt.dwthelper.System; | |
15 | 7 public import Math = tango.math.Math; |
0 | 8 |
98 | 9 public import tango.core.Exception : IllegalArgumentException, IOException; |
97 | 10 |
0 | 11 import tango.io.Stdout; |
15 | 12 import tango.stdc.stringz; |
13 import tango.text.Util; | |
0 | 14 import tango.text.Unicode; |
15 | 15 import tango.text.convert.Utf; |
177 | 16 import tango.core.Exception; |
0 | 17 import tango.stdc.stdlib : exit; |
18 | |
177 | 19 import tango.util.log.Trace; |
20 | |
0 | 21 void implMissing( char[] file, uint line ){ |
22 Stderr.formatln( "implementation missing in file {} line {}", file, line ); | |
23 Stderr.formatln( "exiting ..." ); | |
24 exit(1); | |
25 } | |
26 | |
27 abstract class ArrayWrapper{ | |
28 } | |
33 | 29 abstract class ValueWrapper{ |
30 } | |
0 | 31 |
32 class ArrayWrapperT(T) : ArrayWrapper { | |
33 public T[] array; | |
34 public this( T[] data ){ | |
35 array = data; | |
36 } | |
37 } | |
38 | |
33 | 39 class ValueWrapperT(T) : ValueWrapper { |
40 public T value; | |
41 public this( T data ){ | |
42 value = data; | |
43 } | |
44 } | |
45 | |
46 alias ValueWrapperT!(bool) ValueWrapperBool; | |
98 | 47 alias ValueWrapperT!(int) ValueWrapperInt; |
187
bcdc37794717
Reverted changes from previous wrong reverting changes:
Frank Benoit <benoit@tionex.de>
parents:
183
diff
changeset
|
48 alias ValueWrapperT!(long) ValueWrapperLong; |
0 | 49 alias ArrayWrapperT!(byte) ArrayWrapperByte; |
50 alias ArrayWrapperT!(int) ArrayWrapperInt; | |
51 alias ArrayWrapperT!(Object) ArrayWrapperObject; | |
52 alias ArrayWrapperT!(char) ArrayWrapperString; | |
53 alias ArrayWrapperT!(char[]) ArrayWrapperString2; | |
54 | |
177 | 55 int codepointIndexToIndex( char[] str, int cpIndex ){ |
56 int cps = cpIndex; | |
57 int res = 0; | |
58 while( cps > 0 ){ | |
59 cps--; | |
60 if( str[res] < 0x80 ){ | |
61 res+=1; | |
62 } | |
63 else if( str[res] < 0xE0 ){ | |
64 res+=2; | |
65 } | |
66 else if( str[res] & 0xF0 ){ | |
67 res+=3; | |
68 } | |
69 else{ | |
70 res+=4; | |
71 } | |
72 } | |
73 return res; | |
74 } | |
75 int indexToCodepointIndex( char[] str, int index ){ | |
76 int i = 0; | |
77 int res = 0; | |
78 while( i < index ){ | |
79 if( str[i] < 0x80 ){ | |
80 i+=1; | |
81 } | |
82 else if( str[i] < 0xE0 ){ | |
83 i+=2; | |
84 } | |
85 else if( str[i] & 0xF0 ){ | |
86 i+=3; | |
87 } | |
88 else{ | |
89 i+=4; | |
90 } | |
91 res++; | |
92 } | |
93 return res; | |
94 } | |
95 | |
96 char[] firstCodePointStr( char[] str, out int consumed ){ | |
97 dchar[1] buf; | |
98 uint ate; | |
99 dchar[] res = str.toString32( buf, &ate ); | |
100 consumed = ate; | |
101 return str[ 0 .. ate ]; | |
102 } | |
103 | |
104 dchar firstCodePoint( char[] str ){ | |
105 int dummy; | |
106 return firstCodePoint( str, dummy ); | |
107 } | |
108 dchar firstCodePoint( char[] str, out int consumed ){ | |
109 dchar[1] buf; | |
110 uint ate; | |
111 dchar[] res = str.toString32( buf, &ate ); | |
112 consumed = ate; | |
113 if( ate is 0 || res.length is 0 ){ | |
114 Trace.formatln( "dwthelper.utils {}: str.length={} str={:X2}", __LINE__, str.length, cast(ubyte[])str ); | |
0 | 115 } |
177 | 116 assert( ate > 0 ); |
117 assert( res.length is 1 ); | |
118 return res[0]; | |
0 | 119 } |
177 | 120 |
121 char[] dcharToString( dchar key ){ | |
122 dchar[1] buf; | |
123 buf[0] = key; | |
124 return tango.text.convert.Utf.toString( buf ); | |
125 } | |
126 | |
127 int codepointCount( char[] str ){ | |
128 scope dchar[] buf = new dchar[]( str.length ); | |
129 uint ate; | |
130 dchar[] res = tango.text.convert.Utf.toString32( str, buf, &ate ); | |
131 assert( ate is str.length ); | |
132 return res.length; | |
133 } | |
134 | |
135 alias tango.text.convert.Utf.toString16 toString16; | |
136 alias tango.text.convert.Utf.toString toString; | |
137 | |
138 int getRelativeCodePointOffset( char[] str, int startIndex, int searchRelCp ){ | |
139 int ignore; | |
140 int i = startIndex; | |
141 if( searchRelCp > 0 ){ | |
142 while( searchRelCp !is 0 ){ | |
143 | |
144 if( ( i < str.length ) | |
145 && ( str[i] & 0x80 ) is 0x00 ) | |
146 { | |
147 i+=1; | |
148 } | |
149 else if( ( i+1 < str.length ) | |
150 && (( str[i+1] & 0xC0 ) is 0x80 ) | |
151 && (( str[i ] & 0xE0 ) is 0xC0 )) | |
152 { | |
153 i+=2; | |
154 } | |
155 else if( ( i+2 < str.length ) | |
156 && (( str[i+2] & 0xC0 ) is 0x80 ) | |
157 && (( str[i+1] & 0xC0 ) is 0x80 ) | |
158 && (( str[i ] & 0xF0 ) is 0xE0 )) | |
159 { | |
160 i+=3; | |
161 } | |
162 else if(( i+3 < str.length ) | |
163 && (( str[i+3] & 0xC0 ) is 0x80 ) | |
164 && (( str[i+2] & 0xC0 ) is 0x80 ) | |
165 && (( str[i+1] & 0xC0 ) is 0x80 ) | |
166 && (( str[i ] & 0xF8 ) is 0xF0 )) | |
167 { | |
168 i+=4; | |
169 } | |
170 else{ | |
171 tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i ); | |
172 } | |
173 searchRelCp--; | |
174 } | |
175 } | |
176 else if( searchRelCp < 0 ){ | |
177 while( searchRelCp !is 0 ){ | |
178 do{ | |
179 i--; | |
180 if( i < 0 ){ | |
181 Trace.formatln( "dwthelper.utils getRelativeCodePointOffset {}: str={}, startIndex={}, searchRelCp={}", __LINE__, str, startIndex, searchRelCp ); | |
182 tango.text.convert.Utf.onUnicodeError( "invalid utf8 input", i ); | |
183 } | |
184 } while(( str[i] & 0xC0 ) is 0x80 ); | |
185 searchRelCp++; | |
186 } | |
187 } | |
188 return i - startIndex; | |
189 } | |
190 dchar getRelativeCodePoint( char[] str, int startIndex, int searchRelCp, out int relIndex ){ | |
191 relIndex = getRelativeCodePointOffset( str, startIndex, searchRelCp ); | |
192 int ignore; | |
193 return firstCodePoint( str[ startIndex+relIndex .. $ ], ignore ); | |
194 } | |
195 | |
196 int utf8AdjustOffset( char[] str, int offset ){ | |
197 if( str.length <= offset || offset <= 0 ){ | |
198 return offset; | |
199 } | |
200 while(( str[offset] & 0xC0 ) is 0x80 ){ | |
201 offset--; | |
202 } | |
203 return offset; | |
204 } | |
205 | |
206 dchar CharacterFirstToLower( char[] str ){ | |
207 int consumed; | |
208 return CharacterFirstToLower( str, consumed ); | |
209 } | |
210 dchar CharacterFirstToLower( char[] str, out int consumed ){ | |
211 dchar[1] buf; | |
212 buf[0] = firstCodePoint( str, consumed ); | |
213 dchar[] r = tango.text.Unicode.toLower( buf ); | |
214 return r[0]; | |
215 } | |
216 | |
0 | 217 dchar CharacterToLower( dchar c ){ |
218 dchar[] r = tango.text.Unicode.toLower( [c] ); | |
219 return r[0]; | |
220 } | |
221 dchar CharacterToUpper( dchar c ){ | |
222 dchar[] r = tango.text.Unicode.toUpper( [c] ); | |
223 return r[0]; | |
224 } | |
52 | 225 bool CharacterIsWhitespace( dchar c ){ |
226 return tango.text.Unicode.isWhitespace( c ); | |
227 } | |
65 | 228 bool CharacterIsDigit( dchar c ){ |
229 return tango.text.Unicode.isDigit( c ); | |
230 } | |
0 | 231 |
15 | 232 public int indexOf( char[] str, char searched ){ |
233 int res = tango.text.Util.locate( str, searched ); | |
234 if( res is str.length ) res = -1; | |
235 return res; | |
236 } | |
237 | |
238 public int indexOf( char[] str, char searched, int startpos ){ | |
239 int res = tango.text.Util.locate( str, searched, startpos ); | |
240 if( res is str.length ) res = -1; | |
241 return res; | |
242 } | |
243 | |
65 | 244 public int indexOf(char[] str, char[] ch){ |
245 return indexOf( str, ch, 0 ); | |
246 } | |
247 | |
15 | 248 public int indexOf(char[] str, char[] ch, int start){ |
249 int res = tango.text.Util.locatePattern( str, ch, start ); | |
250 if( res is str.length ) res = -1; | |
251 return res; | |
252 } | |
253 | |
177 | 254 public int lastIndexOf(char[] str, char ch){ |
255 return lastIndexOf( str, ch, str.length ); | |
256 } | |
257 public int lastIndexOf(char[] str, char ch, int formIndex){ | |
258 int res = tango.text.Util.locatePrior( str, ch, formIndex ); | |
259 if( res is str.length ) res = -1; | |
260 return res; | |
261 } | |
262 | |
84
00a333240696
FileDialog, sync dwthelper with dwt-linux, some TCHAR issues
Frank Benoit <benoit@tionex.de>
parents:
79
diff
changeset
|
263 public char[] replace( char[] str, char from, char to ){ |
00a333240696
FileDialog, sync dwthelper with dwt-linux, some TCHAR issues
Frank Benoit <benoit@tionex.de>
parents:
79
diff
changeset
|
264 return tango.text.Util.replace( str.dup, from, to ); |
00a333240696
FileDialog, sync dwthelper with dwt-linux, some TCHAR issues
Frank Benoit <benoit@tionex.de>
parents:
79
diff
changeset
|
265 } |
00a333240696
FileDialog, sync dwthelper with dwt-linux, some TCHAR issues
Frank Benoit <benoit@tionex.de>
parents:
79
diff
changeset
|
266 |
15 | 267 public char[] substring( char[] str, int start ){ |
268 return str[ start .. $ ].dup; | |
269 } | |
270 | |
271 public char[] substring( char[] str, int start, int end ){ | |
272 return str[ start .. end ].dup; | |
273 } | |
274 | |
275 public wchar[] substring( wchar[] str, int start ){ | |
276 return str[ start .. $ ].dup; | |
277 } | |
278 | |
279 public wchar[] substring( wchar[] str, int start, int end ){ | |
280 return str[ start .. end ].dup; | |
281 } | |
282 | |
283 public char charAt( char[] str, int pos ){ | |
284 return str[ pos ]; | |
285 } | |
286 | |
22
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
287 public void getChars( char[] src, int srcBegin, int srcEnd, char[] dst, int dstBegin){ |
23 | 288 dst[ dstBegin .. dstBegin + srcEnd - srcBegin ] = src[ srcBegin .. srcEnd ]; |
22
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
289 } |
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
290 |
28 | 291 public wchar[] toCharArray( char[] str ){ |
30 | 292 return toString16( str ); |
28 | 293 } |
294 | |
22
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
295 public bool endsWith( char[] src, char[] pattern ){ |
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
296 if( src.length < pattern.length ){ |
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
297 return false; |
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
298 } |
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
299 return src[ $-pattern.length .. $ ] == pattern; |
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
300 } |
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
301 |
177 | 302 public bool equals( char[] src, char[] other ){ |
303 return src == other; | |
304 } | |
305 | |
306 public bool equalsIgnoreCase( char[] src, char[] other ){ | |
307 return tango.text.Unicode.toFold(src) == tango.text.Unicode.toFold(other); | |
308 } | |
309 | |
98 | 310 public bool startsWith( char[] src, char[] pattern ){ |
311 if( src.length < pattern.length ){ | |
312 return false; | |
313 } | |
314 return src[ 0 .. pattern.length ] == pattern; | |
315 } | |
22
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
316 public char[] toLowerCase( char[] src ){ |
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
317 return tango.text.Unicode.toLower( src ); |
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
318 } |
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
319 |
177 | 320 public hash_t toHash( char[] src ){ |
321 return typeid(char[]).getHash(&src); | |
322 } | |
323 | |
187
bcdc37794717
Reverted changes from previous wrong reverting changes:
Frank Benoit <benoit@tionex.de>
parents:
183
diff
changeset
|
324 public char* toStringzValidPtr( char[] src ){ |
bcdc37794717
Reverted changes from previous wrong reverting changes:
Frank Benoit <benoit@tionex.de>
parents:
183
diff
changeset
|
325 if( src ){ |
bcdc37794717
Reverted changes from previous wrong reverting changes:
Frank Benoit <benoit@tionex.de>
parents:
183
diff
changeset
|
326 return src.toStringz(); |
bcdc37794717
Reverted changes from previous wrong reverting changes:
Frank Benoit <benoit@tionex.de>
parents:
183
diff
changeset
|
327 } |
bcdc37794717
Reverted changes from previous wrong reverting changes:
Frank Benoit <benoit@tionex.de>
parents:
183
diff
changeset
|
328 else{ |
bcdc37794717
Reverted changes from previous wrong reverting changes:
Frank Benoit <benoit@tionex.de>
parents:
183
diff
changeset
|
329 static const char[] nullPtr = "\0"; |
bcdc37794717
Reverted changes from previous wrong reverting changes:
Frank Benoit <benoit@tionex.de>
parents:
183
diff
changeset
|
330 return nullPtr.ptr; |
bcdc37794717
Reverted changes from previous wrong reverting changes:
Frank Benoit <benoit@tionex.de>
parents:
183
diff
changeset
|
331 } |
bcdc37794717
Reverted changes from previous wrong reverting changes:
Frank Benoit <benoit@tionex.de>
parents:
183
diff
changeset
|
332 } |
bcdc37794717
Reverted changes from previous wrong reverting changes:
Frank Benoit <benoit@tionex.de>
parents:
183
diff
changeset
|
333 |
15 | 334 static char[] toHex(uint value, bool prefix = true, int radix = 8){ |
22
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
335 return tango.text.convert.Integer.toString( |
5f2e72114476
Image in work, this revision does not compile
Frank Benoit <benoit@tionex.de>
parents:
15
diff
changeset
|
336 value, |
15 | 337 radix is 10 ? tango.text.convert.Integer.Style.Signed : |
338 radix is 8 ? tango.text.convert.Integer.Style.Octal : | |
339 radix is 16 ? tango.text.convert.Integer.Style.Hex : | |
340 tango.text.convert.Integer.Style.Signed, | |
341 prefix ? tango.text.convert.Integer.Flags.Prefix : tango.text.convert.Integer.Flags.None | |
342 ); | |
343 } | |
344 | |
345 class NumberFormatException : IllegalArgumentException { | |
346 this( char[] e ){ | |
347 super(e); | |
348 } | |
57
41dbc4d9faab
Update to tango trunk -r3152. Thanks DavidLeon for the adjustment of TracedException to Exception and creating the patch.
Frank Benoit <benoit@tionex.de>
parents:
52
diff
changeset
|
349 this( Exception e ){ |
15 | 350 super(e.toString); |
351 } | |
352 } | |
353 | |
79 | 354 private struct GCStats { |
40 | 355 size_t poolsize; // total size of pool |
356 size_t usedsize; // bytes allocated | |
357 size_t freeblocks; // number of blocks marked FREE | |
358 size_t freelistsize; // total of memory on free lists | |
359 size_t pageblocks; // number of blocks marked PAGE | |
360 } | |
79 | 361 private extern(C) GCStats gc_stats(); |
362 | |
40 | 363 size_t RuntimeTotalMemory(){ |
364 GCStats s = gc_stats(); | |
365 return s.poolsize; | |
366 } | |
15 | 367 |
368 | |
369 | |
370 |