The idea behind the compose library is to provide the following
syntax: String::compose("Price of %1: %2", item,
price)
-> "Price of knife: 15.0".
The main reason it's not a good idea to this manually, e.g. with boost::lexical_cast, is that the program is then very difficult to localize properly, i.e. your translators job will become close to impossible. It's also inconvenient and error-prone since it's easy to get the spacing wrong.
The compose library defines a set of functions on the form
String::compose(format_string, arg1, arg2, etc...)
where the types of arg1, arg2, etc. are templatized to support
arbitrary objects. The format_string
has embedded
%1, %2,... specifications that are replaced by the stringified
representations of arg1, arg2,...,.
So its capabilities are close to those of the functions in the
C printf
family, but without the type and memory
insafety and inconvience. Note that the library consists
of a single header file that you can easily include in your
source code.
The latest version of compose.tar.gz is v. 1.0.5.
Documentation is included in the tarball, and also available here. If you're dealing with
Glib::ustring
s from glibmm/gtkmm, the ucompose.hpp
header defines String::ucompose(fmt, args...)
that
work with ustrings (using the ordinary
String::compose
functions will silently corrupt the
UTF-8 in the ustrings).
What has been changed:
V. 1.0.5 adds a 'char *' specialization to prevent strings from gettext to pass through the stringstream conversion mill.
V. 1.0.4 uses wostringstream in ucompose.hpp instead of ostringstream. The latter is too narrow to contain some characters from number conversions (e.g. thousands separators in some languages) which are then just truncated, causing Glibmm to throw a conversion error.
V. 1.0.3 fixes a serious conversion bug in ucompose.hpp and reduces the total number of conversions quite a lot (std::string, Glib::ustring and char * all skip the internal conversion step). It also fix the ucompose.hpp header so that it is possible to include both ucompose.hpp and compose.hpp in the same source file.
V. 1.0.2 adds a conditional so that ucompose.hpp should now finally compile with both GCC 2.95 and 3.x, although it still requires that the sstream header is present. If you have a very old GCC, it can be grabbed from here.
V. 1.0.1 includes glibmm/convert.h to prevent a compilation error when using ucompose.hpp with recent gtkmm.
Back to home.