[Home]CompileTimeCheckedFormat

BOOST WIKI | RecentChanges | Preferences | Page List | Links List

No diff available--this is the first major revision. (minor diff)

Goal

To present formatting facilities that don't involve dynamic memory allocations but rather check potential buffer overflows at compile-time.

Overview

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.

Streaming

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[20];
  ostream(buf) << "i=" << i;

Manipulators should be implemented too:

  char buf[20];
  ostream(buf) << hex << showbase << 137;

Arguments to manipulators that change a length of output should be passed as template arguments:

  char buf[20];
  ostream(buf) << setw<16>() << setfill(' ') << 5;

Formatting

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[20];
  format(buf, "i=%1%") % i;

  // Example 2
  char buf[20];
  format(buf, "%1%") % group(hex, showbase, 137);

  // Example 3
  char buf[20];
  format(buf, "%1%") % group(setw<16>(), setfill(' '), 5);

Rationale

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.

Misc

Comparison with similar libraries

sprintf/snprintf

ostream

TODO

Boost.Format

TODO


BOOST WIKI | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited April 23, 2006 10:20 pm (diff)
Search:
Disclaimer: This site not officially maintained by Boost Developers