From 912207fe0fa25d21837ced09dcd689c68e449ef7 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 5 Sep 2012 21:10:05 +0000 Subject: Added extra formatting argument indexing method to Bu::String::FormatProxy. You can now use %{3} so that arguments can be followed by a digit. --- src/stable/string.cpp | 24 ++++++++++++++++++++++-- src/stable/string.h | 1 + src/unit/string.unit | 5 +++++ 3 files changed, 28 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/stable/string.cpp b/src/stable/string.cpp index d1f76aa..dd484fd 100644 --- a/src/stable/string.cpp +++ b/src/stable/string.cpp @@ -1269,10 +1269,30 @@ Bu::String Bu::String::FormatProxy::end() const else { String sNum; - while( s && *s >= '0' && *s <= '9' ) + if( *s == '{' ) { - sNum += *s; s++; + while( s && *s >= '0' && *s <= '9' ) + { + sNum += *s; + s++; + } + if( !s || *s != '}' ) + { + delete[] aArg; + throw Bu::ExceptionBase( + "Argument started with a '{' but terminated " + "with a '%c' instead of '}'.", *s ); + } + s++; + } + else + { + while( s && *s >= '0' && *s <= '9' ) + { + sNum += *s; + s++; + } } int iIndex = strtol( sNum.getStr(), 0, 10 )-1; if( iIndex < 0 || iIndex >= iCount ) diff --git a/src/stable/string.h b/src/stable/string.h index c92a704..197821d 100644 --- a/src/stable/string.h +++ b/src/stable/string.h @@ -950,6 +950,7 @@ namespace Bu class FormatProxyEndAction { public: + virtual ~FormatProxyEndAction() { }; virtual void operator()( const Bu::String &sFinal )=0; }; diff --git a/src/unit/string.unit b/src/unit/string.unit index dfa0c38..4ea878e 100644 --- a/src/unit/string.unit +++ b/src/unit/string.unit @@ -581,6 +581,11 @@ suite String unitTest( (Bu::String)Bu::String("%1%2").arg("IQ").arg(4, Bu::Fmt().plus()) == "IQ+4" ); unitTest( (Bu::String)Bu::String("Sup #%1-Guy!").arg( 1 ) == "Sup #1-Guy!" ); } + + test format2 + { + unitTest( Bu::String("0x%{1}00").arg( 75, Bu::Fmt::hex() ).end() == "0x4B00" ); + } } // 03F09CA4F58AC8CA0E80F0D9D409D0A60700A192270004BC3A99E91D0001034F544603362E35013103313130019CA4F58AC8CA0E0002830800002C4200008AC200EBF7D9D4090127BB010000E3 // -- cgit v1.2.3