Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit a8c8978

Browse files
committed
Port ValueStringBuilder updates/fixes from corefx
1 parent 45f1a4f commit a8c8978

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

src/mscorlib/shared/System/Number.Formatting.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public static string FormatDecimal(decimal value, ReadOnlySpan<char> format, Num
306306
NumberToStringFormat(ref sb, ref number, format, info);
307307
}
308308

309-
return sb.GetString();
309+
return sb.ToString();
310310
}
311311

312312
public static bool TryFormatDecimal(decimal value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
@@ -405,7 +405,7 @@ public static string FormatDouble(double value, string format, NumberFormatInfo
405405
NumberToString(ref sb, ref number, 'G', 17, info, isDecimal: false);
406406
}
407407

408-
return sb.GetString();
408+
return sb.ToString();
409409
}
410410

411411
case 'E':
@@ -446,7 +446,7 @@ public static string FormatDouble(double value, string format, NumberFormatInfo
446446
NumberToStringFormat(ref sb, ref number, format, info);
447447
}
448448

449-
return sb.GetString();
449+
return sb.ToString();
450450
}
451451

452452
public static string FormatSingle(float value, string format, NumberFormatInfo info)
@@ -490,7 +490,7 @@ public static string FormatSingle(float value, string format, NumberFormatInfo i
490490
NumberToString(ref sb, ref number, 'G', 9, info, isDecimal: false);
491491
}
492492

493-
return sb.GetString();
493+
return sb.ToString();
494494
}
495495

496496
case 'E':
@@ -531,7 +531,7 @@ public static string FormatSingle(float value, string format, NumberFormatInfo i
531531
NumberToStringFormat(ref sb, ref number, format, info);
532532
}
533533

534-
return sb.GetString();
534+
return sb.ToString();
535535
}
536536

537537
public static string FormatInt32(int value, ReadOnlySpan<char> format, NumberFormatInfo info)
@@ -570,7 +570,7 @@ public static string FormatInt32(int value, ReadOnlySpan<char> format, NumberFor
570570
{
571571
NumberToStringFormat(ref sb, ref number, format, info);
572572
}
573-
return sb.GetString();
573+
return sb.ToString();
574574
}
575575
}
576576

@@ -648,7 +648,7 @@ public static string FormatUInt32(uint value, ReadOnlySpan<char> format, NumberF
648648
{
649649
NumberToStringFormat(ref sb, ref number, format, info);
650650
}
651-
return sb.GetString();
651+
return sb.ToString();
652652
}
653653
}
654654

@@ -727,7 +727,7 @@ public static string FormatInt64(long value, ReadOnlySpan<char> format, NumberFo
727727
{
728728
NumberToStringFormat(ref sb, ref number, format, info);
729729
}
730-
return sb.GetString();
730+
return sb.ToString();
731731
}
732732
}
733733

@@ -807,7 +807,7 @@ public static string FormatUInt64(ulong value, ReadOnlySpan<char> format, Number
807807
{
808808
NumberToStringFormat(ref sb, ref number, format, info);
809809
}
810-
return sb.GetString();
810+
return sb.ToString();
811811
}
812812
}
813813

src/mscorlib/shared/System/Text/ValueStringBuilder.cs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,42 @@ public ValueStringBuilder(Span<char> initialBuffer)
2121
_pos = 0;
2222
}
2323

24-
public string GetString()
24+
public int Length => _pos;
25+
26+
public override string ToString()
2527
{
2628
var s = new string(_chars.Slice(0, _pos));
27-
28-
char[] toReturn = _arrayToReturnToPool;
29-
this = default; // for safety, to avoid using pooled array if this instance is erroneously appended to again
30-
31-
if (toReturn != null)
32-
{
33-
ArrayPool<char>.Shared.Return(toReturn);
34-
}
35-
29+
Clear();
3630
return s;
3731
}
3832

3933
public bool TryCopyTo(Span<char> destination, out int charsWritten)
4034
{
41-
if (_pos > destination.Length)
35+
if (_chars.Slice(0, _pos).TryCopyTo(destination))
36+
{
37+
charsWritten = _pos;
38+
Clear();
39+
return true;
40+
}
41+
else
4242
{
4343
charsWritten = 0;
44+
Clear();
4445
return false;
4546
}
47+
}
4648

47-
bool copied = _chars.Slice(0, _pos).TryCopyTo(destination);
48-
Debug.Assert(copied);
49-
charsWritten = _pos;
50-
51-
char[] toReturn = _arrayToReturnToPool;
52-
this = default; // for safety, to avoid using pooled array if this instance is erroneously appended to again
53-
54-
if (toReturn != null)
49+
public void Insert(int index, char value, int count)
50+
{
51+
if (_pos > _chars.Length - count)
5552
{
56-
ArrayPool<char>.Shared.Return(toReturn);
53+
Grow(count);
5754
}
5855

59-
return true;
56+
int remaining = _pos - index;
57+
_chars.Slice(index, remaining).CopyTo(_chars.Slice(index + count));
58+
_chars.Slice(index, count).Fill(value);
59+
_pos += count;
6060
}
6161

6262
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -158,7 +158,7 @@ private void Grow(int requiredAdditionalCapacity)
158158
{
159159
Debug.Assert(requiredAdditionalCapacity > _chars.Length - _pos);
160160

161-
char[] poolArray = ArrayPool<char>.Shared.Rent(_pos + requiredAdditionalCapacity);
161+
char[] poolArray = ArrayPool<char>.Shared.Rent(Math.Max(_pos + requiredAdditionalCapacity, _chars.Length * 2));
162162

163163
bool success = _chars.TryCopyTo(poolArray);
164164
Debug.Assert(success);
@@ -170,5 +170,16 @@ private void Grow(int requiredAdditionalCapacity)
170170
ArrayPool<char>.Shared.Return(toReturn);
171171
}
172172
}
173+
174+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
175+
private void Clear()
176+
{
177+
char[] toReturn = _arrayToReturnToPool;
178+
this = default; // for safety, to avoid using pooled array if this instance is erroneously appended to again
179+
if (toReturn != null)
180+
{
181+
ArrayPool<char>.Shared.Return(toReturn);
182+
}
183+
}
173184
}
174185
}

0 commit comments

Comments
 (0)