From faeafc9d652ba6caa350ca95dff14408b036ccfb Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 1 Aug 2012 23:43:34 +0300 Subject: [PATCH] Fix several corner case bugs in lexical_cast --- source/strings/fmt.cpp | 3 +++ source/strings/lexicalcast.cpp | 25 ++++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/source/strings/fmt.cpp b/source/strings/fmt.cpp index ec7a782..07484dd 100644 --- a/source/strings/fmt.cpp +++ b/source/strings/fmt.cpp @@ -26,6 +26,9 @@ void Fmt::parse(const char *s) break; } + if(align==LEFT) + fillc = ' '; + wd = 0; for(; *f; ++f) { diff --git a/source/strings/lexicalcast.cpp b/source/strings/lexicalcast.cpp index 6a226e1..ab83b11 100644 --- a/source/strings/lexicalcast.cpp +++ b/source/strings/lexicalcast.cpp @@ -237,7 +237,7 @@ string flt_to_str(T v, const Fmt &f) if(w>=10) { long double div = 1; - while(div*10(digits)); - if(w>10) + if(w>=10) { // Rounding bumped us to the next exponent, deal with it w /= 10; @@ -298,13 +300,19 @@ string flt_to_str(T v, const Fmt &f) } if(!showexp) { - ++digits; - ++point; + if(mode==Fmt::FIXED) + ++digits; + if(leading_zeroes) + --leading_zeroes; + else + ++point; } else ++exp; } + digits += leading_zeroes; + // Create a buffer and start from the end unsigned size = max(f.get_width(), digits+8); char *buf = new char[size]; @@ -326,14 +334,17 @@ string flt_to_str(T v, const Fmt &f) { if(i==point) *mptr++ = '.'; - if(showexp || static_cast(i)>=-exp) + if(!leading_zeroes) { int digit = static_cast(w); *mptr++ = '0'+digit; w = (w-digit)*10; } else + { *mptr++ = '0'; + --leading_zeroes; + } } if(f.get_showpoint()) -- 2.43.0