VCF::Format Class Reference
The Format class is used to format a string, similar to the sprintf/printf family of functions, only made typesafe for C++, and less likely to encounter buffer overflow errors that are so easy to do with sprintf and friends. More...
#include <vcf/FoundationKit/Format.h>
Public Member Functions | |
| Format (const String &fmtStr) | |
| ~Format () | |
| template<typename ValType> | |
| Format & | operator% (const ValType &val) |
| template<String > | |
| Format & | operator% (const String &val) |
| This hack is here to work around the broken (as far as I can tell) unicode support of OSX (yes, this *again*). | |
| operator String () const | |
Protected Member Functions | |
| int | countFormatTokens () const |
| const VCFChar * | getNextFormatTokenEndPos (int startPos) const |
| void | gobblePercents () |
Detailed Description
The Format class is used to format a string, similar to the sprintf/printf family of functions, only made typesafe for C++, and less likely to encounter buffer overflow errors that are so easy to do with sprintf and friends.The inspiration for this class comes unashamedly from the Boost Format library (http://www.boost.org/libs/format/doc/format.html). Any mistakes in the "translation" are mine, not Boost's.
The basic idea is to pass in a string to the Format instance, and then use the "%" operator to separate your arguments. Currently the usage is exactly the same as you would use printf/sprintf, in terms of formatting. For example:
//printf printf( "Hello World, %d times!", 10 ); //Encoding the same using a Format object String s = Format("Hello World, %d times!") % 10;
The Format object will throw assert exceptions (in debug mode) if too few arguments are passed in, or too many. For example:
The above example is expecting 1 argument, and didn't receive any, and will thus assert.The above example expected 1 argument, but received 2, and will assert.
- See also:
- StringUtils::format(const Format&)
StringUtils::traceWithArgs(const Format&)
System::print(const Format&)
System::println(const Format&)
Constructor & Destructor Documentation
|
|
|
|
|
|
Member Function Documentation
|
|
|
|
|
|
|
|
|
|
|
|
|
||||||||||
|
This hack is here to work around the broken (as far as I can tell) unicode support of OSX (yes, this *again*). It would appear that the onlu "widechar" support that printf has is for wchar_t types, not hte UTF16 16bit type that all the rest of the ^&%$$ OS uses (see CFStringRef - it's a 16bit unicode type, like all the other sane Unicode API's use). So using ls, or S both fail to print anything at all here when a WideChar* (a 16bit unsigned short*) and we get errors from snprintf() about this. To workaround this silliness (which of course isn't required on Win32 - yeah Microsoft), I've added this specialization for WideChar* and then converted down to ansi before passing it into sprintf. This is incredibly lame, and if someone ever figures out a better solution, PLEASE let me know!!!!!!! |
|
||||||||||
|
|
The documentation for this class was generated from the following file:
- vcf/FoundationKit/Format.h
