From 414328563a8c3972e8d053fb770741eb0123d826 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 8 Nov 2007 06:56:58 +0000 Subject: Bu::FString now has the functions format, formatAppend, and formatPrepend, all of which use printf style formatting and accept the same parameters as printf. --- src/fstring.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/tests/fstrformat.cpp | 12 ++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/tests/fstrformat.cpp diff --git a/src/fstring.h b/src/fstring.h index de34cb0..2a18362 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -681,6 +681,50 @@ namespace Bu appendChunk( pNew ); } + void format( const char *sFrmt, ...) + { + clear(); + + va_list ap; + va_start( ap, sFrmt ); + + long iLen = vsnprintf( NULL, 0, sFrmt, ap ); + + Chunk *pNew = newChunk( iLen ); + vsnprintf( pNew->pData, iLen+1, sFrmt, ap ); + appendChunk( pNew ); + + va_end( ap ); + } + + void formatAppend( const char *sFrmt, ...) + { + va_list ap; + va_start( ap, sFrmt ); + + long iLen = vsnprintf( NULL, 0, sFrmt, ap ); + + Chunk *pNew = newChunk( iLen ); + vsnprintf( pNew->pData, iLen+1, sFrmt, ap ); + appendChunk( pNew ); + + va_end( ap ); + } + + void formatPrepend( const char *sFrmt, ...) + { + va_list ap; + va_start( ap, sFrmt ); + + long iLen = vsnprintf( NULL, 0, sFrmt, ap ); + + Chunk *pNew = newChunk( iLen ); + vsnprintf( pNew->pData, iLen+1, sFrmt, ap ); + prependChunk( pNew ); + + va_end( ap ); + } + /** * Function the archiver calls to archive your FString. *@param ar (Archive) The archive which is archiving your FString. diff --git a/src/tests/fstrformat.cpp b/src/tests/fstrformat.cpp new file mode 100644 index 0000000..dbf6133 --- /dev/null +++ b/src/tests/fstrformat.cpp @@ -0,0 +1,12 @@ +#include "bu/fstring.h" +#include + +int main() +{ + Bu::FString s; + + s.format("%d, %f, \'%s\'", 144, 12.5, "bob" ); + + printf("test: %s\n", s.getStr() ); +} + -- cgit v1.2.3