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