Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ValueStringBuilder.cs
Go to the documentation of this file.
4
5namespace System.Text;
6
7[DefaultMember("Item")]
8internal ref struct ValueStringBuilder
9{
10 private char[] _arrayToReturnToPool;
11
12 private Span<char> _chars;
13
14 private int _pos;
15
16 public int Length => _pos;
17
18 public ValueStringBuilder(Span<char> initialBuffer)
19 {
21 _chars = initialBuffer;
22 _pos = 0;
23 }
24
25 public override string ToString()
26 {
27 string result = _chars.Slice(0, _pos).ToString();
28 Dispose();
29 return result;
30 }
31
32 public bool TryCopyTo(Span<char> destination, out int charsWritten)
33 {
34 if (_chars.Slice(0, _pos).TryCopyTo(destination))
35 {
36 charsWritten = _pos;
37 Dispose();
38 return true;
39 }
40 charsWritten = 0;
41 Dispose();
42 return false;
43 }
44
45 public void Insert(int index, char value, int count)
46 {
47 if (_pos > _chars.Length - count)
48 {
49 Grow(count);
50 }
51 int length = _pos - index;
54 _pos += count;
55 }
56
57 [MethodImpl(MethodImplOptions.AggressiveInlining)]
58 public void Append(char c)
59 {
60 int pos = _pos;
61 if ((uint)pos < (uint)_chars.Length)
62 {
63 _chars[pos] = c;
64 _pos = pos + 1;
65 }
66 else
67 {
69 }
70 }
71
72 [MethodImpl(MethodImplOptions.AggressiveInlining)]
73 public void Append(string s)
74 {
75 if (s != null)
76 {
77 int pos = _pos;
78 if (s.Length == 1 && (uint)pos < (uint)_chars.Length)
79 {
80 _chars[pos] = s[0];
81 _pos = pos + 1;
82 }
83 else
84 {
86 }
87 }
88 }
89
90 private void AppendSlow(string s)
91 {
92 int pos = _pos;
93 if (pos > _chars.Length - s.Length)
94 {
95 Grow(s.Length);
96 }
97 s.CopyTo(_chars.Slice(pos));
98 _pos += s.Length;
99 }
100
101 public void Append(char c, int count)
102 {
103 if (_pos > _chars.Length - count)
104 {
105 Grow(count);
106 }
108 for (int i = 0; i < span.Length; i++)
109 {
110 span[i] = c;
111 }
112 _pos += count;
113 }
114
115 public unsafe void Append(char* value, int length)
116 {
117 int pos = _pos;
118 if (pos > _chars.Length - length)
119 {
120 Grow(length);
121 }
123 for (int i = 0; i < span.Length; i++)
124 {
125 span[i] = *(value++);
126 }
127 _pos += length;
128 }
129
130 [MethodImpl(MethodImplOptions.AggressiveInlining)]
132 {
133 int pos = _pos;
134 if (pos > _chars.Length - length)
135 {
136 Grow(length);
137 }
138 _pos = pos + length;
139 return _chars.Slice(pos, length);
140 }
141
142 [MethodImpl(MethodImplOptions.NoInlining)]
143 private void GrowAndAppend(char c)
144 {
145 Grow(1);
146 Append(c);
147 }
148
149 [MethodImpl(MethodImplOptions.NoInlining)]
150 private void Grow(int additionalCapacityBeyondPos)
151 {
152 char[] array = ArrayPool<char>.Shared.Rent((int)Math.Max((uint)(_pos + additionalCapacityBeyondPos), (uint)(_chars.Length * 2)));
153 _chars.Slice(0, _pos).CopyTo(array);
154 char[] arrayToReturnToPool = _arrayToReturnToPool;
156 if (arrayToReturnToPool != null)
157 {
158 ArrayPool<char>.Shared.Return(arrayToReturnToPool);
159 }
160 }
161
162 [MethodImpl(MethodImplOptions.AggressiveInlining)]
163 public void Dispose()
164 {
165 char[] arrayToReturnToPool = _arrayToReturnToPool;
166 this = default(System.Text.ValueStringBuilder);
167 if (arrayToReturnToPool != null)
168 {
169 ArrayPool<char>.Shared.Return(arrayToReturnToPool);
170 }
171 }
172}
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)
bool TryCopyTo(Span< char > destination, out int charsWritten)
unsafe void Append(char *value, int length)
Span< char > AppendSpan(int length)
void Grow(int additionalCapacityBeyondPos)
void Insert(int index, char value, int count)