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