Fix several corner case bugs in lexical_cast
authorMikko Rasa <tdb@tdb.fi>
Wed, 1 Aug 2012 20:43:34 +0000 (23:43 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 1 Aug 2012 20:43:34 +0000 (23:43 +0300)
source/strings/fmt.cpp
source/strings/lexicalcast.cpp

index ec7a7821bf3d493ca26fcd92458c46d30c3acee6..07484dd61c7fe27d514ee4a325e1d2fe7bbe366d 100644 (file)
@@ -26,6 +26,9 @@ void Fmt::parse(const char *s)
                        break;
        }
 
+       if(align==LEFT)
+               fillc = ' ';
+
        wd = 0;
        for(; *f; ++f)
        {
index 6a226e1d27ac5ff356589c4e59f06c65085ebde4..ab83b110b432ba408f8bdfa3bd2fb53d3e848c8f 100644 (file)
@@ -237,7 +237,7 @@ string flt_to_str(T v, const Fmt &f)
        if(w>=10)
        {
                long double div = 1;
-               while(div*10<w)
+               while(div*10<=w)
                {
                        ++exp;
                        div *= 10;
@@ -257,6 +257,7 @@ string flt_to_str(T v, const Fmt &f)
 
        // Decide how to format the number
        unsigned digits;
+       unsigned leading_zeroes = 0;
        unsigned point = 1;
        bool showexp = false;
        if(mode==Fmt::FIXED)
@@ -279,15 +280,16 @@ string flt_to_str(T v, const Fmt &f)
                }
                else
                {
-                       point = max(exp, 0)+1;
                        if(exp<0)
-                               digits += -exp;
+                               leading_zeroes = -exp;
+                       else
+                               point = exp+1;
                }
        }
 
        // Apply rounding
        w += 5.0l/pow(10.0l, static_cast<long double>(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<int>(i)>=-exp)
+               if(!leading_zeroes)
                {
                        int digit = static_cast<int>(w);
                        *mptr++ = '0'+digit;
                        w = (w-digit)*10;
                }
                else
+               {
                        *mptr++ = '0';
+                       --leading_zeroes;
+               }
        }
 
        if(f.get_showpoint())