Boost logo

Boost :

From: Rüdiger Loos (loos_at_[hidden])
Date: 2000-04-20 11:06:08


> output. I need to write internationalized code.
>
> -Dave

Dave:
I tried to combine the way Marco Manfredini treats positional parameters with the ideas
of Boris Formitchev for specifiers and your syntax. The result is in the
attached file frmt.cpp which compiles with gcc 2.95.2 (under NT both
with the cygwin and mingw32 version, using the new SGI streams of STLport-4.0b5,
and under Linux), mwcc 2.3, and comeau 4.2.42 online.

The other file frmt1.cpp is for information only since it does not follow boost's
conventions; it compiles with the previous compilers and in addition with VC++ 6.0 SP3
and bcc32 5.5.

In the following examples, No 11 was done with VC++ since I don't know how to switch
locales with the other compilers; the strings for the locales by name are not
standardized. Example No 12 gives different results; we contacted the vendor.

The implementation may help to clarify some minor questions of syntax and some major
questions of semantics of the design of an interface. Probably, manipulators as
format parameters are against la raison d'être of format at all, but I hesitate to
disallow them.

Rüdiger

Examples (the output is indented by 5 spaces):
// 1. easy io
cout << format("please type %1 rows of %2 characters", rows, cols);
     please type 2 rows of 10 characters

// 2. Thus, for a translation to French, simply substitute the format string:
cout << format(fmt[i++], chicken_cnt , coop_num);
cout << format(fmt[i++], chicken_cnt , coop_num);
     3 chickens can be found in coop number 5.
     Dans la cage le numéro 5, on y trouve 3 poulets.

// 3. nested formats
cout << format("The Truth is %1", format("%1 != %2", 1, 2)) << '\n';
     The Truth is 1 != 2

// 4. centering
cout << format("The Truth is %w20%c%1", format("%1 != %2", 1, 2)) << '\n';
     The Truth is 1 != 2

// 5. adjust right (default)
cout << format("The Truth is %w20%1", format("%1 != %2", 1, 2)) << '\n';
     The Truth is 1 != 2

// 6. no right delimiter required, you see the parameter specifier?
const string s21 = format("11%11", 21);
cout << s21;
     11211

// 7. standard inserters inside of format, just in case...
cout
       << format("The Truth is %w25%c%1",
                  format("%3%4%1 != %2", 1.9999, 2.000, setprecision(2), fixed))
                 setfill('.')) << '\n';
     The Truth is ....1.99990 != 2.00000...

// 8. ... you got already used to
cout
       << format("The Truth is %w20%c%1",
                  format("%3%4%1 != %2", 1.9899, 2.000, scientific, setprecision(2)))
       << '\n';
     The Truth is 1.99e+00 != 2.00e+00

// 9. standard inserters left of format retain their meaning
cout << setfill('*') << setw(60)
     << format("The Truth is %w20%c%1",
                format("%3%4%1 != %2", 1.9899, 2.000, scientific, setprecision(2)))
     << '\n';
     ***************************The Truth is 1.99e+00 != 2.00e+00

// 11. user defined inserter 'lang' for switching locales (VC++ only)
  for (i=0; i<7; ++i) {
    locale loc = locale(Name[i]);
    cout <<format("%1|%w37%l%2|%w14%3|%c%w9%4|", lang(loc), loc.name(), 1234567890,
Name[i])
         <<'\n';
     |C | 1234567890| C |
     |English_United States.1252 | 1,234,567,890| US |
     |French_France.1252 | 1á234á567á890| French |
     |Spanish - Traditional Sort_Spain.1252| 1.234.567.890| Spanish |
     |Greek_Greece.1253 | 1.234.567.890| Greek |
     |Dutch_Netherlands.1252 | 1.234.567.890| Dutch |
     |German_Germany.1252 | 1.234.567.890| German |

// 12a. dark corner (one compiler)
const string s12 = format("22%12", 12);
  for (int w=0; w < 8; ++w)
    cout << setw(w) << s21 << ' ' << setw(w) << s12 << '\n'
     11211 22122
     1 2
     11 22
     112 221
     1121 2212
     11211 22122
     *11211 *22122
     **11211 **22122

// 12b. dark corner (other compilers)
const string s12 = format("22%12", 12);
  for (int w=0; w < 8; ++w)
    cout << setw(w) << s21 << ' ' << setw(w) << s12 << '\n'
     11211 22122
     11211 22122
     11211 22122
     11211 22122
     11211 22122
     11211 22122
     *11211 *22122
     **11211 **22122





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