From 3b2f22481f5640d1ef6cef0a2b84040d3ffa2ffa Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 8 Feb 2023 10:03:08 -0500 Subject: [PATCH 1/2] Replace some single-char strings with chars Also a few other minor improvements noticed along the way. --- .../System/Reflection/Runtime/General/ThunkedApis.cs | 2 +- .../Runtime/MethodInfos/RuntimeMethodHelpers.cs | 2 +- .../Runtime/TypeInfos/RuntimeTypeInfo.GetMember.cs | 2 +- .../System/Diagnostics/LocalAppContextSwitches.cs | 12 +----------- .../src/System/Diagnostics/ProcessManager.Windows.cs | 2 +- .../src/System/Drawing/FontConverter.cs | 2 +- .../src/System/Net/ServiceNameStore.cs | 4 ++-- .../src/System/Net/Windows/HttpListener.Windows.cs | 4 ++-- .../src/System/AppContextConfigHelper.cs | 2 +- .../Runtime/Loader/LibraryNameVariation.Windows.cs | 2 +- .../src/Internal/SrgsParser/XmlParser.cs | 4 ++-- .../src/Internal/Synthesis/SSmlParser.cs | 2 +- .../src/System/RuntimeType.Mono.cs | 4 ++-- 13 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/ThunkedApis.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/ThunkedApis.cs index 092e91b63cb553..3e0765205c6373 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/ThunkedApis.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/General/ThunkedApis.cs @@ -210,7 +210,7 @@ private static void SplitTypeName(string fullname, out string name, out string n Debug.Assert(fullname != null); // Get namespace - int nsDelimiter = fullname.LastIndexOf(".", StringComparison.Ordinal); + int nsDelimiter = fullname.LastIndexOf('.'); if (nsDelimiter != -1) { ns = fullname.Substring(0, nsDelimiter); diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/MethodInfos/RuntimeMethodHelpers.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/MethodInfos/RuntimeMethodHelpers.cs index 9ece9ba048a5c6..13198f13979cf8 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/MethodInfos/RuntimeMethodHelpers.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/MethodInfos/RuntimeMethodHelpers.cs @@ -78,7 +78,7 @@ internal static string ComputeParametersString(RuntimeParameterInfo[] parameters // Legacy: Why use "ByRef" for by ref parameters? What language is this? // VB uses "ByRef" but it should precede (not follow) the parameter name. // Why don't we just use "&"? - if (parameterTypeString.EndsWith("&")) + if (parameterTypeString.EndsWith('&')) { sb.Append(parameterTypeString, 0, parameterTypeString.Length - 1); sb.Append(" ByRef"); diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/TypeInfos/RuntimeTypeInfo.GetMember.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/TypeInfos/RuntimeTypeInfo.GetMember.cs index d0c82928f1928f..e40e38faf10ea9 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/TypeInfos/RuntimeTypeInfo.GetMember.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Runtime/TypeInfos/RuntimeTypeInfo.GetMember.cs @@ -29,7 +29,7 @@ public sealed override MemberInfo[] GetMember(string name, MemberTypes type, Bin private MemberInfo[] GetMemberImpl(string optionalNameOrPrefix, MemberTypes type, BindingFlags bindingAttr) { - bool prefixSearch = optionalNameOrPrefix != null && optionalNameOrPrefix.EndsWith("*", StringComparison.Ordinal); + bool prefixSearch = optionalNameOrPrefix != null && optionalNameOrPrefix.EndsWith('*'); string? optionalName = prefixSearch ? null : optionalNameOrPrefix; Func? predicate = null; diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/LocalAppContextSwitches.cs b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/LocalAppContextSwitches.cs index 1da5480d9d70ef..64fd325e8df25a 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/LocalAppContextSwitches.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/LocalAppContextSwitches.cs @@ -18,21 +18,11 @@ private static bool InitializeDefaultActivityIdFormat() string? switchValue = Environment.GetEnvironmentVariable("DOTNET_SYSTEM_DIAGNOSTICS_DEFAULTACTIVITYIDFORMATISHIERARCHIAL"); if (switchValue != null) { - defaultActivityIdFormatIsHierarchial = IsTrueStringIgnoreCase(switchValue) || switchValue.Equals("1"); + defaultActivityIdFormatIsHierarchial = switchValue.Equals("true", StringComparison.OrdinalIgnoreCase) || switchValue.Equals("1"); } } return defaultActivityIdFormatIsHierarchial; } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static bool IsTrueStringIgnoreCase(string value) - { - return value.Length == 4 && - (value[0] == 't' || value[0] == 'T') && - (value[1] == 'r' || value[1] == 'R') && - (value[2] == 'u' || value[2] == 'U') && - (value[3] == 'e' || value[3] == 'E'); - } } } diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs index d0eb17f5732672..2a30091c73371a 100644 --- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs +++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs @@ -527,7 +527,7 @@ private static ProcessInfo[] GetProcessInfos(PerformanceCounterLib library, int // at the end. If instanceName ends in ".", ".e", or ".ex" we remove it. if (instanceName.Length == 15) { - if (instanceName.EndsWith(".", StringComparison.Ordinal)) + if (instanceName.EndsWith('.')) { instanceName = instanceName.Slice(0, 14); } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/FontConverter.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/FontConverter.cs index 1e2125a60a32ca..3897358d9e37e0 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/FontConverter.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/FontConverter.cs @@ -38,7 +38,7 @@ public override bool CanConvertTo(ITypeDescriptorContext? context, [NotNullWhen( ValueStringBuilder sb = default; sb.Append(font.Name); sb.Append(culture.TextInfo.ListSeparator[0]); - sb.Append(" "); + sb.Append(' '); sb.Append(font.Size.ToString(culture.NumberFormat)); switch (font.Unit) diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/ServiceNameStore.cs b/src/libraries/System.Net.HttpListener/src/System/Net/ServiceNameStore.cs index 5decfe72c1aab0..6656dc49cbcca1 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/ServiceNameStore.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/ServiceNameStore.cs @@ -270,8 +270,8 @@ public static string[] BuildServiceNames(string uriPrefix) { string hostname = ExtractHostname(uriPrefix, true)!; - if (string.Equals(hostname, "*", StringComparison.OrdinalIgnoreCase) || - string.Equals(hostname, "+", StringComparison.OrdinalIgnoreCase) || + if (hostname == "*" || + hostname == "+" || IPAddress.TryParse(hostname, out _)) { // for a wildcard, register the machine name. If the caller doesn't have DNS permission diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListener.Windows.cs b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListener.Windows.cs index 91f25c2f1a85e2..5251c414d8ec4a 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListener.Windows.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListener.Windows.cs @@ -1579,8 +1579,8 @@ private static void SendError(HttpListenerSession session, ulong requestId, Http httpResponse.pReason = (sbyte*)pReason; httpResponse.ReasonLength = (ushort)byteReason.Length; - byte[] byteContentLength = Encoding.Default.GetBytes("0"); - fixed (byte* pContentLength = &byteContentLength[0]) + ReadOnlySpan byteContentLength = "0"u8; + fixed (byte* pContentLength = byteContentLength) { (&httpResponse.Headers.KnownHeaders)[(int)HttpResponseHeader.ContentLength].pRawValue = (sbyte*)pContentLength; (&httpResponse.Headers.KnownHeaders)[(int)HttpResponseHeader.ContentLength].RawValueLength = (ushort)byteContentLength.Length; diff --git a/src/libraries/System.Private.CoreLib/src/System/AppContextConfigHelper.cs b/src/libraries/System.Private.CoreLib/src/System/AppContextConfigHelper.cs index 712f8973ca6974..3a8c3e8e7ea72d 100644 --- a/src/libraries/System.Private.CoreLib/src/System/AppContextConfigHelper.cs +++ b/src/libraries/System.Private.CoreLib/src/System/AppContextConfigHelper.cs @@ -88,7 +88,7 @@ internal static short GetInt16Config(string configName, short defaultValue, bool { result = Convert.ToInt16(str, 16); } - else if (str.StartsWith("0")) + else if (str.StartsWith('0')) { result = Convert.ToInt16(str, 8); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Loader/LibraryNameVariation.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Loader/LibraryNameVariation.Windows.cs index d320b03d759963..fe1782f0fc32fe 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/Loader/LibraryNameVariation.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Loader/LibraryNameVariation.Windows.cs @@ -16,7 +16,7 @@ internal static IEnumerable DetermineLibraryNameVariations yield return new LibraryNameVariation(string.Empty, string.Empty); if (isRelativePath - && !libName.EndsWith(".", StringComparison.OrdinalIgnoreCase) + && !libName.EndsWith('.') && !libName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) && !libName.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) { diff --git a/src/libraries/System.Speech/src/Internal/SrgsParser/XmlParser.cs b/src/libraries/System.Speech/src/Internal/SrgsParser/XmlParser.cs index 8be029c4d4b146..79c87dfa6d34b0 100644 --- a/src/libraries/System.Speech/src/Internal/SrgsParser/XmlParser.cs +++ b/src/libraries/System.Speech/src/Internal/SrgsParser/XmlParser.cs @@ -361,7 +361,7 @@ private void ParseGrammar(XmlReader reader, IGrammar grammar) catch (ArgumentException) { // Unknown Culture info, fall back to the base culture. - int pos = reader.Value.IndexOf("-", StringComparison.Ordinal); + int pos = reader.Value.IndexOf('-'); if (pos > 0) { grammar.Culture = _langId = new CultureInfo(reader.Value.Substring(0, pos)); @@ -1767,7 +1767,7 @@ private static void SetRepeatValues(string repeat, out int minRepeat, out int ma minRepeat = maxRepeat = 1; if (!string.IsNullOrEmpty(repeat)) { - int sep = repeat.IndexOf("-", StringComparison.Ordinal); + int sep = repeat.IndexOf('-'); if (sep < 0) { diff --git a/src/libraries/System.Speech/src/Internal/Synthesis/SSmlParser.cs b/src/libraries/System.Speech/src/Internal/Synthesis/SSmlParser.cs index f8122222920f04..1cc7e718a5e729 100644 --- a/src/libraries/System.Speech/src/Internal/Synthesis/SSmlParser.cs +++ b/src/libraries/System.Speech/src/Internal/Synthesis/SSmlParser.cs @@ -128,7 +128,7 @@ private static void ProcessSpeakElement(XmlReader reader, ISsmlParser engine, ob { innerException = e; // Unknown Culture info, fall back to the base culture. - int pos = reader.Value.IndexOf("-", StringComparison.Ordinal); + int pos = reader.Value.IndexOf('-'); if (pos > 0) { try diff --git a/src/mono/System.Private.CoreLib/src/System/RuntimeType.Mono.cs b/src/mono/System.Private.CoreLib/src/System/RuntimeType.Mono.cs index 106879b21af80a..6201c5d610bd29 100644 --- a/src/mono/System.Private.CoreLib/src/System/RuntimeType.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/RuntimeType.Mono.cs @@ -162,7 +162,7 @@ private static void SplitName(string? fullname, out string? name, out string? ns return; // Get namespace - int nsDelimiter = fullname.LastIndexOf(".", StringComparison.Ordinal); + int nsDelimiter = fullname.LastIndexOf('.'); if (nsDelimiter != -1) { ns = fullname.Substring(0, nsDelimiter); @@ -236,7 +236,7 @@ private static void FilterHelper( listType = MemberListType.CaseSensitive; } - if (allowPrefixLookup && name.EndsWith("*", StringComparison.Ordinal)) + if (allowPrefixLookup && name.EndsWith('*')) { // We set prefixLookup to true if name ends with a "*". // We will also set listType to All so that all members are included in From 7ac9f4ea498816180924c94d7834be11d87ff207 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 8 Feb 2023 11:11:32 -0500 Subject: [PATCH 2/2] Update src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs --- .../src/System/Diagnostics/ProcessManager.Windows.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs index 2a30091c73371a..8c5614e5a0d01c 100644 --- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs +++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs @@ -527,7 +527,7 @@ private static ProcessInfo[] GetProcessInfos(PerformanceCounterLib library, int // at the end. If instanceName ends in ".", ".e", or ".ex" we remove it. if (instanceName.Length == 15) { - if (instanceName.EndsWith('.')) + if (instanceName.EndsWith(".")) { instanceName = instanceName.Slice(0, 14); }