Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 5a0af05

Browse files
authored
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.
1 parent e80e040 commit 5a0af05

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

src/System.Private.CoreLib/shared/System/Globalization/DateTimeFormat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ internal static unsafe void FormatDigits(StringBuilder outputBuffer, int value,
209209

210210
private static void HebrewFormatDigits(StringBuilder outputBuffer, int digits)
211211
{
212-
outputBuffer.Append(HebrewNumber.ToString(digits));
212+
HebrewNumber.Append(outputBuffer, digits);
213213
}
214214

215215
internal static int ParseRepeatPattern(ReadOnlySpan<char> format, int pos, char patternChar)

src/System.Private.CoreLib/shared/System/Globalization/HebrewNumber.cs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,21 @@ internal enum HebrewNumberParsingState
5353
// Hebrew text and parsing Hebrew number text.
5454
//
5555
// Limitations:
56-
// Parse can only handles value 1 ~ 999.
57-
// ToString() can only handles 1 ~ 999. If value is greater than 5000,
56+
// Parse can only handle value 1 ~ 999.
57+
// Append() can only handle 1 ~ 999. If value is greater than 5000,
5858
// 5000 will be subtracted from the value.
5959
//
6060
////////////////////////////////////////////////////////////////////////////
6161

62-
internal class HebrewNumber
62+
internal static class HebrewNumber
6363
{
64-
// This class contains only static methods. Add a private ctor so that
65-
// compiler won't generate a default one for us.
66-
private HebrewNumber()
67-
{
68-
}
69-
7064
////////////////////////////////////////////////////////////////////////////
7165
//
72-
// ToString
66+
// Append
7367
//
7468
// Converts the given number to Hebrew letters according to the numeric
75-
// value of each Hebrew letter. Basically, this converts the lunar year
76-
// and the lunar month to letters.
69+
// value of each Hebrew letter, appending to the supplied StringBuilder.
70+
// Basically, this converts the lunar year and the lunar month to letters.
7771
//
7872
// The character of a year is described by three letters of the Hebrew
7973
// alphabet, the first and third giving, respectively, the days of the
@@ -87,13 +81,14 @@ private HebrewNumber()
8781
//
8882
////////////////////////////////////////////////////////////////////////////
8983

90-
internal static string ToString(int Number)
84+
internal static void Append(StringBuilder outputBuffer, int Number)
9185
{
86+
Debug.Assert(outputBuffer != null);
87+
int outputBufferStartingLength = outputBuffer.Length;
88+
9289
char cTens = '\x0';
9390
char cUnits; // tens and units chars
9491
int Hundreds, Tens; // hundreds and tens values
95-
StringBuilder szHebrew = new StringBuilder();
96-
9792

9893
//
9994
// Adjust the number if greater than 5000.
@@ -120,13 +115,13 @@ internal static string ToString(int Number)
120115
// If the number is greater than 400, use the multiples of 400.
121116
for (int i = 0; i < (Hundreds / 4); i++)
122117
{
123-
szHebrew.Append('\x05ea');
118+
outputBuffer.Append('\x05ea');
124119
}
125120

126121
int remains = Hundreds % 4;
127122
if (remains > 0)
128123
{
129-
szHebrew.Append((char)((int)'\x05e6' + remains));
124+
outputBuffer.Append((char)((int)'\x05e6' + remains));
130125
}
131126
}
132127

@@ -195,27 +190,22 @@ internal static string ToString(int Number)
195190

196191
if (cTens != '\x0')
197192
{
198-
szHebrew.Append(cTens);
193+
outputBuffer.Append(cTens);
199194
}
200195

201196
if (cUnits != '\x0')
202197
{
203-
szHebrew.Append(cUnits);
198+
outputBuffer.Append(cUnits);
204199
}
205200

206-
if (szHebrew.Length > 1)
201+
if (outputBuffer.Length - outputBufferStartingLength > 1)
207202
{
208-
szHebrew.Insert(szHebrew.Length - 1, '"');
203+
outputBuffer.Insert(outputBuffer.Length - 1, '"');
209204
}
210205
else
211206
{
212-
szHebrew.Append('\'');
207+
outputBuffer.Append('\'');
213208
}
214-
215-
//
216-
// Return success.
217-
//
218-
return (szHebrew.ToString());
219209
}
220210

221211
////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)