using System; using System.Collections.Generic; using System.Diagnostics; namespace UnityEngine.Rendering { /// /// A mutable string with a size and capacity so you can do string manipulations wile avoiding GC allocs. /// [DebuggerDisplay("Size = {size} Capacity = {capacity}")] public class DynamicString : DynamicArray { /// /// Create a DynamicString string with the default capacity. /// public DynamicString() : base() {} /// /// Create a DynamicString given a string. /// /// The string to initialize with. public DynamicString(string s) : base(s.Length, true) { for (int i = 0; i < s.Length; ++i) m_Array[i] = s[i]; } /// /// Allocate an empty dynamic string with the given number of characters allocated. /// /// The number of characters to pre-allocate. public DynamicString(int capacity) : base(capacity, false) { } /// /// Append a string to the DynamicString. This will not allocate memory if the capacity is still sufficient. /// /// The string to append. public void Append(string s) { int offset = size; Reserve(size + s.Length, true); for (int i = 0; i < s.Length; ++i) m_Array[offset+i] = s[i]; size += s.Length; BumpVersion(); } /// /// Append a DynamicString to this DynamicString. /// /// The string to append. public void Append(DynamicString s) => AddRange(s); /// /// Convert the DyanamicString back to a regular c# string. /// /// A new string with the same contents at the dynamic string. public override string ToString() { return new string(m_Array, 0, size); } } }