<PackageReference Include="Microsoft.Extensions.Primitives" Version="10.0.0-preview.6.25358.103" />

InplaceStringBuilder

public struct InplaceStringBuilder
Provides a mechanism for fast, non-allocating string concatenation.
using System; using System.ComponentModel; using System.Diagnostics; using System.Runtime.CompilerServices; namespace Microsoft.Extensions.Primitives { [NullableContext(2)] [Nullable(0)] [DebuggerDisplay("Value = {_value}")] [EditorBrowsable(EditorBrowsableState.Never)] [Obsolete("This type is retained only for compatibility. The recommended alternative is string.Create<TState> (int length, TState state, System.Buffers.SpanAction<char,TState> action).", true)] public struct InplaceStringBuilder { private int _offset; private int _capacity; private string _value; public int Capacity { get { return _capacity; } set { if (value < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.value); if (_offset > 0) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.Capacity_CannotChangeAfterWriteStarted); _capacity = value; } } public InplaceStringBuilder(int capacity) { this = default(InplaceStringBuilder); if (capacity < 0) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.capacity); _capacity = capacity; } public void Append(string value) { if (value == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); Append(value, 0, value.Length); } public void Append(StringSegment segment) { Append(segment.Buffer, segment.Offset, segment.Length); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe void Append(string value, int offset, int count) { EnsureValueIsInitialized(); if (value == null || offset < 0 || value.Length - offset < count || Capacity - _offset < count) ThrowValidationError(value, offset, count); string value2 = _value; IntPtr intPtr; if (value2 == null) intPtr = (IntPtr)(void*)null; else { ref reference = ref value2.GetPinnableReference(); intPtr = (IntPtr)(&reference); } IntPtr intPtr2; if (value == null) intPtr2 = (IntPtr)(void*)null; else { ref reference2 = ref value.GetPinnableReference(); intPtr2 = (IntPtr)(&reference2); } char* ptr = (char*)(long)intPtr2; Unsafe.CopyBlockUnaligned((void*)((long)intPtr + (long)(IntPtr)(void*)((long)_offset * 2)), ptr + offset, (uint)(count * 2)); _offset += count; ref reference2 = ref *(char*)null; ref reference = ref *(char*)null; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public unsafe void Append(char c) { EnsureValueIsInitialized(); if (_offset >= Capacity) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.Capacity_NotEnough, 1, Capacity - _offset); string value = _value; IntPtr intPtr; if (value == null) intPtr = (IntPtr)(void*)null; else { ref reference = ref value.GetPinnableReference(); intPtr = (IntPtr)(&reference); } *(short*)((long)intPtr + (long)(IntPtr)(void*)((long)_offset++ * 2)) = (short)c; ref reference = ref *(char*)null; } public override string ToString() { if (Capacity != _offset) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.Capacity_NotUsedEntirely, Capacity, _offset); return _value; } private void EnsureValueIsInitialized() { if (_value == null) _value = new string('', _capacity); } private void ThrowValidationError(string value, int offset, int count) { if (value == null) ThrowHelper.ThrowArgumentNullException(ExceptionArgument.value); if (offset < 0 || value.Length - offset < count) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.offset); if (Capacity - _offset < count) ThrowHelper.ThrowInvalidOperationException(ExceptionResource.Capacity_NotEnough, value.Length, Capacity - _offset); } } }