123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- using System.Buffers;
- using System.Diagnostics;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- // ReSharper disable once CheckNamespace
- namespace System.Text;
- internal ref struct ValueStringBuilder
- {
- private char[]? _arrayToReturnToPool;
- private Span<char> _chars;
- private int _pos;
- public ValueStringBuilder(Span<char> initialBuffer)
- {
- _arrayToReturnToPool = null;
- _chars = initialBuffer;
- _pos = 0;
- }
- public ValueStringBuilder(int initialCapacity)
- {
- _arrayToReturnToPool = ArrayPool<char>.Shared.Rent(initialCapacity);
- _chars = _arrayToReturnToPool;
- _pos = 0;
- }
- public int Length
- {
- get => _pos;
- set
- {
- Debug.Assert(value >= 0);
- Debug.Assert(value <= _chars.Length);
- _pos = value;
- }
- }
- public int Capacity => _chars.Length;
- public void EnsureCapacity(int capacity)
- {
- // This is not expected to be called this with negative capacity
- Debug.Assert(capacity >= 0);
- // If the caller has a bug and calls this with negative capacity, make sure to call Grow to throw an exception.
- if ((uint) capacity > (uint) _chars.Length)
- Grow(capacity - _pos);
- }
- /// <summary>
- /// Get a pinnable reference to the builder.
- /// Does not ensure there is a null char after <see cref="Length"/>
- /// This overload is pattern matched in the C# 7.3+ compiler so you can omit
- /// the explicit method call, and write eg "fixed (char* c = builder)"
- /// </summary>
- public ref char GetPinnableReference()
- {
- return ref MemoryMarshal.GetReference(_chars);
- }
- /// <summary>
- /// Get a pinnable reference to the builder.
- /// </summary>
- /// <param name="terminate">Ensures that the builder has a null char after <see cref="Length"/></param>
- public ref char GetPinnableReference(bool terminate)
- {
- if (terminate)
- {
- EnsureCapacity(Length + 1);
- _chars[Length] = '\0';
- }
- return ref MemoryMarshal.GetReference(_chars);
- }
- public ref char this[int index]
- {
- get
- {
- Debug.Assert(index < _pos);
- return ref _chars[index];
- }
- }
- public override string ToString()
- {
- string s = _chars.Slice(0, _pos).ToString();
- Dispose();
- return s;
- }
- /// <summary>Returns the underlying storage of the builder.</summary>
- public Span<char> RawChars => _chars;
- /// <summary>
- /// Returns a span around the contents of the builder.
- /// </summary>
- /// <param name="terminate">Ensures that the builder has a null char after <see cref="Length"/></param>
- public ReadOnlySpan<char> AsSpan(bool terminate)
- {
- if (terminate)
- {
- EnsureCapacity(Length + 1);
- _chars[Length] = '\0';
- }
- return _chars.Slice(0, _pos);
- }
- public ReadOnlySpan<char> AsSpan() => _chars.Slice(0, _pos);
- public ReadOnlySpan<char> AsSpan(int start) => _chars.Slice(start, _pos - start);
- public ReadOnlySpan<char> AsSpan(int start, int length) => _chars.Slice(start, length);
- public void Reverse()
- {
- _chars.Slice(0, _pos).Reverse();
- }
- public bool TryCopyTo(Span<char> destination, out int charsWritten)
- {
- if (_chars.Slice(0, _pos).TryCopyTo(destination))
- {
- charsWritten = _pos;
- Dispose();
- return true;
- }
- else
- {
- charsWritten = 0;
- Dispose();
- return false;
- }
- }
- public void Insert(int index, char value, int count)
- {
- if (_pos > _chars.Length - count)
- {
- Grow(count);
- }
- int remaining = _pos - index;
- _chars.Slice(index, remaining).CopyTo(_chars.Slice(index + count));
- _chars.Slice(index, count).Fill(value);
- _pos += count;
- }
- public void Insert(int index, string? s)
- {
- if (s == null)
- {
- return;
- }
- int count = s.Length;
- if (_pos > (_chars.Length - count))
- {
- Grow(count);
- }
- int remaining = _pos - index;
- _chars.Slice(index, remaining).CopyTo(_chars.Slice(index + count));
- s
- #if !NETCOREAPP
- .AsSpan()
- #endif
- .CopyTo(_chars.Slice(index));
- _pos += count;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void Append(char c)
- {
- int pos = _pos;
- Span<char> chars = _chars;
- if ((uint) pos < (uint) chars.Length)
- {
- chars[pos] = c;
- _pos = pos + 1;
- }
- else
- {
- GrowAndAppend(c);
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void AppendHex(byte b)
- {
- const string Map = "0123456789ABCDEF";
- ReadOnlySpan<char> data =
- [
- Map[b / 16],
- Map[b % 16]
- ];
- Append(data);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void Append(string? s)
- {
- if (s == null)
- {
- return;
- }
- int pos = _pos;
- if (s.Length == 1 && (uint) pos < (uint) _chars.Length) // very common case, e.g. appending strings from NumberFormatInfo like separators, percent symbols, etc.
- {
- _chars[pos] = s[0];
- _pos = pos + 1;
- }
- else
- {
- AppendSlow(s);
- }
- }
- #if NET6_0_OR_GREATER
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void Append<T>(T value) where T : ISpanFormattable
- {
- if (value.TryFormat(_chars.Slice(_pos), out var charsWritten, format: default, provider: null))
- {
- _pos += charsWritten;
- }
- else
- {
- Append(value.ToString());
- }
- }
- #else
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void Append(int value)
- {
- Append(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void Append(long value)
- {
- Append(value.ToString(System.Globalization.CultureInfo.InvariantCulture));
- }
- #endif
- private void AppendSlow(string s)
- {
- int pos = _pos;
- if (pos > _chars.Length - s.Length)
- {
- Grow(s.Length);
- }
- s
- #if !NETCOREAPP
- .AsSpan()
- #endif
- .CopyTo(_chars.Slice(pos));
- _pos += s.Length;
- }
- public void Append(char c, int count)
- {
- if (_pos > _chars.Length - count)
- {
- Grow(count);
- }
- Span<char> dst = _chars.Slice(_pos, count);
- for (int i = 0; i < dst.Length; i++)
- {
- dst[i] = c;
- }
- _pos += count;
- }
- public unsafe void Append(char* value, int length)
- {
- int pos = _pos;
- if (pos > _chars.Length - length)
- {
- Grow(length);
- }
- Span<char> dst = _chars.Slice(_pos, length);
- for (int i = 0; i < dst.Length; i++)
- {
- dst[i] = *value++;
- }
- _pos += length;
- }
- public void Append(scoped ReadOnlySpan<char> value)
- {
- int pos = _pos;
- if (pos > _chars.Length - value.Length)
- {
- Grow(value.Length);
- }
- value.CopyTo(_chars.Slice(_pos));
- _pos += value.Length;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public Span<char> AppendSpan(int length)
- {
- int origPos = _pos;
- if (origPos > _chars.Length - length)
- {
- Grow(length);
- }
- _pos = origPos + length;
- return _chars.Slice(origPos, length);
- }
- [MethodImpl(MethodImplOptions.NoInlining)]
- private void GrowAndAppend(char c)
- {
- Grow(1);
- Append(c);
- }
- /// <summary>
- /// Resize the internal buffer either by doubling current buffer size or
- /// by adding <paramref name="additionalCapacityBeyondPos"/> to
- /// <see cref="_pos"/> whichever is greater.
- /// </summary>
- /// <param name="additionalCapacityBeyondPos">
- /// Number of chars requested beyond current position.
- /// </param>
- [MethodImpl(MethodImplOptions.NoInlining)]
- private void Grow(int additionalCapacityBeyondPos)
- {
- Debug.Assert(additionalCapacityBeyondPos > 0);
- Debug.Assert(_pos > _chars.Length - additionalCapacityBeyondPos, "Grow called incorrectly, no resize is needed.");
- const uint ArrayMaxLength = 0x7FFFFFC7; // same as Array.MaxLength
- // Increase to at least the required size (_pos + additionalCapacityBeyondPos), but try
- // to double the size if possible, bounding the doubling to not go beyond the max array length.
- int newCapacity = (int) Math.Max(
- (uint) (_pos + additionalCapacityBeyondPos),
- Math.Min((uint) _chars.Length * 2, ArrayMaxLength));
- // Make sure to let Rent throw an exception if the caller has a bug and the desired capacity is negative.
- // This could also go negative if the actual required length wraps around.
- char[] poolArray = ArrayPool<char>.Shared.Rent(newCapacity);
- _chars.Slice(0, _pos).CopyTo(poolArray);
- char[]? toReturn = _arrayToReturnToPool;
- _chars = _arrayToReturnToPool = poolArray;
- if (toReturn != null)
- {
- ArrayPool<char>.Shared.Return(toReturn);
- }
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public void Dispose()
- {
- 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);
- }
- }
- }
|