diff options
author | Marco Paland <marco@paland.com> | 2018-09-14 13:16:22 +0200 |
---|---|---|
committer | Marco Paland <marco@paland.com> | 2018-09-14 13:16:22 +0200 |
commit | f8a2be378d6df840ee16e0019435cadc659348c1 (patch) | |
tree | 7706c73f457561dcf1e12a15ff239e72e6d8f831 | |
parent | be3047911075b2e917d73451068e0b84373eefb9 (diff) | |
download | printf-f8a2be378d6df840ee16e0019435cadc659348c1.tar.gz printf-f8a2be378d6df840ee16e0019435cadc659348c1.tar.bz2 printf-f8a2be378d6df840ee16e0019435cadc659348c1.zip |
fix(printf): fix broken right-padding in _ftoa
Fixes #24
-rw-r--r-- | printf.c | 4 | ||||
-rw-r--r-- | test/test_suite.cpp | 6 |
2 files changed, 9 insertions, 1 deletions
@@ -262,6 +262,8 @@ static size_t _ntoa_long_long(out_fct_type out, char* buffer, size_t idx, size_t #if defined(PRINTF_SUPPORT_FLOAT)
static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, double value, unsigned int prec, unsigned int width, unsigned int flags)
{
+ const size_t start_idx = idx;
+
char buf[PRINTF_FTOA_BUFFER_SIZE];
size_t len = 0U;
double diff = 0.0;
@@ -388,7 +390,7 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d // append pad spaces up to given width
if (flags & FLAGS_LEFT) {
- while (idx < width) {
+ while (idx - start_idx < width) {
out(' ', buffer, idx++, maxlen);
}
}
diff --git a/test/test_suite.cpp b/test/test_suite.cpp index 3ecac5f..05b95f7 100644 --- a/test/test_suite.cpp +++ b/test/test_suite.cpp @@ -990,6 +990,12 @@ TEST_CASE("float", "[]" ) { test::sprintf(buffer, "%.1f", 3.49);
REQUIRE(!strcmp(buffer, "3.5"));
+ test::sprintf(buffer, "a%-5.1f", 0.5);
+ REQUIRE(!strcmp(buffer, "a0.5 "));
+
+ test::sprintf(buffer, "a%-5.1fend", 0.5);
+ REQUIRE(!strcmp(buffer, "a0.5 end"));
+
// out of range in the moment, need to be fixed by someone
test::sprintf(buffer, "%.1f", 1E20);
REQUIRE(!strcmp(buffer, ""));
|