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