Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ASCIIEncoding.cs
Go to the documentation of this file.
4
5namespace System.Text;
6
7public class ASCIIEncoding : Encoding
8{
9 internal sealed class ASCIIEncodingSealed : ASCIIEncoding
10 {
11 public override object Clone()
12 {
13 return new ASCIIEncoding
14 {
15 IsReadOnly = false
16 };
17 }
18 }
19
20 internal static readonly ASCIIEncodingSealed s_default = new ASCIIEncodingSealed();
21
22 public override bool IsSingleByte => true;
23
25 : base(20127)
26 {
27 }
28
34
35 public unsafe override int GetByteCount(char[] chars, int index, int count)
36 {
37 if (chars == null)
38 {
40 }
41 if ((index | count) < 0)
42 {
43 ThrowHelper.ThrowArgumentOutOfRangeException((index < 0) ? ExceptionArgument.index : ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
44 }
45 if (chars.Length - index < count)
46 {
47 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.chars, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer);
48 }
49 fixed (char* ptr = chars)
50 {
51 return GetByteCountCommon(ptr + index, count);
52 }
53 }
54
55 public unsafe override int GetByteCount(string chars)
56 {
57 if (chars == null)
58 {
60 }
61 fixed (char* pChars = chars)
62 {
63 return GetByteCountCommon(pChars, chars.Length);
64 }
65 }
66
67 [CLSCompliant(false)]
68 public unsafe override int GetByteCount(char* chars, int count)
69 {
70 if (chars == null)
71 {
73 }
74 if (count < 0)
75 {
77 }
79 }
80
81 public unsafe override int GetByteCount(ReadOnlySpan<char> chars)
82 {
83 fixed (char* pChars = &MemoryMarshal.GetReference(chars))
84 {
85 return GetByteCountCommon(pChars, chars.Length);
86 }
87 }
88
89 [MethodImpl(MethodImplOptions.AggressiveInlining)]
90 private unsafe int GetByteCountCommon(char* pChars, int charCount)
91 {
92 int charsConsumed;
93 int num = GetByteCountFast(pChars, charCount, base.EncoderFallback, out charsConsumed);
94 if (charsConsumed != charCount)
95 {
96 num += GetByteCountWithFallback(pChars, charCount, charsConsumed);
97 if (num < 0)
98 {
100 }
101 }
102 return num;
103 }
104
105 [MethodImpl(MethodImplOptions.AggressiveInlining)]
106 private protected unsafe sealed override int GetByteCountFast(char* pChars, int charsLength, EncoderFallback fallback, out int charsConsumed)
107 {
108 int num = charsLength;
109 if (!(fallback is EncoderReplacementFallback { MaxCharCount: 1 } encoderReplacementFallback) || encoderReplacementFallback.DefaultString[0] > '\u007f')
110 {
111 num = (int)ASCIIUtility.GetIndexOfFirstNonAsciiChar(pChars, (uint)charsLength);
112 }
113 charsConsumed = num;
114 return num;
115 }
116
117 public unsafe override int GetBytes(string chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
118 {
119 if (chars == null || bytes == null)
120 {
122 }
123 if ((charIndex | charCount) < 0)
124 {
125 ThrowHelper.ThrowArgumentOutOfRangeException((charIndex < 0) ? ExceptionArgument.charIndex : ExceptionArgument.charCount, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
126 }
127 if (chars.Length - charIndex < charCount)
128 {
130 }
131 if ((uint)byteIndex > bytes.Length)
132 {
134 }
135 fixed (char* ptr2 = chars)
136 {
137 fixed (byte[] array = bytes)
138 {
139 byte* ptr = (byte*)((bytes != null && array.Length != 0) ? System.Runtime.CompilerServices.Unsafe.AsPointer(ref array[0]) : null);
140 return GetBytesCommon(ptr2 + charIndex, charCount, ptr + byteIndex, bytes.Length - byteIndex);
141 }
142 }
143 }
144
145 public unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
146 {
147 if (chars == null || bytes == null)
148 {
150 }
151 if ((charIndex | charCount) < 0)
152 {
153 ThrowHelper.ThrowArgumentOutOfRangeException((charIndex < 0) ? ExceptionArgument.charIndex : ExceptionArgument.charCount, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
154 }
155 if (chars.Length - charIndex < charCount)
156 {
158 }
159 if ((uint)byteIndex > bytes.Length)
160 {
162 }
163 fixed (char* ptr = chars)
164 {
165 fixed (byte* ptr2 = bytes)
166 {
167 return GetBytesCommon(ptr + charIndex, charCount, ptr2 + byteIndex, bytes.Length - byteIndex);
168 }
169 }
170 }
171
172 [CLSCompliant(false)]
173 public unsafe override int GetBytes(char* chars, int charCount, byte* bytes, int byteCount)
174 {
175 if (chars == null || bytes == null)
176 {
178 }
179 if ((charCount | byteCount) < 0)
180 {
181 ThrowHelper.ThrowArgumentOutOfRangeException((charCount < 0) ? ExceptionArgument.charCount : ExceptionArgument.byteCount, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
182 }
184 }
185
186 public unsafe override int GetBytes(ReadOnlySpan<char> chars, Span<byte> bytes)
187 {
188 fixed (char* pChars = &MemoryMarshal.GetReference(chars))
189 {
190 fixed (byte* pBytes = &MemoryMarshal.GetReference(bytes))
191 {
192 return GetBytesCommon(pChars, chars.Length, pBytes, bytes.Length);
193 }
194 }
195 }
196
197 [MethodImpl(MethodImplOptions.AggressiveInlining)]
198 private unsafe int GetBytesCommon(char* pChars, int charCount, byte* pBytes, int byteCount)
199 {
200 int charsConsumed;
201 int bytesFast = GetBytesFast(pChars, charCount, pBytes, byteCount, out charsConsumed);
202 if (charsConsumed == charCount)
203 {
204 return bytesFast;
205 }
206 return GetBytesWithFallback(pChars, charCount, pBytes, byteCount, charsConsumed, bytesFast);
207 }
208
209 [MethodImpl(MethodImplOptions.AggressiveInlining)]
210 private protected unsafe sealed override int GetBytesFast(char* pChars, int charsLength, byte* pBytes, int bytesLength, out int charsConsumed)
211 {
212 return charsConsumed = (int)ASCIIUtility.NarrowUtf16ToAscii(pChars, pBytes, (uint)Math.Min(charsLength, bytesLength));
213 }
214
215 private protected unsafe sealed override int GetBytesWithFallback(ReadOnlySpan<char> chars, int originalCharsLength, Span<byte> bytes, int originalBytesLength, EncoderNLS encoder)
216 {
217 if (((encoder == null) ? base.EncoderFallback : encoder.Fallback) is EncoderReplacementFallback { MaxCharCount: 1 } encoderReplacementFallback && encoderReplacementFallback.DefaultString[0] <= '\u007f')
218 {
219 byte b = (byte)encoderReplacementFallback.DefaultString[0];
220 int num = Math.Min(chars.Length, bytes.Length);
221 int num2 = 0;
222 fixed (char* ptr2 = &MemoryMarshal.GetReference(chars))
223 {
224 fixed (byte* ptr = &MemoryMarshal.GetReference(bytes))
225 {
226 while (num2 < num)
227 {
228 ptr[num2++] = b;
229 if (num2 < num)
230 {
231 num2 += (int)ASCIIUtility.NarrowUtf16ToAscii(ptr2 + num2, ptr + num2, (uint)(num - num2));
232 }
233 }
234 }
235 }
236 chars = chars.Slice(num);
237 bytes = bytes.Slice(num);
238 }
239 if (chars.IsEmpty)
240 {
241 return originalBytesLength - bytes.Length;
242 }
243 return base.GetBytesWithFallback(chars, originalCharsLength, bytes, originalBytesLength, encoder);
244 }
245
246 public unsafe override int GetCharCount(byte[] bytes, int index, int count)
247 {
248 if (bytes == null)
249 {
251 }
252 if ((index | count) < 0)
253 {
254 ThrowHelper.ThrowArgumentOutOfRangeException((index < 0) ? ExceptionArgument.index : ExceptionArgument.count, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
255 }
256 if (bytes.Length - index < count)
257 {
258 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.bytes, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer);
259 }
260 fixed (byte* ptr = bytes)
261 {
262 return GetCharCountCommon(ptr + index, count);
263 }
264 }
265
266 [CLSCompliant(false)]
267 public unsafe override int GetCharCount(byte* bytes, int count)
268 {
269 if (bytes == null)
270 {
272 }
273 if (count < 0)
274 {
276 }
278 }
279
280 public unsafe override int GetCharCount(ReadOnlySpan<byte> bytes)
281 {
282 fixed (byte* pBytes = &MemoryMarshal.GetReference(bytes))
283 {
284 return GetCharCountCommon(pBytes, bytes.Length);
285 }
286 }
287
288 [MethodImpl(MethodImplOptions.AggressiveInlining)]
289 private unsafe int GetCharCountCommon(byte* pBytes, int byteCount)
290 {
291 int bytesConsumed;
292 int num = GetCharCountFast(pBytes, byteCount, base.DecoderFallback, out bytesConsumed);
293 if (bytesConsumed != byteCount)
294 {
295 num += GetCharCountWithFallback(pBytes, byteCount, bytesConsumed);
296 if (num < 0)
297 {
299 }
300 }
301 return num;
302 }
303
304 [MethodImpl(MethodImplOptions.AggressiveInlining)]
305 private protected unsafe sealed override int GetCharCountFast(byte* pBytes, int bytesLength, DecoderFallback fallback, out int bytesConsumed)
306 {
307 int num = bytesLength;
308 if (!(fallback is DecoderReplacementFallback { MaxCharCount: 1 }))
309 {
310 num = (int)ASCIIUtility.GetIndexOfFirstNonAsciiByte(pBytes, (uint)bytesLength);
311 }
312 bytesConsumed = num;
313 return num;
314 }
315
316 public unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
317 {
318 if (bytes == null || chars == null)
319 {
321 }
322 if ((byteIndex | byteCount) < 0)
323 {
324 ThrowHelper.ThrowArgumentOutOfRangeException((byteIndex < 0) ? ExceptionArgument.byteIndex : ExceptionArgument.byteCount, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
325 }
326 if (bytes.Length - byteIndex < byteCount)
327 {
328 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.bytes, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer);
329 }
330 if ((uint)charIndex > (uint)chars.Length)
331 {
333 }
334 fixed (byte* ptr = bytes)
335 {
336 fixed (char* ptr2 = chars)
337 {
338 return GetCharsCommon(ptr + byteIndex, byteCount, ptr2 + charIndex, chars.Length - charIndex);
339 }
340 }
341 }
342
343 [CLSCompliant(false)]
344 public unsafe override int GetChars(byte* bytes, int byteCount, char* chars, int charCount)
345 {
346 if (bytes == null || chars == null)
347 {
349 }
350 if ((byteCount | charCount) < 0)
351 {
352 ThrowHelper.ThrowArgumentOutOfRangeException((byteCount < 0) ? ExceptionArgument.byteCount : ExceptionArgument.charCount, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
353 }
355 }
356
357 public unsafe override int GetChars(ReadOnlySpan<byte> bytes, Span<char> chars)
358 {
359 fixed (byte* pBytes = &MemoryMarshal.GetReference(bytes))
360 {
361 fixed (char* pChars = &MemoryMarshal.GetReference(chars))
362 {
363 return GetCharsCommon(pBytes, bytes.Length, pChars, chars.Length);
364 }
365 }
366 }
367
368 [MethodImpl(MethodImplOptions.AggressiveInlining)]
369 private unsafe int GetCharsCommon(byte* pBytes, int byteCount, char* pChars, int charCount)
370 {
371 int bytesConsumed;
372 int charsFast = GetCharsFast(pBytes, byteCount, pChars, charCount, out bytesConsumed);
373 if (bytesConsumed == byteCount)
374 {
375 return charsFast;
376 }
377 return GetCharsWithFallback(pBytes, byteCount, pChars, charCount, bytesConsumed, charsFast);
378 }
379
380 [MethodImpl(MethodImplOptions.AggressiveInlining)]
381 private protected unsafe sealed override int GetCharsFast(byte* pBytes, int bytesLength, char* pChars, int charsLength, out int bytesConsumed)
382 {
383 return bytesConsumed = (int)ASCIIUtility.WidenAsciiToUtf16(pBytes, pChars, (uint)Math.Min(bytesLength, charsLength));
384 }
385
386 private protected unsafe sealed override int GetCharsWithFallback(ReadOnlySpan<byte> bytes, int originalBytesLength, Span<char> chars, int originalCharsLength, DecoderNLS decoder)
387 {
388 if (((decoder == null) ? base.DecoderFallback : decoder.Fallback) is DecoderReplacementFallback { MaxCharCount: 1 } decoderReplacementFallback)
389 {
390 char c = decoderReplacementFallback.DefaultString[0];
391 int num = Math.Min(bytes.Length, chars.Length);
392 int num2 = 0;
393 fixed (byte* ptr2 = &MemoryMarshal.GetReference(bytes))
394 {
395 fixed (char* ptr = &MemoryMarshal.GetReference(chars))
396 {
397 while (num2 < num)
398 {
399 ptr[num2++] = c;
400 if (num2 < num)
401 {
402 num2 += (int)ASCIIUtility.WidenAsciiToUtf16(ptr2 + num2, ptr + num2, (uint)(num - num2));
403 }
404 }
405 }
406 }
407 bytes = bytes.Slice(num);
408 chars = chars.Slice(num);
409 }
410 if (bytes.IsEmpty)
411 {
412 return originalCharsLength - chars.Length;
413 }
414 return base.GetCharsWithFallback(bytes, originalBytesLength, chars, originalCharsLength, decoder);
415 }
416
417 public unsafe override string GetString(byte[] bytes, int byteIndex, int byteCount)
418 {
419 if (bytes == null)
420 {
422 }
423 if ((byteIndex | byteCount) < 0)
424 {
425 ThrowHelper.ThrowArgumentOutOfRangeException((byteIndex < 0) ? ExceptionArgument.byteIndex : ExceptionArgument.byteCount, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
426 }
427 if (bytes.Length - byteIndex < byteCount)
428 {
429 ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.bytes, ExceptionResource.ArgumentOutOfRange_IndexCountBuffer);
430 }
431 if (byteCount == 0)
432 {
433 return string.Empty;
434 }
435 fixed (byte* ptr = bytes)
436 {
437 return string.CreateStringFromEncoding(ptr + byteIndex, byteCount, this);
438 }
439 }
440
441 internal sealed override bool TryGetByteCount(Rune value, out int byteCount)
442 {
443 if (value.IsAscii)
444 {
445 byteCount = 1;
446 return true;
447 }
448 byteCount = 0;
449 return false;
450 }
451
452 internal sealed override OperationStatus EncodeRune(Rune value, Span<byte> bytes, out int bytesWritten)
453 {
454 if (value.IsAscii)
455 {
456 if (!bytes.IsEmpty)
457 {
458 bytes[0] = (byte)value.Value;
459 bytesWritten = 1;
460 return OperationStatus.Done;
461 }
462 bytesWritten = 0;
463 return OperationStatus.DestinationTooSmall;
464 }
465 bytesWritten = 0;
466 return OperationStatus.InvalidData;
467 }
468
469 internal sealed override OperationStatus DecodeFirstRune(ReadOnlySpan<byte> bytes, out Rune value, out int bytesConsumed)
470 {
471 if (!bytes.IsEmpty)
472 {
473 byte b = bytes[0];
474 if (b <= 127)
475 {
476 value = new Rune(b);
477 bytesConsumed = 1;
478 return OperationStatus.Done;
479 }
481 bytesConsumed = 1;
482 return OperationStatus.InvalidData;
483 }
485 bytesConsumed = 0;
486 return OperationStatus.NeedMoreData;
487 }
488
489 public override int GetMaxByteCount(int charCount)
490 {
491 if (charCount < 0)
492 {
494 }
495 long num = (long)charCount + 1L;
496 if (base.EncoderFallback.MaxCharCount > 1)
497 {
498 num *= base.EncoderFallback.MaxCharCount;
499 }
500 if (num > int.MaxValue)
501 {
503 }
504 return (int)num;
505 }
506
507 public override int GetMaxCharCount(int byteCount)
508 {
509 if (byteCount < 0)
510 {
512 }
513 long num = byteCount;
514 if (base.DecoderFallback.MaxCharCount > 1)
515 {
516 num *= base.DecoderFallback.MaxCharCount;
517 }
518 if (num > int.MaxValue)
519 {
521 }
522 return (int)num;
523 }
524
525 public override Decoder GetDecoder()
526 {
527 return new DecoderNLS(this);
528 }
529
530 public override Encoder GetEncoder()
531 {
532 return new EncoderNLS(this);
533 }
534}
static byte Min(byte val1, byte val2)
Definition Math.cs:912
static string ArgumentOutOfRange_GetCharCountOverflow
Definition SR.cs:90
static string ArgumentOutOfRange_GetByteCountOverflow
Definition SR.cs:88
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
Definition SR.cs:7
unsafe override int GetByteCount(string chars)
override Encoder GetEncoder()
unsafe override int GetByteCount(char *chars, int count)
override OperationStatus DecodeFirstRune(ReadOnlySpan< byte > bytes, out Rune value, out int bytesConsumed)
unsafe override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
override bool TryGetByteCount(Rune value, out int byteCount)
unsafe override int GetBytesFast(char *pChars, int charsLength, byte *pBytes, int bytesLength, out int charsConsumed)
unsafe override int GetCharsFast(byte *pBytes, int bytesLength, char *pChars, int charsLength, out int bytesConsumed)
override void SetDefaultFallbacks()
unsafe override int GetCharCount(byte[] bytes, int index, int count)
unsafe override int GetCharCount(byte *bytes, int count)
unsafe override int GetCharCount(ReadOnlySpan< byte > bytes)
override Decoder GetDecoder()
unsafe override int GetCharCountFast(byte *pBytes, int bytesLength, DecoderFallback fallback, out int bytesConsumed)
unsafe override int GetChars(byte *bytes, int byteCount, char *chars, int charCount)
unsafe override int GetByteCountFast(char *pChars, int charsLength, EncoderFallback fallback, out int charsConsumed)
unsafe override int GetCharsWithFallback(ReadOnlySpan< byte > bytes, int originalBytesLength, Span< char > chars, int originalCharsLength, DecoderNLS decoder)
unsafe int GetCharsCommon(byte *pBytes, int byteCount, char *pChars, int charCount)
unsafe int GetByteCountCommon(char *pChars, int charCount)
override int GetMaxCharCount(int byteCount)
unsafe override int GetBytes(char *chars, int charCount, byte *bytes, int byteCount)
unsafe override int GetByteCount(char[] chars, int index, int count)
override OperationStatus EncodeRune(Rune value, Span< byte > bytes, out int bytesWritten)
unsafe int GetCharCountCommon(byte *pBytes, int byteCount)
unsafe override int GetBytes(ReadOnlySpan< char > chars, Span< byte > bytes)
unsafe override int GetByteCount(ReadOnlySpan< char > chars)
static readonly ASCIIEncodingSealed s_default
unsafe int GetBytesCommon(char *pChars, int charCount, byte *pBytes, int byteCount)
unsafe override int GetBytesWithFallback(ReadOnlySpan< char > chars, int originalCharsLength, Span< byte > bytes, int originalBytesLength, EncoderNLS encoder)
override int GetMaxByteCount(int charCount)
unsafe override string GetString(byte[] bytes, int byteIndex, int byteCount)
unsafe override int GetChars(ReadOnlySpan< byte > bytes, Span< char > chars)
unsafe override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
unsafe override int GetBytes(string chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
static unsafe nuint GetIndexOfFirstNonAsciiByte(byte *pBuffer, nuint bufferLength)
static unsafe nuint WidenAsciiToUtf16(byte *pAsciiBuffer, char *pUtf16Buffer, nuint elementCount)
static unsafe nuint NarrowUtf16ToAscii(char *pUtf16Buffer, byte *pAsciiBuffer, nuint elementCount)
static unsafe nuint GetIndexOfFirstNonAsciiChar(char *pBuffer, nuint bufferLength)
static DecoderFallback ReplacementFallback
new DecoderFallback Fallback
Definition DecoderNLS.cs:19
static EncoderFallback ReplacementFallback
new EncoderFallback Fallback
Definition EncoderNLS.cs:21
EncoderFallback encoderFallback
Definition Encoding.cs:341
unsafe int GetByteCountWithFallback(char *pCharsOriginal, int originalCharCount, int charsConsumedSoFar)
Definition Encoding.cs:1150
static void ThrowConversionOverflow()
Definition Encoding.cs:1084
DecoderFallback decoderFallback
Definition Encoding.cs:343
unsafe int GetCharCountWithFallback(byte *pBytesOriginal, int originalByteCount, int bytesConsumedSoFar)
Definition Encoding.cs:1348
static void ThrowArgumentOutOfRangeException(System.ExceptionArgument argument)
static void ThrowArgumentNullException(string name)
static Rune ReplacementChar
Definition Rune.cs:53