From 152887154edafaa389cda34c5bfd1e8b0bd52a21 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Wed, 19 Oct 2022 22:18:24 +0100 Subject: [PATCH] Remove some static initializations in `StringManipulationHelper` (#18243) --- .../FormatAndOutput/common/ComplexWriter.cs | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/common/ComplexWriter.cs b/src/System.Management.Automation/FormatAndOutput/common/ComplexWriter.cs index 0ffc713cb9..099bec43dc 100644 --- a/src/System.Management.Automation/FormatAndOutput/common/ComplexWriter.cs +++ b/src/System.Management.Automation/FormatAndOutput/common/ComplexWriter.cs @@ -327,9 +327,10 @@ namespace Microsoft.PowerShell.Commands.Internal.Format /// internal sealed class StringManipulationHelper { - private static readonly char s_softHyphen = '\u00AD'; - private static readonly char s_hardHyphen = '\u2011'; - private static readonly char s_nonBreakingSpace = '\u00A0'; + private const char SoftHyphen = '\u00AD'; + private const char HardHyphen = '\u2011'; + private const char NonBreakingSpace = '\u00A0'; + private static readonly Collection s_cultureCollection = new Collection(); static StringManipulationHelper() @@ -377,13 +378,13 @@ namespace Microsoft.PowerShell.Commands.Internal.Format } string delimiter = null; - if (s[i] == ' ' || s[i] == '\t' || s[i] == s_softHyphen) + if (s[i] is ' ' or '\t' or SoftHyphen) { // Soft hyphen = \u00AD - Should break, and add a hyphen if needed. // If not needed for a break, hyphen should be absent. delimiter = new string(s[i], 1); } - else if (s[i] == s_hardHyphen || s[i] == s_nonBreakingSpace) + else if (s[i] is HardHyphen or NonBreakingSpace) { // Non-breaking space = \u00A0 - ideally shouldn't wrap. // Hard hyphen = \u2011 - Should not break. @@ -596,9 +597,9 @@ namespace Microsoft.PowerShell.Commands.Internal.Format string suffix = null; // Handle soft hyphen - if (word.Delim.Length == 1 && word.Delim[0] == s_softHyphen) + if (word.Delim.Length == 1 && word.Delim[0] is SoftHyphen) { - int wordWidthWithHyphen = displayCells.Length(wordToAdd) + displayCells.Length(s_softHyphen); + int wordWidthWithHyphen = displayCells.Length(wordToAdd) + displayCells.Length(SoftHyphen); // Add hyphen only if necessary if (wordWidthWithHyphen == spacesLeft) @@ -813,7 +814,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format return string.Empty; } - int lineBreak = s.IndexOfAny(s_lineBreakChars); + int lineBreak = s.AsSpan().IndexOfAny('\n', '\r'); if (lineBreak < 0) { @@ -827,8 +828,5 @@ namespace Microsoft.PowerShell.Commands.Internal.Format { return StringUtil.Padding(count) + val; } - - private static readonly char[] s_newLineChar = new char[] { '\n' }; - private static readonly char[] s_lineBreakChars = new char[] { '\n', '\r' }; } }