Boost logo

Boost :

From: Giovanni Bajo (giovannibajo_at_[hidden])
Date: 2002-05-13 06:44:39


----- Original Message -----
From: "Martin Fuchs" <martin-fuchs_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Monday, May 13, 2002 1:25 PM
Subject: Re: [boost] String formatting library: interest?

> cout << fmtstring("This is %1 %2 %3") + "a" + "simple" + "example" <<
"\n";
> Default formating outputs just strings instead of the numbered
placeholders.

I think the operator+ is confusing. The new string type (if any, see my
previous post about using a function instead of a type) ought to be
perfectly compatible with std::string. One should be able to switch from
std::string to boost::string without touching is code. Also, the '+' doesn't
give you a way to specify parameters for substitution: e.g. how do you
implement %25s with operator+? It's just .arg(mystring, 25) in my proposal.

> To change the order of arguments, you just have to alter the format
string:
>
> cout << fmtstring("This is %1 %3, to be treaten as %2")
> + "a" + "simple" + "example" << "\n";

Yes, this happens with my proposal too. When you call arg(), it substitutes
the placeholder with the lowest number found in the string. Changing the
ordering of parameters without touching the code is an important feature
indeed.

> To implement this, fmtstring would at first just collect all the
arguments,
> and build the resulting string on demand. This means, at the time, some
one
> asks for the result calling function str() or operator const char*(). Just
> in case the string is used more than one time, the result can be buffered
> internally in fmtstring.

Lazy update you mean. Yes, I thought of it as well. mystring could cache all
the arguments and then execute the substitution only when operator string()
is called. Not sure if it is really worth the code, but anyway.

> If you want to, you can also use the printf-like formats:
>
> string s = fmtstring("This is %1%3s %2%s %3-20s")
> + "a" + "simple" + "example";

I found that unreadable compared to "This is %1" and arg("a", 20). Also,
notice that %1%3s is a syntax clash, since it could well be placeholder for
parm 1 + place holder for parm 3 + plain 's' character.
If we want to support printf-like format arguments, I'd add it but without
mixmatching it: either %1 %2 %3, or %d %d %s. The latter syntax is still not
so safe (yes we can check for proper types and avoid random crashes, but we
would have to throw an exception when the type is incorrect. I still prefer
the %1 %2 syntax, where the worst that can happen if you specify a wrong
variable is that the wrong information is printed).

> Why use only numbered arguments?
> By using just strings as names, one can do somethiong like this:
>
> string result = fmtstring("name: %name%s age: %age%d") +
> fmtargument("name") * "billy" +
> fmtargument("age") * 18;
>

'Ouch' to the operator* :) We prolly have a different point of view about
the C++ syntax, but I don't like at all operator abuses: I overload '+' only
for operations that are either "add" or "chain".

> This has the benefit of more readable format strings. And we could design
> something like an recordset-class. This would contain values for a list of
> argument names, and it can be taken as the source for formating a string.
I
> think an example might be a bit more clear: ;-)

The recordset is an interesting feature, even if I can't think of a
real-world example of its use.

Giovanni Bajo


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk