Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DecoderFallbackBuffer.cs
Go to the documentation of this file.
2
3namespace System.Text;
4
5public abstract class DecoderFallbackBuffer
6{
7 internal unsafe byte* byteStart;
8
9 internal unsafe char* charEnd;
10
12
14
15 private int _originalByteCount;
16
17 public abstract int Remaining { get; }
18
19 public abstract bool Fallback(byte[] bytesUnknown, int index);
20
21 public abstract char GetNextChar();
22
23 public abstract bool MovePrevious();
24
25 public virtual void Reset()
26 {
27 while (GetNextChar() != 0)
28 {
29 }
30 }
31
32 internal unsafe void InternalReset()
33 {
34 byteStart = null;
35 Reset();
36 }
37
38 internal unsafe void InternalInitialize(byte* byteStart, char* charEnd)
39 {
40 this.byteStart = byteStart;
41 this.charEnd = charEnd;
42 }
43
44 internal static DecoderFallbackBuffer CreateAndInitialize(Encoding encoding, DecoderNLS decoder, int originalByteCount)
45 {
46 DecoderFallbackBuffer decoderFallbackBuffer = ((decoder == null) ? encoding.DecoderFallback.CreateFallbackBuffer() : decoder.FallbackBuffer);
47 decoderFallbackBuffer._encoding = encoding;
48 decoderFallbackBuffer._decoder = decoder;
49 decoderFallbackBuffer._originalByteCount = originalByteCount;
50 return decoderFallbackBuffer;
51 }
52
53 internal unsafe virtual bool InternalFallback(byte[] bytes, byte* pBytes, ref char* chars)
54 {
55 if (Fallback(bytes, (int)(pBytes - byteStart - bytes.Length)))
56 {
57 char* ptr = chars;
58 bool flag = false;
59 char nextChar;
60 while ((nextChar = GetNextChar()) != 0)
61 {
62 if (char.IsSurrogate(nextChar))
63 {
64 if (char.IsHighSurrogate(nextChar))
65 {
66 if (flag)
67 {
69 }
70 flag = true;
71 }
72 else
73 {
74 if (!flag)
75 {
77 }
78 flag = false;
79 }
80 }
81 if (ptr >= charEnd)
82 {
83 return false;
84 }
85 *(ptr++) = nextChar;
86 }
87 if (flag)
88 {
90 }
91 chars = ptr;
92 }
93 return true;
94 }
95
96 internal unsafe virtual int InternalFallback(byte[] bytes, byte* pBytes)
97 {
98 if (Fallback(bytes, (int)(pBytes - byteStart - bytes.Length)))
99 {
100 int num = 0;
101 bool flag = false;
102 char nextChar;
103 while ((nextChar = GetNextChar()) != 0)
104 {
105 if (char.IsSurrogate(nextChar))
106 {
107 if (char.IsHighSurrogate(nextChar))
108 {
109 if (flag)
110 {
112 }
113 flag = true;
114 }
115 else
116 {
117 if (!flag)
118 {
120 }
121 flag = false;
122 }
123 }
124 num++;
125 }
126 if (flag)
127 {
129 }
130 return num;
131 }
132 return 0;
133 }
134
135 internal int InternalFallbackGetCharCount(ReadOnlySpan<byte> remainingBytes, int fallbackLength)
136 {
137 if (!Fallback(remainingBytes.Slice(0, fallbackLength).ToArray(), _originalByteCount - remainingBytes.Length))
138 {
139 return 0;
140 }
142 }
143
144 internal bool TryInternalFallbackGetChars(ReadOnlySpan<byte> remainingBytes, int fallbackLength, Span<char> chars, out int charsWritten)
145 {
146 if (Fallback(remainingBytes.Slice(0, fallbackLength).ToArray(), _originalByteCount - remainingBytes.Length))
147 {
148 return TryDrainRemainingDataForGetChars(chars, out charsWritten);
149 }
150 charsWritten = 0;
151 return true;
152 }
153
155 {
156 char nextChar = GetNextChar();
157 if (!Rune.TryCreate(nextChar, out var result) && !Rune.TryCreate(nextChar, GetNextChar(), out result))
158 {
160 }
161 return result;
162 }
163
165 {
166 int num = 0;
167 while (true)
168 {
169 Rune nextRune;
170 Rune rune = (nextRune = GetNextRune());
171 if (rune.Value == 0)
172 {
173 break;
174 }
175 num += nextRune.Utf16SequenceLength;
176 if (num < 0)
177 {
180 }
181 }
182 return num;
183 }
184
185 internal bool TryDrainRemainingDataForGetChars(Span<char> chars, out int charsWritten)
186 {
187 int length = chars.Length;
188 while (true)
189 {
190 Rune nextRune;
191 Rune rune = (nextRune = GetNextRune());
192 if (rune.Value == 0)
193 {
194 break;
195 }
196 if (nextRune.TryEncodeToUtf16(chars, out var charsWritten2))
197 {
198 chars = chars.Slice(charsWritten2);
199 continue;
200 }
202 charsWritten = 0;
203 return false;
204 }
205 charsWritten = length - chars.Length;
206 return true;
207 }
208
209 [DoesNotReturn]
210 internal static void ThrowLastBytesRecursive(byte[] bytesUnknown)
211 {
212 if (bytesUnknown == null)
213 {
214 bytesUnknown = Array.Empty<byte>();
215 }
216 StringBuilder stringBuilder = new StringBuilder(bytesUnknown.Length * 3);
217 int i;
218 for (i = 0; i < bytesUnknown.Length && i < 20; i++)
219 {
220 if (stringBuilder.Length > 0)
221 {
222 stringBuilder.Append(' ');
223 }
224 StringBuilder stringBuilder2 = stringBuilder;
226 handler.AppendLiteral("\\x");
227 handler.AppendFormatted(bytesUnknown[i], "X2");
228 stringBuilder2.Append(ref handler);
229 }
230 if (i == 20)
231 {
232 stringBuilder.Append(" ...");
233 }
234 throw new ArgumentException(SR.Format(SR.Argument_RecursiveFallbackBytes, stringBuilder.ToString()), "bytesUnknown");
235 }
236}
static string Argument_InvalidCharSequenceNoIndex
Definition SR.cs:92
static string Argument_RecursiveFallbackBytes
Definition SR.cs:846
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
Definition SR.cs:7
virtual unsafe int InternalFallback(byte[] bytes, byte *pBytes)
unsafe void InternalInitialize(byte *byteStart, char *charEnd)
int InternalFallbackGetCharCount(ReadOnlySpan< byte > remainingBytes, int fallbackLength)
static DecoderFallbackBuffer CreateAndInitialize(Encoding encoding, DecoderNLS decoder, int originalByteCount)
bool TryInternalFallbackGetChars(ReadOnlySpan< byte > remainingBytes, int fallbackLength, Span< char > chars, out int charsWritten)
virtual unsafe bool InternalFallback(byte[] bytes, byte *pBytes, ref char *chars)
bool Fallback(byte[] bytesUnknown, int index)
static void ThrowLastBytesRecursive(byte[] bytesUnknown)
bool TryDrainRemainingDataForGetChars(Span< char > chars, out int charsWritten)
DecoderFallbackBuffer CreateFallbackBuffer()
new DecoderFallbackBuffer FallbackBuffer
Definition DecoderNLS.cs:24
DecoderFallback DecoderFallback
Definition Encoding.cs:480
static void ThrowConversionOverflow()
Definition Encoding.cs:1084
override string ToString()
StringBuilder Append(char value, int repeatCount)
ReadOnlySpan< T > Slice(int start)
int Utf16SequenceLength
Definition Rune.cs:55
static bool TryCreate(char ch, out Rune result)
Definition Rune.cs:511
bool TryEncodeToUtf16(Span< char > destination, out int charsWritten)
Definition Rune.cs:553