This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathString.Manipulation.cs
1927 lines (1610 loc) · 69.3 KB
/
String.Manipulation.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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using Internal.Runtime.CompilerServices;
namespace System
{
public partial class String
{
private const int StackallocIntBufferSizeLimit = 128;
private static unsafe void FillStringChecked(string dest, int destPos, string src)
{
Debug.Assert(dest != null);
Debug.Assert(src != null);
if (src.Length > dest.Length - destPos)
{
throw new IndexOutOfRangeException();
}
fixed (char* pDest = &dest._firstChar)
fixed (char* pSrc = &src._firstChar)
{
wstrcpy(pDest + destPos, pSrc, src.Length);
}
}
public static string Concat(object? arg0)
{
if (arg0 == null)
{
return string.Empty;
}
return arg0.ToString();
}
public static string Concat(object? arg0, object? arg1)
{
if (arg0 == null)
{
arg0 = string.Empty;
}
if (arg1 == null)
{
arg1 = string.Empty;
}
return Concat(arg0.ToString(), arg1.ToString());
}
public static string Concat(object? arg0, object? arg1, object? arg2)
{
if (arg0 == null)
{
arg0 = string.Empty;
}
if (arg1 == null)
{
arg1 = string.Empty;
}
if (arg2 == null)
{
arg2 = string.Empty;
}
return Concat(arg0.ToString(), arg1.ToString(), arg2.ToString());
}
public static string Concat(params object?[] args)
{
if (args == null)
{
throw new ArgumentNullException(nameof(args));
}
if (args.Length <= 1)
{
return args.Length == 0 ?
string.Empty :
args[0]?.ToString() ?? string.Empty;
}
// We need to get an intermediary string array
// to fill with each of the args' ToString(),
// and then just concat that in one operation.
// This way we avoid any intermediary string representations,
// or buffer resizing if we use StringBuilder (although the
// latter case is partially alleviated due to StringBuilder's
// linked-list style implementation)
var strings = new string[args.Length];
int totalLength = 0;
for (int i = 0; i < args.Length; i++)
{
object? value = args[i];
string toString = value?.ToString() ?? string.Empty; // We need to handle both the cases when value or value.ToString() is null
strings[i] = toString;
totalLength += toString.Length;
if (totalLength < 0) // Check for a positive overflow
{
throw new OutOfMemoryException();
}
}
// If all of the ToStrings are null/empty, just return string.Empty
if (totalLength == 0)
{
return string.Empty;
}
string result = FastAllocateString(totalLength);
int position = 0; // How many characters we've copied so far
for (int i = 0; i < strings.Length; i++)
{
string s = strings[i];
Debug.Assert(s != null);
Debug.Assert(position <= totalLength - s.Length, "We didn't allocate enough space for the result string!");
FillStringChecked(result, position, s);
position += s.Length;
}
return result;
}
public static string Concat<T>(IEnumerable<T> values)
{
if (values == null)
throw new ArgumentNullException(nameof(values));
if (typeof(T) == typeof(char))
{
// Special-case T==char, as we can handle that case much more efficiently,
// and string.Concat(IEnumerable<char>) can be used as an efficient
// enumerable-based equivalent of new string(char[]).
using (IEnumerator<char> en = Unsafe.As<IEnumerable<char>>(values).GetEnumerator())
{
if (!en.MoveNext())
{
// There weren't any chars. Return the empty string.
return Empty;
}
char c = en.Current; // save the first char
if (!en.MoveNext())
{
// There was only one char. Return a string from it directly.
return CreateFromChar(c);
}
// Create the StringBuilder, add the chars we've already enumerated,
// add the rest, and then get the resulting string.
StringBuilder result = StringBuilderCache.Acquire();
result.Append(c); // first value
do
{
c = en.Current;
result.Append(c);
}
while (en.MoveNext());
return StringBuilderCache.GetStringAndRelease(result);
}
}
else
{
using (IEnumerator<T> en = values.GetEnumerator())
{
if (!en.MoveNext())
return string.Empty;
// We called MoveNext once, so this will be the first item
T currentValue = en.Current;
// Call ToString before calling MoveNext again, since
// we want to stay consistent with the below loop
// Everything should be called in the order
// MoveNext-Current-ToString, unless further optimizations
// can be made, to avoid breaking changes
string? firstString = currentValue?.ToString();
// If there's only 1 item, simply call ToString on that
if (!en.MoveNext())
{
// We have to handle the case of either currentValue
// or its ToString being null
return firstString ?? string.Empty;
}
StringBuilder result = StringBuilderCache.Acquire();
result.Append(firstString);
do
{
currentValue = en.Current;
if (currentValue != null)
{
result.Append(currentValue.ToString());
}
}
while (en.MoveNext());
return StringBuilderCache.GetStringAndRelease(result);
}
}
}
public static string Concat(IEnumerable<string?> values)
{
if (values == null)
throw new ArgumentNullException(nameof(values));
using (IEnumerator<string?> en = values.GetEnumerator())
{
if (!en.MoveNext())
return string.Empty;
string? firstValue = en.Current;
if (!en.MoveNext())
{
return firstValue ?? string.Empty;
}
StringBuilder result = StringBuilderCache.Acquire();
result.Append(firstValue);
do
{
result.Append(en.Current);
}
while (en.MoveNext());
return StringBuilderCache.GetStringAndRelease(result);
}
}
public static string Concat(string? str0, string? str1)
{
if (IsNullOrEmpty(str0))
{
if (IsNullOrEmpty(str1))
{
return string.Empty;
}
return str1;
}
if (IsNullOrEmpty(str1))
{
return str0;
}
int str0Length = str0.Length;
string result = FastAllocateString(str0Length + str1.Length);
FillStringChecked(result, 0, str0);
FillStringChecked(result, str0Length, str1);
return result;
}
public static string Concat(string? str0, string? str1, string? str2)
{
if (IsNullOrEmpty(str0))
{
return Concat(str1, str2);
}
if (IsNullOrEmpty(str1))
{
return Concat(str0, str2);
}
if (IsNullOrEmpty(str2))
{
return Concat(str0, str1);
}
int totalLength = str0.Length + str1.Length + str2.Length;
string result = FastAllocateString(totalLength);
FillStringChecked(result, 0, str0);
FillStringChecked(result, str0.Length, str1);
FillStringChecked(result, str0.Length + str1.Length, str2);
return result;
}
public static string Concat(string? str0, string? str1, string? str2, string? str3)
{
if (IsNullOrEmpty(str0))
{
return Concat(str1, str2, str3);
}
if (IsNullOrEmpty(str1))
{
return Concat(str0, str2, str3);
}
if (IsNullOrEmpty(str2))
{
return Concat(str0, str1, str3);
}
if (IsNullOrEmpty(str3))
{
return Concat(str0, str1, str2);
}
int totalLength = str0.Length + str1.Length + str2.Length + str3.Length;
string result = FastAllocateString(totalLength);
FillStringChecked(result, 0, str0);
FillStringChecked(result, str0.Length, str1);
FillStringChecked(result, str0.Length + str1.Length, str2);
FillStringChecked(result, str0.Length + str1.Length + str2.Length, str3);
return result;
}
public static string Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1)
{
int length = checked(str0.Length + str1.Length);
if (length == 0)
{
return Empty;
}
string result = FastAllocateString(length);
Span<char> resultSpan = new Span<char>(ref result.GetRawStringData(), result.Length);
str0.CopyTo(resultSpan);
str1.CopyTo(resultSpan.Slice(str0.Length));
return result;
}
public static string Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2)
{
int length = checked(str0.Length + str1.Length + str2.Length);
if (length == 0)
{
return Empty;
}
string result = FastAllocateString(length);
Span<char> resultSpan = new Span<char>(ref result.GetRawStringData(), result.Length);
str0.CopyTo(resultSpan);
resultSpan = resultSpan.Slice(str0.Length);
str1.CopyTo(resultSpan);
resultSpan = resultSpan.Slice(str1.Length);
str2.CopyTo(resultSpan);
return result;
}
public static string Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2, ReadOnlySpan<char> str3)
{
int length = checked(str0.Length + str1.Length + str2.Length + str3.Length);
if (length == 0)
{
return Empty;
}
string result = FastAllocateString(length);
Span<char> resultSpan = new Span<char>(ref result.GetRawStringData(), result.Length);
str0.CopyTo(resultSpan);
resultSpan = resultSpan.Slice(str0.Length);
str1.CopyTo(resultSpan);
resultSpan = resultSpan.Slice(str1.Length);
str2.CopyTo(resultSpan);
resultSpan = resultSpan.Slice(str2.Length);
str3.CopyTo(resultSpan);
return result;
}
public static string Concat(params string?[] values)
{
if (values == null)
throw new ArgumentNullException(nameof(values));
if (values.Length <= 1)
{
return values.Length == 0 ?
string.Empty :
values[0] ?? string.Empty;
}
// It's possible that the input values array could be changed concurrently on another
// thread, such that we can't trust that each read of values[i] will be equivalent.
// Worst case, we can make a defensive copy of the array and use that, but we first
// optimistically try the allocation and copies assuming that the array isn't changing,
// which represents the 99.999% case, in particular since string.Concat is used for
// string concatenation by the languages, with the input array being a params array.
// Sum the lengths of all input strings
long totalLengthLong = 0;
for (int i = 0; i < values.Length; i++)
{
string? value = values[i];
if (value != null)
{
totalLengthLong += value.Length;
}
}
// If it's too long, fail, or if it's empty, return an empty string.
if (totalLengthLong > int.MaxValue)
{
throw new OutOfMemoryException();
}
int totalLength = (int)totalLengthLong;
if (totalLength == 0)
{
return string.Empty;
}
// Allocate a new string and copy each input string into it
string result = FastAllocateString(totalLength);
int copiedLength = 0;
for (int i = 0; i < values.Length; i++)
{
string? value = values[i];
if (!string.IsNullOrEmpty(value))
{
int valueLen = value.Length;
if (valueLen > totalLength - copiedLength)
{
copiedLength = -1;
break;
}
FillStringChecked(result, copiedLength, value);
copiedLength += valueLen;
}
}
// If we copied exactly the right amount, return the new string. Otherwise,
// something changed concurrently to mutate the input array: fall back to
// doing the concatenation again, but this time with a defensive copy. This
// fall back should be extremely rare.
return copiedLength == totalLength ? result : Concat((string?[])values.Clone());
}
public static string Format(string format, object? arg0)
{
return FormatHelper(null, format, new ParamsArray(arg0));
}
public static string Format(string format, object? arg0, object? arg1)
{
return FormatHelper(null, format, new ParamsArray(arg0, arg1));
}
public static string Format(string format, object? arg0, object? arg1, object? arg2)
{
return FormatHelper(null, format, new ParamsArray(arg0, arg1, arg2));
}
public static string Format(string format, params object?[] args)
{
if (args == null)
{
// To preserve the original exception behavior, throw an exception about format if both
// args and format are null. The actual null check for format is in FormatHelper.
throw new ArgumentNullException((format == null) ? nameof(format) : nameof(args));
}
return FormatHelper(null, format, new ParamsArray(args));
}
public static string Format(IFormatProvider? provider, string format, object? arg0)
{
return FormatHelper(provider, format, new ParamsArray(arg0));
}
public static string Format(IFormatProvider? provider, string format, object? arg0, object? arg1)
{
return FormatHelper(provider, format, new ParamsArray(arg0, arg1));
}
public static string Format(IFormatProvider? provider, string format, object? arg0, object? arg1, object? arg2)
{
return FormatHelper(provider, format, new ParamsArray(arg0, arg1, arg2));
}
public static string Format(IFormatProvider? provider, string format, params object?[] args)
{
if (args == null)
{
// To preserve the original exception behavior, throw an exception about format if both
// args and format are null. The actual null check for format is in FormatHelper.
throw new ArgumentNullException((format == null) ? nameof(format) : nameof(args));
}
return FormatHelper(provider, format, new ParamsArray(args));
}
private static string FormatHelper(IFormatProvider? provider, string format, ParamsArray args)
{
if (format == null)
throw new ArgumentNullException(nameof(format));
return StringBuilderCache.GetStringAndRelease(
StringBuilderCache
.Acquire(format.Length + args.Length * 8)
.AppendFormatHelper(provider, format, args));
}
public string Insert(int startIndex, string value)
{
if (value == null)
throw new ArgumentNullException(nameof(value));
if (startIndex < 0 || startIndex > this.Length)
throw new ArgumentOutOfRangeException(nameof(startIndex));
int oldLength = Length;
int insertLength = value.Length;
if (oldLength == 0)
return value;
if (insertLength == 0)
return this;
// In case this computation overflows, newLength will be negative and FastAllocateString throws OutOfMemoryException
int newLength = oldLength + insertLength;
string result = FastAllocateString(newLength);
unsafe
{
fixed (char* srcThis = &_firstChar)
{
fixed (char* srcInsert = &value._firstChar)
{
fixed (char* dst = &result._firstChar)
{
wstrcpy(dst, srcThis, startIndex);
wstrcpy(dst + startIndex, srcInsert, insertLength);
wstrcpy(dst + startIndex + insertLength, srcThis + startIndex, oldLength - startIndex);
}
}
}
}
return result;
}
public static string Join(char separator, params string?[] value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
return Join(separator, value, 0, value.Length);
}
public static unsafe string Join(char separator, params object?[] values)
{
// Defer argument validation to the internal function
return JoinCore(&separator, 1, values);
}
public static unsafe string Join<T>(char separator, IEnumerable<T> values)
{
// Defer argument validation to the internal function
return JoinCore(&separator, 1, values);
}
public static unsafe string Join(char separator, string?[] value, int startIndex, int count)
{
// Defer argument validation to the internal function
return JoinCore(&separator, 1, value, startIndex, count);
}
// Joins an array of strings together as one string with a separator between each original string.
//
public static string Join(string? separator, params string?[] value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
return Join(separator, value, 0, value.Length);
}
public static unsafe string Join(string? separator, params object?[] values)
{
separator = separator ?? string.Empty;
fixed (char* pSeparator = &separator._firstChar)
{
// Defer argument validation to the internal function
return JoinCore(pSeparator, separator.Length, values);
}
}
public static unsafe string Join<T>(string? separator, IEnumerable<T> values)
{
separator = separator ?? string.Empty;
fixed (char* pSeparator = &separator._firstChar)
{
// Defer argument validation to the internal function
return JoinCore(pSeparator, separator.Length, values);
}
}
public static string Join(string? separator, IEnumerable<string> values)
{
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
using (IEnumerator<string> en = values.GetEnumerator())
{
if (!en.MoveNext())
{
return string.Empty;
}
string firstValue = en.Current;
if (!en.MoveNext())
{
// Only one value available
return firstValue ?? string.Empty;
}
// Null separator and values are handled by the StringBuilder
StringBuilder result = StringBuilderCache.Acquire();
result.Append(firstValue);
do
{
result.Append(separator);
result.Append(en.Current);
}
while (en.MoveNext());
return StringBuilderCache.GetStringAndRelease(result);
}
}
// Joins an array of strings together as one string with a separator between each original string.
//
public static unsafe string Join(string? separator, string?[] value, int startIndex, int count)
{
separator = separator ?? string.Empty;
fixed (char* pSeparator = &separator._firstChar)
{
// Defer argument validation to the internal function
return JoinCore(pSeparator, separator.Length, value, startIndex, count);
}
}
private static unsafe string JoinCore(char* separator, int separatorLength, object?[] values)
{
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
if (values.Length == 0)
{
return string.Empty;
}
string? firstString = values[0]?.ToString();
if (values.Length == 1)
{
return firstString ?? string.Empty;
}
StringBuilder result = StringBuilderCache.Acquire();
result.Append(firstString);
for (int i = 1; i < values.Length; i++)
{
result.Append(separator, separatorLength);
object? value = values[i];
if (value != null)
{
result.Append(value.ToString());
}
}
return StringBuilderCache.GetStringAndRelease(result);
}
private static unsafe string JoinCore<T>(char* separator, int separatorLength, IEnumerable<T> values)
{
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}
using (IEnumerator<T> en = values.GetEnumerator())
{
if (!en.MoveNext())
{
return string.Empty;
}
// We called MoveNext once, so this will be the first item
T currentValue = en.Current;
// Call ToString before calling MoveNext again, since
// we want to stay consistent with the below loop
// Everything should be called in the order
// MoveNext-Current-ToString, unless further optimizations
// can be made, to avoid breaking changes
string? firstString = currentValue?.ToString();
// If there's only 1 item, simply call ToString on that
if (!en.MoveNext())
{
// We have to handle the case of either currentValue
// or its ToString being null
return firstString ?? string.Empty;
}
StringBuilder result = StringBuilderCache.Acquire();
result.Append(firstString);
do
{
currentValue = en.Current;
result.Append(separator, separatorLength);
if (currentValue != null)
{
result.Append(currentValue.ToString());
}
}
while (en.MoveNext());
return StringBuilderCache.GetStringAndRelease(result);
}
}
private static unsafe string JoinCore(char* separator, int separatorLength, string?[] value, int startIndex, int count)
{
// If the separator is null, it is converted to an empty string before entering this function.
// Even for empty strings, fixed should never return null (it should return a pointer to a null char).
Debug.Assert(separator != null);
Debug.Assert(separatorLength >= 0);
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
if (startIndex < 0)
{
throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);
}
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NegativeCount);
}
if (startIndex > value.Length - count)
{
throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_IndexCountBuffer);
}
if (count <= 1)
{
return count == 0 ?
string.Empty :
value[startIndex] ?? string.Empty;
}
long totalSeparatorsLength = (long)(count - 1) * separatorLength;
if (totalSeparatorsLength > int.MaxValue)
{
throw new OutOfMemoryException();
}
int totalLength = (int)totalSeparatorsLength;
// Calculate the length of the resultant string so we know how much space to allocate.
for (int i = startIndex, end = startIndex + count; i < end; i++)
{
string? currentValue = value[i];
if (currentValue != null)
{
totalLength += currentValue.Length;
if (totalLength < 0) // Check for overflow
{
throw new OutOfMemoryException();
}
}
}
// Copy each of the strings into the resultant buffer, interleaving with the separator.
string result = FastAllocateString(totalLength);
int copiedLength = 0;
for (int i = startIndex, end = startIndex + count; i < end; i++)
{
// It's possible that another thread may have mutated the input array
// such that our second read of an index will not be the same string
// we got during the first read.
// We range check again to avoid buffer overflows if this happens.
string? currentValue = value[i];
if (currentValue != null)
{
int valueLen = currentValue.Length;
if (valueLen > totalLength - copiedLength)
{
copiedLength = -1;
break;
}
// Fill in the value.
FillStringChecked(result, copiedLength, currentValue);
copiedLength += valueLen;
}
if (i < end - 1)
{
// Fill in the separator.
fixed (char* pResult = &result._firstChar)
{
// If we are called from the char-based overload, we will not
// want to call MemoryCopy each time we fill in the separator. So
// specialize for 1-length separators.
if (separatorLength == 1)
{
pResult[copiedLength] = *separator;
}
else
{
wstrcpy(pResult + copiedLength, separator, separatorLength);
}
}
copiedLength += separatorLength;
}
}
// If we copied exactly the right amount, return the new string. Otherwise,
// something changed concurrently to mutate the input array: fall back to
// doing the concatenation again, but this time with a defensive copy. This
// fall back should be extremely rare.
return copiedLength == totalLength ?
result :
JoinCore(separator, separatorLength, (string?[])value.Clone(), startIndex, count);
}
public string PadLeft(int totalWidth) => PadLeft(totalWidth, ' ');
public string PadLeft(int totalWidth, char paddingChar)
{
if (totalWidth < 0)
throw new ArgumentOutOfRangeException(nameof(totalWidth), SR.ArgumentOutOfRange_NeedNonNegNum);
int oldLength = Length;
int count = totalWidth - oldLength;
if (count <= 0)
return this;
string result = FastAllocateString(totalWidth);
unsafe
{
fixed (char* dst = &result._firstChar)
{
for (int i = 0; i < count; i++)
dst[i] = paddingChar;
fixed (char* src = &_firstChar)
{
wstrcpy(dst + count, src, oldLength);
}
}
}
return result;
}
public string PadRight(int totalWidth) => PadRight(totalWidth, ' ');
public string PadRight(int totalWidth, char paddingChar)
{
if (totalWidth < 0)
throw new ArgumentOutOfRangeException(nameof(totalWidth), SR.ArgumentOutOfRange_NeedNonNegNum);
int oldLength = Length;
int count = totalWidth - oldLength;
if (count <= 0)
return this;
string result = FastAllocateString(totalWidth);
unsafe
{
fixed (char* dst = &result._firstChar)
{
fixed (char* src = &_firstChar)
{
wstrcpy(dst, src, oldLength);
}
for (int i = 0; i < count; i++)
dst[oldLength + i] = paddingChar;
}
}
return result;
}
public string Remove(int startIndex, int count)
{
if (startIndex < 0)
throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NegativeCount);
int oldLength = this.Length;
if (count > oldLength - startIndex)
throw new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_IndexCount);
if (count == 0)
return this;
int newLength = oldLength - count;
if (newLength == 0)
return string.Empty;
string result = FastAllocateString(newLength);
unsafe
{
fixed (char* src = &_firstChar)
{
fixed (char* dst = &result._firstChar)
{
wstrcpy(dst, src, startIndex);
wstrcpy(dst + startIndex, src + startIndex + count, newLength - startIndex);
}
}
}
return result;
}
// a remove that just takes a startindex.
public string Remove(int startIndex)
{
if (startIndex < 0)
throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndex);
if (startIndex >= Length)
throw new ArgumentOutOfRangeException(nameof(startIndex), SR.ArgumentOutOfRange_StartIndexLessThanLength);
return Substring(0, startIndex);
}
public string Replace(string oldValue, string? newValue, bool ignoreCase, CultureInfo? culture)
{
return ReplaceCore(oldValue, newValue, culture, ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None);
}
public string Replace(string oldValue, string? newValue, StringComparison comparisonType)
{
switch (comparisonType)
{
case StringComparison.CurrentCulture:
case StringComparison.CurrentCultureIgnoreCase:
return ReplaceCore(oldValue, newValue, CultureInfo.CurrentCulture, GetCaseCompareOfComparisonCulture(comparisonType));
case StringComparison.InvariantCulture:
case StringComparison.InvariantCultureIgnoreCase:
return ReplaceCore(oldValue, newValue, CultureInfo.InvariantCulture, GetCaseCompareOfComparisonCulture(comparisonType));
case StringComparison.Ordinal:
return Replace(oldValue, newValue);
case StringComparison.OrdinalIgnoreCase:
return ReplaceCore(oldValue, newValue, CultureInfo.InvariantCulture, CompareOptions.OrdinalIgnoreCase);
default: