A game about forced loneliness, made by TACStudios
1using System; 2using System.Collections.Generic; 3using System.Diagnostics; 4 5namespace UnityEngine.Rendering 6{ 7 /// <summary> 8 /// A mutable string with a size and capacity so you can do string manipulations wile avoiding GC allocs. 9 /// </summary> 10 [DebuggerDisplay("Size = {size} Capacity = {capacity}")] 11 public class DynamicString : DynamicArray<char> 12 { 13 /// <summary> 14 /// Create a DynamicString string with the default capacity. 15 /// </summary> 16 public DynamicString() : base() 17 {} 18 19 /// <summary> 20 /// Create a DynamicString given a string. 21 /// </summary> 22 /// <param name="s">The string to initialize with.</param> 23 public DynamicString(string s) : base(s.Length, true) 24 { 25 for (int i = 0; i < s.Length; ++i) 26 m_Array[i] = s[i]; 27 } 28 29 /// <summary> 30 /// Allocate an empty dynamic string with the given number of characters allocated. 31 /// </summary> 32 /// <param name="capacity">The number of characters to pre-allocate.</param> 33 public DynamicString(int capacity) : base(capacity, false) { } 34 35 /// <summary> 36 /// Append a string to the DynamicString. This will not allocate memory if the capacity is still sufficient. 37 /// </summary> 38 /// <param name="s">The string to append.</param> 39 public void Append(string s) 40 { 41 int offset = size; 42 Reserve(size + s.Length, true); 43 for (int i = 0; i < s.Length; ++i) 44 m_Array[offset+i] = s[i]; 45 size += s.Length; 46 BumpVersion(); 47 } 48 49 /// <summary> 50 /// Append a DynamicString to this DynamicString. 51 /// </summary> 52 /// <param name="s">The string to append.</param> 53 public void Append(DynamicString s) => AddRange(s); 54 55 /// <summary> 56 /// Convert the DyanamicString back to a regular c# string. 57 /// </summary> 58 /// <returns>A new string with the same contents at the dynamic string.</returns> 59 public override string ToString() 60 { 61 return new string(m_Array, 0, size); 62 } 63 } 64 65}