< Summary

Information
Class: System.Reflection.AssemblyNameFormatter
Assembly: System.Reflection.Metadata
File(s): D:\runner\runtime\src\libraries\Common\src\System\Reflection\AssemblyNameFormatter.cs
Line coverage
86%
Covered lines: 78
Uncovered lines: 12
Coverable lines: 90
Total lines: 150
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)

D:\runner\runtime\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
 470421        {
 22            const int PUBLIC_KEY_TOKEN_LEN = 8;
 470423            Debug.Assert(name.Length != 0);
 24
 470425            vsb.AppendQuoted(name);
 26
 470427            if (version != null)
 45228            {
 45229                ushort major = (ushort)version.Major;
 45230                if (major != ushort.MaxValue)
 45231                {
 45232                    vsb.Append(", Version=");
 45233                    vsb.AppendSpanFormattable(major);
 34
 45235                    ushort minor = (ushort)version.Minor;
 45236                    if (minor != ushort.MaxValue)
 45237                    {
 45238                        vsb.Append('.');
 45239                        vsb.AppendSpanFormattable(minor);
 40
 45241                        ushort build = (ushort)version.Build;
 45242                        if (build != ushort.MaxValue)
 36843                        {
 36844                            vsb.Append('.');
 36845                            vsb.AppendSpanFormattable(build);
 46
 36847                            ushort revision = (ushort)version.Revision;
 36848                            if (revision != ushort.MaxValue)
 27249                            {
 27250                                vsb.Append('.');
 27251                                vsb.AppendSpanFormattable(revision);
 27252                            }
 36853                        }
 45254                    }
 45255                }
 45256            }
 57
 470458            if (cultureName != null)
 059            {
 060                if (cultureName.Length == 0)
 061                    cultureName = "neutral";
 062                vsb.Append(", Culture=");
 063                vsb.AppendQuoted(cultureName);
 064            }
 65
 470466            byte[]? keyOrToken = pkt ?? pk;
 470467            if (keyOrToken != null)
 56868            {
 56869                if (pkt != null)
 15270                {
 15271                    if (pkt.Length > PUBLIC_KEY_TOKEN_LEN)
 072                        throw new ArgumentException();
 73
 15274                    vsb.Append(", PublicKeyToken=");
 15275                }
 76                else
 41677                {
 41678                    vsb.Append(", PublicKey=");
 41679                }
 80
 56881                if (keyOrToken.Length == 0)
 3282                {
 3283                    vsb.Append("null");
 3284                }
 85                else
 53686                {
 53687                    HexConverter.EncodeToUtf16(keyOrToken, vsb.AppendSpan(keyOrToken.Length * 2), HexConverter.Casing.Lo
 53688                }
 56889            }
 90
 470491            if (0 != (flags & AssemblyNameFlags.Retargetable))
 2092                vsb.Append(", Retargetable=Yes");
 93
 470494            if (contentType == AssemblyContentType.WindowsRuntime)
 2895                vsb.Append(", ContentType=WindowsRuntime");
 96
 97            // NOTE: By design (desktop compat) AssemblyName.FullName and ToString() do not include ProcessorArchitectur
 470498        }
 99
 100        private static void AppendQuoted(this ref ValueStringBuilder vsb, string s)
 4704101        {
 4704102            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.
 4704107            ReadOnlySpan<char> span = s.AsSpan();
 4704108            if (s.Length != span.Trim().Length || span.IndexOfAny('\"', '\'') >= 0)
 1088109                needsQuoting = true;
 110
 4704111            if (needsQuoting)
 1088112                vsb.Append(quoteChar);
 113
 473512114            for (int i = 0; i < s.Length; i++)
 232052115            {
 232052116                switch (s[i])
 117                {
 118                    case '\\':
 119                    case ',':
 120                    case '=':
 121                    case '\'':
 122                    case '"':
 22048123                        vsb.Append('\\');
 22048124                        break;
 125                    case '\t':
 20380126                        vsb.Append("\\t");
 20380127                        continue;
 128                    case '\r':
 6692129                        vsb.Append("\\r");
 6692130                        continue;
 131                    case '\n':
 11432132                        vsb.Append("\\n");
 11432133                        continue;
 134                }
 135
 193548136                vsb.Append(s[i]);
 193548137            }
 138
 4704139            if (needsQuoting)
 1088140                vsb.Append(quoteChar);
 4704141        }
 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}