Refactoring the ARM Hardware Intrinsics based on the latest design decisions. (#26895)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / Text / DecoderExceptionFallback.cs
blob4ea00ef65a86e5704a03a8c82e3e0d86be30cbbb
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System.Diagnostics.CodeAnalysis;
6 using System.Globalization;
7 using System.Runtime.Serialization;
9 namespace System.Text
11 public sealed class DecoderExceptionFallback : DecoderFallback
13 public override DecoderFallbackBuffer CreateFallbackBuffer() =>
14 new DecoderExceptionFallbackBuffer();
16 // Maximum number of characters that this instance of this fallback could return
17 public override int MaxCharCount => 0;
19 public override bool Equals(object? value) =>
20 value is DecoderExceptionFallback;
22 public override int GetHashCode() => 879;
26 public sealed class DecoderExceptionFallbackBuffer : DecoderFallbackBuffer
28 public override bool Fallback(byte[] bytesUnknown, int index)
30 Throw(bytesUnknown, index);
31 return true;
34 public override char GetNextChar() => (char)0;
36 // Exception fallback doesn't have anywhere to back up to.
37 public override bool MovePrevious() => false;
39 // Exceptions are always empty
40 public override int Remaining => 0;
42 [DoesNotReturn]
43 private void Throw(byte[] bytesUnknown, int index)
45 bytesUnknown ??= Array.Empty<byte>();
47 // Create a string representation of our bytes.
48 StringBuilder strBytes = new StringBuilder(bytesUnknown.Length * 4);
50 const int MaxLength = 20;
51 for (int i = 0; i < bytesUnknown.Length && i < MaxLength; i++)
53 strBytes.Append('[');
54 strBytes.Append(bytesUnknown[i].ToString("X2", CultureInfo.InvariantCulture));
55 strBytes.Append(']');
58 // In case the string's really long
59 if (bytesUnknown.Length > MaxLength)
61 strBytes.Append(" ...");
64 // Known index
65 throw new DecoderFallbackException(
66 SR.Format(SR.Argument_InvalidCodePageBytesIndex,
67 strBytes, index), bytesUnknown, index);
71 // Exception for decoding unknown byte sequences.
72 [Serializable]
73 [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
74 public sealed class DecoderFallbackException : ArgumentException
76 private readonly byte[]? _bytesUnknown = null;
77 private readonly int _index = 0;
79 public DecoderFallbackException()
80 : base(SR.Arg_ArgumentException)
82 HResult = HResults.COR_E_ARGUMENT;
85 public DecoderFallbackException(string? message)
86 : base(message)
88 HResult = HResults.COR_E_ARGUMENT;
91 public DecoderFallbackException(string? message, Exception? innerException)
92 : base(message, innerException)
94 HResult = HResults.COR_E_ARGUMENT;
97 public DecoderFallbackException(string? message, byte[]? bytesUnknown, int index)
98 : base(message)
100 _bytesUnknown = bytesUnknown;
101 _index = index;
104 private DecoderFallbackException(SerializationInfo serializationInfo, StreamingContext streamingContext)
105 : base(serializationInfo, streamingContext)
109 public byte[]? BytesUnknown => _bytesUnknown;
111 public int Index => _index;