]> git.tdb.fi Git - libs/core.git/commitdiff
Use format instead of stringstream in TimeDelta
authorMikko Rasa <tdb@tdb.fi>
Sat, 28 May 2011 10:23:01 +0000 (13:23 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 28 May 2011 10:23:01 +0000 (13:23 +0300)
source/time/timedelta.cpp
source/time/timedelta.h

index c74ffcef4f71fa08de3124b65c1c5171e721da80..68e836178a325dfbcbba429c58923085d95e0899 100644 (file)
@@ -5,8 +5,7 @@ Copyright © 2006  Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
-#include <sstream>
-#include <iomanip>
+#include <msp/strings/format.h>
 #include "timedelta.h"
 #include "units.h"
 
@@ -16,17 +15,20 @@ namespace {
 
 using Msp::Time::RawTime;
 
-void print_part(ostream &out, RawTime &value, RawTime unit, char sep, bool &first)
+string format_part(RawTime &value, RawTime unit, char sep, bool &first)
 {
        if(value<unit && first)
-               return;
-       
-       if(!first)
-               out<<sep<<setw(2);
-       
-       out<<value/unit;
+               return string();
+
+       RawTime v = value/unit;
        value %= unit;
-       first = false;
+       if(first)
+       {
+               first = false;
+               return Msp::format("%d", v);
+       }
+       else
+               return Msp::format("%c%02d", sep, v);
 }
 
 }
@@ -34,53 +36,51 @@ void print_part(ostream &out, RawTime &value, RawTime unit, char sep, bool &firs
 namespace Msp {
 namespace Time {
 
-ostream &operator<<(ostream &out, const TimeDelta &td)
+void operator<<(LexicalConverter &conv, const TimeDelta &td)
 {
-       ostringstream ss;
-       ss.fill('0');
+       string result;
 
        RawTime value = td.raw();
 
        if(value<0)
        {
-               ss<<'-';
+               result += '-';
                value = -value;
        }
 
        if(value==0)
-               ss<<'0';
+               result += '0';
        else if(value<1000)
-               ss<<value<<"µs";
+               result += format("%dµs", value);
        else if(value<1000000)
        {
-               ss<<value/1000;
+               result += format("%d", value/1000);
                value %= 1000;
                if(value)
-                       ss<<'.'<<setw(3)<<value;
-               ss<<"ms";
+                       result += format(".%03d", value);
+               result += "ms";
        }
        else
        {
                bool first = true;
-               print_part(ss, value, 86400000000LL,  0,  first);
-               print_part(ss, value, 3600000000LL,  '-', first);
-               print_part(ss, value, 60000000LL,    ':', first);
-               print_part(ss, value, 1000000LL,     ':', first);
+               bool unit = value<60000000LL;
+               result += format_part(value, 86400000000LL,  0,  first);
+               result += format_part(value, 3600000000LL,  '-', first);
+               result += format_part(value, 60000000LL,    ':', first);
+               result += format_part(value, 1000000LL,     ':', first);
 
                if(value)
                {
-                       ss<<'.';
                        if(value%1000)
-                               ss<<setw(6)<<value;
+                               result += format(".%06d", value);
                        else
-                               ss<<setw(3)<<value/1000;
+                               result += format(".%03d", value/1000);
                }
-               ss<<"s";
+               if(unit)
+                       result += 's';
        }
 
-       out<<ss.str();
-
-       return out;
+       conv.result(result);
 }
 
 } // namespace Time
index 0ced98ecd3e7e581795056959895a8941ae3e966..631c4b0554a6549bcc79b810854a4167990cd72e 100644 (file)
@@ -10,7 +10,7 @@ Distributed under the LGPL
 
 #include <time.h>
 #include <sys/time.h>
-#include <ostream>
+#include <msp/strings/lexicalcast.h>
 #include "types.h"
 
 namespace Msp {
@@ -83,7 +83,7 @@ public:
 template<typename T>
 inline TimeDelta operator*(T a, const TimeDelta &t)   { return t*a; }
 
-extern std::ostream &operator<<(std::ostream &, const TimeDelta &);
+void operator<<(LexicalConverter &, const TimeDelta &);
 
 } // namespace Time
 } // namespace Msp