This commit is contained in:
J. Nick Koston
2026-04-14 08:09:00 -10:00
parent f4f56cfaaa
commit 57d9e508ea
3 changed files with 11 additions and 11 deletions
+1 -1
View File
@@ -559,7 +559,7 @@ static size_t value_accuracy_to_buf_fast(char *buf, float value, int8_t accuracy
p = uint32_to_str_unchecked(p, scaled / mult);
if (accuracy_decimals > 0) {
*p++ = '.';
p = frac_to_str_(p, scaled % mult, mult / 10);
p = frac_to_str_unchecked(p, scaled % mult, mult / 10);
}
*p = '\0';
return static_cast<size_t>(p - buf);
+1 -1
View File
@@ -1331,7 +1331,7 @@ inline size_t uint32_to_str(std::span<char, UINT32_MAX_STR_SIZE> buf, uint32_t v
/// Write fractional digits with leading zeros to buffer (internal, no size check).
/// frac is the fractional value, divisor is the highest place value (e.g. 100 for 3 digits).
/// Returns pointer past last char written.
inline char *frac_to_str_(char *buf, uint32_t frac, uint32_t divisor) {
inline char *frac_to_str_unchecked(char *buf, uint32_t frac, uint32_t divisor) {
while (divisor > 0) {
*buf++ = '0' + static_cast<char>(frac / divisor);
frac %= divisor;
+9 -9
View File
@@ -124,11 +124,11 @@ TEST(SmallPow10, One) { EXPECT_EQ(small_pow10(1), 10u); }
TEST(SmallPow10, Two) { EXPECT_EQ(small_pow10(2), 100u); }
TEST(SmallPow10, Three) { EXPECT_EQ(small_pow10(3), 1000u); }
// --- frac_to_str_() ---
// --- frac_to_str_unchecked() ---
TEST(FracToStr, OneDigit) {
char buf[8];
char *end = frac_to_str_(buf, 5, 1);
char *end = frac_to_str_unchecked(buf, 5, 1);
*end = '\0';
EXPECT_STREQ(buf, "5");
EXPECT_EQ(end - buf, 1);
@@ -136,14 +136,14 @@ TEST(FracToStr, OneDigit) {
TEST(FracToStr, TwoDigits) {
char buf[8];
char *end = frac_to_str_(buf, 46, 10);
char *end = frac_to_str_unchecked(buf, 46, 10);
*end = '\0';
EXPECT_STREQ(buf, "46");
}
TEST(FracToStr, ThreeDigits) {
char buf[8];
char *end = frac_to_str_(buf, 456, 100);
char *end = frac_to_str_unchecked(buf, 456, 100);
*end = '\0';
EXPECT_STREQ(buf, "456");
EXPECT_EQ(end - buf, 3);
@@ -151,22 +151,22 @@ TEST(FracToStr, ThreeDigits) {
TEST(FracToStr, LeadingZeros) {
char buf[8];
char *end = frac_to_str_(buf, 1, 100);
char *end = frac_to_str_unchecked(buf, 1, 100);
*end = '\0';
EXPECT_STREQ(buf, "001");
end = frac_to_str_(buf, 5, 10);
end = frac_to_str_unchecked(buf, 5, 10);
*end = '\0';
EXPECT_STREQ(buf, "05");
}
TEST(FracToStr, AllZeros) {
char buf[8];
char *end = frac_to_str_(buf, 0, 100);
char *end = frac_to_str_unchecked(buf, 0, 100);
*end = '\0';
EXPECT_STREQ(buf, "000");
end = frac_to_str_(buf, 0, 1);
end = frac_to_str_unchecked(buf, 0, 1);
*end = '\0';
EXPECT_STREQ(buf, "0");
}
@@ -174,7 +174,7 @@ TEST(FracToStr, AllZeros) {
TEST(FracToStr, ZeroDivisor) {
char buf[8];
buf[0] = 'X';
char *end = frac_to_str_(buf, 0, 0);
char *end = frac_to_str_unchecked(buf, 0, 0);
EXPECT_EQ(end, buf); // writes nothing
}