[libc-commits] [libc] 89cdaa8 - [libc] Fix printf %a padding issue
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Tue Aug 15 16:23:30 PDT 2023
Author: Michael Jones
Date: 2023-08-15T16:23:08-07:00
New Revision: 89cdaa8d49246cc5c797c5eb54a06a4fb09a07c5
URL: https://github.com/llvm/llvm-project/commit/89cdaa8d49246cc5c797c5eb54a06a4fb09a07c5
DIFF: https://github.com/llvm/llvm-project/commit/89cdaa8d49246cc5c797c5eb54a06a4fb09a07c5.diff
LOG: [libc] Fix printf %a padding issue
The trailing zeroes were previously not counted when calculating the
padding, which caused a high-precision number to get too much padding.
Reviewed By: sivachandra
Differential Revision: https://reviews.llvm.org/D157534
Added:
Modified:
libc/src/stdio/printf_core/float_hex_converter.h
libc/test/src/stdio/sprintf_test.cpp
Removed:
################################################################################
diff --git a/libc/src/stdio/printf_core/float_hex_converter.h b/libc/src/stdio/printf_core/float_hex_converter.h
index c19179884948dc..4bd0f91b68a7ba 100644
--- a/libc/src/stdio/printf_core/float_hex_converter.h
+++ b/libc/src/stdio/printf_core/float_hex_converter.h
@@ -204,7 +204,7 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,
constexpr int EXP_SEPERATOR_LEN = 1;
padding = static_cast<int>(to_conv.min_width - (sign_char > 0 ? 1 : 0) -
- PREFIX_LEN - mant_digits -
+ PREFIX_LEN - mant_digits - trailing_zeroes -
static_cast<int>(has_hexadecimal_point) -
EXP_SEPERATOR_LEN - (EXP_LEN - exp_cur));
if (padding < 0)
diff --git a/libc/test/src/stdio/sprintf_test.cpp b/libc/test/src/stdio/sprintf_test.cpp
index 98bda95d0fdab2..20c3c75376d5e6 100644
--- a/libc/test/src/stdio/sprintf_test.cpp
+++ b/libc/test/src/stdio/sprintf_test.cpp
@@ -865,6 +865,20 @@ TEST_F(LlvmLibcSPrintfTest, FloatHexExpConv) {
written = __llvm_libc::sprintf(buff, "%+-#12.3a % 012.3a", 0.1256, 1256.0);
ASSERT_STREQ_LEN(written, buff, "+0x1.014p-3 0x1.3a0p+10");
+
+ // These tests check that the padding is properly calculated based on the
+ // min_width field. Specifically, they check that the extra zeroes added by
+ // the high precision are accounted for correctly.
+ written = __llvm_libc::sprintf(buff, "%50.50a", 0x1.0p0);
+ ASSERT_STREQ_LEN(written, buff,
+ "0x1.00000000000000000000000000000000000000000000000000p+0");
+
+ // The
diff erence with this test is that the formatted number is exactly 57
+ // characters, so padding to 58 adds a space.
+ written = __llvm_libc::sprintf(buff, "%58.50a", 0x1.0p0);
+ ASSERT_STREQ_LEN(
+ written, buff,
+ " 0x1.00000000000000000000000000000000000000000000000000p+0");
}
TEST_F(LlvmLibcSPrintfTest, FloatDecimalConv) {
More information about the libc-commits
mailing list