Skip to content

Commit d20a6bb

Browse files
committed
fix Utf8Formatter always assume invariant culture when formatting a floating-point whole number
1 parent b9c61b1 commit d20a6bb

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

Hexa.NET.Utilities.Tests/Utf8FormatterTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,40 @@ public unsafe void FormatDoubleTest(double value, int digit, string expected)
4949
// Assert
5050
Assert.That(Encoding.UTF8.GetString(utf8Span), Is.EqualTo(expected));
5151
}
52+
53+
[TestCase(10, 0, "en-US","10")]
54+
[TestCase(10, 0, "de-DE","10")]
55+
[TestCase(0.75f, 2, "en-US","0.75")]
56+
[TestCase(0.75f, 2, "de-DE","0,75")]
57+
public unsafe void FormatFloatCultureTest(float value, int digit, string cultureName, string expected)
58+
{
59+
// Arrange
60+
byte* buffer = stackalloc byte[128];
61+
CultureInfo culture = new CultureInfo(cultureName, false);
62+
63+
// Act
64+
int len = Utf8Formatter.Format(value, buffer, 128, culture, digit);
65+
ReadOnlySpan<byte> utf8Span = new ReadOnlySpan<byte>(buffer, len);
66+
67+
// Assert
68+
Assert.That(Encoding.UTF8.GetString(utf8Span), Is.EqualTo(expected));
69+
}
70+
71+
[TestCase(10, 0, "en-US","10")]
72+
[TestCase(10, 0, "de-DE","10")]
73+
[TestCase(0.75f, 2, "en-US","0.75")]
74+
[TestCase(0.75f, 2, "de-DE","0,75")]
75+
public unsafe void FormatDoubleCultureTest(double value, int digit, string cultureName, string expected)
76+
{
77+
// Arrange
78+
byte* buffer = stackalloc byte[128];
79+
CultureInfo culture = new CultureInfo(cultureName, false); // uses comma as decimal operator
80+
81+
// Act
82+
int len = Utf8Formatter.Format(value, buffer, 128, culture, digit);
83+
ReadOnlySpan<byte> utf8Span = new ReadOnlySpan<byte>(buffer, len);
84+
85+
// Assert
86+
Assert.That(Encoding.UTF8.GetString(utf8Span), Is.EqualTo(expected));
87+
}
5288
}

Hexa.NET.Utilities/Text/Utf8Formatter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ public static unsafe int Format(float value, byte* buffer, int bufSize, CultureI
327327
}
328328

329329
buffer += ConvertUtf16ToUtf8(format.CurrencyDecimalSeparator, buffer, (int)(end - buffer));
330+
byte separator = *(buffer - 1);
330331

331332
digits = digits >= 0 ? digits : 7;
332333

@@ -345,7 +346,7 @@ public static unsafe int Format(float value, byte* buffer, int bufSize, CultureI
345346
buffer--;
346347
}
347348

348-
while (*(buffer - 1) == '.')
349+
while (*(buffer - 1) == separator)
349350
{
350351
buffer--;
351352
}
@@ -410,6 +411,7 @@ public static unsafe int Format(double value, byte* buffer, int bufSize, Culture
410411
}
411412

412413
buffer += ConvertUtf16ToUtf8(format.CurrencyDecimalSeparator, buffer, (int)(end - buffer));
414+
byte separator = *(buffer - 1);
413415

414416
digits = digits >= 0 ? digits : 7;
415417

@@ -428,7 +430,7 @@ public static unsafe int Format(double value, byte* buffer, int bufSize, Culture
428430
buffer--;
429431
}
430432

431-
while (*(buffer - 1) == '.')
433+
while (*(buffer - 1) == separator)
432434
{
433435
buffer--;
434436
}

0 commit comments

Comments
 (0)