* sprintf and snprintf have parsing phase. This library has a porential of outperforming them.|
* sprintf and snprintf have parsing phase. This library has a potential of outperforming them.|
To present formatting facilities that don't involve dynamic memory allocations but rather check potential buffer overflows at compile-time.
The library transforms a sequence of values into a textual form placed at some container with fixed capacity known at compile-time. This is usually an array of chars but it can also be boost::array<char,N>.
If a sequence of values can potentially exceed the container's capacity, compile-time error should be emited. In other words, the library calculates a size of output in a pessimistic case of all values having longest textual representation. Compilation fails if that size is greater then a capacity.
The library consists of two major parts: streaming and formatting.
The syntax should be familiar to std::ostream interface. For example, the following code should copy "i=4294967296" into buf when compiled on a compiler with 32-bit int and produce a compilation error on a compiler with 64-bit int:
unsigned int i = UINT_MAX; char buf; ostream(buf) << "i=" << i;
Manipulators should be implemented too:
char buf; ostream(buf) << hex << showbase << 137;
Arguments to manipulators that change a length of output should be passed as template arguments:
char buf; ostream(buf) << setw<16>() << setfill(' ') << 5;
Streaming is a useful concept but many people still prefer printf-like syntax. This library should provide a subset of Boost.Format library. A syntax of format string in Boost.Format is very flexible and can't be fully supported because a compiler cannot get flags that affect a length of output from a format string. Only simple positional notation %N% should be supported. Positional directive N should appear only once in a format string (eg. "%1% %1%" is invalid).
Examples from previous section can be rewritten like this:
// Example 1 unsigned int i = UINT_MAX; char buf; format(buf, "i=%1%") % i;
// Example 2 char buf; format(buf, "%1%") % group(hex, showbase, 137);
// Example 3 char buf; format(buf, "%1%") % group(setw<16>(), setfill(' '), 5);
Similarity with std::ostream and Boost.Format allows one to rewrite critical execution paths that use any of these two libraries with minimal changes in the code.
Other way around, if new requirements can not be met with this library (for example, internationalisation or unlimited input), the code can be easily changed to use either std::ostream or Boost.Format.