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 ValueStringBuilder(Span<char> initialBuffer)
17 {
19 _chars = initialBuffer;
20 _pos = 0;
21 }
22
23 public override string ToString()
24 {
25 string result = _chars.Slice(0, _pos).ToString();
26 Dispose();
27 return result;
28 }
29
30 [MethodImpl(MethodImplOptions.AggressiveInlining)]
31 public void Append(char c)
32 {
33 int pos = _pos;
34 if ((uint)pos < (uint)_chars.Length)
35 {
36 _chars[pos] = c;
37 _pos = pos + 1;
38 }
39 else
40 {
42 }
43 }
44
45 [MethodImpl(MethodImplOptions.AggressiveInlining)]
46 public void Append(string s)
47 {
48 if (s != null)
49 {
50 int pos = _pos;
51 if (s.Length == 1 && (uint)pos < (uint)_chars.Length)
52 {
53 _chars[pos] = s[0];
54 _pos = pos + 1;
55 }
56 else
57 {
59 }
60 }
61 }
62
63 private void AppendSlow(string s)
64 {
65 int pos = _pos;
66 if (pos > _chars.Length - s.Length)
67 {
68 Grow(s.Length);
69 }
70 s.CopyTo(_chars.Slice(pos));
71 _pos += s.Length;
72 }
73
75 {
76 int pos = _pos;
77 if (pos > _chars.Length - value.Length)
78 {
79 Grow(value.Length);
80 }
81 value.CopyTo(_chars.Slice(_pos));
82 _pos += value.Length;
83 }
84
85 [MethodImpl(MethodImplOptions.NoInlining)]
86 private void GrowAndAppend(char c)
87 {
88 Grow(1);
89 Append(c);
90 }
91
92 [MethodImpl(MethodImplOptions.NoInlining)]
93 private void Grow(int additionalCapacityBeyondPos)
94 {
95 char[] array = ArrayPool<char>.Shared.Rent((int)Math.Max((uint)(_pos + additionalCapacityBeyondPos), (uint)(_chars.Length * 2)));
96 _chars.Slice(0, _pos).CopyTo(array);
97 char[] arrayToReturnToPool = _arrayToReturnToPool;
99 if (arrayToReturnToPool != null)
100 {
101 ArrayPool<char>.Shared.Return(arrayToReturnToPool);
102 }
103 }
104
105 [MethodImpl(MethodImplOptions.AggressiveInlining)]
106 public void Dispose()
107 {
108 char[] arrayToReturnToPool = _arrayToReturnToPool;
109 this = default(System.Text.ValueStringBuilder);
110 if (arrayToReturnToPool != null)
111 {
112 ArrayPool<char>.Shared.Return(arrayToReturnToPool);
113 }
114 }
115}
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)
void Append(ReadOnlySpan< char > value)
void Grow(int additionalCapacityBeyondPos)