No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DynamicString.cs 2.2KB

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