Mercurial > projects > orange
comparison orange/util/string.d @ 1:11a31bd929f9
Removed dependency on private library
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Mon, 31 May 2010 16:06:36 +0200 |
parents | f7b078e85f7f |
children | 99c52d46822a |
comparison
equal
deleted
inserted
replaced
0:f7b078e85f7f | 1:11a31bd929f9 |
---|---|
1 /** | 1 /** |
2 * Copyright: Copyright (c) 2010 Jacob Carlborg. | 2 * Copyright: Copyright (c) 2008-2009 Jacob Carlborg. All rights reserved. |
3 * Authors: Jacob Carlborg | 3 * Authors: Jacob Carlborg |
4 * Version: Initial created: Jan 26, 2010 | 4 * Version: Initial created: 2008 |
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) | 5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) |
6 * | |
6 */ | 7 */ |
7 module orange.util.string; | 8 module orange.util.string; |
8 | 9 |
9 public import mambo.string; | 10 public import orange.util.collection.Array; |
11 | |
12 version (Tango) | |
13 { | |
14 static import tango.stdc.stringz; | |
15 import tango.text.Unicode : toFold, isDigit; | |
16 import tango.text.convert.Utf; | |
17 import tango.text.Util; | |
18 | |
19 alias tango.stdc.stringz.toStringz toStringz; | |
20 alias tango.stdc.stringz.toString16z toString16z; | |
21 alias tango.stdc.stringz.toString32z toString32z; | |
22 | |
23 alias tango.stdc.stringz.fromStringz fromStringz; | |
24 alias tango.stdc.stringz.fromString16z fromString16z; | |
25 alias tango.stdc.stringz.fromString32z fromString32z; | |
26 | |
27 alias tango.text.convert.Utf.toString16 toString16; | |
28 alias tango.text.convert.Utf.toString32 toString32; | |
29 } | |
30 | |
31 else | |
32 { | |
33 import std.string; | |
34 import std.utf; | |
35 import std.ctype : isxdigit; | |
36 | |
37 version = Phobos; | |
38 | |
39 private alias std.string.tolower toFold; | |
40 | |
41 alias std.utf.toUTF8 toString; | |
42 alias std.utf.toUTF16 toString16; | |
43 alias std.utf.toUTF32 toString32; | |
44 | |
45 alias std.string.toStringz toStringz; | |
46 alias std.utf.toUTF16z toString16z; | |
47 | |
48 alias std.string.toString fromStringz; | |
49 } | |
50 | |
51 version (Tango) | |
52 { | |
53 /** | |
54 * string alias | |
55 */ | |
56 alias char[] string; | |
57 | |
58 /** | |
59 * wstring alias | |
60 */ | |
61 alias wchar[] wstring; | |
62 | |
63 /** | |
64 * dstring alias | |
65 */ | |
66 alias dchar[] dstring; | |
67 } | |
68 | |
69 /** | |
70 * Compares the $(D_PSYMBOL string) to another $(D_PSYMBOL string), ignoring case | |
71 * considerations. Two strings are considered equal ignoring case if they are of the | |
72 * same length and corresponding characters in the two strings are equal ignoring case. | |
73 * | |
74 * Params: | |
75 * str = The $(D_PSYMBOL string) first string to compare to | |
76 * anotherString = The $(D_PSYMBOL string) to compare the first $(D_PSYMBOL string) with | |
77 * | |
78 * Returns: $(D_KEYWORD true) if the arguments is not $(D_KEYWORD null) and it | |
79 * represents an equivalent $(D_PSYMBOL string) ignoring case; $(D_KEYWORD false) otherwise | |
80 * | |
81 * Throws: AssertException if the length of any of the strings is 0 | |
82 * | |
83 * See_Also: opEquals(Object) | |
84 */ | |
85 bool equalsIgnoreCase (string str, string anotherString) | |
86 in | |
87 { | |
88 assert(str.length > 0, "mambo.string.equalsIgnoreCase: The length of the first string was 0"); | |
89 assert(anotherString.length > 0, "mambo.string.equalsIgnoreCase: The length of the second string was 0"); | |
90 } | |
91 body | |
92 { | |
93 if (str == anotherString) | |
94 return true; | |
95 | |
96 return toFold(str) == toFold(anotherString); | |
97 } | |
98 | |
99 /** | |
100 * Compares the $(D_PSYMBOL wstring) to another $(D_PSYMBOL wstring), ignoring case | |
101 * considerations. Two wstrings are considered equal ignoring case if they are of the | |
102 * same length and corresponding characters in the two wstrings are equal ignoring case. | |
103 * | |
104 * Params: | |
105 * str = The $(D_PSYMBOL wstring) first string to compre to | |
106 * anotherString = The $(D_PSYMBOL wstring) to compare the first $(D_PSYMBOL wstring) against | |
107 * | |
108 * Returns: $(D_KEYWORD true) if the argument is not $(D_KEYWORD null) and it | |
109 * represents an equivalent $(D_PSYMBOL wstring) ignoring case; (D_KEYWORD | |
110 * false) otherwise | |
111 * | |
112 * Throws: AssertException if the length of any of the wstrings is 0 | |
113 * | |
114 * See_Also: opEquals(Object) | |
115 */ | |
116 bool equalsIgnoreCase (wstring str, wstring anotherString) | |
117 in | |
118 { | |
119 assert(str.length > 0, "mambo.string.equalsIgnoreCase: The length of the first string was 0"); | |
120 assert(anotherString.length > 0, "mambo.string.equalsIgnoreCase: The length of the second string was 0"); | |
121 } | |
122 body | |
123 { | |
124 if (str == anotherString) | |
125 return true; | |
126 | |
127 version (Tango) | |
128 return toFold(str) == toFold(anotherString); | |
129 | |
130 else | |
131 return toFold(toUTF8(str)) == toFold(toUTF8(anotherString)); | |
132 } | |
133 | |
134 /** | |
135 * Compares the $(D_PSYMBOL dstring) to another $(D_PSYMBOL dstring), ignoring case | |
136 * considerations. Two wstrings are considered equal ignoring case if they are of the | |
137 * same length and corresponding characters in the two wstrings are equal ignoring case. | |
138 * | |
139 * Params: | |
140 * str = The $(D_PSYMBOL dstring) first string to compare to | |
141 * anotherString = The $(D_PSYMBOL wstring) to compare the first $(D_PSYMBOL dstring) against | |
142 * | |
143 * Returns: $(D_KEYWORD true) if the argument is not $(D_KEYWORD null) and it | |
144 * represents an equivalent $(D_PSYMBOL dstring) ignoring case; $(D_KEYWORD false) otherwise | |
145 * | |
146 * Throws: AssertException if the length of any of the dstrings are 0 | |
147 * | |
148 * See_Also: opEquals(Object) | |
149 */ | |
150 bool equalsIgnoreCase (dstring str, dstring anotherString) | |
151 in | |
152 { | |
153 assert(str.length > 0, "mambo.string.equalsIgnoreCase: The length of the first string was 0"); | |
154 assert(anotherString.length > 0, "mambo.string.equalsIgnoreCase: The length of the second string was 0"); | |
155 } | |
156 body | |
157 { | |
158 if (str == anotherString) | |
159 return true; | |
160 | |
161 version (Tango) | |
162 return toFold(str) == toFold(anotherString); | |
163 | |
164 else | |
165 return toFold(toUTF8(str)) == toFold(toUTF8(anotherString)); | |
166 } | |
167 | |
168 /** | |
169 * Returns the char value at the specified index. An index ranges from 0 to length - 1. | |
170 * The first $(D_KEYWORD char) value of the sequence is at index 0, the next at index 1, | |
171 * and so on, as for array indexing. | |
172 * | |
173 * Params: | |
174 * str = the string to get the $(D_KEYWORD char) from | |
175 * index = the index of the $(D_KEYWORD char) value. | |
176 * | |
177 * Returns: the $(D_KEYWORD char) value at the specified index of the string. | |
178 * The first $(D_KEYWORD char) value is at index 0. | |
179 * | |
180 * Throws: AssertException if the length of the string is 0 | |
181 * Throws: AssertException if the $(D_CODE index) argument is | |
182 * not less than the length of the string. | |
183 */ | |
184 char charAt (string str, size_t index) | |
185 in | |
186 { | |
187 assert(str.length > 0, "mambo.string.charAt: The length of the string was 0"); | |
188 assert(index <= str.length, "mambo.string.charAt: The index was to greater than the length of the string"); | |
189 } | |
190 body | |
191 { | |
192 return str[index]; | |
193 } | |
194 | |
195 /** | |
196 * Returns the $(D_KEYWORD char) value at the specified index. An index ranges from 0 to | |
197 * length - 1. The first $(D_KEYWORD char) value of the sequence is at index 0, the next | |
198 * at index 1, and so on, as for array indexing. | |
199 * | |
200 * Params: | |
201 * str = the wstring to get the $(D_KEYWORD char) from | |
202 * index = the index of the $(D_KEYWORD char) value. | |
203 * | |
204 * Returns: the $(D_KEYWORD char) value at the specified index of the wstring. | |
205 * The first $(D_KEYWORD char) value is at index 0. | |
206 * | |
207 * Throws: AssertException if the length of the wstring is 0 | |
208 * Throws: AssertException if the $(D_CODE index) argument is | |
209 * not less than the length of the wstring. | |
210 */ | |
211 wchar charAt (wstring str, size_t index) | |
212 in | |
213 { | |
214 assert(str.length > 0, "mambo.string.charAt: The length of the string was 0"); | |
215 assert(index <= str.length, "mambo.string.charAt: The index was to greater than the length of the string"); | |
216 } | |
217 body | |
218 { | |
219 return str[index]; | |
220 } | |
221 | |
222 /** | |
223 * Returns the $(D_KEYWORD char) value at the specified index. An index ranges from 0 to | |
224 * length - 1. The first $(D_KEYWORD char) value of the sequence is at index 0, the next | |
225 * at index 1, and so on, as for array indexing. | |
226 * | |
227 * Params: | |
228 * str = the dstring to get the $(D_KEYWORD char) from | |
229 * index = the index of the $(D_KEYWORD char) value. | |
230 * | |
231 * Returns: the $(D_KEYWORD char) value at the specified index of the dstring. | |
232 * The first $(D_KEYWORD char) value is at index 0. | |
233 * | |
234 * Throws: AssertException if the length of the dstring is 0 | |
235 * Throws: AssertException if the $(D_CODE index) argument is | |
236 * not less than the length of the dstring. | |
237 */ | |
238 dchar charAt (dstring str, size_t index) | |
239 in | |
240 { | |
241 assert(str.length > 0, "mambo.string.charAt: The length of the string was 0"); | |
242 assert(index <= str.length, "mambo.string.charAt: The index was to greater than the length of the string"); | |
243 } | |
244 body | |
245 { | |
246 return str[index]; | |
247 } | |
248 | |
249 /** | |
250 * Returns a new string that is a substring of the specified string. The substring begins | |
251 * at the specified $(D_PARAM beginIndex) and extends to the character at index | |
252 * $(D_PARAM endIndex) - 1. Thus the length of the substring is $(D_PARAM endIndex - beginIndex). | |
253 * | |
254 * Examples: | |
255 * --- | |
256 * "hamburger".substring(4, 8) returns "urge" | |
257 * "smiles".substring(1, 5) returns "mile" | |
258 * --- | |
259 * | |
260 * Params: | |
261 * str = the string to get the substring from | |
262 * beginIndex = the beginning index, inclusive. | |
263 * endIndex = the ending index, exclusive. | |
264 * | |
265 * Returns: the specified substring. | |
266 * | |
267 * Throws: AssertException if the length of the string is 0 | |
268 * Throws: AssertException if the $(D_PARAM beginIndex) is | |
269 * larger than $(D_PARAM endIndex). | |
270 * Throws: AssertException if $(D_PARAM endIndex) is larger than the | |
271 * length of the $(D_PSYMBOL string). | |
272 */ | |
273 string substring (string str, size_t beginIndex, size_t endIndex) | |
274 in | |
275 { | |
276 assert(str.length > 0, "mambo.string.substring: The length of the string was 0"); | |
277 assert(beginIndex < endIndex, "mambo.string.substring: The first index was greater the second"); | |
278 assert(endIndex <= str.length, "mambo.string.substring: The second index was greater then the length of the string"); | |
279 } | |
280 body | |
281 { | |
282 return str[beginIndex .. endIndex].dup; | |
283 } | |
284 | |
285 /** | |
286 * Returns a new string that is a substring of the specified string. The substring begins | |
287 * at the specified $(D_PARAM beginIndex) and extends to the character at index | |
288 * $(D_PARAM endIndex) - 1. Thus the length of the substring is $(D_PARAM endIndex - beginIndex). | |
289 * | |
290 * Examples: | |
291 * --- | |
292 * "hamburger".substring(4, 8) returns "urge" | |
293 * "smiles".substring(1, 5) returns "mile" | |
294 * --- | |
295 * | |
296 * Params: | |
297 * str = the string to get the substring from | |
298 * beginIndex = the beginning index, inclusive. | |
299 * endIndex = the ending index, exclusive. | |
300 * | |
301 * Returns: the specified substring. | |
302 * | |
303 * Throws: AssertException if the length of the string is 0 | |
304 * Throws: AssertException if the $(D_PARAM beginIndex) is | |
305 * larger than $(D_PARAM endIndex). | |
306 * Throws: AssertException if $(D_PARAM endIndex) is larger than the | |
307 * length of the $(D_PSYMBOL string). | |
308 */ | |
309 wstring substring (wstring str, size_t beginIndex, size_t endIndex) | |
310 in | |
311 { | |
312 assert(str.length > 0, "mambo.string.substring: The length of the string was 0"); | |
313 assert(beginIndex < endIndex, "mambo.string.substring: The first index was greater the second"); | |
314 assert(endIndex <= str.length, "mambo.string.substring: The second index was greater then the length of the string"); | |
315 } | |
316 body | |
317 { | |
318 return str[beginIndex .. endIndex].dup; | |
319 } | |
320 | |
321 /** | |
322 * Returns a new string that is a substring of the specified string. The substring begins | |
323 * at the specified $(D_PARAM beginIndex) and extends to the character at index | |
324 * $(D_PARAM endIndex) - 1. Thus the length of the substring is $(D_PARAM endIndex - beginIndex). | |
325 * | |
326 * Examples: | |
327 * --- | |
328 * "hamburger".substring(4, 8) returns "urge" | |
329 * "smiles".substring(1, 5) returns "mile" | |
330 * --- | |
331 * | |
332 * Params: | |
333 * str = the string to get the substring from | |
334 * beginIndex = the beginning index, inclusive. | |
335 * endIndex = the ending index, exclusive. | |
336 * | |
337 * Returns: the specified substring. | |
338 * | |
339 * Throws: AssertException if the length of the string is 0 | |
340 * Throws: AssertException if the $(D_PARAM beginIndex) is | |
341 * larger than $(D_PARAM endIndex). | |
342 * Throws: AssertException if $(D_PARAM endIndex) is larger than the | |
343 * length of the $(D_PSYMBOL string). | |
344 */ | |
345 dstring substring (dstring str, size_t beginIndex, size_t endIndex) | |
346 in | |
347 { | |
348 assert(str.length > 0, "mambo.string.substring: The length of the string was 0"); | |
349 assert(beginIndex < endIndex, "mambo.string.substring: The first index was greater the second"); | |
350 assert(endIndex <= str.length, "mambo.string.substring: The second index was greater then the length of the string"); | |
351 } | |
352 body | |
353 { | |
354 return str[beginIndex .. endIndex].dup; | |
355 } | |
356 | |
357 /** | |
358 * Returns a new string that is a substring of the specified string. The substring begins | |
359 * with the character at the specified index and extends to the end of the string. | |
360 * | |
361 * Examples: | |
362 * --- | |
363 * "unhappy".substring(2) returns "happy" | |
364 * "Harbison".substring(3) returns "bison" | |
365 * "emptiness".substring(9) returns "" (an empty string) | |
366 * --- | |
367 * | |
368 * Params: | |
369 * str = the string to get the substring from | |
370 * beginIndex = the beginning index, inclusive | |
371 * | |
372 * Returns: the specified substring | |
373 * | |
374 * Throws: AssertException if the length of the string is 0 | |
375 * Throws: AssertException if the $(D_PARAM beginIndex) is | |
376 * larger than the length of the string. | |
377 */ | |
378 string substring (string str, size_t index) | |
379 in | |
380 { | |
381 assert(str.length > 0, "mambo.string.substring: The length of the string was 0"); | |
382 assert(index < str.length, "mambo.string.substring: The index was greater than the length of the string"); | |
383 } | |
384 body | |
385 { | |
386 return str.substring(index, str.length); | |
387 } | |
388 | |
389 /** | |
390 * Returns a new string that is a substring of the specified string. The substring begins | |
391 * with the character at the specified index and extends to the end of the string. | |
392 * | |
393 * Examples: | |
394 * --- | |
395 * "unhappy".substring(2) returns "happy" | |
396 * "Harbison".substring(3) returns "bison" | |
397 * "emptiness".substring(9) returns "" (an empty string) | |
398 * --- | |
399 * | |
400 * Params: | |
401 * str = the string to get the substring from | |
402 * beginIndex = the beginning index, inclusive | |
403 * | |
404 * Returns: the specified substring | |
405 * | |
406 * Throws: AssertException if the length of the string is 0 | |
407 * Throws: AssertException if the $(D_PARAM beginIndex) is | |
408 * larger than the length of the string. | |
409 */ | |
410 wstring substring (wstring str, size_t index) | |
411 in | |
412 { | |
413 assert(str.length > 0, "mambo.string.substring: The length of the string was 0"); | |
414 assert(index < str.length, "mambo.string.substring: The index was greater than the length of the string"); | |
415 } | |
416 body | |
417 { | |
418 return str.substring(index, str.length); | |
419 } | |
420 | |
421 /** | |
422 * Returns a new string that is a substring of the specified string. The substring begins | |
423 * with the character at the specified index and extends to the end of the string. | |
424 * | |
425 * Examples: | |
426 * --- | |
427 * "unhappy".substring(2) returns "happy" | |
428 * "Harbison".substring(3) returns "bison" | |
429 * "emptiness".substring(9) returns "" (an empty string) | |
430 * --- | |
431 * | |
432 * Params: | |
433 * str = the string to get the substring from | |
434 * beginIndex = the beginning index, inclusive | |
435 * | |
436 * Returns: the specified substring | |
437 * | |
438 * Throws: AssertException if the length of the string is 0 | |
439 * Throws: AssertException if the $(D_PARAM beginIndex) is | |
440 * larger than the length of the string. | |
441 */ | |
442 dstring substring (dstring str, size_t index) | |
443 in | |
444 { | |
445 assert(str.length > 0, "mambo.string.substring: The length of the string was 0"); | |
446 assert(index < str.length, "mambo.string.substring: The index was greater than the length of the string"); | |
447 } | |
448 body | |
449 { | |
450 return str.substring(index, str.length); | |
451 } | |
452 | |
453 /** | |
454 * Returns a new string that is a substring of the given string. | |
455 * | |
456 * This substring is the character sequence that starts at character | |
457 * position pos and has a length of n characters. | |
458 * | |
459 * Params: | |
460 * str = the string to get the substring from | |
461 * pos = position of a character in the current string to be used | |
462 * as starting character for the substring. | |
463 * n = Length of the substring. If this value would make the | |
464 * substring to span past the end of the current string content, | |
465 * only those characters until the end of the string are used. | |
466 * size_t.max is the greatest possible value for an element of | |
467 * type size_t, therefore, when this value is used, all the | |
468 * characters between pos and the end of the string are used as | |
469 * the initialization substring. | |
470 * | |
471 * Returns: a string containing a substring of the given string | |
472 * | |
473 * Throws: AssertException if pos is greater than the length of the string | |
474 */ | |
475 string substr (string str, size_t pos = 0, size_t n = size_t.max) | |
476 in | |
477 { | |
478 assert(pos < str.length, "mambo.string.substr: The given position was greater than the length of the string."); | |
479 } | |
480 body | |
481 { | |
482 size_t end; | |
483 | |
484 if (n == size_t.max) | |
485 end = str.length; | |
486 | |
487 else | |
488 { | |
489 end = pos + n; | |
490 | |
491 if (end > str.length) | |
492 end = str.length; | |
493 } | |
494 | |
495 return str[pos .. end].dup; | |
496 } | |
497 | |
498 /** | |
499 * Returns a new string that is a substring of the given string. | |
500 * | |
501 * This substring is the character sequence that starts at character | |
502 * position pos and has a length of n characters. | |
503 * | |
504 * Params: | |
505 * str = the string to get the substring from | |
506 * pos = position of a character in the current string to be used | |
507 * as starting character for the substring. | |
508 * n = Length of the substring. If this value would make the | |
509 * substring to span past the end of the current string content, | |
510 * only those characters until the end of the string are used. | |
511 * size_t.max is the greatest possible value for an element of | |
512 * type size_t, therefore, when this value is used, all the | |
513 * characters between pos and the end of the string are used as | |
514 * the initialization substring. | |
515 * | |
516 * Returns: a string containing a substring of the given string | |
517 * | |
518 * Throws: AssertException if pos is greater than the length of the string | |
519 */ | |
520 wstring substr (wstring str, size_t pos = 0, size_t n = size_t.max) | |
521 in | |
522 { | |
523 assert(pos < str.length, "mambo.string.substr: The given position was greater than the length of the string."); | |
524 } | |
525 body | |
526 { | |
527 size_t end; | |
528 | |
529 if (n == size_t.max) | |
530 end = str.length; | |
531 | |
532 else | |
533 { | |
534 end = pos + n; | |
535 | |
536 if (end > str.length) | |
537 end = str.length; | |
538 } | |
539 | |
540 return str[pos .. end].dup; | |
541 } | |
542 | |
543 /** | |
544 * Returns a new string that is a substring of the given string. | |
545 * | |
546 * This substring is the character sequence that starts at character | |
547 * position pos and has a length of n characters. | |
548 * | |
549 * Params: | |
550 * str = the string to get the substring from | |
551 * pos = position of a character in the current string to be used | |
552 * as starting character for the substring. | |
553 * n = Length of the substring. If this value would make the | |
554 * substring to span past the end of the current string content, | |
555 * only those characters until the end of the string are used. | |
556 * size_t.max is the greatest possible value for an element of | |
557 * type size_t, therefore, when this value is used, all the | |
558 * characters between pos and the end of the string are used as | |
559 * the initialization substring. | |
560 * | |
561 * Returns: a string containing a substring of the given string | |
562 * | |
563 * Throws: AssertException if pos is greater than the length of the string | |
564 */ | |
565 dstring substr (dstring str, size_t pos = 0, size_t n = size_t.max) | |
566 in | |
567 { | |
568 assert(pos < str.length, "mambo.string.substr: The given position was greater than the length of the string."); | |
569 } | |
570 body | |
571 { | |
572 size_t end; | |
573 | |
574 if (n == size_t.max) | |
575 end = str.length; | |
576 | |
577 else | |
578 { | |
579 end = pos + n; | |
580 | |
581 if (end > str.length) | |
582 end = str.length; | |
583 } | |
584 | |
585 return str[pos .. end].dup; | |
586 } | |
587 | |
588 /** | |
589 * Finds the first occurence of sub in str | |
590 * | |
591 * Params: | |
592 * str = the string to find in | |
593 * sub = the substring to find | |
594 * start = where to start finding | |
595 * | |
596 * Returns: the index of the substring or size_t.max when nothing was found | |
597 */ | |
598 size_t find (string str, string sub, size_t start = 0) | |
599 { | |
600 version (Tango) | |
601 { | |
602 size_t index = str.locatePattern(sub, start); | |
603 | |
604 if (index == str.length) | |
605 return size_t.max; | |
606 | |
607 return index; | |
608 } | |
609 | |
610 else | |
611 return std.string.find(str, sub, start); | |
612 } | |
613 | |
614 /** | |
615 * Finds the first occurence of sub in str | |
616 * | |
617 * Params: | |
618 * str = the string to find in | |
619 * sub = the substring to find | |
620 * start = where to start finding | |
621 * | |
622 * Returns: the index of the substring or size_t.max when nothing was found | |
623 */ | |
624 size_t find (wstring str, wstring sub, size_t start = 0) | |
625 { | |
626 version (Tango) | |
627 { | |
628 size_t index = str.locatePattern(sub, start); | |
629 | |
630 if (index == str.length) | |
631 return size_t.max; | |
632 | |
633 return index; | |
634 } | |
635 | |
636 else | |
637 return std.string.find(str, sub, start); | |
638 } | |
639 | |
640 /** | |
641 * Finds the first occurence of sub in str | |
642 * | |
643 * Params: | |
644 * str = the string to find in | |
645 * sub = the substring to find | |
646 * start = where to start finding | |
647 * | |
648 * Returns: the index of the substring or size_t.max when nothing was found | |
649 */ | |
650 size_t find (dstring str, dstring sub, size_t start = 0) | |
651 { | |
652 version (Tango) | |
653 { | |
654 size_t index = str.locatePattern(sub, start); | |
655 | |
656 if (index == str.length) | |
657 return size_t.max; | |
658 | |
659 return index; | |
660 } | |
661 | |
662 else | |
663 return std.string.find(str, sub, start); | |
664 } | |
665 | |
666 /** | |
667 * Compares to strings, ignoring case differences. Returns 0 if the content | |
668 * matches, less than zero if a is "less" than b, or greater than zero | |
669 * where a is "bigger". | |
670 * | |
671 * Params: | |
672 * a = the first array | |
673 * b = the second array | |
674 * end = the index where the comparision will end | |
675 * | |
676 * Returns: Returns 0 if the content matches, less than zero if a is | |
677 * "less" than b, or greater than zero where a is "bigger". | |
678 * | |
679 * See_Also: mambo.collection.array.compare | |
680 */ | |
681 int compareIgnoreCase (U = size_t) (string a, string b, U end = U.max) | |
682 { | |
683 return a.toFold().compare(b.toFold(), end); | |
684 } | |
685 | |
686 /** | |
687 * Compares to strings, ignoring case differences. Returns 0 if the content | |
688 * matches, less than zero if a is "less" than b, or greater than zero | |
689 * where a is "bigger". | |
690 * | |
691 * Params: | |
692 * a = the first array | |
693 * b = the second array | |
694 * end = the index where the comparision will end | |
695 * | |
696 * Returns: Returns 0 if the content matches, less than zero if a is | |
697 * "less" than b, or greater than zero where a is "bigger". | |
698 * | |
699 * See_Also: mambo.collection.array.compare | |
700 */ | |
701 int compareIgnoreCase (U = size_t) (wstring a, wstring b, U end = U.max) | |
702 { | |
703 return a.toFold().compare(b.toFold(), end); | |
704 } | |
705 | |
706 /** | |
707 * Compares to strings, ignoring case differences. Returns 0 if the content | |
708 * matches, less than zero if a is "less" than b, or greater than zero | |
709 * where a is "bigger". | |
710 * | |
711 * Params: | |
712 * a = the first array | |
713 * b = the second array | |
714 * end = the index where the comparision will end | |
715 * | |
716 * Returns: Returns 0 if the content matches, less than zero if a is | |
717 * "less" than b, or greater than zero where a is "bigger". | |
718 * | |
719 * See_Also: mambo.collection.array.compare | |
720 */ | |
721 int compareIgnoreCase (U = size_t) (dstring a, dstring b, U end = U.max) | |
722 { | |
723 return a.toFold().compare(b.toFold(), end); | |
724 } | |
725 | |
726 /** | |
727 * Compares to strings, ignoring case differences. Returns 0 if the content | |
728 * matches, less than zero if a is "less" than b, or greater than zero | |
729 * where a is "bigger". | |
730 * | |
731 * Params: | |
732 * a = the first array | |
733 * b = the second array | |
734 * end = the index where the comparision will end | |
735 * | |
736 * Returns: Returns 0 if the content matches, less than zero if a is | |
737 * "less" than b, or greater than zero where a is "bigger". | |
738 * | |
739 * See_Also: mambo.string.compareIgnoreCase | |
740 */ | |
741 alias compareIgnoreCase icompare; | |
742 | |
743 /** | |
744 * Checks if the given character is a hexdecimal digit character. | |
745 * Hexadecimal digits are any of: 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F | |
746 * | |
747 * Params: | |
748 * ch = the character to be checked | |
749 * | |
750 * Returns: true if the given character is a hexdecimal digit character otherwise false | |
751 */ | |
752 bool isHexDigit (dchar ch) | |
753 { | |
754 version (Tango) | |
755 { | |
756 switch (ch) | |
757 { | |
758 case 'A': return true; | |
759 case 'B': return true; | |
760 case 'C': return true; | |
761 case 'D': return true; | |
762 case 'E': return true; | |
763 case 'F': return true; | |
764 | |
765 case 'a': return true; | |
766 case 'b': return true; | |
767 case 'c': return true; | |
768 case 'd': return true; | |
769 case 'e': return true; | |
770 case 'f': return true; | |
771 | |
772 default: break; | |
773 } | |
774 | |
775 if (isDigit(ch)) | |
776 return true; | |
777 } | |
778 | |
779 else | |
780 if (isxdigit(ch) != 0) | |
781 return true; | |
782 | |
783 return false; | |
784 } | |
785 | |
786 /*version (Tango) | |
787 { | |
788 string toString (string str) | |
789 { | |
790 return str; | |
791 } | |
792 | |
793 string toString (wstring str) | |
794 { | |
795 return tango.text.convert.Utf.toString(str); | |
796 } | |
797 | |
798 string toString (dstring str) | |
799 { | |
800 return tango.text.convert.Utf.toString(str); | |
801 } | |
802 }*/ | |
803 | |
804 version (Phobos) | |
805 { | |
806 /** | |
807 * Converts the given string to C-style 0 terminated string. | |
808 * | |
809 * Params: | |
810 * str = the string to convert | |
811 * | |
812 * Returns: the a C-style 0 terminated string. | |
813 */ | |
814 dchar* toString32z (dstring str) | |
815 { | |
816 return (str ~ '\0').ptr; | |
817 } | |
818 | |
819 /** | |
820 * Converts a C-style 0 terminated string to a wstring | |
821 * | |
822 * Params: | |
823 * str = the C-style 0 terminated string | |
824 * | |
825 * Returns: the converted wstring | |
826 */ | |
827 wstring fromString16z (wchar* str) | |
828 { | |
829 return str[0 .. strlen(str)]; | |
830 } | |
831 | |
832 /** | |
833 * Converts a C-style 0 terminated string to a dstring | |
834 * Params: | |
835 * str = the C-style 0 terminated string | |
836 * | |
837 * Returns: the converted dstring | |
838 */ | |
839 dstring fromString32z (dchar* str) | |
840 { | |
841 return str[0 .. strlen(str)]; | |
842 } | |
843 | |
844 /** | |
845 * Gets the length of the given C-style 0 terminated string | |
846 * | |
847 * Params: | |
848 * str = the C-style 0 terminated string to get the length of | |
849 * | |
850 * Returns: the length of the string | |
851 */ | |
852 size_t strlen (wchar* str) | |
853 { | |
854 size_t i = 0; | |
855 | |
856 if (str) | |
857 while(*str++) | |
858 ++i; | |
859 | |
860 return i; | |
861 } | |
862 | |
863 /** | |
864 * Gets the length of the given C-style 0 terminated string | |
865 * | |
866 * Params: | |
867 * str = the C-style 0 terminated string to get the length of | |
868 * | |
869 * Returns: the length of the string | |
870 */ | |
871 size_t strlen (dchar* str) | |
872 { | |
873 size_t i = 0; | |
874 | |
875 if (str) | |
876 while(*str++) | |
877 ++i; | |
878 | |
879 return i; | |
880 } | |
881 } |