< Summary

Information
Line coverage
36%
Covered lines: 33
Uncovered lines: 57
Coverable lines: 90
Total lines: 151
Line coverage: 36.6%
Branch coverage
60%
Covered branches: 34
Total branches: 56
Branch coverage: 60.7%
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(...)21.42%282816.12%
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
 1005621        {
 22            const int PUBLIC_KEY_TOKEN_LEN = 8;
 1005623            Debug.Assert(name.Length != 0);
 24
 1005625            vsb.AppendQuoted(name);
 26
 1005627            if (version != null)
 028            {
 029                ushort major = (ushort)version.Major;
 030                if (major != ushort.MaxValue)
 031                {
 032                    vsb.Append(", Version=");
 033                    vsb.AppendSpanFormattable(major);
 34
 035                    ushort minor = (ushort)version.Minor;
 036                    if (minor != ushort.MaxValue)
 037                    {
 038                        vsb.Append('.');
 039                        vsb.AppendSpanFormattable(minor);
 40
 041                        ushort build = (ushort)version.Build;
 042                        if (build != ushort.MaxValue)
 043                        {
 044                            vsb.Append('.');
 045                            vsb.AppendSpanFormattable(build);
 46
 047                            ushort revision = (ushort)version.Revision;
 048                            if (revision != ushort.MaxValue)
 049                            {
 050                                vsb.Append('.');
 051                                vsb.AppendSpanFormattable(revision);
 052                            }
 053                        }
 054                    }
 055                }
 056            }
 57
 1005658            if (cultureName != null)
 059            {
 060                if (cultureName.Length == 0)
 061                    cultureName = "neutral";
 062                vsb.Append(", Culture=");
 063                vsb.AppendQuoted(cultureName);
 064            }
 65
 1005666            byte[]? keyOrToken = pkt ?? pk;
 1005667            if (keyOrToken != null)
 068            {
 069                if (pkt != null)
 070                {
 071                    if (pkt.Length > PUBLIC_KEY_TOKEN_LEN)
 072                        throw new ArgumentException();
 73
 074                    vsb.Append(", PublicKeyToken=");
 075                }
 76                else
 077                {
 078                    vsb.Append(", PublicKey=");
 079                }
 80
 081                if (keyOrToken.Length == 0)
 082                {
 083                    vsb.Append("null");
 084                }
 85                else
 086                {
 087                    HexConverter.EncodeToUtf16(keyOrToken, vsb.AppendSpan(keyOrToken.Length * 2), HexConverter.Casing.Lo
 088                }
 089            }
 90
 1005691            if (0 != (flags & AssemblyNameFlags.Retargetable))
 092                vsb.Append(", Retargetable=Yes");
 93
 1005694            if (contentType == AssemblyContentType.WindowsRuntime)
 095                vsb.Append(", ContentType=WindowsRuntime");
 96
 97            // NOTE: By design (desktop compat) AssemblyName.FullName and ToString() do not include ProcessorArchitectur
 1005698        }
 99
 100        private static void AppendQuoted(this ref ValueStringBuilder vsb, string s)
 10056101        {
 10056102            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.
 10056107            ReadOnlySpan<char> span = s.AsSpan();
 10056108            if (s.Length != span.Trim().Length || span.IndexOfAny('\"', '\'') >= 0)
 3520109                needsQuoting = true;
 110
 10056111            if (needsQuoting)
 3520112                vsb.Append(quoteChar);
 113
 1204816114            for (int i = 0; i < s.Length; i++)
 592352115            {
 592352116                switch (s[i])
 117                {
 118                    case '\\':
 119                    case ',':
 120                    case '=':
 121                    case '\'':
 122                    case '"':
 83684123                        vsb.Append('\\');
 83684124                        break;
 125                    case '\t':
 2872126                        vsb.Append("\\t");
 2872127                        continue;
 128                    case '\r':
 15900129                        vsb.Append("\\r");
 15900130                        continue;
 131                    case '\n':
 4688132                        vsb.Append("\\n");
 4688133                        continue;
 134                }
 135
 568892136                vsb.Append(s[i]);
 568892137            }
 138
 10056139            if (needsQuoting)
 3520140                vsb.Append(quoteChar);
 10056141        }
 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