aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--printf.c13
-rw-r--r--test/test_suite.cpp18
2 files changed, 21 insertions, 10 deletions
diff --git a/printf.c b/printf.c
index 9d70faf..1770122 100644
--- a/printf.c
+++ b/printf.c
@@ -1,6 +1,6 @@
///////////////////////////////////////////////////////////////////////////////
// \author (c) Marco Paland (info@paland.com)
-// 2014-2018, PALANDesign Hannover, Germany
+// 2014-2019, PALANDesign Hannover, Germany
//
// \license The MIT License (MIT)
//
@@ -158,8 +158,7 @@ static inline void _out_fct(char character, void* buffer, size_t idx, size_t max
// internal secure strlen
-// \return The length of the string (excluding the terminating 0)
-// limited by 'max' size if non-zero
+// \return The length of the string (excluding the terminating 0) limited by 'maxsize'
static inline unsigned int _strnlen_s(const char* str, size_t maxsize)
{
const char* s;
@@ -389,11 +388,7 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
if (prec == 0U) {
diff = value - (double)whole;
- if (diff > 0.5) {
- // greater than 0.5, round up, e.g. 1.6 -> 2
- ++whole;
- }
- else if ((diff == 0.5) && (whole & 1)) {
+ if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) {
// exactly 0.5 and ODD, then round up
// 1.5 -> 2, but 2.5 -> 2
++whole;
@@ -776,7 +771,7 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
}
case 's' : {
- char* p = va_arg(va, char*);
+ const char* p = va_arg(va, char*);
unsigned int l = _strnlen_s(p, precision ? precision : (size_t)-1);
// pre padding
if (flags & FLAGS_PRECISION) {
diff --git a/test/test_suite.cpp b/test/test_suite.cpp
index 27efa4e..a113db0 100644
--- a/test/test_suite.cpp
+++ b/test/test_suite.cpp
@@ -1,6 +1,6 @@
///////////////////////////////////////////////////////////////////////////////
// \author (c) Marco Paland (info@paland.com)
-// 2017-2018, PALANDesign Hannover, Germany
+// 2017-2019, PALANDesign Hannover, Germany
//
// \license The MIT License (MIT)
//
@@ -361,6 +361,10 @@ TEST_CASE("# flag", "[]" ) {
test::sprintf(buffer, "%#.0x", 0);
REQUIRE(!strcmp(buffer, ""));
+ test::sprintf(buffer, "%#.1x", 0);
+ REQUIRE(!strcmp(buffer, "0"));
+ test::sprintf(buffer, "%#.0llx", (long long)0);
+ REQUIRE(!strcmp(buffer, ""));
test::sprintf(buffer, "%#.8x", 0x614e);
REQUIRE(!strcmp(buffer, "0x0000614e"));
test::sprintf(buffer,"%#b", 6);
@@ -1067,6 +1071,15 @@ TEST_CASE("float", "[]" ) {
test::sprintf(buffer, "%.0f", 34.1415354);
REQUIRE(!strcmp(buffer, "34"));
+ test::sprintf(buffer, "%.0f", 1.3);
+ REQUIRE(!strcmp(buffer, "1"));
+
+ test::sprintf(buffer, "%.0f", 1.55);
+ REQUIRE(!strcmp(buffer, "2"));
+
+ test::sprintf(buffer, "%.1f", 1.64);
+ REQUIRE(!strcmp(buffer, "1.6"));
+
test::sprintf(buffer, "%.2f", 42.8952);
REQUIRE(!strcmp(buffer, "42.90"));
@@ -1113,6 +1126,9 @@ TEST_CASE("float", "[]" ) {
test::sprintf(buffer, "%.0f", 3.5);
REQUIRE(!strcmp(buffer, "4"));
+ test::sprintf(buffer, "%.0f", 4.5);
+ REQUIRE(!strcmp(buffer, "4"));
+
test::sprintf(buffer, "%.0f", 3.49);
REQUIRE(!strcmp(buffer, "3"));