< Summary

Information
Line coverage
86%
Covered lines: 78
Uncovered lines: 12
Coverable lines: 90
Total lines: 151
Line coverage: 86.6%
Branch coverage
89%
Covered branches: 50
Total branches: 56
Branch coverage: 89.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
ComputeDisplayName(...)100%110%
AppendDisplayName(...)78.57%282888.7%
AppendQuoted(...)100%2828100%

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/Common/src/System/Reflection/AssemblyNameFormatter.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.Generic;
 5using System.Diagnostics;
 6using System.Globalization;
 7using System.Text;
 8
 9namespace System.Reflection
 10{
 11    internal static class AssemblyNameFormatter
 12    {
 13        public static unsafe string ComputeDisplayName(string name, Version? version, string? cultureName, byte[]? pkt, 
 014        {
 015            ValueStringBuilder vsb = new(stackalloc char[256]);
 016            AppendDisplayName(ref vsb, name, version, cultureName, pkt, flags, contentType, pk);
 017            return vsb.ToString();
 018        }
 19
 20        public static void AppendDisplayName(ref ValueStringBuilder vsb, string name, Version? version, string? cultureN
 494021        {
 22            const int PUBLIC_KEY_TOKEN_LEN = 8;
 494023            Debug.Assert(name.Length != 0);
 24
 494025            vsb.AppendQuoted(name);
 26
 494027            if (version != null)
 36828            {
 36829                ushort major = (ushort)version.Major;
 36830                if (major != ushort.MaxValue)
 36831                {
 36832                    vsb.Append(", Version=");
 36833                    vsb.AppendSpanFormattable(major);
 34
 36835                    ushort minor = (ushort)version.Minor;
 36836                    if (minor != ushort.MaxValue)
 36837                    {
 36838                        vsb.Append('.');
 36839                        vsb.AppendSpanFormattable(minor);
 40
 36841                        ushort build = (ushort)version.Build;
 36842                        if (build != ushort.MaxValue)
 20843                        {
 20844                            vsb.Append('.');
 20845                            vsb.AppendSpanFormattable(build);
 46
 20847                            ushort revision = (ushort)version.Revision;
 20848                            if (revision != ushort.MaxValue)
 12049                            {
 12050                                vsb.Append('.');
 12051                                vsb.AppendSpanFormattable(revision);
 12052                            }
 20853                        }
 36854                    }
 36855                }
 36856            }
 57
 494058            if (cultureName != null)
 059            {
 060                if (cultureName.Length == 0)
 061                    cultureName = "neutral";
 062                vsb.Append(", Culture=");
 063                vsb.AppendQuoted(cultureName);
 064            }
 65
 494066            byte[]? keyOrToken = pkt ?? pk;
 494067            if (keyOrToken != null)
 74468            {
 74469                if (pkt != null)
 7270                {
 7271                    if (pkt.Length > PUBLIC_KEY_TOKEN_LEN)
 072                        throw new ArgumentException();
 73
 7274                    vsb.Append(", PublicKeyToken=");
 7275                }
 76                else
 67277                {
 67278                    vsb.Append(", PublicKey=");
 67279                }
 80
 74481                if (keyOrToken.Length == 0)
 5282                {
 5283                    vsb.Append("null");
 5284                }
 85                else
 69286                {
 69287                    HexConverter.EncodeToUtf16(keyOrToken, vsb.AppendSpan(keyOrToken.Length * 2), HexConverter.Casing.Lo
 69288                }
 74489            }
 90
 494091            if (0 != (flags & AssemblyNameFlags.Retargetable))
 1692                vsb.Append(", Retargetable=Yes");
 93
 494094            if (contentType == AssemblyContentType.WindowsRuntime)
 1295                vsb.Append(", ContentType=WindowsRuntime");
 96
 97            // NOTE: By design (desktop compat) AssemblyName.FullName and ToString() do not include ProcessorArchitectur
 494098        }
 99
 100        private static void AppendQuoted(this ref ValueStringBuilder vsb, string s)
 4940101        {
 4940102            bool needsQuoting = false;
 103            const char quoteChar = '\"';
 104
 105            // App-compat: You can use double or single quotes to quote a name, and Fusion (or rather the IdentityAuthor
 106            // by some algorithm. Rather than guess at it, we use double quotes consistently.
 4940107            ReadOnlySpan<char> span = s.AsSpan();
 4940108            if (s.Length != span.Trim().Length || span.IndexOfAny('\"', '\'') >= 0)
 700109                needsQuoting = true;
 110
 4940111            if (needsQuoting)
 700112                vsb.Append(quoteChar);
 113
 789256114            for (int i = 0; i < s.Length; i++)
 389688115            {
 389688116                switch (s[i])
 117                {
 118                    case '\\':
 119                    case ',':
 120                    case '=':
 121                    case '\'':
 122                    case '"':
 12948123                        vsb.Append('\\');
 12948124                        break;
 125                    case '\t':
 90200126                        vsb.Append("\\t");
 90200127                        continue;
 128                    case '\r':
 9236129                        vsb.Append("\\r");
 9236130                        continue;
 131                    case '\n':
 4904132                        vsb.Append("\\n");
 4904133                        continue;
 134                }
 135
 285348136                vsb.Append(s[i]);
 285348137            }
 138
 4940139            if (needsQuoting)
 700140                vsb.Append(quoteChar);
 4940141        }
 142
 143#if !NET
 144        private static void AppendSpanFormattable(this ref ValueStringBuilder vsb, ushort value)
 145        {
 146            vsb.Append(value.ToString());
 147        }
 148#endif
 149    }
 150}
 151