Skip to content

Commit dfd618d

Browse files
committed
Use interpolated strings in more places
1 parent 1446bdb commit dfd618d

File tree

254 files changed

+668
-976
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

254 files changed

+668
-976
lines changed

src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcFsStat.ParseMapModules.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ internal static partial class procfs
1515
{
1616
private const string MapsFileName = "/maps";
1717

18-
private static string GetMapsFilePathForProcess(int pid) =>
19-
RootPath + pid.ToString(CultureInfo.InvariantCulture) + MapsFileName;
18+
private static string GetMapsFilePathForProcess(int pid) => string.Create(null, stackalloc char[256], $"{RootPath}{(uint)pid}{MapsFileName}");
2019

2120
internal static ProcessModuleCollection? ParseMapsModules(int pid)
2221
{

src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcFsStat.TryReadStatusFile.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ internal struct ParsedStatus
3030
internal ulong VmPeak;
3131
}
3232

33-
internal static string GetStatusFilePathForProcess(int pid)
34-
{
35-
return RootPath + pid.ToString(CultureInfo.InvariantCulture) + StatusFileName;
36-
}
33+
internal static string GetStatusFilePathForProcess(int pid) => string.Create(null, stackalloc char[256], $"{RootPath}{(uint)pid}{StatusFileName}");
3734

3835
internal static bool TryReadStatusFile(int pid, out ParsedStatus result)
3936
{

src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcFsStat.cs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -76,41 +76,17 @@ internal struct ParsedStat
7676
//internal long cguest_time;
7777
}
7878

79-
internal static string GetExeFilePathForProcess(int pid)
80-
{
81-
return RootPath + pid.ToString(CultureInfo.InvariantCulture) + ExeFileName;
82-
}
79+
internal static string GetExeFilePathForProcess(int pid) => string.Create(null, stackalloc char[256], $"{RootPath}{(uint)pid}{ExeFileName}");
8380

84-
internal static string GetCmdLinePathForProcess(int pid)
85-
{
86-
return RootPath + pid.ToString(CultureInfo.InvariantCulture) + CmdLineFileName;
87-
}
81+
internal static string GetCmdLinePathForProcess(int pid) => string.Create(null, stackalloc char[256], $"{RootPath}{(uint)pid}{CmdLineFileName}");
8882

89-
internal static string GetStatFilePathForProcess(int pid)
90-
{
91-
return RootPath + pid.ToString(CultureInfo.InvariantCulture) + StatFileName;
92-
}
83+
internal static string GetStatFilePathForProcess(int pid) => string.Create(null, stackalloc char[256], $"{RootPath}{(uint)pid}{StatFileName}");
9384

94-
internal static string GetTaskDirectoryPathForProcess(int pid)
95-
{
96-
return RootPath + pid.ToString(CultureInfo.InvariantCulture) + TaskDirectoryName;
97-
}
85+
internal static string GetTaskDirectoryPathForProcess(int pid) => string.Create(null, stackalloc char[256], $"{RootPath}{(uint)pid}{TaskDirectoryName}");
9886

99-
internal static string GetFileDescriptorDirectoryPathForProcess(int pid)
100-
{
101-
return RootPath + pid.ToString(CultureInfo.InvariantCulture) + FileDescriptorDirectoryName;
102-
}
87+
internal static string GetFileDescriptorDirectoryPathForProcess(int pid) => string.Create(null, stackalloc char[256], $"{RootPath}{(uint)pid}{FileDescriptorDirectoryName}");
10388

104-
private static string GetStatFilePathForThread(int pid, int tid)
105-
{
106-
// Perf note: Calling GetTaskDirectoryPathForProcess will allocate a string,
107-
// which we then use in another Concat call to produce another string. The straightforward alternative,
108-
// though, since we have five input strings, is to use the string.Concat overload that takes a params array.
109-
// This results in allocating not only the params array but also a defensive copy inside of Concat,
110-
// which means allocating two five-element arrays. This two-string approach will result not only in fewer
111-
// allocations, but also typically in less memory allocated, and it's a bit more maintainable.
112-
return GetTaskDirectoryPathForProcess(pid) + tid.ToString(CultureInfo.InvariantCulture) + StatFileName;
113-
}
89+
private static string GetStatFilePathForThread(int pid, int tid) => string.Create(null, stackalloc char[256], $"{RootPath}{(uint)pid}{TaskDirectoryName}{(uint)tid}{StatFileName}");
11490

11591
internal static bool TryReadStatFile(int pid, out ParsedStat result)
11692
{

src/libraries/Common/src/Interop/Unix/System.Net.Security.Native/Interop.GssBuffer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal struct GssBuffer : IDisposable
1919
internal int Copy(byte[] destination, int offset)
2020
{
2121
Debug.Assert(destination != null, "target destination cannot be null");
22-
Debug.Assert((offset >= 0 && offset < destination.Length) || destination.Length == 0, "invalid offset " + offset);
22+
Debug.Assert((offset >= 0 && offset < destination.Length) || destination.Length == 0, $"invalid offset {offset}");
2323

2424
if (_data == IntPtr.Zero || _length == 0)
2525
{

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSsl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ internal static int Encrypt(SafeSslHandle context, ReadOnlySpan<byte> input, ref
295295
{
296296
#if DEBUG
297297
ulong assertNoError = Crypto.ErrPeekError();
298-
Debug.Assert(assertNoError == 0, "OpenSsl error queue is not empty, run: 'openssl errstr " + assertNoError.ToString("X") + "' for original error.");
298+
Debug.Assert(assertNoError == 0, $"OpenSsl error queue is not empty, run: 'openssl errstr {assertNoError:X}' for original error.");
299299
#endif
300300
errorCode = Ssl.SslErrorCode.SSL_ERROR_NONE;
301301

@@ -349,7 +349,7 @@ internal static int Decrypt(SafeSslHandle context, Span<byte> buffer, out Ssl.Ss
349349
{
350350
#if DEBUG
351351
ulong assertNoError = Crypto.ErrPeekError();
352-
Debug.Assert(assertNoError == 0, "OpenSsl error queue is not empty, run: 'openssl errstr " + assertNoError.ToString("X") + "' for original error.");
352+
Debug.Assert(assertNoError == 0, $"OpenSsl error queue is not empty, run: 'openssl errstr {assertNoError:X}' for original error.");
353353
#endif
354354
errorCode = Ssl.SslErrorCode.SSL_ERROR_NONE;
355355

src/libraries/Common/src/Interop/Windows/SspiCli/SecurityPackageInfoClass.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,7 @@ internal unsafe SecurityPackageInfoClass(SafeHandle safeHandle, int index)
5959
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, this.ToString());
6060
}
6161

62-
public override string ToString()
63-
{
64-
return "Capabilities:" + string.Format(CultureInfo.InvariantCulture, "0x{0:x}", Capabilities)
65-
+ " Version:" + Version.ToString(NumberFormatInfo.InvariantInfo)
66-
+ " RPCID:" + RPCID.ToString(NumberFormatInfo.InvariantInfo)
67-
+ " MaxToken:" + MaxToken.ToString(NumberFormatInfo.InvariantInfo)
68-
+ " Name:" + ((Name == null) ? "(null)" : Name)
69-
+ " Comment:" + ((Comment == null) ? "(null)" : Comment);
70-
}
62+
public override string ToString() =>
63+
$"Capabilities:0x{Capabilities:x} Version:{Version} RPCID:{RPCID} MaxToken:{MaxToken} Name:{Name ?? "(null)"} Comment: {Comment ?? "(null)"}";
7164
}
7265
}

src/libraries/Common/src/System/CodeDom/CodeTypeReference.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public string BaseType
327327

328328
string returnType = _baseType;
329329
return _needsFixup && TypeArguments.Count > 0 ?
330-
returnType + '`' + TypeArguments.Count.ToString(CultureInfo.InvariantCulture) :
330+
$"{returnType}`{(uint)TypeArguments.Count}" :
331331
returnType;
332332
}
333333
set

src/libraries/Common/src/System/Composition/Diagnostics/DebuggerTraceWriter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ private static string CreateLogMessage(TraceEventType eventType, CompositionTrac
5757
StringBuilder messageBuilder = new StringBuilder();
5858

5959
// Format taken from TraceListener.TraceEvent in .NET Framework
60-
messageBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0} {1}: {2} : ",
61-
s_sourceName, eventType.ToString(), (int)traceId);
60+
messageBuilder.Append($"{s_sourceName} {eventType}: {(int)traceId} : ");
6261

6362
if (arguments == null)
6463
{

src/libraries/Common/src/System/Data/Common/DbConnectionOptions.Common.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,11 +525,11 @@ private static void ParseComparison(Dictionary<string, string?> parsetable, stri
525525
}
526526
}
527527
}
528-
Debug.Assert(isEquivalent, "ParseInternal code vs regex message mismatch: <" + msg1 + "> <" + msg2 + ">");
528+
Debug.Assert(isEquivalent, $"ParseInternal code vs regex message mismatch: <{msg1}> <{msg2}>");
529529
}
530530
else
531531
{
532-
Debug.Fail("ParseInternal code vs regex throw mismatch " + f.Message);
532+
Debug.Fail($"ParseInternal code vs regex throw mismatch {f.Message}");
533533
}
534534
e = null;
535535
}

