From fcf2dde54316a3ac35936157babccae8c8d8d90b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 18 Mar 2011 03:48:35 +0000 Subject: The inline, printf/qstring style formatting for Bu::String is just about there, it just needs to be integrated with the Bu::String class itself, pretty exciting. --- src/tools/format.cpp | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) (limited to 'src/tools/format.cpp') diff --git a/src/tools/format.cpp b/src/tools/format.cpp index 6a2f3bd..6aa4c56 100644 --- a/src/tools/format.cpp +++ b/src/tools/format.cpp @@ -4,6 +4,8 @@ #include #include +#include + using namespace Bu; class Fmter @@ -32,15 +34,47 @@ public: operator Bu::String() const { + int iCount = lParm.getSize(); + ParmList::const_iterator *aParm = + new ParmList::const_iterator[iCount]; + { + int j = 0; + for( ParmList::const_iterator i = lParm.begin(); i; i++, j++ ) + { + aParm[j] = i; + } + } Bu::MemBuf mbOut; Bu::Formatter f( mbOut ); - ParmList::const_iterator i = lParm.begin(); for( Bu::String::const_iterator s = sSrc.begin(); s; s++ ) { if( *s == '%' ) { - f << (*i).format << (*i).value; - i++; + s++; + if( *s == '%' ) + f << *s; + else + { + Bu::String sNum; + while( s && *s >= '0' && *s <= '9' ) + { + sNum += *s; + s++; + } + int iIndex = strtol( sNum.getStr(), 0, 10 )-1; + if( iIndex < 0 || iIndex >= iCount ) + { + delete[] aParm; + throw Bu::ExceptionBase( + "Argument index %d is outside of " + "valid range (1-%d).", iIndex+1, iCount + ); + } + + f << (*aParm[iIndex]).format << (*aParm[iIndex]).value; + if( s ) + f << *s; + } } else { @@ -48,6 +82,7 @@ public: } } + delete[] aParm; return mbOut.getString(); } @@ -83,6 +118,7 @@ Bu::Formatter &operator<<( Bu::Formatter &f, const Fmter &r ) int main() { - sio << Fmter("A word is % and a number is % %.").arg("Hello").arg(75, Fmt::hex() ).arg(" - ") << sio.nl; + sio << Fmter("A word is %1 and a number is %2 %1"). + arg("Hello").arg(75, Fmt::hex() ).arg(" - ") << sio.nl; } -- cgit v1.2.3