Skip to content

Commit

Permalink
Replace StringBuilder with ValueStringBuilder in AssemblyName.FullName (
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Mar 17, 2022
1 parent 7fc07e9 commit ac84ea6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace System.Text
{
internal ref partial struct ValueStringBuilder
{
internal void AppendSpanFormattable<T>(T value, string? format, IFormatProvider? provider) where T : ISpanFormattable
internal void AppendSpanFormattable<T>(T value, string? format = null, IFormatProvider? provider = null) where T : ISpanFormattable
{
if (value.TryFormat(_chars.Slice(_pos), out int charsWritten, format, provider))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,31 @@ public static string ComputeDisplayName(string name, Version? version, string? c
const int PUBLIC_KEY_TOKEN_LEN = 8;
Debug.Assert(name.Length != 0);

StringBuilder sb = new StringBuilder();
sb.AppendQuoted(name);
var vsb = new ValueStringBuilder(stackalloc char[256]);
vsb.AppendQuoted(name);

if (version != null)
{
Version canonicalizedVersion = version.CanonicalizeVersion();
if (canonicalizedVersion.Major != ushort.MaxValue)
{
sb.Append(", Version=");
sb.Append(canonicalizedVersion.Major);
vsb.Append(", Version=");
vsb.AppendSpanFormattable(canonicalizedVersion.Major);

if (canonicalizedVersion.Minor != ushort.MaxValue)
{
sb.Append('.');
sb.Append(canonicalizedVersion.Minor);
vsb.Append('.');
vsb.AppendSpanFormattable(canonicalizedVersion.Minor);

if (canonicalizedVersion.Build != ushort.MaxValue)
{
sb.Append('.');
sb.Append(canonicalizedVersion.Build);
vsb.Append('.');
vsb.AppendSpanFormattable(canonicalizedVersion.Build);

if (canonicalizedVersion.Revision != ushort.MaxValue)
{
sb.Append('.');
sb.Append(canonicalizedVersion.Revision);
vsb.Append('.');
vsb.AppendSpanFormattable(canonicalizedVersion.Revision);
}
}
}
Expand All @@ -50,36 +50,38 @@ public static string ComputeDisplayName(string name, Version? version, string? c
{
if (cultureName.Length == 0)
cultureName = "neutral";
sb.Append(", Culture=");
sb.AppendQuoted(cultureName);
vsb.Append(", Culture=");
vsb.AppendQuoted(cultureName);
}

if (pkt != null)
{
if (pkt.Length > PUBLIC_KEY_TOKEN_LEN)
throw new ArgumentException();

sb.Append(", PublicKeyToken=");
vsb.Append(", PublicKeyToken=");
if (pkt.Length == 0)
sb.Append("null");
{
vsb.Append("null");
}
else
{
sb.Append(HexConverter.ToString(pkt, HexConverter.Casing.Lower));
HexConverter.EncodeToUtf16(pkt, vsb.AppendSpan(pkt.Length * 2), HexConverter.Casing.Lower);
}
}

if (0 != (flags & AssemblyNameFlags.Retargetable))
sb.Append(", Retargetable=Yes");
vsb.Append(", Retargetable=Yes");

if (contentType == AssemblyContentType.WindowsRuntime)
sb.Append(", ContentType=WindowsRuntime");
vsb.Append(", ContentType=WindowsRuntime");

// NOTE: By design (desktop compat) AssemblyName.FullName and ToString() do not include ProcessorArchitecture.

return sb.ToString();
return vsb.ToString();
}

private static void AppendQuoted(this StringBuilder sb, string s)
private static void AppendQuoted(this ref ValueStringBuilder vsb, string s)
{
bool needsQuoting = false;
const char quoteChar = '\"';
Expand All @@ -90,7 +92,7 @@ private static void AppendQuoted(this StringBuilder sb, string s)
needsQuoting = true;

if (needsQuoting)
sb.Append(quoteChar);
vsb.Append(quoteChar);

for (int i = 0; i < s.Length; i++)
{
Expand All @@ -101,24 +103,24 @@ private static void AppendQuoted(this StringBuilder sb, string s)
case '=':
case '\'':
case '"':
sb.Append('\\');
vsb.Append('\\');
break;
case '\t':
sb.Append("\\t");
vsb.Append("\\t");
continue;
case '\r':
sb.Append("\\r");
vsb.Append("\\r");
continue;
case '\n':
sb.Append("\\n");
vsb.Append("\\n");
continue;
}

sb.Append(s[i]);
vsb.Append(s[i]);
}

if (needsQuoting)
sb.Append(quoteChar);
vsb.Append(quoteChar);
}

private static Version CanonicalizeVersion(this Version version)
Expand Down

0 comments on commit ac84ea6

Please sign in to comment.