diff options
author | Stephen Toub <stoub@microsoft.com> | 2018-11-20 15:59:37 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-20 15:59:37 -0500 |
commit | 5a0af055e4c39f4f49b7ff35141739dc3fda4f9f (patch) | |
tree | 8953c3679d569943b662e9ca4927fd479da59b04 /src | |
parent | e80e04020c3281ec675817c4fff025a3e347353e (diff) | |
download | coreclr-5a0af055e4c39f4f49b7ff35141739dc3fda4f9f.tar.gz coreclr-5a0af055e4c39f4f49b7ff35141739dc3fda4f9f.tar.bz2 coreclr-5a0af055e4c39f4f49b7ff35141739dc3fda4f9f.zip |
Remove StringBuilder from HebrewNumber formatting (#21122)
Rather than allocating a new StringBuilder, writing into that, getting its string, and then appending that string to another StringBuilder, we can just write directly to the original one.
Diffstat (limited to 'src')
-rw-r--r-- | src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormat.cs | 2 | ||||
-rw-r--r-- | src/System.Private.CoreLib/shared/System/Globalization/HebrewNumber.cs | 44 |
2 files changed, 18 insertions, 28 deletions
diff --git a/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormat.cs b/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormat.cs index 9d29afbe2c..b0c6342800 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormat.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormat.cs @@ -209,7 +209,7 @@ namespace System private static void HebrewFormatDigits(StringBuilder outputBuffer, int digits) { - outputBuffer.Append(HebrewNumber.ToString(digits)); + HebrewNumber.Append(outputBuffer, digits); } internal static int ParseRepeatPattern(ReadOnlySpan<char> format, int pos, char patternChar) diff --git a/src/System.Private.CoreLib/shared/System/Globalization/HebrewNumber.cs b/src/System.Private.CoreLib/shared/System/Globalization/HebrewNumber.cs index 4413cd9fa0..db9b379041 100644 --- a/src/System.Private.CoreLib/shared/System/Globalization/HebrewNumber.cs +++ b/src/System.Private.CoreLib/shared/System/Globalization/HebrewNumber.cs @@ -53,27 +53,21 @@ namespace System.Globalization // Hebrew text and parsing Hebrew number text. // // Limitations: - // Parse can only handles value 1 ~ 999. - // ToString() can only handles 1 ~ 999. If value is greater than 5000, + // Parse can only handle value 1 ~ 999. + // Append() can only handle 1 ~ 999. If value is greater than 5000, // 5000 will be subtracted from the value. // //////////////////////////////////////////////////////////////////////////// - internal class HebrewNumber + internal static class HebrewNumber { - // This class contains only static methods. Add a private ctor so that - // compiler won't generate a default one for us. - private HebrewNumber() - { - } - //////////////////////////////////////////////////////////////////////////// // - // ToString + // Append // // Converts the given number to Hebrew letters according to the numeric - // value of each Hebrew letter. Basically, this converts the lunar year - // and the lunar month to letters. + // value of each Hebrew letter, appending to the supplied StringBuilder. + // Basically, this converts the lunar year and the lunar month to letters. // // The character of a year is described by three letters of the Hebrew // alphabet, the first and third giving, respectively, the days of the @@ -87,13 +81,14 @@ namespace System.Globalization // //////////////////////////////////////////////////////////////////////////// - internal static string ToString(int Number) + internal static void Append(StringBuilder outputBuffer, int Number) { + Debug.Assert(outputBuffer != null); + int outputBufferStartingLength = outputBuffer.Length; + char cTens = '\x0'; char cUnits; // tens and units chars int Hundreds, Tens; // hundreds and tens values - StringBuilder szHebrew = new StringBuilder(); - // // Adjust the number if greater than 5000. @@ -120,13 +115,13 @@ namespace System.Globalization // If the number is greater than 400, use the multiples of 400. for (int i = 0; i < (Hundreds / 4); i++) { - szHebrew.Append('\x05ea'); + outputBuffer.Append('\x05ea'); } int remains = Hundreds % 4; if (remains > 0) { - szHebrew.Append((char)((int)'\x05e6' + remains)); + outputBuffer.Append((char)((int)'\x05e6' + remains)); } } @@ -195,27 +190,22 @@ namespace System.Globalization if (cTens != '\x0') { - szHebrew.Append(cTens); + outputBuffer.Append(cTens); } if (cUnits != '\x0') { - szHebrew.Append(cUnits); + outputBuffer.Append(cUnits); } - if (szHebrew.Length > 1) + if (outputBuffer.Length - outputBufferStartingLength > 1) { - szHebrew.Insert(szHebrew.Length - 1, '"'); + outputBuffer.Insert(outputBuffer.Length - 1, '"'); } else { - szHebrew.Append('\''); + outputBuffer.Append('\''); } - - // - // Return success. - // - return (szHebrew.ToString()); } //////////////////////////////////////////////////////////////////////////// |