src/libraries/Common/src/System/Drawing/ColorTranslator.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,7 @@ public static string ToHtml(Color c)
386386
}
387387
else
388388
{
389-
colorString = "#" + c.R.ToString("X2", null) +
390-
c.G.ToString("X2", null) +
391-
c.B.ToString("X2", null);
389+
colorString = $"#{c.R:X2}{c.G:X2}{c.B:X2}";
392390
}
393391

394392
return colorString;

src/libraries/Common/src/System/IO/RowConfigReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private bool TryFindNextKeyOccurrence(string key, int startIndex, out int keyInd
132132
}
133133
// Check If the match is at the beginning of the string, or is preceded by a newline.
134134
else if (keyIndex == 0
135-
|| (keyIndex >= Environment.NewLine.Length && _buffer.Substring(keyIndex - Environment.NewLine.Length, Environment.NewLine.Length) == Environment.NewLine))
135+
|| (keyIndex >= Environment.NewLine.Length && _buffer.AsSpan(keyIndex - Environment.NewLine.Length, Environment.NewLine.Length).SequenceEqual(Environment.NewLine)))
136136
{
137137
// Check if the match is followed by whitespace, meaning it is not part of a larger word.
138138
if (HasFollowingWhitespace(keyIndex, key.Length))

src/libraries/Common/src/System/Net/Security/NegotiateStreamPal.Windows.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ internal static int VerifySignature(SafeDeleteContext securityContext, byte[] bu
212212
// throw if error
213213
if (errorCode != 0)
214214
{
215-
NetEventSource.Info($"VerifySignature threw error: {errorCode.ToString("x", NumberFormatInfo.InvariantInfo)}");
215+
NetEventSource.Info($"VerifySignature threw error: {errorCode:x}");
216216
throw new Win32Exception(errorCode);
217217
}
218218

@@ -256,7 +256,7 @@ internal static int MakeSignature(SafeDeleteContext securityContext, byte[] buff
256256
// throw if error
257257
if (errorCode != 0)
258258
{
259-
NetEventSource.Info($"MakeSignature threw error: {errorCode.ToString("x", NumberFormatInfo.InvariantInfo)}");
259+
NetEventSource.Info($"MakeSignature threw error: {errorCode:x}");
260260
throw new Win32Exception(errorCode);
261261
}
262262

src/libraries/Common/tests/StaticTestGenerator/Program.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -710,12 +710,12 @@ private static string EncodeLiteral(object? literal, Type? expectedType)
710710

711711
if (literal is IntPtr ptr)
712712
{
713-
return $"new IntPtr(0x{((long)ptr).ToString("X")})";
713+
return $"new IntPtr(0x{(long)ptr:X})";
714714
}
715715

716716
if (literal is UIntPtr uptr)
717717
{
718-
return $"new UIntPtr(0x{((ulong)uptr).ToString("X")})";
718+
return $"new UIntPtr(0x{(ulong)uptr:X})";
719719
}
720720

721721
string? result = null;
@@ -732,34 +732,34 @@ private static string EncodeLiteral(object? literal, Type? expectedType)
732732
result = ((bool)literal).ToString().ToLowerInvariant();
733733
break;
734734
case TypeCode.Char:
735-
result = $"'\\u{((int)(char)literal).ToString("X4")}'";
735+
result = $"'\\u{(int)(char)literal:X4}'";
736736
break;
737737
case TypeCode.SByte:
738-
result = $"(sbyte)({literal.ToString()})";
738+
result = $"(sbyte)({literal})";
739739
break;
740740
case TypeCode.Byte:
741-
result = $"(byte){literal.ToString()}";
741+
result = $"(byte){literal}";
742742
break;
743743
case TypeCode.Int16:
744-
result = $"(short)({literal.ToString()})";
744+
result = $"(short)({literal})";
745745
break;
746746
case TypeCode.UInt16:
747-
result = $"(ushort){literal.ToString()}";
747+
result = $"(ushort){literal}";
748748
break;
749749
case TypeCode.Int32:
750-
result = $"({literal.ToString()})";
750+
result = $"({literal})";
751751
break;
752752
case TypeCode.UInt32:
753-
result = $"{literal.ToString()}U";
753+
result = $"{literal}U";
754754
break;
755755
case TypeCode.Int64:
756-
result = $"({literal.ToString()}L)";
756+
result = $"({literal}L)";
757757
break;
758758
case TypeCode.UInt64:
759-
result = $"{literal.ToString()}UL";
759+
result = $"{literal}UL";
760760
break;
761761
case TypeCode.Decimal:
762-
result = $"({literal.ToString()}M)";
762+
result = $"({literal}M)";
763763
break;
764764
case TypeCode.Single:
765765
result =

src/libraries/Common/tests/System/Net/Http/HttpProtocolTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ await server.AcceptConnectionAsync(async connection =>
447447
{
448448
int bytesRemaining = expectedData.Length - bytesSent;
449449
int bytesToSend = rand.Next(1, Math.Min(bytesRemaining, maxChunkSize + 1));
450-
await connection.WriteStringAsync(bytesToSend.ToString("X") + lineEnding);
450+
await connection.WriteStringAsync($"{bytesToSend:X}{lineEnding}");
451451
await connection.Stream.WriteAsync(new Memory<byte>(expectedData, bytesSent, bytesToSend));
452452
await connection.WriteStringAsync(lineEnding);
453453
bytesSent += bytesToSend;

src/libraries/Common/tests/System/Security/Cryptography/ByteUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ internal static string ByteArrayToHex(this ReadOnlySpan<byte> bytes)
5555

5656
for (int i = 0; i < bytes.Length; i++)
5757
{
58-
builder.Append(bytes[i].ToString("X2"));
58+
builder.Append($"{bytes[i]:X2}");
5959
}
6060

6161
return builder.ToString();

src/libraries/Common/tests/System/Xml/XmlCoreTest/ManagedNodeWriter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,14 @@ public void WriteDocType(string name, string sysid, string pubid, string subset)
109109
if (pubid == null)
110110
{
111111
if (sysid != null)
112-
dt.Append(" SYSTEM " + sysid);
112+
dt.Append($" SYSTEM {sysid}");
113113
}
114114
else
115115
{
116-
dt.Append(" PUBLIC " + pubid);
116+
dt.Append($" PUBLIC {pubid}");
117117
if (sysid != null)
118118
{
119-
dt.Append(" " + sysid);
119+
dt.Append($" {sysid}");
120120
}
121121
}
122122

@@ -298,23 +298,23 @@ public void PutByte()
298298
/// </summary>
299299
public void PutCData()
300300
{
301-
_q.Append("<![CDATA[" + CDATA + _cCount++ + "]]>");
301+
_q.Append($"<![CDATA[{CDATA}{_cCount++}]]>");
302302
}
303303

304304
/// <summary>
305305
/// Writes out a PI Node.
306306
/// </summary>
307307
public void PutPI()
308308
{
309-
_q.Append("<?" + PI + _pCount++ + "?>");
309+
_q.Append($"<?{PI}{_pCount++}?>");
310310
}
311311

312312
/// <summary>
313313
/// Writes out a Comment Node.
314314
/// </summary>
315315
public void PutComment()
316316
{
317-
_q.Append("<!--" + COMMENT + _mCount++ + " -->");
317+
_q.Append($"<!--{COMMENT}{_mCount++} -->");
318318
}
319319

320320
/// <summary>

src/libraries/Common/tests/System/Xml/XmlDiff/XmlDiffDocument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,7 @@ public override void WriteTo(XmlWriter w)
15941594
w.WriteString(Value);
15951595
break;
15961596
default:
1597-
Debug.Assert(false, "Wrong type for text-like node : " + this._nodetype.ToString());
1597+
Debug.Assert(false, $"Wrong type for text-like node : {this._nodetype}");
15981598
break;
15991599
}
16001600
}

src/libraries/Common/tests/TestUtilities.Unicode/System/Text/Unicode/ParsedUnicodeData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private static Dictionary<int, string> ProcessDerivedNameFile()
151151
string baseName = value.PropName[..^1];
152152
for (int i = value.FirstCodePoint; i <= value.LastCodePoint /* inclusive */; i++)
153153
{
154-
dict.Add(i, baseName + i.ToString("X4", CultureInfo.InvariantCulture));
154+
dict.Add(i, $"{baseName}{i:X4}");
155155
}
156156
}
157157
}

src/libraries/Microsoft.CSharp/src/Microsoft.CSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@
247247
<Reference Include="System.Diagnostics.Tools" />
248248
<Reference Include="System.Linq" />
249249
<Reference Include="System.Linq.Expressions" />
250+
<Reference Include="System.Memory" />
250251
<Reference Include="System.ObjectModel" />
251252
<Reference Include="System.Reflection" />
252253
<Reference Include="System.Resources.ResourceManager" />

src/libraries/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/ComInterop/VariantArray.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ internal static Type GetStructType(int args)
8282
// See if we can find an existing type
8383
foreach (Type t in s_generatedTypes)
8484
{
85-
int arity = int.Parse(t.Name.Substring("VariantArray".Length), CultureInfo.InvariantCulture);
85+
int arity = int.Parse(t.Name.AsSpan("VariantArray".Length), provider: CultureInfo.InvariantCulture);
8686
if (size == arity)
8787
{
8888
return t;

src/libraries/Microsoft.Win32.Primitives/src/System/ComponentModel/Win32Exception.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ public override string ToString()
9191
: NativeErrorCode.ToString(CultureInfo.InvariantCulture);
9292
if (HResult == E_FAIL)
9393
{
94-
s.AppendFormat(CultureInfo.InvariantCulture, " ({0})", nativeErrorString);
94+
s.AppendFormat($" ({nativeErrorString})");
9595
}
9696
else
9797
{
98-
s.AppendFormat(CultureInfo.InvariantCulture, " ({0:X8}, {1})", HResult, nativeErrorString);
98+
s.AppendFormat($" ({HResult:X8}, {nativeErrorString})");
9999
}
100100

101101
if (!(string.IsNullOrEmpty(message)))

src/libraries/Microsoft.Win32.Registry/src/Microsoft/Win32/RegistryKey.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private void DeleteValueCore(string name, bool throwOnMissingValue)
173173
}
174174
// We really should throw an exception here if errorCode was bad,
175175
// but we can't for compatibility reasons.
176-
Debug.Assert(errorCode == 0, "RegDeleteValue failed. Here's your error code: " + errorCode);
176+
Debug.Assert(errorCode == 0, $"RegDeleteValue failed. Here's your error code: {errorCode}");
177177
}
178178

179179
/// <summary>

src/libraries/System.CodeDom/src/Microsoft/CSharp/CSharpCodeGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -793,12 +793,12 @@ private void AppendEscapedChar(StringBuilder b, char value)
793793
if (b == null)
794794
{
795795
Output.Write("\\u");
796-
Output.Write(((int)value).ToString("X4", CultureInfo.InvariantCulture));
796+
Output.Write(((int)value).ToString("X4"));
797797
}
798798
else
799799
{
800800
b.Append("\\u");
801-
b.Append(((int)value).ToString("X4", CultureInfo.InvariantCulture));
801+
b.Append(((int)value).ToString("X4"));
802802
}
803803
}
804804

@@ -2501,7 +2501,7 @@ private void GenerateChecksumPragma(CodeChecksumPragma checksumPragma)
25012501
{
25022502
foreach (byte b in checksumPragma.ChecksumData)
25032503
{
2504-
Output.Write(b.ToString("X2", CultureInfo.InvariantCulture));
2504+
Output.Write(b.ToString("X2"));
25052505
}
25062506
}
25072507
Output.WriteLine("\"");

src/libraries/System.CodeDom/src/Microsoft/VisualBasic/VBCodeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2273,7 +2273,7 @@ private void GenerateChecksumPragma(CodeChecksumPragma checksumPragma)
22732273
{
22742274
foreach (byte b in checksumPragma.ChecksumData)
22752275
{
2276-
Output.Write(b.ToString("X2", CultureInfo.InvariantCulture));
2276+
Output.Write(b.ToString("X2"));
22772277
}
22782278
}
22792279
Output.WriteLine("\")");

0 commit comments

Comments
 (0)