< Summary

Line coverage
49%
Covered lines: 116
Uncovered lines: 117
Coverable lines: 233
Total lines: 520
Line coverage: 49.7%
Branch coverage
11%
Covered branches: 9
Total branches: 80
Branch coverage: 11.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
File 2: GetCodePageFromName(...)50%22100%
File 2: InternalGetCodePageFromName(...)80%101080.95%
File 2: GetEncodings()0%880%
File 2: GetEncodings(...)0%12120%
File 2: GetCodePageDataItem(...)0%26260%
File 2: InternalGetCodePageDataItem(...)100%110%
File 2: GetDisplayName(...)0%22220%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.CoreLib/src/System/Text/EncodingData.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
 4// THIS IS AUTOGENERATED FILE CREATED BY
 5// https://github.com/dotnet/buildtools/blob/6736870b84e06b75e7df32bb84d442db1b2afa10/src/Microsoft.DotNet.Build.Tasks/P
 6
 7namespace System.Text
 8{
 9    internal static partial class EncodingTable
 10    {
 11        //
 12        // EncodingNames is the concatenation of all supported IANA names for each codepage.
 13        // This is done rather than using a large readonly array of strings to avoid
 14        // generating a large amount of code in the static constructor.
 15        // Using indices from EncodingNamesIndices, we binary search this string when mapping
 16        // an encoding name to a codepage. Note that these names are all lowercase and are
 17        // sorted alphabetically.
 18        //
 19        private const string EncodingNames =
 20            "ansi_x3.4-1968" + // 20127
 21            "ansi_x3.4-1986" + // 20127
 22            "ascii" + // 20127
 23            "cp367" + // 20127
 24            "cp819" + // 28591
 25            "csascii" + // 20127
 26            "csisolatin1" + // 28591
 27            "csunicode11utf7" + // 65000
 28            "ibm367" + // 20127
 29            "ibm819" + // 28591
 30            "iso-10646-ucs-2" + // 1200
 31            "iso-8859-1" + // 28591
 32            "iso-ir-100" + // 28591
 33            "iso-ir-6" + // 20127
 34            "iso646-us" + // 20127
 35            "iso8859-1" + // 28591
 36            "iso_646.irv:1991" + // 20127
 37            "iso_8859-1" + // 28591
 38            "iso_8859-1:1987" + // 28591
 39            "l1" + // 28591
 40            "latin1" + // 28591
 41            "ucs-2" + // 1200
 42            "unicode" + // 1200
 43            "unicode-1-1-utf-7" + // 65000
 44            "unicode-1-1-utf-8" + // 65001
 45            "unicode-2-0-utf-7" + // 65000
 46            "unicode-2-0-utf-8" + // 65001
 47            "unicodefffe" + // 1201
 48            "us" + // 20127
 49            "us-ascii" + // 20127
 50            "utf-16" + // 1200
 51            "utf-16be" + // 1201
 52            "utf-16le" + // 1200
 53            "utf-32" + // 12000
 54            "utf-32be" + // 12001
 55            "utf-32le" + // 12000
 56            "utf-7" + // 65000
 57            "utf-8" + // 65001
 58            "utf16" + // 1200
 59            "utf32" + // 12000
 60            "utf8" + // 65001
 61            "x-unicode-1-1-utf-7" + // 65000
 62            "x-unicode-1-1-utf-8" + // 65001
 63            "x-unicode-2-0-utf-7" + // 65000
 64            "x-unicode-2-0-utf-8"; // 65001
 65
 66        //
 67        // EncodingNameIndices contains the start index of every encoding name in the string
 68        // EncodingNames. We infer the length of each string by looking at the start index
 69        // of the next string.
 70        //
 71        private static ReadOnlySpan<int> EncodingNameIndices =>
 3172        [
 3173            0, // ansi_x3.4-1968 (20127)
 3174            14, // ansi_x3.4-1986 (20127)
 3175            28, // ascii (20127)
 3176            33, // cp367 (20127)
 3177            38, // cp819 (28591)
 3178            43, // csascii (20127)
 3179            50, // csisolatin1 (28591)
 3180            61, // csunicode11utf7 (65000)
 3181            76, // ibm367 (20127)
 3182            82, // ibm819 (28591)
 3183            88, // iso-10646-ucs-2 (1200)
 3184            103, // iso-8859-1 (28591)
 3185            113, // iso-ir-100 (28591)
 3186            123, // iso-ir-6 (20127)
 3187            131, // iso646-us (20127)
 3188            140, // iso8859-1 (28591)
 3189            149, // iso_646.irv:1991 (20127)
 3190            165, // iso_8859-1 (28591)
 3191            175, // iso_8859-1:1987 (28591)
 3192            190, // l1 (28591)
 3193            192, // latin1 (28591)
 3194            198, // ucs-2 (1200)
 3195            203, // unicode (1200)
 3196            210, // unicode-1-1-utf-7 (65000)
 3197            227, // unicode-1-1-utf-8 (65001)
 3198            244, // unicode-2-0-utf-7 (65000)
 3199            261, // unicode-2-0-utf-8 (65001)
 31100            278, // unicodefffe (1201)
 31101            289, // us (20127)
 31102            291, // us-ascii (20127)
 31103            299, // utf-16 (1200)
 31104            305, // utf-16be (1201)
 31105            313, // utf-16le (1200)
 31106            321, // utf-32 (12000)
 31107            327, // utf-32be (12001)
 31108            335, // utf-32le (12000)
 31109            343, // utf-7 (65000)
 31110            348, // utf-8 (65001)
 31111            353, // utf16 (1200)
 31112            358, // utf32 (12000)
 31113            363, // utf8 (65001)
 31114            367, // x-unicode-1-1-utf-7 (65000)
 31115            386, // x-unicode-1-1-utf-8 (65001)
 31116            405, // x-unicode-2-0-utf-7 (65000)
 31117            424, // x-unicode-2-0-utf-8 (65001)
 31118            443
 31119        ];
 120
 121        //
 122        // CodePagesByName contains the list of supported codepages which match the encoding
 123        // names listed in EncodingNames. The way mapping works is we binary search
 124        // EncodingNames using EncodingNamesIndices until we find a match for a given name.
 125        // The index of the entry in EncodingNamesIndices will be the index of codepage in
 126        // CodePagesByName.
 127        //
 128        private static ReadOnlySpan<ushort> CodePagesByName =>
 2129        [
 2130            20127, // ansi_x3.4-1968
 2131            20127, // ansi_x3.4-1986
 2132            20127, // ascii
 2133            20127, // cp367
 2134            28591, // cp819
 2135            20127, // csascii
 2136            28591, // csisolatin1
 2137            65000, // csunicode11utf7
 2138            20127, // ibm367
 2139            28591, // ibm819
 2140            1200, // iso-10646-ucs-2
 2141            28591, // iso-8859-1
 2142            28591, // iso-ir-100
 2143            20127, // iso-ir-6
 2144            20127, // iso646-us
 2145            28591, // iso8859-1
 2146            20127, // iso_646.irv:1991
 2147            28591, // iso_8859-1
 2148            28591, // iso_8859-1:1987
 2149            28591, // l1
 2150            28591, // latin1
 2151            1200, // ucs-2
 2152            1200, // unicode
 2153            65000, // unicode-1-1-utf-7
 2154            65001, // unicode-1-1-utf-8
 2155            65000, // unicode-2-0-utf-7
 2156            65001, // unicode-2-0-utf-8
 2157            1201, // unicodefffe
 2158            20127, // us
 2159            20127, // us-ascii
 2160            1200, // utf-16
 2161            1201, // utf-16be
 2162            1200, // utf-16le
 2163            12000, // utf-32
 2164            12001, // utf-32be
 2165            12000, // utf-32le
 2166            65000, // utf-7
 2167            65001, // utf-8
 2168            1200, // utf16
 2169            12000, // utf32
 2170            65001, // utf8
 2171            65000, // x-unicode-1-1-utf-7
 2172            65001, // x-unicode-1-1-utf-8
 2173            65000, // x-unicode-2-0-utf-7
 2174            65001 // x-unicode-2-0-utf-8
 2175        ];
 176
 177        //
 178        // When retrieving the value for System.Text.Encoding.WebName or
 179        // System.Text.Encoding.EncodingName given System.Text.Encoding.CodePage,
 180        // we perform a linear search on MappedCodePages to find the index of the
 181        // given codepage. This is used to index WebNameIndices to get the start
 182        // index of the web name in the string WebNames, and to index
 183        // EnglishNameIndices to get the start of the English name in
 184        // EnglishNames. In addition, this arrays indices correspond to the indices
 185        // into UiFamilyCodePages and Flags.
 186        //
 187        private static ReadOnlySpan<ushort> MappedCodePages =>
 0188        [
 0189            1200, // utf-16
 0190            1201, // utf-16be
 0191            12000, // utf-32
 0192            12001, // utf-32be
 0193            20127, // us-ascii
 0194            28591, // iso-8859-1
 0195            65000, // utf-7
 0196            65001 // utf-8
 0197        ];
 198
 199        //
 200        // UiFamilyCodePages is indexed by the corresponding index in MappedCodePages.
 201        //
 202        private static ReadOnlySpan<int> UiFamilyCodePages =>
 0203        [
 0204            1200,
 0205            1200,
 0206            1200,
 0207            1200,
 0208            1252,
 0209            1252,
 0210            1200,
 0211            1200
 0212        ];
 213
 214        //
 215        // WebNames is a concatenation of the default encoding names
 216        // for each code page. It is used in retrieving the value for
 217        // System.Text.Encoding.WebName given System.Text.Encoding.CodePage.
 218        // This is done rather than using a large readonly array of strings to avoid
 219        // generating a large amount of code in the static constructor.
 220        //
 221        private const string WebNames =
 222            "utf-16" + // 1200
 223            "utf-16BE" + // 1201
 224            "utf-32" + // 12000
 225            "utf-32BE" + // 12001
 226            "us-ascii" + // 20127
 227            "iso-8859-1" + // 28591
 228            "utf-7" + // 65000
 229            "utf-8"; // 65001
 230
 231        //
 232        // WebNameIndices contains the start index of each code page's default
 233        // web name in the string WebNames. It is indexed by an index into
 234        // MappedCodePages.
 235        //
 236        private static ReadOnlySpan<int> WebNameIndices =>
 0237        [
 0238            0, // utf-16 (1200)
 0239            6, // utf-16be (1201)
 0240            14, // utf-32 (12000)
 0241            20, // utf-32be (12001)
 0242            28, // us-ascii (20127)
 0243            36, // iso-8859-1 (28591)
 0244            46, // utf-7 (65000)
 0245            51, // utf-8 (65001)
 0246            56
 0247        ];
 248
 249        // redeclaring these constants here for readability below
 250        private const uint MIMECONTF_MAILNEWS = Encoding.MIMECONTF_MAILNEWS;
 251        private const uint MIMECONTF_BROWSER = Encoding.MIMECONTF_BROWSER;
 252        private const uint MIMECONTF_SAVABLE_MAILNEWS = Encoding.MIMECONTF_SAVABLE_MAILNEWS;
 253        private const uint MIMECONTF_SAVABLE_BROWSER = Encoding.MIMECONTF_SAVABLE_BROWSER;
 254
 255        // Flags is indexed by the corresponding index in MappedCodePages.
 256        private static ReadOnlySpan<uint> Flags =>
 0257        [
 0258            MIMECONTF_SAVABLE_BROWSER,
 0259            0,
 0260            0,
 0261            0,
 0262            MIMECONTF_MAILNEWS | MIMECONTF_SAVABLE_MAILNEWS,
 0263            MIMECONTF_MAILNEWS | MIMECONTF_BROWSER | MIMECONTF_SAVABLE_MAILNEWS | MIMECONTF_SAVABLE_BROWSER,
 0264            MIMECONTF_MAILNEWS | MIMECONTF_SAVABLE_MAILNEWS,
 0265            MIMECONTF_MAILNEWS | MIMECONTF_BROWSER | MIMECONTF_SAVABLE_MAILNEWS | MIMECONTF_SAVABLE_BROWSER
 0266        ];
 267    }
 268}
 269

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/System.Private.CoreLib/src/System/Text/EncodingTable.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.Collections.Concurrent;
 5using System.Collections.Generic;
 6using System.Diagnostics;
 7using System.Threading;
 8
 9namespace System.Text
 10{
 11    //
 12    // Data table for encoding classes.  Used by System.Text.Encoding.
 13    // This class contains two hashtables to allow System.Text.Encoding
 14    // to retrieve the data item either by codepage value or by webName.
 15    //
 16    internal static partial class EncodingTable
 17    {
 18        private static ConcurrentDictionary<string, int>? s_nameToCodePage;
 19        private static CodePageDataItem?[]? s_codePageToCodePageData;
 20
 21        /*=================================GetCodePageFromName==========================
 22        **Action: Given a encoding name, return the correct code page number for this encoding.
 23        **Returns: The code page for the encoding.
 24        **Arguments:
 25        **  name    the name of the encoding
 26        **Exceptions:
 27        **  ArgumentNullException if name is null.
 28        **  internalGetCodePageFromName will throw ArgumentException if name is not a valid encoding name.
 29        ============================================================================*/
 30
 31        internal static int GetCodePageFromName(string name)
 32        {
 133            ArgumentNullException.ThrowIfNull(name);
 34
 135            if (s_nameToCodePage == null)
 36            {
 137                Interlocked.CompareExchange(ref s_nameToCodePage, new(StringComparer.OrdinalIgnoreCase), null);
 38            }
 39
 140            return s_nameToCodePage.GetOrAdd(name, InternalGetCodePageFromName);
 41        }
 42
 43        // Find the data item by binary searching the table.
 44        private static int InternalGetCodePageFromName(string name)
 45        {
 146            int left = 0;
 147            int right = EncodingNameIndices.Length - 2;
 48            int index;
 49            int result;
 50
 151            Debug.Assert(EncodingNameIndices.Length == CodePagesByName.Length + 1);
 152            Debug.Assert(EncodingNameIndices[^1] == EncodingNames.Length);
 53
 154            ReadOnlySpan<char> invariantName = name.ToLowerInvariant().AsSpan();
 55
 56            // Binary search the array until we have only a couple of elements left and then
 57            // just walk those elements.
 558            while ((right - left) > 3)
 59            {
 460                index = ((right - left) / 2) + left;
 61
 462                Debug.Assert(index < EncodingNameIndices.Length - 1);
 463                result = string.CompareOrdinal(invariantName, EncodingNames.AsSpan(EncodingNameIndices[index], EncodingN
 64
 465                if (result == 0)
 66                {
 67                    // We found the item, return the associated codePage.
 068                    return CodePagesByName[index];
 69                }
 470                else if (result < 0)
 71                {
 72                    // The name that we're looking for is less than our current index.
 173                    right = index;
 74                }
 75                else
 76                {
 77                    // The name that we're looking for is greater than our current index
 378                    left = index;
 79                }
 80            }
 81
 82            // Walk the remaining elements (it'll be 3 or fewer).
 583            for (; left <= right; left++)
 84            {
 385                Debug.Assert(left < EncodingNameIndices.Length - 1);
 386                if (invariantName.SequenceEqual(EncodingNames.AsSpan(EncodingNameIndices[left], EncodingNameIndices[left
 87                {
 188                    return CodePagesByName[left];
 89                }
 90            }
 91
 92            // The encoding name is not valid.
 093            throw new ArgumentException(
 094                SR.Format(SR.Argument_EncodingNotSupported, name),
 095                nameof(name));
 96        }
 97
 98        // Return a list of all EncodingInfo objects describing all of our encodings
 99        internal static EncodingInfo[] GetEncodings()
 100        {
 101            // If UTF-7 encoding is not enabled, we adjust the return array length by -1
 102            // to account for the skipped EncodingInfo element.
 103
 0104            ReadOnlySpan<ushort> mappedCodePages = MappedCodePages;
 0105            EncodingInfo[] arrayEncodingInfo = new EncodingInfo[(LocalAppContextSwitches.EnableUnsafeUTF7Encoding) ? map
 0106            string webNames = WebNames;
 0107            ReadOnlySpan<int> webNameIndices = WebNameIndices;
 0108            int arrayEncodingInfoIdx = 0;
 109
 0110            for (int i = 0; i < mappedCodePages.Length; i++)
 111            {
 0112                int codePage = mappedCodePages[i];
 0113                if (codePage == Encoding.CodePageUTF7 && !LocalAppContextSwitches.EnableUnsafeUTF7Encoding)
 114                {
 115                    continue; // skip this entry; UTF-7 is disabled
 116                }
 117
 0118                arrayEncodingInfo[arrayEncodingInfoIdx++] = new EncodingInfo(
 0119                    codePage,
 0120                    webNames[webNameIndices[i]..webNameIndices[i + 1]],
 0121                    GetDisplayName(codePage)
 0122                    );
 123            }
 124
 0125            Debug.Assert(arrayEncodingInfoIdx == arrayEncodingInfo.Length);
 0126            return arrayEncodingInfo;
 127        }
 128
 129        internal static EncodingInfo[] GetEncodings(Dictionary<int, EncodingInfo> encodingInfoList)
 130        {
 0131            Debug.Assert(encodingInfoList != null);
 0132            ReadOnlySpan<ushort> mappedCodePages = MappedCodePages;
 0133            string webNames = WebNames;
 0134            ReadOnlySpan<int> webNameIndices = WebNameIndices;
 135
 0136            for (int i = 0; i < mappedCodePages.Length; i++)
 137            {
 0138                int codePage = mappedCodePages[i];
 0139                if (!encodingInfoList.ContainsKey(codePage))
 140                {
 141                    // If UTF-7 encoding is not enabled, don't add it to the provided dictionary instance.
 142                    // Exception: If somebody already registered a custom UTF-7 provider, the dictionary
 143                    // will already contain an entry for the UTF-7 code page key, and we'll let it go through.
 144
 0145                    if (codePage != Encoding.CodePageUTF7 || LocalAppContextSwitches.EnableUnsafeUTF7Encoding)
 146                    {
 0147                        encodingInfoList[codePage] = new EncodingInfo(codePage, webNames[webNameIndices[i]..webNameIndic
 0148                                                                                GetDisplayName(codePage));
 149                    }
 150                }
 151            }
 152
 153            // Just in case a provider registered UTF-7 without the application's consent
 154
 0155            if (!LocalAppContextSwitches.EnableUnsafeUTF7Encoding)
 156            {
 0157                encodingInfoList.Remove(Encoding.CodePageUTF7); // won't throw if doesn't exist
 158            }
 159
 0160            var result = new EncodingInfo[encodingInfoList.Count];
 0161            int j = 0;
 0162            foreach (KeyValuePair<int, EncodingInfo> pair in encodingInfoList)
 163            {
 0164                result[j++] = pair.Value;
 165            }
 166
 0167            return result;
 168        }
 169
 170        internal static CodePageDataItem? GetCodePageDataItem(int codePage)
 171        {
 0172            if (s_codePageToCodePageData == null)
 173            {
 0174                Interlocked.CompareExchange<CodePageDataItem?[]?>(ref s_codePageToCodePageData, new CodePageDataItem[Map
 175            }
 176
 177            // Keep in sync with MappedCodePages
 178            int index;
 179            switch (codePage)
 180            {
 181                case 1200: // utf-16
 0182                    index = 0;
 0183                    break;
 184                case 1201: // utf-16be
 0185                    index = 1;
 0186                    break;
 187                case 12000: // utf-32
 0188                    index = 2;
 0189                    break;
 190                case 12001: // utf-32be
 0191                    index = 3;
 0192                    break;
 193                case 20127: // us-ascii
 0194                    index = 4;
 0195                    break;
 196                case 28591: // iso-8859-1
 0197                    index = 5;
 0198                    break;
 199                case 65000: // utf-7
 0200                    index = 6;
 0201                    break;
 202                case 65001: // utf-8
 0203                    index = 7;
 0204                    break;
 205                default:
 0206                    return null;
 207            }
 208
 0209            CodePageDataItem? data = s_codePageToCodePageData[index];
 0210            if (data == null)
 211            {
 0212                Interlocked.CompareExchange<CodePageDataItem?>(ref s_codePageToCodePageData[index], InternalGetCodePageD
 0213                data = s_codePageToCodePageData[index];
 214            }
 215
 0216            return data;
 217        }
 218
 219        private static CodePageDataItem InternalGetCodePageDataItem(int codePage, int index)
 220        {
 0221            int uiFamilyCodePage = UiFamilyCodePages[index];
 0222            string webName = WebNames[WebNameIndices[index]..WebNameIndices[index + 1]];
 223            // All supported code pages have identical header names, and body names.
 0224            string headerName = webName;
 0225            string bodyName = webName;
 0226            string displayName = GetDisplayName(codePage);
 0227            uint flags = Flags[index];
 228
 0229            return new CodePageDataItem(uiFamilyCodePage, webName, headerName, bodyName, displayName, flags);
 230        }
 231
 232        private static string GetDisplayName(int codePage)
 233        {
 234            switch (codePage)
 235            {
 0236                case 1200: return SR.Globalization_cp_1200;
 0237                case 1201: return SR.Globalization_cp_1201;
 0238                case 12000: return SR.Globalization_cp_12000;
 0239                case 12001: return SR.Globalization_cp_12001;
 0240                case 20127: return SR.Globalization_cp_20127;
 0241                case 28591: return SR.Globalization_cp_28591;
 0242                case 65000: return SR.Globalization_cp_65000;
 0243                case 65001: return SR.Globalization_cp_65001;
 244            };
 245
 0246            Debug.Fail("Unexpected code page");
 247            return "";
 248        }
 249    }
 250}
 251