diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index d8b5a1e87f9..e4ce6725700 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -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(p - buf); diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index d41b8181a45..55f411fdec4 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -1331,7 +1331,7 @@ inline size_t uint32_to_str(std::span 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(frac / divisor); frac %= divisor; diff --git a/tests/components/core/test_helpers.cpp b/tests/components/core/test_helpers.cpp index 68568982e44..5fb77ef7536 100644 --- a/tests/components/core/test_helpers.cpp +++ b/tests/components/core/test_helpers.cpp @@ -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 }