From 30e841d5b6f082bce1f98c6df198aeea28a2d95b Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 17 Mar 2011 23:14:52 +0000 Subject: Tweaks to the variant class make it much cooler, and there's a test/tool called format, it's a proof of concept of a text formatter. I think it's gonna' rock. --- src/tools/format.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/tools/format.cpp (limited to 'src/tools') diff --git a/src/tools/format.cpp b/src/tools/format.cpp new file mode 100644 index 0000000..6a2f3bd --- /dev/null +++ b/src/tools/format.cpp @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include + +using namespace Bu; + +class Fmter +{ +public: + Fmter( const Bu::String &sSrc ) : + sSrc( sSrc ) + { + } + + template + Fmter &arg( const T &x ) + { + lParm.append( Pair( x ) ); + + return *this; + } + + template + Fmter &arg( const T &x, Bu::Formatter::Fmt f ) + { + lParm.append( Pair( x, f ) ); + + return *this; + } + + operator Bu::String() const + { + 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++; + } + else + { + f << *s; + } + } + + return mbOut.getString(); + } + +private: + const Bu::String &sSrc; + class Pair + { + public: + template + Pair( const T &v ) : + value( v ) + { + } + + template + Pair( const T &v, Bu::Formatter::Fmt f ) : + value( v ), + format( f ) + { + } + + Bu::Variant value; + Bu::Formatter::Fmt format; + }; + typedef Bu::List ParmList; + ParmList lParm; +}; + +Bu::Formatter &operator<<( Bu::Formatter &f, const Fmter &r ) +{ + return f << (Bu::String)r; +} + +int main() +{ + sio << Fmter("A word is % and a number is % %.").arg("Hello").arg(75, Fmt::hex() ).arg(" - ") << sio.nl; +} + -- cgit v1.2.3