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