forked from oznetmaster/SSharpCrestronExtensionsLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringExtensions.cs
739 lines (630 loc) · 18.3 KB
/
StringExtensions.cs
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace System
{
public static class StringExtensions
{
public static String[] Split (this string s, char[] separator, StringSplitOptions options)
{
return s.Split (separator, Int32.MaxValue, options);
}
public static String[] Split (this string s, char[] separator, int count)
{
return s.Split (separator, count, StringSplitOptions.None);
}
public static String[] Split (this string s, char[] separator, int count, StringSplitOptions options)
{
if (count < 0)
throw new ArgumentOutOfRangeException ("count", "Count cannot be less than zero.");
if ((options != StringSplitOptions.None) && (options != StringSplitOptions.RemoveEmptyEntries))
throw new ArgumentException ("Illegal enum value: " + options + ".");
if (s.Length == 0 && (options & StringSplitOptions.RemoveEmptyEntries) != 0)
return ArrayEx.Empty<string> ();
if (count <= 1)
{
return count == 0
? ArrayEx.Empty<string> ()
: new[] {s};
}
return s.SplitByCharacters (separator, count, options != 0);
}
public static String[] Split (this string s, string[] separator, StringSplitOptions options)
{
return s.Split (separator, Int32.MaxValue, options);
}
public static String[] Split (this string s, string[] separator, int count, StringSplitOptions options)
{
if (count < 0)
throw new ArgumentOutOfRangeException ("count", "Count cannot be less than zero.");
if ((options != StringSplitOptions.None) && (options != StringSplitOptions.RemoveEmptyEntries))
throw new ArgumentException ("Illegal enum value: " + options + ".");
if (count <= 1)
{
return count == 0
? ArrayEx.Empty<string> ()
: new[] {s};
}
bool removeEmpty = (options & StringSplitOptions.RemoveEmptyEntries) != 0;
if (separator == null || separator.Length == 0)
return s.SplitByCharacters (null, count, removeEmpty);
if (s.Length == 0 && removeEmpty)
return ArrayEx.Empty<string> ();
var arr = new List<String> ();
int pos = 0;
int matchCount = 0;
while (pos < s.Length)
{
int matchIndex = -1;
int matchPos = Int32.MaxValue;
// Find the first position where any of the separators matches
for (int i = 0; i < separator.Length; ++i)
{
string sep = separator[i];
if (string.IsNullOrEmpty (sep))
continue;
int match = s.IndexOfOrdinalUnchecked (sep, pos, s.Length - pos);
if (match >= 0 && match < matchPos)
{
matchIndex = i;
matchPos = match;
}
}
if (matchIndex == -1)
break;
if (!(matchPos == pos && removeEmpty))
{
if (arr.Count == count - 1)
break;
arr.Add (s.Substring (pos, matchPos - pos));
}
pos = matchPos + separator[matchIndex].Length;
matchCount++;
}
if (matchCount == 0)
return new[] {s};
// string contained only separators
if (removeEmpty && matchCount != 0 && pos == s.Length && arr.Count == 0)
return ArrayEx.Empty<string> ();
if (!(removeEmpty && pos == s.Length))
arr.Add (s.Substring (pos));
return arr.ToArray ();
}
private static readonly char[] WhiteChars =
{
(char)0x9, (char)0xA, (char)0xB, (char)0xC, (char)0xD,
(char)0x85, (char)0x1680, (char)0x2028, (char)0x2029,
(char)0x20, (char)0xA0, (char)0x2000, (char)0x2001, (char)0x2002, (char)0x2003, (char)0x2004,
(char)0x2005, (char)0x2006, (char)0x2007, (char)0x2008, (char)0x2009, (char)0x200A, (char)0x200B,
(char)0x3000, (char)0xFEFF
};
#if ALLOWUNSAFE
unsafe static string[] SplitByCharacters (this string s, char[] sep, int count, bool removeEmpty)
{
if (sep == null || sep.Length == 0)
sep = WhiteChars;
int[] split_points = null;
int total_points = 0;
--count;
if (sep == null || sep.Length == 0)
{
fixed (char* src = s)
{
char* src_ptr = src;
int len = s.Length;
while (len > 0)
{
if (char.IsWhiteSpace (*src_ptr++))
{
if (split_points == null)
{
split_points = new int[8];
}
else if (split_points.Length == total_points)
{
Array.Resize (ref split_points, split_points.Length * 2);
}
split_points[total_points++] = s.Length - len;
if (total_points == count && !removeEmpty)
break;
}
--len;
}
}
}
else
{
fixed (char* src = s)
{
fixed (char* sep_src = sep)
{
char* src_ptr = src;
char* sep_ptr_end = sep_src + sep.Length;
int len = s.Length;
while (len > 0)
{
char* sep_ptr = sep_src;
do
{
if (*sep_ptr++ == *src_ptr)
{
if (split_points == null)
{
split_points = new int[8];
}
else if (split_points.Length == total_points)
{
Array.Resize (ref split_points, split_points.Length * 2);
}
split_points[total_points++] = s.Length - len;
if (total_points == count && !removeEmpty)
len = 0;
break;
}
} while (sep_ptr != sep_ptr_end);
++src_ptr;
--len;
}
}
}
}
if (total_points == 0)
return new string[] { s };
var res = new string[Math.Min (total_points, count) + 1];
int prev_index = 0;
int i = 0;
if (!removeEmpty)
{
for (; i < total_points; ++i)
{
var start = split_points[i];
res[i] = s.SubstringUnchecked (prev_index, start - prev_index);
prev_index = start + 1;
}
res[i] = s.SubstringUnchecked (prev_index, s.Length - prev_index);
}
else
{
int used = 0;
int length;
for (; i < total_points; ++i)
{
var start = split_points[i];
length = start - prev_index;
if (length != 0)
{
if (used == count)
break;
res[used++] = s.SubstringUnchecked (prev_index, length);
}
prev_index = start + 1;
}
length = s.Length - prev_index;
if (length != 0)
res[used++] = s.SubstringUnchecked (prev_index, length);
if (used != res.Length)
Array.Resize (ref res, used);
}
return res;
}
#else
private static string[] SplitByCharacters (this string s, char[] sep, int count, bool removeEmpty)
{
if (sep == null || sep.Length == 0)
sep = WhiteChars;
int[] split_points = null;
int total_points = 0;
--count;
if (sep == null || sep.Length == 0)
{
const int srcIx = 0;
{
int src_ptrIx = srcIx;
int len = s.Length;
while (len > 0)
{
if (char.IsWhiteSpace (s[src_ptrIx++]))
{
if (split_points == null)
{
split_points = new int[8];
}
else if (split_points.Length == total_points)
{
Array.Resize (ref split_points, split_points.Length * 2);
}
split_points[total_points++] = s.Length - len;
if (total_points == count && !removeEmpty)
break;
}
--len;
}
}
}
else
{
const int srcIx = 0;
{
const int sep_srcIx = 0;
{
int src_ptrIx = srcIx;
int sep_ptr_endIx = sep_srcIx + sep.Length;
int len = s.Length;
while (len > 0)
{
int sep_ptrIx = sep_srcIx;
do
{
if (sep[sep_ptrIx++] == s[src_ptrIx])
{
if (split_points == null)
{
split_points = new int[8];
}
else if (split_points.Length == total_points)
{
Array.Resize (ref split_points, split_points.Length * 2);
}
split_points[total_points++] = s.Length - len;
if (total_points == count && !removeEmpty)
len = 0;
break;
}
} while (sep_ptrIx != sep_ptr_endIx);
++src_ptrIx;
--len;
}
}
}
}
if (total_points == 0)
return new[] {s};
var res = new string[Math.Min (total_points, count) + 1];
int prev_index = 0;
int i = 0;
if (!removeEmpty)
{
for (; i < total_points; ++i)
{
var start = split_points[i];
res[i] = s.SubstringUnchecked (prev_index, start - prev_index);
prev_index = start + 1;
}
res[i] = s.SubstringUnchecked (prev_index, s.Length - prev_index);
}
else
{
int used = 0;
int length;
for (; i < total_points; ++i)
{
var start = split_points[i];
length = start - prev_index;
if (length != 0)
{
if (used == count)
break;
res[used++] = s.SubstringUnchecked (prev_index, length);
}
prev_index = start + 1;
}
length = s.Length - prev_index;
if (length != 0)
res[used++] = s.SubstringUnchecked (prev_index, length);
if (used != res.Length)
Array.Resize (ref res, used);
}
return res;
}
#endif
internal static String SubstringUnchecked (this string s, int startIndex, int length)
{
return s.Substring (startIndex, length);
/*
if (length == 0)
return String.Empty;
string tmp = InternalAllocateStr (length);
fixed (char* dest = tmp, src = s)
{
CharCopy (dest, src + startIndex, length);
}
return tmp;
*/
}
internal static int IndexOfOrdinalUnchecked (this string s, string value)
{
return s.IndexOfOrdinalUnchecked (value, 0, s.Length);
}
#if ALLOWUNSAFE
internal static unsafe int IndexOfOrdinalUnchecked (this string s, string value, int startIndex, int count)
{
int valueLen = value.Length;
if (count < valueLen)
return -1;
if (valueLen <= 1)
{
if (valueLen == 1)
return s.IndexOfUnchecked (value[0], startIndex, count);
return startIndex;
}
fixed (char* thisptr = s, valueptr = value)
{
char* ap = thisptr + startIndex;
char* thisEnd = ap + count - valueLen + 1;
while (ap != thisEnd)
{
if (*ap == *valueptr)
{
for (int i = 1; i < valueLen; i++)
{
if (ap[i] != valueptr[i])
goto NextVal;
}
return (int)(ap - thisptr);
}
NextVal:
ap++;
}
}
return -1;
}
internal static unsafe int IndexOfUnchecked (this string s, char value, int startIndex, int count)
{
// It helps JIT compiler to optimize comparison
int value_32 = (int)value;
fixed (char* start = s)
{
char* ptr = start + startIndex;
char* end_ptr = ptr + (count >> 3 << 3);
while (ptr != end_ptr)
{
if (*ptr == value_32)
return (int)(ptr - start);
if (ptr[1] == value_32)
return (int)(ptr - start + 1);
if (ptr[2] == value_32)
return (int)(ptr - start + 2);
if (ptr[3] == value_32)
return (int)(ptr - start + 3);
if (ptr[4] == value_32)
return (int)(ptr - start + 4);
if (ptr[5] == value_32)
return (int)(ptr - start + 5);
if (ptr[6] == value_32)
return (int)(ptr - start + 6);
if (ptr[7] == value_32)
return (int)(ptr - start + 7);
ptr += 8;
}
end_ptr += count & 0x07;
while (ptr != end_ptr)
{
if (*ptr == value_32)
return (int)(ptr - start);
ptr++;
}
return -1;
}
}
#else
internal static int IndexOfOrdinalUnchecked (this string s, string value, int startIndex, int count)
{
int valueLen = value.Length;
if (count < valueLen)
return -1;
if (valueLen <= 1)
{
if (valueLen == 1)
return s.IndexOfUnchecked (value[0], startIndex, count);
return startIndex;
}
const int thisptrIx = 0;
const int valueptrIx = 0;
{
int apIx = thisptrIx + startIndex;
int thisEndIx = apIx + count - valueLen + 1;
while (apIx != thisEndIx)
{
if (s[apIx] == value[valueptrIx])
{
for (int i = 1; i < valueLen; i++)
{
if (s[apIx + i] != value[valueptrIx + i])
goto NextVal;
}
return apIx - thisptrIx;
}
NextVal:
apIx++;
}
}
return -1;
}
internal static int IndexOfUnchecked (this string s, char value, int startIndex, int count)
{
// It helps JIT compiler to optimize comparison
int value_32 = value;
const int startIx = 0;
{
int ptrIx = startIx + startIndex;
int end_ptrIx = ptrIx + (count >> 3 << 3);
while (ptrIx != end_ptrIx)
{
if (s[ptrIx] == value_32)
return ptrIx - startIx;
if (s[ptrIx + 1] == value_32)
return ptrIx - startIx + 1;
if (s[ptrIx + 2] == value_32)
return ptrIx - startIx + 2;
if (s[ptrIx + 3] == value_32)
return ptrIx - startIx + 3;
if (s[ptrIx + 4] == value_32)
return ptrIx - startIx + 4;
if (s[ptrIx + 5] == value_32)
return ptrIx - startIx + 5;
if (s[ptrIx + 6] == value_32)
return ptrIx - startIx + 6;
if (s[ptrIx + 7] == value_32)
return ptrIx - startIx + 7;
ptrIx += 8;
}
end_ptrIx += count & 0x07;
while (ptrIx != end_ptrIx)
{
if (s[ptrIx] == value_32)
return ptrIx - startIx;
ptrIx++;
}
return -1;
}
}
#endif
public static string ToLowerInvariant (this string str)
{
return str.ToLower (CultureInfo.InvariantCulture);
}
public static string ToUpperInvariant (this string str)
{
return str.ToUpper (CultureInfo.InvariantCulture);
}
public static string Remove (this string str, int startIndex)
{
if (startIndex >= str.Length || startIndex < 0)
throw new ArgumentOutOfRangeException ("startIndex");
return str.Remove (startIndex, str.Length - startIndex);
}
private static readonly byte[] LookupTable =
{
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,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xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
private static byte Lookup (char c)
{
if (c > 255)
throw new ArgumentOutOfRangeException ("c", "Not ANSII character");
var b = LookupTable[c];
if (b == 255)
throw new ArgumentOutOfRangeException ("c", "Expected a hex character, got " + c);
return b;
}
public static byte HexToByte (this char[] chars, int offset)
{
return (byte)(Lookup (chars[offset]) << 4 | Lookup (chars[offset + 1]));
}
public static byte[] HexToBytes (this string str)
{
if (str == null)
throw new ArgumentNullException ("str");
var ca = str.ToCharArray ();
var cl = ca.Length;
if ((cl & 1) != 0)
throw new ArgumentException ("must be even length", "str");
var ba = new byte[cl >> 1];
for (var i = 0; i < cl; i += 2)
ba[i >> 1] = (byte)(Lookup (ca[i]) << 4 | Lookup (ca[i + 1]));
return ba;
}
public static bool Contains (this string str, char value)
{
return str.IndexOf (value) != -1;
}
public static bool Contains (this string str, string value, StringComparison comparisonType)
{
return str.IndexOf (value, comparisonType) != -1;
}
public static bool Contains (this string str, char value, StringComparison comparisonType)
{
return str.IndexOf (value.ToString (), comparisonType) != -1;
}
}
public static class StringEx
{
public static bool IsNullOrWhiteSpace (string str)
{
if (string.IsNullOrEmpty (str))
return true;
return str.All (Char.IsWhiteSpace);
}
public static string Join<T> (string separator, IEnumerable<T> values)
{
if (separator == null)
return Concat (values);
if (values == null)
throw new ArgumentNullException ("values");
var stringList = values as IList<T> ?? new List<T> (values);
var strCopy = new string[stringList.Count];
int i = 0;
foreach (var v in stringList)
strCopy[i++] = v.ToString ();
return String.Join (separator, strCopy);
}
public static string Join (string separator, params object[] values)
{
if (separator == null)
return Concat (values);
if (values == null)
throw new ArgumentNullException ("values");
var strCopy = new string[values.Length];
int i = 0;
foreach (var v in values)
strCopy[i++] = v.ToString ();
return String.Join (separator, strCopy);
}
public static string Join (string separator, IEnumerable<string> values)
{
if (separator == null)
return Concat (values);
if (values == null)
throw new ArgumentNullException ("values");
var stringList = new List<string> (values);
return String.Join (separator, stringList.ToArray ());
}
public static string Concat<T> (IEnumerable<T> values)
{
if (values == null)
throw new ArgumentNullException ("values");
var stringList = new List<string> ();
int len = 0;
foreach (var v in values)
{
string sr = v.ToString ();
len += sr.Length;
if (len < 0)
throw new OutOfMemoryException ();
stringList.Add (sr);
}
return String.Concat (stringList.ToArray ());
}
public static string Concat (IEnumerable<string> values)
{
if (values == null)
throw new ArgumentNullException ("values");
var stringList = new List<string> ();
int len = 0;
foreach (var v in values)
{
if (v == null)
continue;
len += v.Length;
if (len < 0)
throw new OutOfMemoryException ();
stringList.Add (v);
}
return String.Concat (stringList.ToArray ());
}
}
}