Skip to content

Commit ac84ea6

Browse files
authored
Replace StringBuilder with ValueStringBuilder in AssemblyName.FullName (#66750)
1 parent 7fc07e9 commit ac84ea6

2 files changed

Lines changed: 29 additions & 27 deletions

File tree

src/libraries/Common/src/System/Text/ValueStringBuilder.AppendSpanFormattable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace System.Text
55
{
66
internal ref partial struct ValueStringBuilder
77
{
8-
internal void AppendSpanFormattable<T>(T value, string? format, IFormatProvider? provider) where T : ISpanFormattable
8+
internal void AppendSpanFormattable<T>(T value, string? format = null, IFormatProvider? provider = null) where T : ISpanFormattable
99
{
1010
if (value.TryFormat(_chars.Slice(_pos), out int charsWritten, format, provider))
1111
{

src/libraries/System.Private.CoreLib/src/System/Reflection/AssemblyNameFormatter.cs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,31 @@ public static string ComputeDisplayName(string name, Version? version, string? c
1515
const int PUBLIC_KEY_TOKEN_LEN = 8;
1616
Debug.Assert(name.Length != 0);
1717

18-
StringBuilder sb = new StringBuilder();
19-
sb.AppendQuoted(name);
18+
var vsb = new ValueStringBuilder(stackalloc char[256]);
19+
vsb.AppendQuoted(name);
2020

2121
if (version != null)
2222
{
2323
Version canonicalizedVersion = version.CanonicalizeVersion();
2424
if (canonicalizedVersion.Major != ushort.MaxValue)
2525
{
26-
sb.Append(", Version=");
27-
sb.Append(canonicalizedVersion.Major);
26+
vsb.Append(", Version=");
27+
vsb.AppendSpanFormattable(canonicalizedVersion.Major);
2828

2929
if (canonicalizedVersion.Minor != ushort.MaxValue)
3030
{
31-
sb.Append('.');
32-
sb.Append(canonicalizedVersion.Minor);
31+
vsb.Append('.');
32+
vsb.AppendSpanFormattable(canonicalizedVersion.Minor);
3333

3434
if (canonicalizedVersion.Build != ushort.MaxValue)
3535
{
36-
sb.Append('.');
37-
sb.Append(canonicalizedVersion.Build);
36+
vsb.Append('.');
37+
vsb.AppendSpanFormattable(canonicalizedVersion.Build);
3838

3939
if (canonicalizedVersion.Revision != ushort.MaxValue)
4040
{
41-
sb.Append('.');
42-
sb.Append(canonicalizedVersion.Revision);
41+
vsb.Append('.');
42+
vsb.AppendSpanFormattable(canonicalizedVersion.Revision);
4343
}
4444
}
4545
}
@@ -50,36 +50,38 @@ public static string ComputeDisplayName(string name, Version? version, string? c
5050
{
5151
if (cultureName.Length == 0)
5252
cultureName = "neutral";
53-
sb.Append(", Culture=");
54-
sb.AppendQuoted(cultureName);
53+
vsb.Append(", Culture=");
54+
vsb.AppendQuoted(cultureName);
5555
}
5656

5757
if (pkt != null)
5858
{
5959
if (pkt.Length > PUBLIC_KEY_TOKEN_LEN)
6060
throw new ArgumentException();
6161

62-
sb.Append(", PublicKeyToken=");
62+
vsb.Append(", PublicKeyToken=");
6363
if (pkt.Length == 0)
64-
sb.Append("null");
64+
{
65+
vsb.Append("null");
66+
}
6567
else
6668
{
67-
sb.Append(HexConverter.ToString(pkt, HexConverter.Casing.Lower));
69+
HexConverter.EncodeToUtf16(pkt, vsb.AppendSpan(pkt.Length * 2), HexConverter.Casing.Lower);
6870
}
6971
}
7072

7173
if (0 != (flags & AssemblyNameFlags.Retargetable))
72-
sb.Append(", Retargetable=Yes");
74+
vsb.Append(", Retargetable=Yes");
7375

7476
if (contentType == AssemblyContentType.WindowsRuntime)
75-
sb.Append(", ContentType=WindowsRuntime");
77+
vsb.Append(", ContentType=WindowsRuntime");
7678

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

79-
return sb.ToString();
81+
return vsb.ToString();
8082
}
8183

82-
private static void AppendQuoted(this StringBuilder sb, string s)
84+
private static void AppendQuoted(this ref ValueStringBuilder vsb, string s)
8385
{
8486
bool needsQuoting = false;
8587
const char quoteChar = '\"';
@@ -90,7 +92,7 @@ private static void AppendQuoted(this StringBuilder sb, string s)
9092
needsQuoting = true;
9193

9294
if (needsQuoting)
93-
sb.Append(quoteChar);
95+
vsb.Append(quoteChar);
9496

9597
for (int i = 0; i < s.Length; i++)
9698
{
@@ -101,24 +103,24 @@ private static void AppendQuoted(this StringBuilder sb, string s)
101103
case '=':
102104
case '\'':
103105
case '"':
104-
sb.Append('\\');
106+
vsb.Append('\\');
105107
break;
106108
case '\t':
107-
sb.Append("\\t");
109+
vsb.Append("\\t");
108110
continue;
109111
case '\r':
110-
sb.Append("\\r");
112+
vsb.Append("\\r");
111113
continue;
112114
case '\n':
113-
sb.Append("\\n");
115+
vsb.Append("\\n");
114116
continue;
115117
}
116118

117-
sb.Append(s[i]);
119+
vsb.Append(s[i]);
118120
}
119121

120122
if (needsQuoting)
121-
sb.Append(quoteChar);
123+
vsb.Append(quoteChar);
122124
}
123125

124126
private static Version CanonicalizeVersion(this Version version)

0 commit comments

Comments
 (0)