Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace StringBuilder with ValueStringBuilder in AssemblyName.FullName #66750

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]);
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
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");
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
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