NumberToStringFormat(ref sb, ref number, format, info);
}
- return sb.GetString();
+ return sb.ToString();
}
public static bool TryFormatDecimal(decimal value, ReadOnlySpan<char> format, NumberFormatInfo info, Span<char> destination, out int charsWritten)
NumberToString(ref sb, ref number, 'G', 17, info, isDecimal: false);
}
- return sb.GetString();
+ return sb.ToString();
}
case 'E':
NumberToStringFormat(ref sb, ref number, format, info);
}
- return sb.GetString();
+ return sb.ToString();
}
public static string FormatSingle(float value, string format, NumberFormatInfo info)
NumberToString(ref sb, ref number, 'G', 9, info, isDecimal: false);
}
- return sb.GetString();
+ return sb.ToString();
}
case 'E':
NumberToStringFormat(ref sb, ref number, format, info);
}
- return sb.GetString();
+ return sb.ToString();
}
public static string FormatInt32(int value, ReadOnlySpan<char> format, NumberFormatInfo info)
{
NumberToStringFormat(ref sb, ref number, format, info);
}
- return sb.GetString();
+ return sb.ToString();
}
}
{
NumberToStringFormat(ref sb, ref number, format, info);
}
- return sb.GetString();
+ return sb.ToString();
}
}
{
NumberToStringFormat(ref sb, ref number, format, info);
}
- return sb.GetString();
+ return sb.ToString();
}
}
{
NumberToStringFormat(ref sb, ref number, format, info);
}
- return sb.GetString();
+ return sb.ToString();
}
}
_pos = 0;
}
- public string GetString()
+ public int Length => _pos;
+
+ public override string ToString()
{
var s = new string(_chars.Slice(0, _pos));
-
- char[] toReturn = _arrayToReturnToPool;
- this = default; // for safety, to avoid using pooled array if this instance is erroneously appended to again
-
- if (toReturn != null)
- {
- ArrayPool<char>.Shared.Return(toReturn);
- }
-
+ Clear();
return s;
}
public bool TryCopyTo(Span<char> destination, out int charsWritten)
{
- if (_pos > destination.Length)
+ if (_chars.Slice(0, _pos).TryCopyTo(destination))
+ {
+ charsWritten = _pos;
+ Clear();
+ return true;
+ }
+ else
{
charsWritten = 0;
+ Clear();
return false;
}
+ }
- bool copied = _chars.Slice(0, _pos).TryCopyTo(destination);
- Debug.Assert(copied);
- charsWritten = _pos;
-
- char[] toReturn = _arrayToReturnToPool;
- this = default; // for safety, to avoid using pooled array if this instance is erroneously appended to again
-
- if (toReturn != null)
+ public void Insert(int index, char value, int count)
+ {
+ if (_pos > _chars.Length - count)
{
- ArrayPool<char>.Shared.Return(toReturn);
+ Grow(count);
}
- return true;
+ int remaining = _pos - index;
+ _chars.Slice(index, remaining).CopyTo(_chars.Slice(index + count));
+ _chars.Slice(index, count).Fill(value);
+ _pos += count;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
{
Debug.Assert(requiredAdditionalCapacity > _chars.Length - _pos);
- char[] poolArray = ArrayPool<char>.Shared.Rent(_pos + requiredAdditionalCapacity);
+ char[] poolArray = ArrayPool<char>.Shared.Rent(Math.Max(_pos + requiredAdditionalCapacity, _chars.Length * 2));
bool success = _chars.TryCopyTo(poolArray);
Debug.Assert(success);
ArrayPool<char>.Shared.Return(toReturn);
}
}
+
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ private void Clear()
+ {
+ char[] toReturn = _arrayToReturnToPool;
+ this = default; // for safety, to avoid using pooled array if this instance is erroneously appended to again
+ if (toReturn != null)
+ {
+ ArrayPool<char>.Shared.Return(toReturn);
+ }
+ }
}
}