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

Commit a8724a2

Browse files
justinvpsafern
authored andcommitted
Version: Use int.TryFormat & StringBuilder.Append(int) (#15132)
Now that `int.TryFormat` is available, along with the more efficient `StringBuilder.Append(int)`, use them in `Version`. Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
1 parent 258e971 commit a8724a2

File tree

1 file changed

+17
-37
lines changed

1 file changed

+17
-37
lines changed

src/Common/src/CoreLib/System/Version.cs

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,10 @@ public bool TryFormat(Span<char> destination, int fieldCount, out int charsWritt
210210
charsWritten = 0;
211211
return true;
212212
}
213-
214-
// TODO https://github.com/dotnet/corefx/issues/22403: fieldCount==1 can just use int.TryFormat
213+
else if (fieldCount == 1)
214+
{
215+
return _Major.TryFormat(destination, out charsWritten);
216+
}
215217

216218
StringBuilder sb = ToCachedStringBuilder(fieldCount);
217219
if (sb.Length <= destination.Length)
@@ -240,18 +242,15 @@ bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, st
240242

241243
private StringBuilder ToCachedStringBuilder(int fieldCount)
242244
{
243-
if (fieldCount == 1)
244-
{
245-
StringBuilder sb = StringBuilderCache.Acquire();
246-
AppendNonNegativeNumber(_Major, sb);
247-
return sb;
248-
}
249-
else if (fieldCount == 2)
245+
// Note: As we always have positive numbers then it is safe to convert the number to string
246+
// regardless of the current culture as we'll not have any punctuation marks in the number.
247+
248+
if (fieldCount == 2)
250249
{
251250
StringBuilder sb = StringBuilderCache.Acquire();
252-
AppendNonNegativeNumber(_Major, sb);
251+
sb.Append(_Major);
253252
sb.Append('.');
254-
AppendNonNegativeNumber(_Minor, sb);
253+
sb.Append(_Minor);
255254
return sb;
256255
}
257256
else
@@ -264,11 +263,11 @@ private StringBuilder ToCachedStringBuilder(int fieldCount)
264263
if (fieldCount == 3)
265264
{
266265
StringBuilder sb = StringBuilderCache.Acquire();
267-
AppendNonNegativeNumber(_Major, sb);
266+
sb.Append(_Major);
268267
sb.Append('.');
269-
AppendNonNegativeNumber(_Minor, sb);
268+
sb.Append(_Minor);
270269
sb.Append('.');
271-
AppendNonNegativeNumber(_Build, sb);
270+
sb.Append(_Build);
272271
return sb;
273272
}
274273

@@ -280,39 +279,20 @@ private StringBuilder ToCachedStringBuilder(int fieldCount)
280279
if (fieldCount == 4)
281280
{
282281
StringBuilder sb = StringBuilderCache.Acquire();
283-
AppendNonNegativeNumber(_Major, sb);
282+
sb.Append(_Major);
284283
sb.Append('.');
285-
AppendNonNegativeNumber(_Minor, sb);
284+
sb.Append(_Minor);
286285
sb.Append('.');
287-
AppendNonNegativeNumber(_Build, sb);
286+
sb.Append(_Build);
288287
sb.Append('.');
289-
AppendNonNegativeNumber(_Revision, sb);
288+
sb.Append(_Revision);
290289
return sb;
291290
}
292291

293292
throw new ArgumentException(SR.Format(SR.ArgumentOutOfRange_Bounds_Lower_Upper, "0", "4"), nameof(fieldCount));
294293
}
295294
}
296295

297-
// TODO https://github.com/dotnet/corefx/issues/22616:
298-
// Use StringBuilder.Append(int) once it's been updated to use spans internally.
299-
//
300-
// AppendNonNegativeNumber is an optimization to append a number to a StringBuilder object without
301-
// doing any boxing and not even creating intermediate string.
302-
// Note: as we always have positive numbers then it is safe to convert the number to string
303-
// regardless of the current culture as we'll not have any punctuation marks in the number
304-
private static void AppendNonNegativeNumber(int num, StringBuilder sb)
305-
{
306-
Debug.Assert(num >= 0, "AppendPositiveNumber expect positive numbers");
307-
308-
int index = sb.Length;
309-
do
310-
{
311-
num = Math.DivRem(num, 10, out int remainder);
312-
sb.Insert(index, (char)('0' + remainder));
313-
} while (num > 0);
314-
}
315-
316296
public static Version Parse(string input)
317297
{
318298
if (input == null)

0 commit comments

Comments
 (0)