1
2
3
4
5
6 using System;
7
8 namespace System.Text
9 {
10 public nothrow string Trim(const string& s)
11 {
12 long b = 0;
13 while (b < s.Length() && IsSpace(s[b]))
14 {
15 ++b;
16 }
17 long e = s.Length() - 1;
18 while (e >= b && IsSpace(s[e]))
19 {
20 --e;
21 }
22 return s.Substring(b, e - b + 1);
23 }
24
25 public wstring Trim(const wstring& s)
26 {
27 return ToUtf16(Trim(ToUtf32(s)));
28 }
29
30 public ustring Trim(const ustring& s)
31 {
32 long b = 0;
33 while (b < s.Length() && IsWhiteSpace(s[b]))
34 {
35 ++b;
36 }
37 long e = s.Length() - 1;
38 while (e >= b && IsWhiteSpace(s[e]))
39 {
40 --e;
41 }
42 return s.Substring(b, e - b + 1);
43 }
44
45 public string TrimAll(const string& s)
46 {
47 string result;
48 result.Reserve(s.Length());
49 int state = 0;
50 for (char c : s)
51 {
52 switch (state)
53 {
54 case 0:
55 {
56 if (!IsSpace(c))
57 {
58 result.Append(c);
59 state = 1;
60 }
61 break;
62 }
63 case 1:
64 {
65 if (IsSpace(c))
66 {
67 state = 2;
68 }
69 else
70 {
71 result.Append(c);
72 }
73 break;
74 }
75 case 2:
76 {
77 if (!IsSpace(c))
78 {
79 result.Append(' ');
80 result.Append(c);
81 state = 1;
82 }
83 break;
84 }
85 }
86 }
87 return result;
88 }
89
90 public wstring TrimAll(const wstring& s)
91 {
92 return ToUtf16(TrimAll(ToUtf32(s)));
93 }
94
95 public ustring TrimAll(const ustring& s)
96 {
97 ustring result;
98 result.Reserve(s.Length());
99 int state = 0;
100 for (uchar c : s)
101 {
102 switch (state)
103 {
104 case 0:
105 {
106 if (!IsWhiteSpace(c))
107 {
108 result.Append(c);
109 state = 1;
110 }
111 break;
112 }
113 case 1:
114 {
115 if (IsWhiteSpace(c))
116 {
117 state = 2;
118 }
119 else
120 {
121 result.Append(c);
122 }
123 break;
124 }
125 case 2:
126 {
127 if (!IsWhiteSpace(c))
128 {
129 result.Append(u' ');
130 result.Append(c);
131 state = 1;
132 }
133 break;
134 }
135 }
136 }
137 return result;
138 }
139
140 public nothrow string HexEscape(char c)
141 {
142 return "\\x" + ToHexString(cast<byte>(c));
143 }
144
145 public nothrow string HexEscape(wchar c)
146 {
147 return "\\x" + ToHexString(cast<ushort>(c));
148 }
149
150 public nothrow string HexEscape(uchar c)
151 {
152 return "\\x" + ToHexString(cast<uint>(c));
153 }
154
155 public nothrow string CharStr(char c)
156 {
157 switch (c)
158 {
159 case '\\': return "\\\\";
160 case '"': return "\\\"";
161 case '\'': return "\\'";
162 case '\a': return "\\a";
163 case '\b': return "\\b";
164 case '\f': return "\\f";
165 case '\n': return "\\n";
166 case '\r': return "\\r";
167 case '\t': return "\\t";
168 case '\v': return "\\v";
169 case '\0': return "\\0";
170 default:
171 {
172 if (IsPrintable(c))
173 {
174 return string(c);
175 }
176 else
177 {
178 return HexEscape(c);
179 }
180 }
181 }
182 return string();
183 }
184
185 public nothrow string MakeCharLiteral(char c)
186 {
187 if (c == '"')
188 {
189 return string("'\"'");
190 }
191 return "'" + CharStr(c) + "'";
192 }
193
194
195 public wstring CharStr(wchar c)
196 {
197 switch (c)
198 {
199 case w'\\': return w"\\\\";
200 case w'"': return w"\\\"";
201 case w'\'': return w"\\'";
202 case w'\a': return w"\\a";
203 case w'\b': return w"\\b";
204 case w'\f': return w"\\f";
205 case w'\n': return w"\\n";
206 case w'\r': return w"\\r";
207 case w'\t': return w"\\t";
208 case w'\v': return w"\\v";
209 case w'\0': return w"\\0";
210 default:
211 {
212 if (cast<ushort>(c) < 256u && IsPrintable(cast<char>(c)))
213 {
214 return wstring(c);
215 }
216 else
217 {
218 return ToUtf16(HexEscape(c));
219 }
220 }
221 }
222 return wstring();
223 }
224
225 public ustring CharStr(uchar c)
226 {
227 switch (c)
228 {
229 case u'\\': return u"\\\\";
230 case u'"': return u"\\\"";
231 case u'\'': return u"\\'";
232 case u'\a': return u"\\a";
233 case u'\b': return u"\\b";
234 case u'\f': return u"\\f";
235 case u'\n': return u"\\n";
236 case u'\r': return u"\\r";
237 case u'\t': return u"\\t";
238 case u'\v': return u"\\v";
239 case u'\0': return u"\\0";
240 default:
241 {
242 if (cast<uint>(c) < 256u && IsPrintable(cast<char>(c)))
243 {
244 return ustring(c);
245 }
246 else
247 {
248 return ToUtf32(HexEscape(c));
249 }
250 }
251 }
252 return ustring();
253 }
254
255 public nothrow string StringStr(const string& s)
256 {
257 string result;
258 for (char c : s)
259 {
260 if (c == '\'')
261 {
262 result.Append(c);
263 }
264 else
265 {
266 result.Append(CharStr(c));
267 }
268 }
269 return result;
270 }
271
272 public ustring StringStr(const ustring& s)
273 {
274 ustring result;
275 for (uchar c : s)
276 {
277 if (c == u'\'')
278 {
279 result.Append(c);
280 }
281 else
282 {
283 result.Append(CharStr(c));
284 }
285 }
286 return result;
287 }
288
289 public nothrow string MakeStringLiteral(const string& s)
290 {
291 string result = "\"";
292 result.Append(StringStr(s));
293 result.Append('"');
294 return result;
295 }
296
297 public ustring MakeStringLiteral(const ustring& s)
298 {
299 ustring result = u"\"";
300 result.Append(StringStr(s));
301 result.Append(u'"');
302 return result;
303 }
304
305 public enum FormatWidth
306 {
307 exact, min
308 }
309
310 public enum FormatJustify
311 {
312 left, right
313 }
314
315 public nothrow string Format(const string& s, int width)
316 {
317 return Format(s, width, FormatWidth.exact, FormatJustify.left);
318 }
319
320 public nothrow string Format(const string& s, int width, FormatJustify justify)
321 {
322 return Format(s, width, FormatWidth.exact, justify);
323 }
324
325 public nothrow string Format(const string& s, int width, FormatWidth fw)
326 {
327 return Format(s, width, fw, FormatJustify.left);
328 }
329
330 public nothrow string Format(const string& s, int width, FormatWidth fw, FormatJustify justify)
331 {
332 return Format(s, width, fw, justify, ' ');
333 }
334
335 public nothrow string Format(const string& s, int width, FormatWidth fw, FormatJustify justify, char fillChar)
336 {
337 if (fw == FormatWidth.min)
338 {
339 width = Max(width, cast<int>(s.Length()));
340 }
341 string result(fillChar, width);
342 int n = Min(cast<int>(s.Length()), width);
343 int offset = 0;
344 if (justify == FormatJustify.right)
345 {
346 offset = width - n;
347 }
348 for (int i = 0; i < n; ++i;)
349 {
350 result[i + offset] = s[i];
351 }
352 return result;
353 }
354
355 public nothrow ustring Format(const ustring& s, int width)
356 {
357 return Format(s, width, FormatWidth.exact, FormatJustify.left);
358 }
359
360 public nothrow ustring Format(const ustring& s, int width, FormatJustify justify)
361 {
362 return Format(s, width, FormatWidth.exact, justify);
363 }
364
365 public nothrow ustring Format(const ustring& s, int width, FormatWidth fw)
366 {
367 return Format(s, width, fw, FormatJustify.left);
368 }
369
370 public nothrow ustring Format(const ustring& s, int width, FormatWidth fw, FormatJustify justify)
371 {
372 return Format(s, width, fw, justify, ' ');
373 }
374
375 public nothrow ustring Format(const ustring& s, int width, FormatWidth fw, FormatJustify justify, char fillChar)
376 {
377 ustring result;
378 int m = cast<int>(s.Length());
379 if (fw == FormatWidth.min)
380 {
381 width = Max(width, m);
382 }
383 else if (fw == FormatWidth.exact)
384 {
385 m = Min(m, width);
386 }
387 int n = Max(0, width - m);
388 if (justify == FormatJustify.right)
389 {
390 for (int i = 0; i < n; ++i;)
391 {
392 result.Append(fillChar);
393 }
394 }
395 for (int i = 0; i < m; ++i;)
396 {
397 result.Append(s[i]);
398 }
399 if (justify == FormatJustify.left)
400 {
401 for (int i = 0; i < n; ++i;)
402 {
403 result.Append(fillChar);
404 }
405 }
406 return result;
407 }
408 }