// // Stamped from StringBuilder.Append.cs - do not edit; edit the source and rebuild. #nullable enable //#define IMPLEMENT_STRING_BUILDER using System.Buffers; namespace Brutal.Strings; #if IMPLEMENT_STRING_BUILDER public partial class StringBuilder #else public partial struct ValueStringBuilder #endif { private Span GetSpan(int contentSizeHint, int alignment) { return GetSpan(Math.Max(contentSizeHint, Math.Abs(alignment))); } private void Align(Span destination, ref int elementsWritten, int alignment) { if (alignment == 0) return; // Alignment is based on grapheme count, which means we may write more than "alignment", // i.e. more than we pre-grew for - retry until it fits. while (!Utf.TryAlign(destination, ref elementsWritten, alignment)) destination = GetSpan(destination.Length + 1); } /// Pads content already written and advanced since to , /// growing as needed. Used by nested interpolation: the inner handler writes through directly, then the outer /// hole's alignment is applied after the fact. is the position before the inner wrote. public void AlignFrom(int start, int alignment) { if (alignment == 0) return; // rewind - don't use MoveTo() as it would null-terminate over its first element var elementsWritten = _position - start; _position = start; Align(FreeSpan, ref elementsWritten, alignment); Advance(elementsWritten); } /// Format into elements and append it; the writer grows as needed. /// Value to format and append. /// If non-zero, pads the appended content to this many elements (positive: right-align, negative: left-align). /// Optional format string, forwarded to the value's formatting interfaces. /// Optional format provider. /// The writer's couldn't satisfy the request (e.g. fixed-capacity writer hit its limit). public void Append(T value, int alignment = 0, string? format = null, IFormatProvider? provider = null) { var sizeHint = Math.Abs(alignment); Span destination; int elementsWritten; while (!Utf.TryFormat(value, destination = GetSpan(sizeHint), out elementsWritten, format, provider)) { // Format didn't fit - ask for strictly more than what we just got (progress guarantee). sizeHint = destination.Length + 1; } Align(destination, ref elementsWritten, alignment); Advance(elementsWritten); } /// Tries to format into elements and append it. /// The value to format and append. /// If non-zero, pads the appended content to this many elements (positive: right-align, negative: left-align). /// Optional format string, forwarded to the value's formatting interfaces. /// Optional format provider. /// true on success; false if remaining capacity was insufficient. public bool TryAppend(T value, int alignment = 0, string? format = null, IFormatProvider? provider = null) { if (!TryGetSpan(0, alignment, out var destination)) return false; if (!Utf.TryFormat(value, destination, out var elementsWritten, format, provider)) return Fail(); // can't fit formatted value if (!Utf.TryAlign(destination, ref elementsWritten, alignment)) return Fail(); // alignment overflow (grapheme→element shift exceeded pre-grow) Advance(elementsWritten); return true; } #region string/ref struct specializations /// public void Append(String8View value, int alignment = 0) { var elementsWritten = Utf.GetElementCount(value); var destination = GetSpan(elementsWritten, alignment); // grow if needed / throw if doesn't fit Utf.GetElements(value, destination); Align(destination, ref elementsWritten, alignment); Advance(elementsWritten); } /// public void Append(String16View value, int alignment = 0) { var elementsWritten = Utf.GetElementCount(value); var destination = GetSpan(elementsWritten, alignment); // grow if needed / throw if doesn't fit Utf.GetElements(value, destination); Align(destination, ref elementsWritten, alignment); Advance(elementsWritten); } /// public bool TryAppend(String8View value, int alignment = 0) { var elementsWritten = Utf.GetElementCount(value); if (!TryGetSpan(elementsWritten, alignment, out var destination)) return false; Utf.GetElements(value, destination); if (!Utf.TryAlign(destination, ref elementsWritten, alignment)) return Fail(); // alignment overflow (grapheme→element shift exceeded pre-grow) Advance(elementsWritten); return true; } /// public bool TryAppend(String16View value, int alignment = 0) { var elementsWritten = Utf.GetElementCount(value); if (!TryGetSpan(elementsWritten, alignment, out var destination)) return false; Utf.GetElements(value, destination); if (!Utf.TryAlign(destination, ref elementsWritten, alignment)) return Fail(); // alignment overflow (grapheme→element shift exceeded pre-grow) Advance(elementsWritten); return true; } #endregion #region AppendLine /// Append a , growing as needed. /// The required capacity exceeds the builder's limit. public void AppendLine() => Append(NewLine); /// Appends (optionally padded) followed by , growing as needed. /// public void AppendLine(T value, int alignment = 0, string? format = null, IFormatProvider? provider = null) { Append(value, alignment, format, provider); AppendLine(); } /// public void AppendLine(String8View value, int alignment = 0) { Append(value, alignment); AppendLine(); } /// public void AppendLine(String16View value, int alignment = 0) { Append(value, alignment); AppendLine(); } /// Tries to append a . public bool TryAppendLine() => TryAppend((String16View)NewLine); /// Tries to append (optionally padded) followed by . /// Rolls back atomically on failure. public bool TryAppendLine(T value, int alignment = 0, string? format = null, IFormatProvider? provider = null) { var checkpoint = _position; if (TryAppend(value, alignment, format, provider) && TryAppendLine()) return true; return Fail(checkpoint); // restore, in case value was appended but newline wasn't } /// public bool TryAppendLine(String8View value, int alignment = 0) { var checkpoint = _position; if (TryAppend(value, alignment) && TryAppendLine()) return true; return Fail(checkpoint); // restore, in case value was appended but newline wasn't } /// public bool TryAppendLine(String16View value, int alignment = 0) { var checkpoint = _position; if (TryAppend(value, alignment) && TryAppendLine()) return true; return Fail(checkpoint); // restore, in case value was appended but newline wasn't } #endregion }