Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DecoderNLS.cs
Go to the documentation of this file.
3
4namespace System.Text;
5
6internal class DecoderNLS : Decoder
7{
8 private readonly Encoding _encoding;
9
10 private bool _mustFlush;
11
12 internal bool _throwOnOverflow;
13
14 internal int _bytesUsed;
15
16 private int _leftoverBytes;
17
18 private int _leftoverByteCount;
19
20 public bool MustFlush => _mustFlush;
21
22 internal virtual bool HasState => _leftoverByteCount != 0;
23
24 internal bool HasLeftoverData => _leftoverByteCount != 0;
25
26 internal DecoderNLS(Encoding encoding)
27 {
28 _encoding = encoding;
30 Reset();
31 }
32
33 public override void Reset()
34 {
37 }
38
39 public override int GetCharCount(byte[] bytes, int index, int count)
40 {
41 return GetCharCount(bytes, index, count, flush: false);
42 }
43
44 public unsafe override int GetCharCount(byte[] bytes, int index, int count, bool flush)
45 {
46 if (bytes == null)
47 {
49 }
50 if (index < 0 || count < 0)
51 {
52 throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", SR.ArgumentOutOfRange_NeedNonNegNum);
53 }
54 if (bytes.Length - index < count)
55 {
57 }
58 fixed (byte* ptr = &MemoryMarshal.GetReference<byte>(bytes))
59 {
60 return GetCharCount(ptr + index, count, flush);
61 }
62 }
63
64 public unsafe override int GetCharCount(byte* bytes, int count, bool flush)
65 {
66 if (bytes == null)
67 {
69 }
70 if (count < 0)
71 {
73 }
74 _mustFlush = flush;
75 _throwOnOverflow = true;
76 return _encoding.GetCharCount(bytes, count, this);
77 }
78
79 public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
80 {
81 return GetChars(bytes, byteIndex, byteCount, chars, charIndex, flush: false);
82 }
83
84 public unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, bool flush)
85 {
86 if (bytes == null || chars == null)
87 {
88 throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", SR.ArgumentNull_Array);
89 }
90 if (byteIndex < 0 || byteCount < 0)
91 {
92 throw new ArgumentOutOfRangeException((byteIndex < 0) ? "byteIndex" : "byteCount", SR.ArgumentOutOfRange_NeedNonNegNum);
93 }
94 if (bytes.Length - byteIndex < byteCount)
95 {
97 }
98 if (charIndex < 0 || charIndex > chars.Length)
99 {
101 }
102 int charCount = chars.Length - charIndex;
103 fixed (byte* ptr = &MemoryMarshal.GetReference<byte>(bytes))
104 {
105 fixed (char* ptr2 = &MemoryMarshal.GetReference<char>(chars))
106 {
107 return GetChars(ptr + byteIndex, byteCount, ptr2 + charIndex, charCount, flush);
108 }
109 }
110 }
111
112 public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int charCount, bool flush)
113 {
114 if (chars == null || bytes == null)
115 {
116 throw new ArgumentNullException((chars == null) ? "chars" : "bytes", SR.ArgumentNull_Array);
117 }
118 if (byteCount < 0 || charCount < 0)
119 {
120 throw new ArgumentOutOfRangeException((byteCount < 0) ? "byteCount" : "charCount", SR.ArgumentOutOfRange_NeedNonNegNum);
121 }
122 _mustFlush = flush;
123 _throwOnOverflow = true;
125 }
126
127 public unsafe override void Convert(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed)
128 {
129 if (bytes == null || chars == null)
130 {
131 throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", SR.ArgumentNull_Array);
132 }
133 if (byteIndex < 0 || byteCount < 0)
134 {
135 throw new ArgumentOutOfRangeException((byteIndex < 0) ? "byteIndex" : "byteCount", SR.ArgumentOutOfRange_NeedNonNegNum);
136 }
137 if (charIndex < 0 || charCount < 0)
138 {
139 throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", SR.ArgumentOutOfRange_NeedNonNegNum);
140 }
141 if (bytes.Length - byteIndex < byteCount)
142 {
144 }
145 if (chars.Length - charIndex < charCount)
146 {
148 }
149 fixed (byte* ptr = &MemoryMarshal.GetReference<byte>(bytes))
150 {
151 fixed (char* ptr2 = &MemoryMarshal.GetReference<char>(chars))
152 {
153 Convert(ptr + byteIndex, byteCount, ptr2 + charIndex, charCount, flush, out bytesUsed, out charsUsed, out completed);
154 }
155 }
156 }
157
158 public unsafe override void Convert(byte* bytes, int byteCount, char* chars, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed)
159 {
160 if (chars == null || bytes == null)
161 {
162 throw new ArgumentNullException((chars == null) ? "chars" : "bytes", SR.ArgumentNull_Array);
163 }
164 if (byteCount < 0 || charCount < 0)
165 {
166 throw new ArgumentOutOfRangeException((byteCount < 0) ? "byteCount" : "charCount", SR.ArgumentOutOfRange_NeedNonNegNum);
167 }
168 _mustFlush = flush;
169 _throwOnOverflow = false;
170 _bytesUsed = 0;
171 charsUsed = _encoding.GetChars(bytes, byteCount, chars, charCount, this);
172 bytesUsed = _bytesUsed;
173 completed = bytesUsed == byteCount && (!flush || !HasState) && (_fallbackBuffer == null || _fallbackBuffer.Remaining == 0);
174 }
175
176 internal void ClearMustFlush()
177 {
178 _mustFlush = false;
179 }
180
182 {
183 return MemoryMarshal.AsBytes(new ReadOnlySpan<int>(ref _leftoverBytes, 1)).Slice(0, _leftoverByteCount);
184 }
185
187 {
188 bytes.CopyTo(MemoryMarshal.AsBytes(new Span<int>(ref _leftoverBytes, 1)));
189 _leftoverByteCount = bytes.Length;
190 }
191
192 internal void ClearLeftoverData()
193 {
195 }
196
197 internal int DrainLeftoverDataForGetCharCount(ReadOnlySpan<byte> bytes, out int bytesConsumed)
198 {
199 Span<byte> span = stackalloc byte[4];
200 span = span.Slice(0, ConcatInto(GetLeftoverData(), bytes, span));
201 int result = 0;
202 Rune value;
203 int bytesConsumed2;
204 switch (_encoding.DecodeFirstRune(span, out value, out bytesConsumed2))
205 {
206 case OperationStatus.Done:
207 result = value.Utf16SequenceLength;
208 break;
209 case OperationStatus.NeedMoreData:
210 if (!MustFlush)
211 {
212 break;
213 }
214 goto default;
215 default:
216 if (base.FallbackBuffer.Fallback(span.Slice(0, bytesConsumed2).ToArray(), -_leftoverByteCount))
217 {
219 }
220 break;
221 }
222 bytesConsumed = bytesConsumed2 - _leftoverByteCount;
223 return result;
224 }
225
227 {
228 Span<byte> span = stackalloc byte[4];
229 span = span.Slice(0, ConcatInto(GetLeftoverData(), bytes, span));
230 int charsWritten = 0;
231 bool flag = false;
232 Rune value;
233 int bytesConsumed2;
234 switch (_encoding.DecodeFirstRune(span, out value, out bytesConsumed2))
235 {
236 case OperationStatus.Done:
237 if (!value.TryEncodeToUtf16(chars, out charsWritten))
238 {
239 break;
240 }
241 goto IL_00aa;
242 case OperationStatus.NeedMoreData:
243 if (MustFlush)
244 {
245 goto default;
246 }
247 flag = true;
248 goto IL_00aa;
249 default:
250 {
251 if (base.FallbackBuffer.Fallback(span.Slice(0, bytesConsumed2).ToArray(), -_leftoverByteCount) && !_fallbackBuffer.TryDrainRemainingDataForGetChars(chars, out charsWritten))
252 {
253 break;
254 }
255 goto IL_00aa;
256 }
257 IL_00aa:
258 bytesConsumed = bytesConsumed2 - _leftoverByteCount;
259 if (flag)
260 {
261 SetLeftoverData(span);
262 }
263 else
264 {
266 }
267 return charsWritten;
268 }
269 _encoding.ThrowCharsOverflow(this, nothingDecoded: true);
270 throw null;
271 }
272
273 private static int ConcatInto(ReadOnlySpan<byte> srcLeft, ReadOnlySpan<byte> srcRight, Span<byte> dest)
274 {
275 int num = 0;
276 int num2 = 0;
277 while (true)
278 {
279 if (num2 < srcLeft.Length)
280 {
281 if ((uint)num >= (uint)dest.Length)
282 {
283 break;
284 }
285 dest[num++] = srcLeft[num2];
286 num2++;
287 continue;
288 }
289 for (int i = 0; i < srcRight.Length; i++)
290 {
291 if ((uint)num >= (uint)dest.Length)
292 {
293 break;
294 }
295 dest[num++] = srcRight[i];
296 }
297 break;
298 }
299 return num;
300 }
301}
static string ArgumentOutOfRange_Index
Definition SR.cs:30
static string ArgumentOutOfRange_IndexCountBuffer
Definition SR.cs:78
static string ArgumentNull_Array
Definition SR.cs:24
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
Definition SR.cs:7
bool TryDrainRemainingDataForGetChars(Span< char > chars, out int charsWritten)
override void Reset()
Definition DecoderNLS.cs:33
static int ConcatInto(ReadOnlySpan< byte > srcLeft, ReadOnlySpan< byte > srcRight, Span< byte > dest)
virtual bool HasState
Definition DecoderNLS.cs:22
int DrainLeftoverDataForGetChars(ReadOnlySpan< byte > bytes, Span< char > chars, out int bytesConsumed)
unsafe override void Convert(byte *bytes, int byteCount, char *chars, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed)
unsafe override int GetCharCount(byte *bytes, int count, bool flush)
Definition DecoderNLS.cs:64
void SetLeftoverData(ReadOnlySpan< byte > bytes)
ReadOnlySpan< byte > GetLeftoverData()
readonly Encoding _encoding
Definition DecoderNLS.cs:8
int DrainLeftoverDataForGetCharCount(ReadOnlySpan< byte > bytes, out int bytesConsumed)
override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
Definition DecoderNLS.cs:79
unsafe override int GetCharCount(byte[] bytes, int index, int count, bool flush)
Definition DecoderNLS.cs:44
unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, bool flush)
Definition DecoderNLS.cs:84
DecoderNLS(Encoding encoding)
Definition DecoderNLS.cs:26
unsafe override void Convert(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed)
override int GetCharCount(byte[] bytes, int index, int count)
Definition DecoderNLS.cs:39
unsafe override int GetChars(byte *bytes, int byteCount, char *chars, int charCount, bool flush)
DecoderFallback _fallback
Definition Decoder.cs:7
DecoderFallbackBuffer _fallbackBuffer
Definition Decoder.cs:9
DecoderFallback DecoderFallback
Definition Encoding.cs:480
virtual int GetCharCount(byte[] bytes)
Definition Encoding.cs:887
virtual OperationStatus DecodeFirstRune(ReadOnlySpan< byte > bytes, out Rune value, out int bytesConsumed)
Definition Encoding.cs:1109
virtual char[] GetChars(byte[] bytes)
Definition Encoding.cs:921
Span< T > Slice(int start)
Definition Span.cs:271
int Length
Definition Span.cs:70