Skip to content

Commit

Permalink
Replace some single-char strings with chars (#81831)
Browse files Browse the repository at this point in the history
* Replace some single-char strings with chars

Also a few other minor improvements noticed along the way.

* Update src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Windows.cs
  • Loading branch information
stephentoub committed Feb 13, 2023
1 parent f857468 commit a491daa
Show file tree
Hide file tree
Showing 13 changed files with 17 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MemberInfo, bool>? predicate = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal static IEnumerable<LibraryNameVariation> 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))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a491daa

Please sign in to comment.