Mercurial > projects > orange
comparison orange/core/string.d @ 26:78e5fef4bbf2 experimental
Third step in refactoring the API. Stating to add unit tests.
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Tue, 19 Oct 2010 10:22:10 +0200 |
parents | |
children | fc315d786f24 |
comparison
equal
deleted
inserted
replaced
25:b51e953f79eb | 26:78e5fef4bbf2 |
---|---|
1 /** | |
2 * Copyright: Copyright (c) 2008-2009 Jacob Carlborg. All rights reserved. | |
3 * Authors: Jacob Carlborg | |
4 * Version: Initial created: 2008 | |
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) | |
6 * | |
7 */ | |
8 module orange.core.string; | |
9 | |
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 version (Tango) return str[beginIndex .. endIndex].dup; | |
283 else return str[beginIndex .. endIndex].idup; | |
284 } | |
285 | |
286 /** | |
287 * Returns a new string that is a substring of the specified string. The substring begins | |
288 * at the specified $(D_PARAM beginIndex) and extends to the character at index | |
289 * $(D_PARAM endIndex) - 1. Thus the length of the substring is $(D_PARAM endIndex - beginIndex). | |
290 * | |
291 * Examples: | |
292 * --- | |
293 * "hamburger".substring(4, 8) returns "urge" | |
294 * "smiles".substring(1, 5) returns "mile" | |
295 * --- | |
296 * | |
297 * Params: | |
298 * str = the string to get the substring from | |
299 * beginIndex = the beginning index, inclusive. | |
300 * endIndex = the ending index, exclusive. | |
301 * | |
302 * Returns: the specified substring. | |
303 * | |
304 * Throws: AssertException if the length of the string is 0 | |
305 * Throws: AssertException if the $(D_PARAM beginIndex) is | |
306 * larger than $(D_PARAM endIndex). | |
307 * Throws: AssertException if $(D_PARAM endIndex) is larger than the | |
308 * length of the $(D_PSYMBOL string). | |
309 */ | |
310 wstring substring (wstring str, size_t beginIndex, size_t endIndex) | |
311 in | |
312 { | |
313 assert(str.length > 0, "mambo.string.substring: The length of the string was 0"); | |
314 assert(beginIndex < endIndex, "mambo.string.substring: The first index was greater the second"); | |
315 assert(endIndex <= str.length, "mambo.string.substring: The second index was greater then the length of the string"); | |
316 } | |
317 body | |
318 { | |
319 version (Tango) return str[beginIndex .. endIndex].dup; | |
320 else return str[beginIndex .. endIndex].idup; | |
321 } | |
322 | |
323 /** | |
324 * Returns a new string that is a substring of the specified string. The substring begins | |
325 * at the specified $(D_PARAM beginIndex) and extends to the character at index | |
326 * $(D_PARAM endIndex) - 1. Thus the length of the substring is $(D_PARAM endIndex - beginIndex). | |
327 * | |
328 * Examples: | |
329 * --- | |
330 * "hamburger".substring(4, 8) returns "urge" | |
331 * "smiles".substring(1, 5) returns "mile" | |
332 * --- | |
333 * | |
334 * Params: | |
335 * str = the string to get the substring from | |
336 * beginIndex = the beginning index, inclusive. | |
337 * endIndex = the ending index, exclusive. | |
338 * | |
339 * Returns: the specified substring. | |
340 * | |
341 * Throws: AssertException if the length of the string is 0 | |
342 * Throws: AssertException if the $(D_PARAM beginIndex) is | |
343 * larger than $(D_PARAM endIndex). | |
344 * Throws: AssertException if $(D_PARAM endIndex) is larger than the | |
345 * length of the $(D_PSYMBOL string). | |
346 */ | |
347 dstring substring (dstring str, size_t beginIndex, size_t endIndex) | |
348 in | |
349 { | |
350 assert(str.length > 0, "mambo.string.substring: The length of the string was 0"); | |
351 assert(beginIndex < endIndex, "mambo.string.substring: The first index was greater the second"); | |
352 assert(endIndex <= str.length, "mambo.string.substring: The second index was greater then the length of the string"); | |
353 } | |
354 body | |
355 { | |
356 version (Tango) return str[beginIndex .. endIndex].dup; | |
357 else return str[beginIndex .. endIndex].idup; | |
358 } | |
359 | |
360 /** | |
361 * Returns a new string that is a substring of the specified string. The substring begins | |
362 * with the character at the specified index and extends to the end of the string. | |
363 * | |
364 * Examples: | |
365 * --- | |
366 * "unhappy".substring(2) returns "happy" | |
367 * "Harbison".substring(3) returns "bison" | |
368 * "emptiness".substring(9) returns "" (an empty string) | |
369 * --- | |
370 * | |
371 * Params: | |
372 * str = the string to get the substring from | |
373 * beginIndex = the beginning index, inclusive | |
374 * | |
375 * Returns: the specified substring | |
376 * | |
377 * Throws: AssertException if the length of the string is 0 | |
378 * Throws: AssertException if the $(D_PARAM beginIndex) is | |
379 * larger than the length of the string. | |
380 */ | |
381 string substring (string str, size_t index) | |
382 in | |
383 { | |
384 assert(str.length > 0, "mambo.string.substring: The length of the string was 0"); | |
385 assert(index < str.length, "mambo.string.substring: The index was greater than the length of the string"); | |
386 } | |
387 body | |
388 { | |
389 return str.substring(index, str.length); | |
390 } | |
391 | |
392 /** | |
393 * Returns a new string that is a substring of the specified string. The substring begins | |
394 * with the character at the specified index and extends to the end of the string. | |
395 * | |
396 * Examples: | |
397 * --- | |
398 * "unhappy".substring(2) returns "happy" | |
399 * "Harbison".substring(3) returns "bison" | |
400 * "emptiness".substring(9) returns "" (an empty string) | |
401 * --- | |
402 * | |
403 * Params: | |
404 * str = the string to get the substring from | |
405 * beginIndex = the beginning index, inclusive | |
406 * | |
407 * Returns: the specified substring | |
408 * | |
409 * Throws: AssertException if the length of the string is 0 | |
410 * Throws: AssertException if the $(D_PARAM beginIndex) is | |
411 * larger than the length of the string. | |
412 */ | |
413 wstring substring (wstring str, size_t index) | |
414 in | |
415 { | |
416 assert(str.length > 0, "mambo.string.substring: The length of the string was 0"); | |
417 assert(index < str.length, "mambo.string.substring: The index was greater than the length of the string"); | |
418 } | |
419 body | |
420 { | |
421 return str.substring(index, str.length); | |
422 } | |
423 | |
424 /** | |
425 * Returns a new string that is a substring of the specified string. The substring begins | |
426 * with the character at the specified index and extends to the end of the string. | |
427 * | |
428 * Examples: | |
429 * --- | |
430 * "unhappy".substring(2) returns "happy" | |
431 * "Harbison".substring(3) returns "bison" | |
432 * "emptiness".substring(9) returns "" (an empty string) | |
433 * --- | |
434 * | |
435 * Params: | |
436 * str = the string to get the substring from | |
437 * beginIndex = the beginning index, inclusive | |
438 * | |
439 * Returns: the specified substring | |
440 * | |
441 * Throws: AssertException if the length of the string is 0 | |
442 * Throws: AssertException if the $(D_PARAM beginIndex) is | |
443 * larger than the length of the string. | |
444 */ | |
445 dstring substring (dstring str, size_t index) | |
446 in | |
447 { | |
448 assert(str.length > 0, "mambo.string.substring: The length of the string was 0"); | |
449 assert(index < str.length, "mambo.string.substring: The index was greater than the length of the string"); | |
450 } | |
451 body | |
452 { | |
453 return str.substring(index, str.length); | |
454 } | |
455 | |
456 /** | |
457 * Returns a new string that is a substring of the given string. | |
458 * | |
459 * This substring is the character sequence that starts at character | |
460 * position pos and has a length of n characters. | |
461 * | |
462 * Params: | |
463 * str = the string to get the substring from | |
464 * pos = position of a character in the current string to be used | |
465 * as starting character for the substring. | |
466 * n = Length of the substring. If this value would make the | |
467 * substring to span past the end of the current string content, | |
468 * only those characters until the end of the string are used. | |
469 * size_t.max is the greatest possible value for an element of | |
470 * type size_t, therefore, when this value is used, all the | |
471 * characters between pos and the end of the string are used as | |
472 * the initialization substring. | |
473 * | |
474 * Returns: a string containing a substring of the given string | |
475 * | |
476 * Throws: AssertException if pos is greater than the length of the string | |
477 */ | |
478 string substr (string str, size_t pos = 0, size_t n = size_t.max) | |
479 in | |
480 { | |
481 assert(pos < str.length, "mambo.string.substr: The given position was greater than the length of the string."); | |
482 } | |
483 body | |
484 { | |
485 size_t end; | |
486 | |
487 if (n == size_t.max) | |
488 end = str.length; | |
489 | |
490 else | |
491 { | |
492 end = pos + n; | |
493 | |
494 if (end > str.length) | |
495 end = str.length; | |
496 } | |
497 | |
498 version (Tango) return str[pos .. end].dup; | |
499 else return str[pos .. end].idup; | |
500 } | |
501 | |
502 /** | |
503 * Returns a new string that is a substring of the given string. | |
504 * | |
505 * This substring is the character sequence that starts at character | |
506 * position pos and has a length of n characters. | |
507 * | |
508 * Params: | |
509 * str = the string to get the substring from | |
510 * pos = position of a character in the current string to be used | |
511 * as starting character for the substring. | |
512 * n = Length of the substring. If this value would make the | |
513 * substring to span past the end of the current string content, | |
514 * only those characters until the end of the string are used. | |
515 * size_t.max is the greatest possible value for an element of | |
516 * type size_t, therefore, when this value is used, all the | |
517 * characters between pos and the end of the string are used as | |
518 * the initialization substring. | |
519 * | |
520 * Returns: a string containing a substring of the given string | |
521 * | |
522 * Throws: AssertException if pos is greater than the length of the string | |
523 */ | |
524 wstring substr (wstring str, size_t pos = 0, size_t n = size_t.max) | |
525 in | |
526 { | |
527 assert(pos < str.length, "mambo.string.substr: The given position was greater than the length of the string."); | |
528 } | |
529 body | |
530 { | |
531 size_t end; | |
532 | |
533 if (n == size_t.max) | |
534 end = str.length; | |
535 | |
536 else | |
537 { | |
538 end = pos + n; | |
539 | |
540 if (end > str.length) | |
541 end = str.length; | |
542 } | |
543 | |
544 version (Tango) return str[pos .. end].dup; | |
545 else return str[pos .. end].idup; | |
546 } | |
547 | |
548 /** | |
549 * Returns a new string that is a substring of the given string. | |
550 * | |
551 * This substring is the character sequence that starts at character | |
552 * position pos and has a length of n characters. | |
553 * | |
554 * Params: | |
555 * str = the string to get the substring from | |
556 * pos = position of a character in the current string to be used | |
557 * as starting character for the substring. | |
558 * n = Length of the substring. If this value would make the | |
559 * substring to span past the end of the current string content, | |
560 * only those characters until the end of the string are used. | |
561 * size_t.max is the greatest possible value for an element of | |
562 * type size_t, therefore, when this value is used, all the | |
563 * characters between pos and the end of the string are used as | |
564 * the initialization substring. | |
565 * | |
566 * Returns: a string containing a substring of the given string | |
567 * | |
568 * Throws: AssertException if pos is greater than the length of the string | |
569 */ | |
570 dstring substr (dstring str, size_t pos = 0, size_t n = size_t.max) | |
571 in | |
572 { | |
573 assert(pos < str.length, "mambo.string.substr: The given position was greater than the length of the string."); | |
574 } | |
575 body | |
576 { | |
577 size_t end; | |
578 | |
579 if (n == size_t.max) | |
580 end = str.length; | |
581 | |
582 else | |
583 { | |
584 end = pos + n; | |
585 | |
586 if (end > str.length) | |
587 end = str.length; | |
588 } | |
589 | |
590 version (Tango) return str[pos .. end].dup; | |
591 else return str[pos .. end].idup; | |
592 } | |
593 | |
594 /** | |
595 * Finds the first occurence of sub in str | |
596 * | |
597 * Params: | |
598 * str = the string to find in | |
599 * sub = the substring to find | |
600 * start = where to start finding | |
601 * | |
602 * Returns: the index of the substring or size_t.max when nothing was found | |
603 */ | |
604 size_t find (string str, string sub) | |
605 { | |
606 version (Tango) | |
607 { | |
608 size_t index = str.locatePattern(sub); | |
609 | |
610 if (index == str.length) | |
611 return size_t.max; | |
612 | |
613 return index; | |
614 } | |
615 | |
616 else | |
617 return std.string.indexOf(str, sub); | |
618 | |
619 } | |
620 | |
621 /** | |
622 * Finds the first occurence of sub in str | |
623 * | |
624 * Params: | |
625 * str = the string to find in | |
626 * sub = the substring to find | |
627 * start = where to start finding | |
628 * | |
629 * Returns: the index of the substring or size_t.max when nothing was found | |
630 */ | |
631 size_t find (wstring str, wstring sub) | |
632 { | |
633 version (Tango) | |
634 { | |
635 size_t index = str.locatePattern(sub); | |
636 | |
637 if (index == str.length) | |
638 return size_t.max; | |
639 | |
640 return index; | |
641 } | |
642 | |
643 else | |
644 return std.string.indexOf(str, sub); | |
645 } | |
646 | |
647 /** | |
648 * Finds the first occurence of sub in str | |
649 * | |
650 * Params: | |
651 * str = the string to find in | |
652 * sub = the substring to find | |
653 * start = where to start finding | |
654 * | |
655 * Returns: the index of the substring or size_t.max when nothing was found | |
656 */ | |
657 size_t find (dstring str, dstring sub) | |
658 { | |
659 version (Tango) | |
660 { | |
661 size_t index = str.locatePattern(sub); | |
662 | |
663 if (index == str.length) | |
664 return size_t.max; | |
665 | |
666 return index; | |
667 } | |
668 | |
669 else | |
670 return std.string.indexOf(str, sub); | |
671 } | |
672 | |
673 /** | |
674 * Compares to strings, ignoring case differences. Returns 0 if the content | |
675 * matches, less than zero if a is "less" than b, or greater than zero | |
676 * where a is "bigger". | |
677 * | |
678 * Params: | |
679 * a = the first array | |
680 * b = the second array | |
681 * end = the index where the comparision will end | |
682 * | |
683 * Returns: Returns 0 if the content matches, less than zero if a is | |
684 * "less" than b, or greater than zero where a is "bigger". | |
685 * | |
686 * See_Also: mambo.collection.array.compare | |
687 */ | |
688 int compareIgnoreCase (U = size_t) (string a, string b, U end = U.max) | |
689 { | |
690 return a.toFold().compare(b.toFold(), end); | |
691 } | |
692 | |
693 /** | |
694 * Compares to strings, ignoring case differences. Returns 0 if the content | |
695 * matches, less than zero if a is "less" than b, or greater than zero | |
696 * where a is "bigger". | |
697 * | |
698 * Params: | |
699 * a = the first array | |
700 * b = the second array | |
701 * end = the index where the comparision will end | |
702 * | |
703 * Returns: Returns 0 if the content matches, less than zero if a is | |
704 * "less" than b, or greater than zero where a is "bigger". | |
705 * | |
706 * See_Also: mambo.collection.array.compare | |
707 */ | |
708 int compareIgnoreCase (U = size_t) (wstring a, wstring b, U end = U.max) | |
709 { | |
710 return a.toFold().compare(b.toFold(), end); | |
711 } | |
712 | |
713 /** | |
714 * Compares to strings, ignoring case differences. Returns 0 if the content | |
715 * matches, less than zero if a is "less" than b, or greater than zero | |
716 * where a is "bigger". | |
717 * | |
718 * Params: | |
719 * a = the first array | |
720 * b = the second array | |
721 * end = the index where the comparision will end | |
722 * | |
723 * Returns: Returns 0 if the content matches, less than zero if a is | |
724 * "less" than b, or greater than zero where a is "bigger". | |
725 * | |
726 * See_Also: mambo.collection.array.compare | |
727 */ | |
728 int compareIgnoreCase (U = size_t) (dstring a, dstring b, U end = U.max) | |
729 { | |
730 return a.toFold().compare(b.toFold(), end); | |
731 } | |
732 | |
733 /** | |
734 * Compares to strings, ignoring case differences. Returns 0 if the content | |
735 * matches, less than zero if a is "less" than b, or greater than zero | |
736 * where a is "bigger". | |
737 * | |
738 * Params: | |
739 * a = the first array | |
740 * b = the second array | |
741 * end = the index where the comparision will end | |
742 * | |
743 * Returns: Returns 0 if the content matches, less than zero if a is | |
744 * "less" than b, or greater than zero where a is "bigger". | |
745 * | |
746 * See_Also: mambo.string.compareIgnoreCase | |
747 */ | |
748 alias compareIgnoreCase icompare; | |
749 | |
750 /** | |
751 * Checks if the given character is a hexdecimal digit character. | |
752 * 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 | |
753 * | |
754 * Params: | |
755 * ch = the character to be checked | |
756 * | |
757 * Returns: true if the given character is a hexdecimal digit character otherwise false | |
758 */ | |
759 bool isHexDigit (dchar ch) | |
760 { | |
761 version (Tango) | |
762 { | |
763 switch (ch) | |
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 case 'a': return true; | |
773 case 'b': return true; | |
774 case 'c': return true; | |
775 case 'd': return true; | |
776 case 'e': return true; | |
777 case 'f': return true; | |
778 | |
779 default: break; | |
780 } | |
781 | |
782 if (isDigit(ch)) | |
783 return true; | |
784 } | |
785 | |
786 else | |
787 if (isxdigit(ch) != 0) | |
788 return true; | |
789 | |
790 return false; | |
791 } | |
792 | |
793 /*version (Tango) | |
794 { | |
795 string toString (string str) | |
796 { | |
797 return str; | |
798 } | |
799 | |
800 string toString (wstring str) | |
801 { | |
802 return tango.text.convert.Utf.toString(str); | |
803 } | |
804 | |
805 string toString (dstring str) | |
806 { | |
807 return tango.text.convert.Utf.toString(str); | |
808 } | |
809 }*/ | |
810 | |
811 version (Phobos) | |
812 { | |
813 /** | |
814 * Converts the given string to C-style 0 terminated string. | |
815 * | |
816 * Params: | |
817 * str = the string to convert | |
818 * | |
819 * Returns: the a C-style 0 terminated string. | |
820 */ | |
821 dchar* toString32z (dstring str) | |
822 { | |
823 return (str ~ '\0').dup.ptr; | |
824 } | |
825 | |
826 /** | |
827 * Converts a C-style 0 terminated string to a wstring | |
828 * | |
829 * Params: | |
830 * str = the C-style 0 terminated string | |
831 * | |
832 * Returns: the converted wstring | |
833 */ | |
834 wstring fromString16z (wchar* str) | |
835 { | |
836 return str[0 .. strlen(str)].idup; | |
837 } | |
838 | |
839 /** | |
840 * Converts a C-style 0 terminated string to a dstring | |
841 * Params: | |
842 * str = the C-style 0 terminated string | |
843 * | |
844 * Returns: the converted dstring | |
845 */ | |
846 dstring fromString32z (dchar* str) | |
847 { | |
848 return str[0 .. strlen(str)].idup; | |
849 } | |
850 | |
851 /** | |
852 * Gets the length of the given C-style 0 terminated string | |
853 * | |
854 * Params: | |
855 * str = the C-style 0 terminated string to get the length of | |
856 * | |
857 * Returns: the length of the string | |
858 */ | |
859 size_t strlen (wchar* str) | |
860 { | |
861 size_t i = 0; | |
862 | |
863 if (str) | |
864 while(*str++) | |
865 ++i; | |
866 | |
867 return i; | |
868 } | |
869 | |
870 /** | |
871 * Gets the length of the given C-style 0 terminated string | |
872 * | |
873 * Params: | |
874 * str = the C-style 0 terminated string to get the length of | |
875 * | |
876 * Returns: the length of the string | |
877 */ | |
878 size_t strlen (dchar* str) | |
879 { | |
880 size_t i = 0; | |
881 | |
882 if (str) | |
883 while(*str++) | |
884 ++i; | |
885 | |
886 return i; | |
887 } | |
888 } |