From 322daba74c122c4ba8c89b73ca8107be02990e5c Mon Sep 17 00:00:00 2001 From: Chander Sudanthi Date: Fri, 2 Nov 2012 11:32:00 -0500 Subject: [PATCH] base: Fix a few incorrectly handled print format cases This patch ensures cases like %0.6u, %06f, and %.6u are processed correctly. The case like %06f is ambiguous and was made to match printf. Also, this patch removes the goto statement in cprintf.cc in favor of a function call. --- src/base/cprintf.cc | 29 ++++++++++++++++++++++------- src/base/cprintf.hh | 1 + 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/base/cprintf.cc b/src/base/cprintf.cc index 4f825c097..2dd2ab486 100644 --- a/src/base/cprintf.cc +++ b/src/base/cprintf.cc @@ -69,9 +69,10 @@ Print::process() while (*ptr) { switch (*ptr) { case '%': - if (ptr[1] != '%') - goto processing; - + if (ptr[1] != '%') { + process_flag(); + return; + } stream.put('%'); ptr += 2; break; @@ -93,10 +94,11 @@ Print::process() break; } } +} - return; - - processing: +void +Print::process_flag() +{ bool done = false; bool end_number = false; bool have_precision = false; @@ -248,7 +250,20 @@ Print::process() end_number = false; number = 0; } - } + + if (done) { + if ((fmt.format == Format::integer) && have_precision) { + // specified a . but not a float, set width + fmt.width = fmt.precision; + // precision requries digits for width, must fill with 0 + fmt.fill_zero = true; + } else if ((fmt.format == Format::floating) && !have_precision && + fmt.fill_zero) { + // ambiguous case, matching printf + fmt.precision = fmt.width; + } + } + } // end while ++ptr; } diff --git a/src/base/cprintf.hh b/src/base/cprintf.hh index 6124d8c73..e702fa3a6 100644 --- a/src/base/cprintf.hh +++ b/src/base/cprintf.hh @@ -59,6 +59,7 @@ struct Print Format fmt; void process(); + void process_flag(); public: Print(std::ostream &stream, const std::string &format); -- 2.30.2