Mercurial > projects > dynamin
annotate dynamin/core/string.d @ 102:604d20cac836
Add dynamin.core.array and put contains() there.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Tue, 15 May 2012 21:14:48 -0500 |
parents | 3b0c5c599003 |
children | 73060bc3f004 |
rev | line source |
---|---|
0 | 1 // Written in the D programming language |
2 // www.digitalmars.com/d/ | |
3 | |
4 /* | |
5 * The contents of this file are subject to the Mozilla Public License Version | |
6 * 1.1 (the "License"); you may not use this file except in compliance with | |
7 * the License. You may obtain a copy of the License at | |
8 * http://www.mozilla.org/MPL/ | |
9 * | |
10 * Software distributed under the License is distributed on an "AS IS" basis, | |
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 * for the specific language governing rights and limitations under the | |
13 * License. | |
14 * | |
15 * The Original Code is the Dynamin library. | |
16 * | |
17 * The Initial Developer of the Original Code is Jordan Miner. | |
18 * Portions created by the Initial Developer are Copyright (C) 2006-2009 | |
19 * the Initial Developer. All Rights Reserved. | |
20 * | |
21 * Contributor(s): | |
22 * Jordan Miner <jminer7@gmail.com> | |
23 * | |
24 */ | |
25 | |
26 /** | |
27 * These functions should all return a new string if it is possible for them | |
28 * to ever modify their input. If they will never modify their input, they | |
29 * should always return a slice. | |
30 */ | |
31 module dynamin.core.string; | |
32 | |
102
604d20cac836
Add dynamin.core.array and put contains() there.
Jordan Miner <jminer7@gmail.com>
parents:
94
diff
changeset
|
33 public import dynamin.core.array; |
604d20cac836
Add dynamin.core.array and put contains() there.
Jordan Miner <jminer7@gmail.com>
parents:
94
diff
changeset
|
34 |
0 | 35 import tango.core.Exception; |
36 import tango.text.convert.Utf; | |
37 import tango.text.convert.Layout; | |
38 import tango.text.Unicode; | |
39 import dynamin.core.global; | |
40 import dynamin.core.math; | |
41 | |
42 /// Defined as a char[] | |
43 alias char[] string; | |
44 | |
45 /// | |
46 char* toCharPtr(char[] str) { | |
47 return (str~'\0').ptr; | |
48 } | |
49 /// | |
50 wchar* toWcharPtr(char[] str) { | |
51 return toString16(str~'\0').ptr; | |
52 } | |
53 | |
54 /* | |
55 string ToString(ulong num, uint base = 10) { | |
56 if(base > 16) | |
57 throw new Exception("ToString() - radix more than 16"); | |
58 char[] digits = "0123456789abcdef"; | |
59 string str; | |
60 ulong div = base; | |
61 ulong prevDiv = 1; | |
62 do { | |
63 uint rem = num % div; | |
64 str ~= digits[rem/prevDiv]; | |
65 prevDiv = div; | |
66 div *= base; | |
67 num -= rem; | |
68 } while(num > 0); | |
69 str.reverse; | |
70 return str; | |
71 } | |
72 */ | |
94
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
73 |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
74 // TODO: move to encoding.d |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
75 /** |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
76 * Returns true if the specified code unit is a high surrogate, in the range of 0xD800 to 0xDBFF. |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
77 * A high surrogate comes before a low surrogate in a surrogate pair. |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
78 */ |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
79 bool isHighSurrogate(wchar c) { |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
80 return c >= 0xD800 && c <= 0xDBFF; |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
81 } |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
82 /** |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
83 * Returns true if the specified code unit is a low surrogate, in the range of 0xDC00 to 0xDFFF. |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
84 * A low surrogate comes after a high surrogate in a surrogate pair. |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
85 */ |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
86 bool isLowSurrogate(wchar c) { |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
87 return c >= 0xDC00 && c <= 0xDFFF; |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
88 } |
3b0c5c599003
Add isHighSurrogate() and isLowSurrogate().
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
89 |
0 | 90 Layout!(char) formatter; |
91 static this() { | |
92 formatter = new Layout!(char); | |
93 } | |
94 string format(char[] str, ...) { | |
95 return formatter.convert(_arguments, _argptr, str); | |
96 } | |
97 unittest { | |
98 assert(format("I am {}", 20) == "I am 20"); | |
99 } | |
100 | |
101 /** | |
102 * Converts all lowercase characters in the specified string to uppercase. | |
103 * Examples: | |
104 * ----- | |
105 * "Bounce the ball.".upcase() == "BOUNCE THE BALL." | |
106 * "Mañana".upcase() == "MAÑANA" | |
107 * "æóëø".upcase() == "ÆÓËØ" | |
108 * ----- | |
109 */ | |
110 string upcase(string str) { | |
111 return toUpper(str); | |
112 } | |
113 unittest { | |
114 assert("Bounce the ball.".upcase() == "BOUNCE THE BALL."); | |
115 assert("Mañana".upcase() == "MAÑANA"); | |
116 assert("æóëø".upcase() == "ÆÓËØ"); | |
117 } | |
118 /** | |
119 * Converts all uppercase characters in the specified string to lowercase. | |
120 * Examples: | |
121 * ----- | |
122 * "BoUnCe ThE BALL.".downcase() == "bounce the ball." | |
123 * "MAÑANA".downcase() == "mañana" | |
124 * "ÆÓËØ".downcase() == "æóëø" | |
125 * ----- | |
126 */ | |
127 string downcase(string str) { | |
128 return toLower(str); | |
129 } | |
130 unittest { | |
131 assert("BoUnCe ThE BALL.".downcase() == "bounce the ball."); | |
132 assert("MAÑANA".downcase() == "mañana"); | |
133 assert("ÆÓËØ".downcase() == "æóëø"); | |
134 } | |
135 | |
136 // TODO: make more use of delegates in these? | |
137 // TODO; use templates so that these work with wchar and dchar? | |
138 bool startsWith(string str, string subStr, int start = 0) { | |
139 if(start+subStr.length > str.length) | |
140 return false; | |
141 return str[start..start+subStr.length] == subStr; | |
142 } | |
143 bool endsWith(string str, string subStr) { | |
144 return endsWith(str, subStr, str.length); | |
145 } | |
146 bool endsWith(string str, string subStr, int start) { | |
147 if(start-subStr.length < 0) | |
148 return false; | |
149 return str[str.length-subStr.length..str.length] == subStr; | |
150 } | |
151 int findLast(string str, string subStr) { | |
152 return findLast(str, subStr, str.length); | |
153 } | |
154 int findLast(string str, string subStr, int start) { | |
155 for(int i = start-subStr.length; i >= 0; --i) | |
156 if(str[i..i+subStr.length] == subStr) | |
157 return i; | |
158 return -1; | |
159 } | |
160 int find(string str, string subStr, int start = 0) { | |
161 for(int i = start; i < str.length-subStr.length; ++i) | |
162 if(str[i..i+subStr.length] == subStr) | |
163 return i; | |
164 return -1; | |
165 } | |
102
604d20cac836
Add dynamin.core.array and put contains() there.
Jordan Miner <jminer7@gmail.com>
parents:
94
diff
changeset
|
166 |
0 | 167 string remove(string str, int start, int count = 1) { |
168 return str[0..start] ~ str[start+count..str.length]; | |
169 } | |
170 // TODO: ? | |
171 // split(string str, int delegate(string s) func) | |
172 //string[] split(string str, string subStr) { | |
173 // return split(str, (string s) { return s.startsWith(subStr) ? subStr.length, : -1; }; | |
174 //} | |
175 // TODO: return slices to string | |
176 //split1("50=20=10", "=") -> ["50", "20=10"] | |
177 string[] split1(string str, string subStr) { | |
178 if(subStr.length == 0) | |
179 return [str]; | |
180 int index = find(str, subStr); | |
181 if(index == -1) | |
182 return [str]; | |
183 string[] strs = new string[2]; | |
184 strs[0] = str[0..index].dup; | |
185 strs[1] = str[index+subStr.length..str.length].dup; | |
186 return strs; | |
187 } | |
188 // TODO: return slices to string | |
189 //split("50=20=10", "=") -> ["50", "20", "10"] | |
190 string[] split(string str, string subStr) { | |
191 if(subStr.length == 0) | |
192 return [str]; | |
193 string[] strs; | |
194 int index, searchFrom; | |
195 int i = 0; | |
196 while(searchFrom < str.length) { | |
197 index = find(str, subStr, searchFrom); | |
198 if(index == -1) index = str.length; | |
199 strs.length = strs.length+1; | |
200 strs[i] = str[searchFrom..index].dup; | |
201 ++i; | |
202 searchFrom = index+subStr.length; | |
203 } | |
204 return strs; | |
205 } | |
206 /// | |
207 enum Newline { | |
208 /// | |
209 Cr = 0, | |
210 /// | |
211 Lf = 1, | |
212 /// | |
213 Crlf = 2, | |
214 /// | |
215 Macintosh = 0, | |
216 /// | |
217 Linux = 1, | |
218 /// | |
219 Windows = 2 | |
220 } | |
221 /** | |
222 * Changes every occurrence of a newline in the specified string to the specified newline. | |
223 * Examples: | |
224 * ----- | |
225 * "\r\n\n\r".convertNewlines(Newline.Lf) == "\n\n\n" | |
226 * "\r\n\n\r".convertNewlines(Newline.Windows) == "\r\n\r\n\r\n" | |
227 * "\n\r\n".convertNewlines(Newline.Macintosh) == "\r\r" | |
228 * ----- | |
229 */ | |
230 string convertNewlines(string str, Newline nl) { | |
231 string lineSep; | |
232 switch(nl) { | |
233 case Newline.Cr: lineSep = "\r"; break; | |
234 case Newline.Lf: lineSep = "\n"; break; | |
235 case Newline.Crlf: lineSep = "\r\n"; break; | |
236 } | |
237 return str.replace([cast(string)"\r\n", "\r", "\n"], lineSep); | |
238 } | |
239 unittest { | |
240 assert("\r\n\n\r".convertNewlines(Newline.Lf) == "\n\n\n"); | |
241 assert("\r\n\n\r".convertNewlines(Newline.Windows) == "\r\n\r\n\r\n"); | |
242 assert("\n\r\n".convertNewlines(Newline.Macintosh) == "\r\r"); | |
243 } | |
244 | |
245 /** | |
246 * Joins all the strings in the specified array together into one string, putting | |
247 * the specified separator between them. | |
248 * Examples: | |
249 * ----- | |
250 * join(["10", "15", "17"], " - ") == "10 - 15 - 17" | |
251 * join(["789", "672", "484"], ",") == "789,672,484" | |
252 * join(["aol.com", "join", "intro.html"], "/") == "aol.com/join/intro.html" | |
253 * ----- | |
254 */ | |
255 string join(string[] strs, string sep) { | |
256 if(strs.length == 0) | |
257 return ""; | |
258 int len; | |
259 foreach(string s; strs) | |
260 len += s.length; | |
261 len += sep.length*(strs.length-1); | |
262 | |
263 string newStr = new char[len]; | |
264 newStr[0..strs[0].length] = strs[0]; | |
265 int start = strs[0].length; | |
266 for(int i = 1; i < strs.length; ++i) { | |
267 auto str = strs[i]; | |
268 newStr[start..start+sep.length] = sep; | |
269 start += sep.length; | |
270 newStr[start..start+str.length] = str; | |
271 start += str.length; | |
272 } | |
273 return newStr; | |
274 } | |
275 unittest { | |
276 // TODO: remove cast(string) when D has bugs fixed | |
277 assert(join(["10", "15", "17"], " - ") == "10 - 15 - 17"); | |
278 assert(join(["789", "672", "484"], ",") == "789,672,484"); | |
279 assert(join([cast(string)"aol.com", "join", "intro.html"], "/") == "aol.com/join/intro.html"); | |
280 } | |
281 | |
282 /** | |
283 * Multiplies the given string the specified number of times. | |
284 * Returns: a string that is the result of adding the specified string onto | |
285 * an empty string the specified number of times | |
286 * Examples: | |
287 * ----- | |
288 * "Hi...".times(3) == "Hi...Hi...Hi..." | |
289 * "0".times(20) == "00000000000000000000" | |
290 * "Hi".times(0) == "" | |
291 * ----- | |
292 */ | |
293 string times(string str, int n) { | |
294 string newStr = new char[n * str.length]; | |
295 for(int i = 0; i < newStr.length; i += str.length) | |
296 newStr[i..i+str.length] = str; | |
297 return newStr; | |
298 } | |
299 unittest { | |
300 assert("0".times(4) == "0000"); | |
301 assert("Hello! ".times(2) == "Hello! Hello! "); | |
302 assert("".times(50) == ""); | |
303 assert("Hi".times(0) == ""); | |
304 } | |
305 | |
306 // TODO: flesh out and make public | |
307 struct sbuilder { | |
308 int Count; | |
309 string Data; | |
310 void Add(char c) { | |
311 if(Count + 1 > Data.length) | |
312 Data.length = (Data.length + 1) * 2; | |
313 Data[Count] = c; | |
314 ++Count; | |
315 } | |
316 void Add(string str) { | |
317 if(Count + str.length > Data.length) | |
318 Data.length = max((Data.length + 1) * 2, Count + str.length); | |
319 Data[Count..Count+str.length] = str; | |
320 Count += str.length; | |
321 } | |
322 string ToString() { | |
323 return Data[0..Count].dup; | |
324 } | |
325 } | |
326 /** | |
327 * Replaces any occurrence of a specified search string in the specified string | |
328 * with corresponding replacement string. The length of the searchStrs array | |
329 * must equal the length of the replacements array. | |
330 * Examples: | |
331 * ----- | |
332 * "Mississippi".replace(["is", "i"], ["..", "*"]) == "M..s..s*pp*" | |
333 * "Mississippi".replace("ss", "...") == "Mi...i...ippi" | |
334 * "Hello".replace("ll", "y") == "Heyo" | |
335 * "Hi".replace([], []) == "Hi" | |
336 * ----- | |
337 * Note: If multiple search strings have the same prefix, the longer search | |
338 * strings must be given first. Otherwise, any occurrence will match a | |
339 * shorter one and will not have a chance to match any longer one. | |
340 * Examples: | |
341 * ----- | |
342 * "Speaker".replace(["ea", "e"], ":") == "Sp:k:r" | |
343 * "Speaker".replace(["e", "ea"], ":") == "Sp:ak:r" | |
344 * ----- | |
345 * Bug: If a search string has a length of zero, this method will go into an infinite loop. | |
346 */ | |
347 string replace(string str, string[] searchStrs, string[] replacements) { | |
348 if(replacements.length == 1 && searchStrs.length > 1) { | |
349 string tmp = replacements[0]; | |
350 replacements = new string[searchStrs.length]; | |
351 foreach(i, dummy; searchStrs) | |
352 replacements[i] = tmp; | |
353 } | |
354 if(searchStrs.length != replacements.length) | |
355 throw new IllegalArgumentException( | |
356 "Replace(): searchStrs and replacements must be same length"); | |
357 sbuilder builder; | |
358 loop: | |
359 for(int i = 0; i < str.length; ) { | |
360 foreach(j, subStr; searchStrs) { | |
361 if(i+subStr.length <= str.length && str[i..i+subStr.length] == subStr) { | |
362 // skip the part of string that matched | |
363 i += subStr.length; | |
364 builder.Add(replacements[j]); | |
365 continue loop; | |
366 } | |
367 } | |
368 builder.Add(str[i]); | |
369 ++i; | |
370 } | |
371 return builder.ToString(); | |
372 } | |
373 /// ditto | |
374 string replace(string str, string[] searchStrs, string replacement) { | |
375 return str.replace(searchStrs, [replacement]); | |
376 } | |
377 /// ditto | |
378 string replace(string str, string searchStr, string replacement) { | |
379 return str.replace([searchStr], [replacement]); | |
380 } | |
381 unittest { | |
382 assert("Mississippi".replace([cast(string)"is", "i"], [cast(string)"..", "*"]) == "M..s..s*pp*"); | |
383 assert("Mississippi".replace("ss", "...") == "Mi...i...ippi"); | |
384 assert("Hello".replace("ll", "y") == "Heyo"); | |
385 //assert("Hi".Replace(cast(string[])[], cast(string[])[]) == "Hi"); | |
386 assert("Speaker".replace([cast(string)"ea", "e"], ":") == "Sp:k:r"); | |
387 assert("Speaker".replace([cast(string)"e", "ea"], ":") == "Sp:ak:r"); | |
388 } | |
389 | |
390 /** | |
391 * Changes every occurrence of a specified character in chars to the | |
392 * corresponding character in escChars. | |
393 * Examples: | |
394 * ----- | |
395 * "Line1\r\nLine2\\".escape() == "Line1\\r\\nLine2\\\\" | |
396 * "Line1\tLine2".escape() == "Line1\\tLine2" | |
397 * "Part1|Part2\r\n".escape("|\r\n", "|rn") == "Part1\\|Part2\\r\\n" | |
398 * ----- | |
399 */ | |
400 string escape(string str, char[] chars, char[] escChars) { | |
401 if(chars.length != escChars.length) | |
402 throw new IllegalArgumentException("Escape(): chars and escChars must be same length"); | |
403 sbuilder builder; | |
404 loop: | |
405 foreach(i, c; str) { | |
406 foreach(j, c2; chars) { | |
407 if(c == '\\') { // always escape backslash | |
408 builder.Add('\\'); | |
409 builder.Add('\\'); | |
410 continue loop; | |
411 } | |
412 if(c == c2) { | |
413 builder.Add('\\'); | |
414 builder.Add(escChars[j]); | |
415 continue loop; | |
416 } | |
417 } | |
418 builder.Add(c); | |
419 } | |
420 return builder.ToString(); | |
421 } | |
422 /// ditto | |
423 string escape(string str) { | |
424 return str.escape("\t\r\n", "trn"); | |
425 } | |
426 unittest { | |
427 assert("Line1\r\nLine2\\".escape() == "Line1\\r\\nLine2\\\\"); | |
428 assert("Line1\tLine2".escape() == "Line1\\tLine2"); | |
429 assert("Part1|Part2\r\n".escape("|\r\n", "|rn") == "Part1\\|Part2\\r\\n"); | |
430 } | |
431 /** | |
432 * Changes every occurrence of a specified character in escChars to the | |
433 * corresponding character in chars. | |
434 * Examples: | |
435 * ----- | |
436 * "Line1\\r\\nLine2".unescape() == "Line1\r\nLine2" | |
437 * "Line1\\tLine2".unescape() == "Line1\tLine2" | |
438 * "Part1\\|Part2\\r\\n".unescape("|rn", "|\r\n") == "Part1|Part2\r\n" | |
439 * // error: | |
440 * "test\\".unescape() | |
441 * ----- | |
442 */ | |
443 string unescape(string str, char[] escChars, char[] chars) { | |
444 if(escChars.length != chars.length) | |
445 throw new IllegalArgumentException("Unescape(): escChars and chars must be same length"); | |
446 sbuilder builder; | |
447 loop: | |
448 foreach(i, c; str) { | |
449 if(c == '\\') { | |
450 if(i == str.length-1) | |
451 throw new IllegalArgumentException("Unescape(): partial escape sequence at end of string"); | |
452 if(str[i+1] == '\\') { | |
453 builder.Add('\\'); | |
454 ++i; | |
455 continue loop; | |
456 } | |
457 foreach(j, c2; escChars) { | |
458 if(str[i+1] == c2) { | |
459 builder.Add(chars[j]); | |
460 ++i; | |
461 continue loop; | |
462 } | |
463 } | |
464 throw new IllegalArgumentException("Unescape(): invalid escape sequence"); | |
465 } | |
466 builder.Add(str[i]); | |
467 } | |
468 return builder.ToString(); | |
469 } | |
470 /// ditto | |
471 string unescape(string str) { | |
472 return str.unescape("trn", "\t\r\n"); | |
473 } | |
474 unittest { | |
475 assert("Line1\\r\\nLine2\\\\".unescape() == "Line1\r\nLine2\\"); | |
476 assert("Line1\\tLine2".unescape() == "Line1\tLine2"); | |
477 assert("Part1\\|Part2\\r\\n".unescape("|rn", "|\r\n") == "Part1|Part2\r\n"); | |
478 } | |
479 unittest { | |
480 string str = r"C:\\n"; | |
481 assert(str.escape().unescape() == str); | |
482 } | |
483 /** | |
484 * Removes all whitespace characters from the specified string. | |
485 * Examples: | |
486 * ----- | |
487 * "4a d2 7c 3f".removeWhitespace() == "4ad27c3f" | |
488 * " Hello \r\n".removeWhitespace() == "Hello" | |
489 * "How are you?".removeWhitespace() == "Howareyou?" | |
490 * "\t \n\r\f\v".removeWhitespace() == "" | |
491 * ----- | |
492 */ | |
493 string removeWhitespace(string str) { | |
494 sbuilder builder; | |
495 foreach(c; str) | |
496 if(!" \t\n\r\v\f".contains(c)) | |
497 builder.Add(c); | |
498 return builder.ToString(); | |
499 } | |
500 unittest { | |
501 assert("4a d2 7c 3f".removeWhitespace() == "4ad27c3f"); | |
502 assert(" Hello \r\n".removeWhitespace() == "Hello"); | |
503 assert("How are you?".removeWhitespace() == "Howareyou?"); | |
504 assert("\t \n\r\f\v".removeWhitespace() == ""); | |
505 } | |
506 /** | |
507 * Removes all the whitespace characters from the start and from the end | |
508 * of the specified string. Returns a slice. | |
509 * Examples: | |
510 * ----- | |
511 * " Hello \r\n".trim() == "Hello" | |
512 * "How are you?".trim() == "How are you?" | |
513 * "\n la di da ".trim() == "la di da" | |
514 * " \n".trim() == "" | |
515 * "".trim() == "" | |
516 * ----- | |
517 */ | |
518 string trim(string str) { | |
519 int start = -1, end = str.length; | |
520 while( --end >= 0 && " \t\n\r\v\f".contains(str[end]) ) { } | |
521 end++; | |
522 if(end == 0) // means all whitespace | |
523 return ""; | |
524 while(" \t\n\r\v\f".contains(str[++start])) { } | |
525 return str[start..end]; | |
526 } | |
527 unittest { | |
528 assert(" Hello \r\n".trim() == "Hello"); | |
529 assert("How are you?".trim() == "How are you?"); | |
530 assert("\n la di da ".trim() == "la di da"); | |
531 assert(" \n".trim() == ""); | |
532 assert("".trim() == ""); | |
533 } | |
534 /** | |
535 * Removes all the whitespace characters from the start | |
536 * of the specified string. Returns a slice. | |
537 * Examples: | |
538 * ----- | |
539 * " Hello \r\n".trimLeft() == "Hello \r\n" | |
540 * "How are you?".trimLeft() == "How are you?" | |
541 * "\n la di da ".trimLeft() == "la di da " | |
542 * " \n".trimLeft() == "" | |
543 * "".trimLeft() == "" | |
544 * ----- | |
545 */ | |
546 string trimLeft(string str) { | |
547 int start = -1; | |
548 while(++start < str.length && " \t\n\r\v\f".contains(str[start])) { } | |
549 return str[start..$]; | |
550 } | |
551 unittest { | |
552 assert(" Hello \r\n".trimLeft() == "Hello \r\n"); | |
553 assert("How are you?".trimLeft() == "How are you?"); | |
554 assert("\n la di da ".trimLeft() == "la di da "); | |
555 assert(" \n".trimLeft() == ""); | |
556 assert("".trimLeft() == ""); | |
557 } | |
558 /** | |
559 * Removes all the whitespace characters from the start | |
560 * of the specified string. Returns a slice. | |
561 * Examples: | |
562 * ----- | |
563 * " Hello \r\n".trimRight() == " Hello" | |
564 * "How are you?".trimRight() == "How are you?" | |
565 * "\n la di da ".trimRight() == "\n la di da" | |
566 * " \n".trimRight() == "" | |
567 * "".trimRight() == "" | |
568 * ----- | |
569 */ | |
570 string trimRight(string str) { | |
571 int end = str.length; | |
572 while( --end >= 0 && " \t\n\r\v\f".contains(str[end]) ) { } | |
573 end++; | |
574 return str[0..end]; | |
575 } | |
576 unittest { | |
577 assert(" Hello \r\n".trimRight() == " Hello"); | |
578 assert("How are you?".trimRight() == "How are you?"); | |
579 assert("\n la di da ".trimRight() == "\n la di da"); | |
580 assert(" \n".trimRight() == ""); | |
581 assert("".trimRight() == ""); | |
582 } | |
583 | |
584 | |
585 unittest { | |
586 assert(".NET Framework".startsWith(".N")); | |
587 assert(".NET Framework".startsWith("Frame", 5)); | |
588 assert(!".NET Framework".startsWith(".NEW")); | |
589 assert(".NET Framework".find("NET") == 1); | |
590 assert(".NET Framework".find("NET", 2) == -1); | |
591 assert(".NET Framework".find("") == 0); | |
592 assert("Mississippi".findLast("ss") == 5); | |
593 assert("Mississippi".findLast("ss", 4) == 2); | |
594 assert("Jordan=20".split("=") == [cast(string)"Jordan", "20"]); | |
595 assert("Jordan".split("") == [cast(string)"Jordan"]); | |
596 assert("Jordan".split1("=") == [cast(string)"Jordan"]); | |
597 } | |
598 | |
599 /*class Encoding { | |
600 private: | |
601 //TODO: remove dependency on std.utf | |
602 static Encoding[] encodings = [ | |
603 //new Encoding("windows-1252".Str(), "Western European (Windows)".Str(), encodeTableWindows1252) | |
604 ]; | |
605 String name; | |
606 String desc; | |
607 public: | |
608 //property | |
609 static Encoding[] Encodings() { | |
610 return encodings; | |
611 } | |
612 static Encoding GetEncoding(String name) { | |
613 } | |
614 static byte[] Convert(Encoding src, Encoding dst, byte[] bytes) {} | |
615 | |
616 this(String name, String description, wchar[] table) { | |
617 this.name = name; | |
618 } | |
619 // example: utf-8 | |
620 String Name() { | |
621 return name; | |
622 } | |
623 // example: Unicode (UTF-8) | |
624 String Description() { | |
625 return desc; | |
626 } | |
627 int GetEncodedCount() {} | |
628 byte[] Encode(String str) { | |
629 } | |
630 //Returns the number of characters | |
631 int GetDecodedLength() { | |
632 } | |
633 String Decode(byte[] bytes) { | |
634 } | |
635 }*/ | |
636 //class Utf8Encoding : Encoding { | |
637 //public: | |
638 //} | |
639 | |
640 //characters that cannot be mapped to unicode are 0xFFFD in the tables | |
641 wchar[] encodeTableWindows1252 = [ | |
642 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, | |
643 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, 0x0010, 0x0011, | |
644 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001A, | |
645 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, 0x0020, 0x0021, 0x0022, 0x0023, | |
646 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, | |
647 0x002D, 0x002E, 0x002F, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, | |
648 0x0036, 0x0037, 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, | |
649 0x003F, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, | |
650 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050, | |
651 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, | |
652 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, 0x0060, 0x0061, 0x0062, | |
653 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006A, 0x006B, | |
654 0x006C, 0x006D, 0x006E, 0x006F, 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, | |
655 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, | |
656 0x007E, 0x007F, 0x20AC, 0xFFFD, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, | |
657 0x2021, 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, 0xFFFD, 0x017D, 0xFFFD, | |
658 0xFFFD, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x02DC, | |
659 0x2122, 0x0161, 0x203A, 0x0153, 0xFFFD, 0x017E, 0x0178, 0x00A0, 0x00A1, | |
660 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00AA, | |
661 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, | |
662 0x00B4, 0x00B5, 0x00B6, 0x00B7, 0x00B8, 0x00B9, 0x00BA, 0x00BB, 0x00BC, | |
663 0x00BD, 0x00BE, 0x00BF, 0x00C0, 0x00C1, 0x00C2, 0x00C3, 0x00C4, 0x00C5, | |
664 0x00C6, 0x00C7, 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, | |
665 0x00CF, 0x00D0, 0x00D1, 0x00D2, 0x00D3, 0x00D4, 0x00D5, 0x00D6, 0x00D7, | |
666 0x00D8, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x00DD, 0x00DE, 0x00DF, 0x00E0, | |
667 0x00E1, 0x00E2, 0x00E3, 0x00E4, 0x00E5, 0x00E6, 0x00E7, 0x00E8, 0x00E9, | |
668 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, 0x00F0, 0x00F1, 0x00F2, | |
669 0x00F3, 0x00F4, 0x00F5, 0x00F6, 0x00F7, 0x00F8, 0x00F9, 0x00FA, 0x00FB, | |
670 0x00FC, 0x00FD, 0x00FE, 0x00FF | |
671 ]; | |
672 | |
673 //iso8859-1 does not need a conversion table, as its values are all the same as Unicode's | |
674 | |
675 wchar[] encodeTableIso8859_2 = [ | |
676 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, | |
677 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, 0x0010, 0x0011, | |
678 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001A, | |
679 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, 0x0020, 0x0021, 0x0022, 0x0023, | |
680 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, | |
681 0x002D, 0x002E, 0x002F, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, | |
682 0x0036, 0x0037, 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, | |
683 0x003F, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, | |
684 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050, | |
685 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, | |
686 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, 0x0060, 0x0061, 0x0062, | |
687 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006A, 0x006B, | |
688 0x006C, 0x006D, 0x006E, 0x006F, 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, | |
689 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, | |
690 0x007E, 0x007F, 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, | |
691 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F, | |
692 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, | |
693 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F, 0x00A0, 0x0104, | |
694 0x02D8, 0x0141, 0x00A4, 0x013D, 0x015A, 0x00A7, 0x00A8, 0x0160, 0x015E, | |
695 0x0164, 0x0179, 0x00AD, 0x017D, 0x017B, 0x00B0, 0x0105, 0x02DB, 0x0142, | |
696 0x00B4, 0x013E, 0x015B, 0x02C7, 0x00B8, 0x0161, 0x015F, 0x0165, 0x017A, | |
697 0x02DD, 0x017E, 0x017C, 0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, | |
698 0x0106, 0x00C7, 0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, | |
699 0x010E, 0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7, | |
700 0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF, 0x0155, | |
701 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7, 0x010D, 0x00E9, | |
702 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F, 0x0111, 0x0144, 0x0148, | |
703 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7, 0x0159, 0x016F, 0x00FA, 0x0171, | |
704 0x00FC, 0x00FD, 0x0163, 0x02D9 | |
705 ]; | |
706 | |
707 wchar[] encodeTableIso8859_3 = [ | |
708 0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, | |
709 0x0009, 0x000A, 0x000B, 0x000C, 0x000D, 0x000E, 0x000F, 0x0010, 0x0011, | |
710 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001A, | |
711 0x001B, 0x001C, 0x001D, 0x001E, 0x001F, 0x0020, 0x0021, 0x0022, 0x0023, | |
712 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, | |
713 0x002D, 0x002E, 0x002F, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, | |
714 0x0036, 0x0037, 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, | |
715 0x003F, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, | |
716 0x0048, 0x0049, 0x004A, 0x004B, 0x004C, 0x004D, 0x004E, 0x004F, 0x0050, | |
717 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, | |
718 0x005A, 0x005B, 0x005C, 0x005D, 0x005E, 0x005F, 0x0060, 0x0061, 0x0062, | |
719 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006A, 0x006B, | |
720 0x006C, 0x006D, 0x006E, 0x006F, 0x0070, 0x0071, 0x0072, 0x0073, 0x0074, | |
721 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007A, 0x007B, 0x007C, 0x007D, | |
722 0x007E, 0x007F, 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, | |
723 0x0087, 0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F, | |
724 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097, 0x0098, | |
725 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F, 0x00A0, 0x0126, | |
726 0x02D8, 0x00A3, 0x00A4, 0x0124, 0x00A7, 0x00A8, 0x0130, 0x015E, 0x011E, | |
727 0x0134, 0x00AD, 0x017B, 0x00B0, 0x0127, 0x00B2, 0x00B3, 0x00B4, 0x00B5, | |
728 0x0125, 0x00B7, 0x00B8, 0x0131, 0x015F, 0x011F, 0x0135, 0x00BD, 0x017C, | |
729 0x00C0, 0x00C1, 0x00C2, 0x00C4, 0x010A, 0x0108, 0x00C7, 0x00C8, 0x00C9, | |
730 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF, 0x00D1, 0x00D2, 0x00D3, | |
731 0x00D4, 0x0120, 0x00D6, 0x00D7, 0x011C, 0x00D9, 0x00DA, 0x00DB, 0x00DC, | |
732 0x016C, 0x015C, 0x00DF, 0x00E0, 0x00E1, 0x00E2, 0x00E4, 0x010B, 0x0109, | |
733 0x00E7, 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF, | |
734 0x00F1, 0x00F2, 0x00F3, 0x00F4, 0x0121, 0x00F6, 0x00F7, 0x011D, 0x00F9, | |
735 0x00FA, 0x00FB, 0x00FC, 0x016D, 0x015D, 0x02D9 | |
736 ]; |