< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 17
Coverable lines: 17
Total lines: 115
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
.ctor(...)100%110%
.ctor(...)100%110%
.ctor(...)100%110%
.ctor(...)100%110%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.CoreLib/src/System/Text/DecoderExceptionFallback.cs

#LineLine coverage
 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
 4using System.Diagnostics.CodeAnalysis;
 5using System.Globalization;
 6using System.Runtime.CompilerServices;
 7using System.Runtime.Serialization;
 8
 9namespace System.Text
 10{
 11    public sealed class DecoderExceptionFallback : DecoderFallback
 12    {
 13        internal static readonly DecoderExceptionFallback s_default = new DecoderExceptionFallback();
 14
 15        public override DecoderFallbackBuffer CreateFallbackBuffer() =>
 16            new DecoderExceptionFallbackBuffer();
 17
 18        // Maximum number of characters that this instance of this fallback could return
 19        public override int MaxCharCount => 0;
 20
 21        public override bool Equals([NotNullWhen(true)] object? value) =>
 22            value is DecoderExceptionFallback;
 23
 24        public override int GetHashCode() => 879;
 25    }
 26
 27
 28    public sealed class DecoderExceptionFallbackBuffer : DecoderFallbackBuffer
 29    {
 30        public override bool Fallback(byte[] bytesUnknown, int index)
 31        {
 32            Throw(bytesUnknown, index);
 33            return true;
 34        }
 35
 36        public override char GetNextChar() => (char)0;
 37
 38        // Exception fallback doesn't have anywhere to back up to.
 39        public override bool MovePrevious() => false;
 40
 41        // Exceptions are always empty
 42        public override int Remaining => 0;
 43
 44        [DoesNotReturn]
 45        private static void Throw(byte[] bytesUnknown, int index)
 46        {
 47            bytesUnknown ??= [];
 48
 49            // Create a string representation of our bytes.
 50            StringBuilder strBytes = new StringBuilder(bytesUnknown.Length * 4);
 51
 52            const int MaxLength = 20;
 53            for (int i = 0; i < bytesUnknown.Length && i < MaxLength; i++)
 54            {
 55                strBytes.Append($"[{bytesUnknown[i]:X2}]");
 56            }
 57
 58            // In case the string's really long
 59            if (bytesUnknown.Length > MaxLength)
 60            {
 61                strBytes.Append(" ...");
 62            }
 63
 64            // Known index
 65            throw new DecoderFallbackException(
 66                SR.Format(SR.Argument_InvalidCodePageBytesIndex,
 67                   strBytes, index), bytesUnknown, index);
 68        }
 69    }
 70
 71    // Exception for decoding unknown byte sequences.
 72    [Serializable]
 73    [TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
 74    public sealed class DecoderFallbackException : ArgumentException
 75    {
 76        private readonly byte[]? _bytesUnknown;
 77        private readonly int _index;
 78
 79        public DecoderFallbackException()
 080            : base(SR.Arg_ArgumentException)
 81        {
 082            HResult = HResults.COR_E_ARGUMENT;
 083        }
 84
 85        public DecoderFallbackException(string? message)
 086            : base(message)
 87        {
 088            HResult = HResults.COR_E_ARGUMENT;
 089        }
 90
 91        public DecoderFallbackException(string? message, Exception? innerException)
 092            : base(message, innerException)
 93        {
 094            HResult = HResults.COR_E_ARGUMENT;
 095        }
 96
 97        public DecoderFallbackException(string? message, byte[]? bytesUnknown, int index)
 098            : base(message)
 99        {
 0100            _bytesUnknown = bytesUnknown;
 0101            _index = index;
 0102        }
 103
 104        [Obsolete(Obsoletions.LegacyFormatterImplMessage, DiagnosticId = Obsoletions.LegacyFormatterImplDiagId, UrlForma
 105        private DecoderFallbackException(SerializationInfo serializationInfo, StreamingContext streamingContext)
 0106            : base(serializationInfo, streamingContext)
 107        {
 0108        }
 109
 0110        public byte[]? BytesUnknown => _bytesUnknown;
 111
 0112        public int Index => _index;
 113    }
 114}
 115