forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStringExtensions.cs
More file actions
539 lines (452 loc) · 17.2 KB
/
StringExtensions.cs
File metadata and controls
539 lines (452 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//
// http://code.google.com/p/servicestack/wiki/TypeSerializer
// ServiceStack.Text: .NET C# POCO Type Text Serializer.
//
// Authors:
// Demis Bellot (demis.bellot@gmail.com)
//
// Copyright 2011 Liquidbit Ltd.
//
// Licensed under the same terms of ServiceStack: new BSD license.
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Text.RegularExpressions;
using ServiceStack.Text.Support;
namespace ServiceStack.Text
{
public static class StringExtensions
{
public static T To<T>(this string value)
{
return TypeSerializer.DeserializeFromString<T>(value);
}
public static T To<T>(this string value, T defaultValue)
{
return string.IsNullOrEmpty(value) ? defaultValue : TypeSerializer.DeserializeFromString<T>(value);
}
public static T ToOrDefaultValue<T>(this string value)
{
return string.IsNullOrEmpty(value) ? default(T) : TypeSerializer.DeserializeFromString<T>(value);
}
public static object To(this string value, Type type)
{
return TypeSerializer.DeserializeFromString(value, type);
}
/// <summary>
/// Converts from base: 0 - 62
/// </summary>
/// <param name="source">The source.</param>
/// <param name="from">From.</param>
/// <param name="to">To.</param>
/// <returns></returns>
public static string BaseConvert(this string source, int from, int to)
{
const string chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var result = "";
var length = source.Length;
var number = new int[length];
for (var i = 0; i < length; i++)
{
number[i] = chars.IndexOf(source[i]);
}
int newlen;
do
{
var divide = 0;
newlen = 0;
for (var i = 0; i < length; i++)
{
divide = divide * from + number[i];
if (divide >= to)
{
number[newlen++] = divide / to;
divide = divide % to;
}
else if (newlen > 0)
{
number[newlen++] = 0;
}
}
length = newlen;
result = chars[divide] + result;
}
while (newlen != 0);
return result;
}
public static string EncodeXml(this string value)
{
return value.Replace("<", "<").Replace(">", ">").Replace("&", "&");
}
public static string EncodeJson(this string value)
{
return string.Concat
("\"",
value.Replace("\\", "\\\\").Replace("\"", "\\\"").Replace("\r", "").Replace("\n", "\\n"),
"\""
);
}
public static string EncodeJsv(this string value)
{
return value.ToCsvField();
}
public static string UrlEncode(this string text)
{
if (string.IsNullOrEmpty(text)) return text;
var sb = new StringBuilder();
var textLength = text.Length;
for (var i = 0; i < textLength; i++)
{
var c = text.Substring(i, 1);
int charCode = text[i];
if (
charCode >= 65 && charCode <= 90 // A-Z
|| charCode >= 97 && charCode <= 122 // a-z
|| charCode >= 48 && charCode <= 57 // 0-9
|| charCode >= 44 && charCode <= 46 // ,-.
)
{
sb.Append(c);
}
else
{
sb.Append('%' + charCode.ToString("x"));
}
}
return sb.ToString();
}
public static string UrlDecode(this string text)
{
if (string.IsNullOrEmpty(text)) return null;
var sb = new StringBuilder();
var textLength = text.Length;
for (var i = 0; i < textLength; i++)
{
var c = text.Substring(i, 1);
if (c == "+")
{
sb.Append(" ");
}
else if (c == "%")
{
var hexNo = Convert.ToInt32(text.Substring(i + 1, 2), 16);
sb.Append((char)hexNo);
i += 2;
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
#if !XBOX
public static string HexEscape(this string text, params char[] anyCharOf)
{
if (string.IsNullOrEmpty(text)) return text;
if (anyCharOf == null || anyCharOf.Length == 0) return text;
var encodeCharMap = new HashSet<char>(anyCharOf);
var sb = new StringBuilder();
var textLength = text.Length;
for (var i = 0; i < textLength; i++)
{
var c = text[i];
if (encodeCharMap.Contains(c))
{
sb.Append('%' + ((int)c).ToString("x"));
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
#endif
public static string HexUnescape(this string text, params char[] anyCharOf)
{
if (string.IsNullOrEmpty(text)) return null;
if (anyCharOf == null || anyCharOf.Length == 0) return text;
var sb = new StringBuilder();
var textLength = text.Length;
for (var i = 0; i < textLength; i++)
{
var c = text.Substring(i, 1);
if (c == "%")
{
var hexNo = Convert.ToInt32(text.Substring(i + 1, 2), 16);
sb.Append((char)hexNo);
i += 2;
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
public static string UrlFormat(this string url, params string[] urlComponents)
{
var encodedUrlComponents = new string[urlComponents.Length];
for (var i = 0; i < urlComponents.Length; i++)
{
var x = urlComponents[i];
encodedUrlComponents[i] = x.UrlEncode();
}
return string.Format(url, encodedUrlComponents);
}
public static string ToRot13(this string value)
{
var array = value.ToCharArray();
for (var i = 0; i < array.Length; i++)
{
var number = (int)array[i];
if (number >= 'a' && number <= 'z')
number += (number > 'm') ? -13 : 13;
else if (number >= 'A' && number <= 'Z')
number += (number > 'M') ? -13 : 13;
array[i] = (char)number;
}
return new string(array);
}
public static string WithTrailingSlash(this string path)
{
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException("path");
if (path[path.Length - 1] != '/')
{
return path + "/";
}
return path;
}
public static string AppendUrlPaths(this string uri, params string[] uriComponents)
{
var sb = new StringBuilder(uri.WithTrailingSlash());
var i = 0;
foreach (var uriComponent in uriComponents)
{
if (i++ > 0) sb.Append('/');
sb.Append(uriComponent.UrlEncode());
}
return sb.ToString();
}
public static string FromUtf8Bytes(this byte[] bytes)
{
return bytes == null ? null
: Encoding.UTF8.GetString(bytes, 0, bytes.Length);
}
public static byte[] ToUtf8Bytes(this string value)
{
return Encoding.UTF8.GetBytes(value);
}
public static byte[] ToUtf8Bytes(this int intVal)
{
return FastToUtf8Bytes(intVal.ToString());
}
public static byte[] ToUtf8Bytes(this long longVal)
{
return FastToUtf8Bytes(longVal.ToString());
}
public static byte[] ToUtf8Bytes(this double doubleVal)
{
var doubleStr = doubleVal.ToString(CultureInfo.InvariantCulture.NumberFormat);
if (doubleStr.IndexOf('E') != -1 || doubleStr.IndexOf('e') != -1)
doubleStr = DoubleConverter.ToExactString(doubleVal);
return FastToUtf8Bytes(doubleStr);
}
/// <summary>
/// Skip the encoding process for 'safe strings'
/// </summary>
/// <param name="strVal"></param>
/// <returns></returns>
private static byte[] FastToUtf8Bytes(string strVal)
{
var bytes = new byte[strVal.Length];
for (var i = 0; i < strVal.Length; i++)
bytes[i] = (byte)strVal[i];
return bytes;
}
public static string[] SplitOnFirst(this string strVal, char needle)
{
if (strVal == null) return new string[0];
var pos = strVal.IndexOf(needle);
return pos == -1
? new[] { strVal }
: new[] { strVal.Substring(0, pos), strVal.Substring(pos + 1) };
}
public static string[] SplitOnFirst(this string strVal, string needle)
{
if (strVal == null) return new string[0];
var pos = strVal.IndexOf(needle);
return pos == -1
? new[] { strVal }
: new[] { strVal.Substring(0, pos), strVal.Substring(pos + 1) };
}
public static string[] SplitOnLast(this string strVal, char needle)
{
if (strVal == null) return new string[0];
var pos = strVal.LastIndexOf(needle);
return pos == -1
? new[] { strVal }
: new[] { strVal.Substring(0, pos), strVal.Substring(pos + 1) };
}
public static string[] SplitOnLast(this string strVal, string needle)
{
if (strVal == null) return new string[0];
var pos = strVal.LastIndexOf(needle);
return pos == -1
? new[] { strVal }
: new[] { strVal.Substring(0, pos), strVal.Substring(pos + 1) };
}
public static string WithoutExtension(this string filePath)
{
if (string.IsNullOrEmpty(filePath)) return null;
var extPos = filePath.LastIndexOf('.');
if (extPos == -1) return filePath;
var dirPos = filePath.LastIndexOfAny(DirSeps);
return extPos > dirPos ? filePath.Substring(0, extPos) : filePath;
}
private static readonly char DirSep = Path.DirectorySeparatorChar;
private static readonly char AltDirSep = Path.DirectorySeparatorChar == '/' ? '\\' : '/';
static readonly char[] DirSeps = new[] { '\\', '/' };
public static string ParentDirectory(this string filePath)
{
if (string.IsNullOrEmpty(filePath)) return null;
var dirSep = filePath.IndexOf(DirSep) != -1
? DirSep
: filePath.IndexOf(AltDirSep) != -1 ? AltDirSep : (char)0;
return dirSep == 0 ? null : filePath.TrimEnd(dirSep).SplitOnLast(dirSep)[0];
}
public static string ToJsv<T>(this T obj)
{
return TypeSerializer.SerializeToString<T>(obj);
}
public static T FromJsv<T>(this string jsv)
{
return TypeSerializer.DeserializeFromString<T>(jsv);
}
public static string ToJson<T>(this T obj)
{
return JsonSerializer.SerializeToString<T>(obj);
}
public static T FromJson<T>(this string json)
{
return JsonSerializer.DeserializeFromString<T>(json);
}
#if !XBOX && !SILVERLIGHT
public static string ToXml<T>(this T obj)
{
return XmlSerializer.SerializeToString<T>(obj);
}
#endif
#if !XBOX && !SILVERLIGHT
public static T FromXml<T>(this string json)
{
return XmlSerializer.DeserializeFromString<T>(json);
}
#endif
public static string FormatWith(this string text, params object[] args)
{
return string.Format(text, args);
}
public static string Fmt(this string text, params object[] args)
{
return string.Format(text, args);
}
public static bool StartsWithIgnoreCase(this string text, string startsWith)
{
return text != null
&& text.StartsWith(startsWith, StringComparison.InvariantCultureIgnoreCase);
}
public static string ReadAllText(this string filePath)
{
#if XBOX && !SILVERLIGHT
using( var fileStream = new FileStream( filePath, FileMode.Open, FileAccess.Read ) )
{
return new StreamReader( fileStream ).ReadToEnd( ) ;
}
#else
return File.ReadAllText(filePath);
#endif
}
public static int IndexOfAny(this string text, params string[] needles)
{
return IndexOfAny(text, 0, needles);
}
public static int IndexOfAny(this string text, int startIndex, params string[] needles)
{
if (text == null) return -1;
var firstPos = -1;
foreach (var needle in needles)
{
var pos = text.IndexOf(needle);
if (firstPos == -1 || pos < firstPos) firstPos = pos;
}
return firstPos;
}
public static string ExtractContents(this string fromText, string startAfter, string endAt)
{
return ExtractContents(fromText, startAfter, startAfter, endAt);
}
public static string ExtractContents(this string fromText, string uniqueMarker, string startAfter, string endAt)
{
if (string.IsNullOrEmpty(uniqueMarker))
throw new ArgumentNullException("uniqueMarker");
if (string.IsNullOrEmpty(startAfter))
throw new ArgumentNullException("startAfter");
if (string.IsNullOrEmpty(endAt))
throw new ArgumentNullException("endAt");
if (string.IsNullOrEmpty(fromText)) return null;
var markerPos = fromText.IndexOf(uniqueMarker);
if (markerPos == -1) return null;
var startPos = fromText.IndexOf(startAfter, markerPos);
if (startPos == -1) return null;
startPos += startAfter.Length;
var endPos = fromText.IndexOf(endAt, startPos);
if (endPos == -1) endPos = fromText.Length;
return fromText.Substring(startPos, endPos - startPos);
}
#if XBOX && !SILVERLIGHT
static readonly Regex StripHtmlRegEx = new Regex(@"<(.|\n)*?>", RegexOptions.Compiled);
#else
static readonly Regex StripHtmlRegEx = new Regex(@"<(.|\n)*?>");
#endif
public static string StripHtml(this string html)
{
return string.IsNullOrEmpty(html) ? null : StripHtmlRegEx.Replace(html, "");
}
#if XBOX && !SILVERLIGHT
static readonly Regex StripBracketsRegEx = new Regex(@"\[(.|\n)*?\]", RegexOptions.Compiled);
static readonly Regex StripBracesRegEx = new Regex(@"\((.|\n)*?\)", RegexOptions.Compiled);
#else
static readonly Regex StripBracketsRegEx = new Regex(@"\[(.|\n)*?\]");
static readonly Regex StripBracesRegEx = new Regex(@"\((.|\n)*?\)");
#endif
public static string StripMarkdownMarkup(this string markdown)
{
if (string.IsNullOrEmpty(markdown)) return null;
markdown = StripBracketsRegEx.Replace(markdown, "");
markdown = StripBracesRegEx.Replace(markdown, "");
markdown = markdown
.Replace("*", "")
.Replace("!", "")
.Replace("\r", "")
.Replace("\n", "")
.Replace("#", "");
return markdown;
}
private const int LowerCaseOffset = 'a' - 'A';
public static string ToCamelCase(this string value)
{
if (string.IsNullOrEmpty(value)) return value;
var firstChar = value[0];
if (firstChar < 'A' || firstChar > 'Z')
return value;
return (char)(firstChar + LowerCaseOffset) + value.Substring(1);
}
}
}