Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ValueStringBuilder.cs
Go to the documentation of this file.
5
6namespace System.Text;
7
8[DefaultMember("Item")]
9internal ref struct ValueStringBuilder
10{
11 private char[] _arrayToReturnToPool;
12
13 private Span<char> _chars;
14
15 private int _pos;
16
17 public int Length => _pos;
18
19 public ValueStringBuilder(Span<char> initialBuffer)
20 {
22 _chars = initialBuffer;
23 _pos = 0;
24 }
25
26 public void EnsureCapacity(int capacity)
27 {
28 if ((uint)capacity > (uint)_chars.Length)
29 {
31 }
32 }
33
34 public ref char GetPinnableReference(bool terminate)
35 {
36 if (terminate)
37 {
39 _chars[Length] = '\0';
40 }
41 return ref MemoryMarshal.GetReference(_chars);
42 }
43
44 public override string ToString()
45 {
46 string result = _chars.Slice(0, _pos).ToString();
47 Dispose();
48 return result;
49 }
50
51 [MethodImpl(MethodImplOptions.AggressiveInlining)]
52 public void Append(char c)
53 {
54 int pos = _pos;
55 if ((uint)pos < (uint)_chars.Length)
56 {
57 _chars[pos] = c;
58 _pos = pos + 1;
59 }
60 else
61 {
63 }
64 }
65
66 [MethodImpl(MethodImplOptions.AggressiveInlining)]
67 public void Append(string s)
68 {
69 if (s != null)
70 {
71 int pos = _pos;
72 if (s.Length == 1 && (uint)pos < (uint)_chars.Length)
73 {
74 _chars[pos] = s[0];
75 _pos = pos + 1;
76 }
77 else
78 {
80 }
81 }
82 }
83
84 private void AppendSlow(string s)
85 {
86 int pos = _pos;
87 if (pos > _chars.Length - s.Length)
88 {
89 Grow(s.Length);
90 }
91 s.CopyTo(_chars.Slice(pos));
92 _pos += s.Length;
93 }
94
95 public void Append(char c, int count)
96 {
97 if (_pos > _chars.Length - count)
98 {
99 Grow(count);
100 }
102 for (int i = 0; i < span.Length; i++)
103 {
104 span[i] = c;
105 }
106 _pos += count;
107 }
108
110 {
111 int pos = _pos;
112 if (pos > _chars.Length - value.Length)
113 {
114 Grow(value.Length);
115 }
116 value.CopyTo(_chars.Slice(_pos));
117 _pos += value.Length;
118 }
119
120 [MethodImpl(MethodImplOptions.NoInlining)]
121 private void GrowAndAppend(char c)
122 {
123 Grow(1);
124 Append(c);
125 }
126
127 [MethodImpl(MethodImplOptions.NoInlining)]
128 private void Grow(int additionalCapacityBeyondPos)
129 {
130 char[] array = ArrayPool<char>.Shared.Rent((int)Math.Max((uint)(_pos + additionalCapacityBeyondPos), (uint)(_chars.Length * 2)));
131 _chars.Slice(0, _pos).CopyTo(array);
132 char[] arrayToReturnToPool = _arrayToReturnToPool;
134 if (arrayToReturnToPool != null)
135 {
136 ArrayPool<char>.Shared.Return(arrayToReturnToPool);
137 }
138 }
139
140 [MethodImpl(MethodImplOptions.AggressiveInlining)]
141 public void Dispose()
142 {
143 char[] arrayToReturnToPool = _arrayToReturnToPool;
144 this = default(System.Text.ValueStringBuilder);
145 if (arrayToReturnToPool != null)
146 {
147 ArrayPool<char>.Shared.Return(arrayToReturnToPool);
148 }
149 }
150}
static ArrayPool< T > Shared
Definition ArrayPool.cs:7
static byte Max(byte val1, byte val2)
Definition Math.cs:738
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70
ValueStringBuilder(Span< char > initialBuffer)
ref char GetPinnableReference(bool terminate)
void Append(char c, int count)
void Append(ReadOnlySpan< char > value)
void Grow(int additionalCapacityBeyondPos)