Skip to content

Commit b9c61b1

Browse files
committed
fix Utf8Formatter mistakenly removing zeros when formatting a floating-point whole number
The prev logic would format floating-point value of 10 as "1". To fix this, the removal of trailing zeros and the decimal separator should be handled separately.
1 parent 08e1ab7 commit b9c61b1

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Globalization;
2+
using System.Text;
3+
using Hexa.NET.Utilities.Text;
4+
5+
namespace Hexa.NET.Utilities.Tests;
6+
7+
public class Utf8FormatterTests
8+
{
9+
[TestCase(1f, 0, "1")]
10+
[TestCase(10f, 0, "10")]
11+
[TestCase(0.5f, 1, "0.5")]
12+
[TestCase(0.75f, 2, "0.75")]
13+
[TestCase(0.125f, 3, "0.125")]
14+
[TestCase(0.0625f, 4, "0.0625")]
15+
[TestCase(0.03125f, 5, "0.03125")]
16+
[TestCase(0.015625f, 6, "0.015625")]
17+
[TestCase(0.0078125f, 7, "0.0078125")]
18+
public unsafe void FormatFloatTest(float value, int digit, string expected)
19+
{
20+
// Arrange
21+
byte* buffer = stackalloc byte[128];
22+
23+
// Act
24+
int len = Utf8Formatter.Format(value, buffer, 128, CultureInfo.InvariantCulture, digit);
25+
ReadOnlySpan<byte> utf8Span = new ReadOnlySpan<byte>(buffer, len);
26+
27+
// Assert
28+
Assert.That(Encoding.UTF8.GetString(utf8Span), Is.EqualTo(expected));
29+
}
30+
31+
[TestCase(1, 0, "1")]
32+
[TestCase(10, 0, "10")]
33+
[TestCase(0.5, 1, "0.5")]
34+
[TestCase(0.75, 2, "0.75")]
35+
[TestCase(0.125, 3, "0.125")]
36+
[TestCase(0.0625, 4, "0.0625")]
37+
[TestCase(0.03125, 5, "0.03125")]
38+
[TestCase(0.015625, 6, "0.015625")]
39+
[TestCase(0.0078125, 7, "0.0078125")]
40+
public unsafe void FormatDoubleTest(double value, int digit, string expected)
41+
{
42+
// Arrange
43+
byte* buffer = stackalloc byte[128];
44+
45+
// Act
46+
int len = Utf8Formatter.Format(value, buffer, 128, CultureInfo.InvariantCulture, digit);
47+
ReadOnlySpan<byte> utf8Span = new ReadOnlySpan<byte>(buffer, len);
48+
49+
// Assert
50+
Assert.That(Encoding.UTF8.GetString(utf8Span), Is.EqualTo(expected));
51+
}
52+
}

Hexa.NET.Utilities/Text/Utf8Formatter.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,12 @@ public static unsafe int Format(float value, byte* buffer, int bufSize, CultureI
340340
if (fraction < 1e-14) break;
341341
}
342342

343-
while (*(buffer - 1) == '0' || *(buffer - 1) == '.')
343+
while (*(buffer - 1) == '0')
344+
{
345+
buffer--;
346+
}
347+
348+
while (*(buffer - 1) == '.')
344349
{
345350
buffer--;
346351
}
@@ -418,7 +423,12 @@ public static unsafe int Format(double value, byte* buffer, int bufSize, Culture
418423
if (fraction < 1e-14) break;
419424
}
420425

421-
while (*(buffer - 1) == '0' || *(buffer - 1) == '.')
426+
while (*(buffer - 1) == '0')
427+
{
428+
buffer--;
429+
}
430+
431+
while (*(buffer - 1) == '.')
422432
{
423433
buffer--;
424434
}

0 commit comments

Comments
 (0)