< 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
 456021        {
 22            const int PUBLIC_KEY_TOKEN_LEN = 8;
 456023            Debug.Assert(name.Length != 0);
 24
 456025            vsb.AppendQuoted(name);
 26
 456027            if (version != null)
 44028            {
 44029                ushort major = (ushort)version.Major;
 44030                if (major != ushort.MaxValue)
 44031                {
 44032                    vsb.Append(", Version=");
 44033                    vsb.AppendSpanFormattable(major);
 34
 44035                    ushort minor = (ushort)version.Minor;
 44036                    if (minor != ushort.MaxValue)
 44037                    {
 44038                        vsb.Append('.');
 44039                        vsb.AppendSpanFormattable(minor);
 40
 44041                        ushort build = (ushort)version.Build;
 44042                        if (build != ushort.MaxValue)
 35643                        {
 35644                            vsb.Append('.');
 35645                            vsb.AppendSpanFormattable(build);
 46
 35647                            ushort revision = (ushort)version.Revision;
 35648                            if (revision != ushort.MaxValue)
 26049                            {
 26050                                vsb.Append('.');
 26051                                vsb.AppendSpanFormattable(revision);
 26052                            }
 35653                        }
 44054                    }
 44055                }
 44056            }
 57
 456058            if (cultureName != null)
 059            {
 060                if (cultureName.Length == 0)
 061                    cultureName = "neutral";
 062                vsb.Append(", Culture=");
 063                vsb.AppendQuoted(cultureName);
 064            }
 65
 456066            byte[]? keyOrToken = pkt ?? pk;
 456067            if (keyOrToken != null)
 56468            {
 56469                if (pkt != null)
 14870                {
 14871                    if (pkt.Length > PUBLIC_KEY_TOKEN_LEN)
 072                        throw new ArgumentException();
 73
 14874                    vsb.Append(", PublicKeyToken=");
 14875                }
 76                else
 41677                {
 41678                    vsb.Append(", PublicKey=");
 41679                }
 80
 56481                if (keyOrToken.Length == 0)
 3282                {
 3283                    vsb.Append("null");
 3284                }
 85                else
 53286                {
 53287                    HexConverter.EncodeToUtf16(keyOrToken, vsb.AppendSpan(keyOrToken.Length * 2), HexConverter.Casing.Lo
 53288                }
 56489            }
 90
 456091            if (0 != (flags & AssemblyNameFlags.Retargetable))
 2092                vsb.Append(", Retargetable=Yes");
 93
 456094            if (contentType == AssemblyContentType.WindowsRuntime)
 2495                vsb.Append(", ContentType=WindowsRuntime");
 96
 97            // NOTE: By design (desktop compat) AssemblyName.FullName and ToString() do not include ProcessorArchitectur
 456098        }
 99
 100        private static void AppendQuoted(this ref ValueStringBuilder vsb, string s)
 4560101        {
 4560102            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.
 4560107            ReadOnlySpan<char> span = s.AsSpan();
 4560108            if (s.Length != span.Trim().Length || span.IndexOfAny('\"', '\'') >= 0)
 1080109                needsQuoting = true;
 110
 4560111            if (needsQuoting)
 1080112                vsb.Append(quoteChar);
 113
 445008114            for (int i = 0; i < s.Length; i++)
 217944115            {
 217944116                switch (s[i])
 117                {
 118                    case '\\':
 119                    case ',':
 120                    case '=':
 121                    case '\'':
 122                    case '"':
 20544123                        vsb.Append('\\');
 20544124                        break;
 125                    case '\t':
 18196126                        vsb.Append("\\t");
 18196127                        continue;
 128                    case '\r':
 5496129                        vsb.Append("\\r");
 5496130                        continue;
 131                    case '\n':
 10428132                        vsb.Append("\\n");
 10428133                        continue;
 134                }
 135
 183824136                vsb.Append(s[i]);
 183824137            }
 138
 4560139            if (needsQuoting)
 1080140                vsb.Append(quoteChar);
 4560141        }
 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}