How it works

Top  Previous  Next

Scripts > Class elements and c++ instructions > Formatting instructions > How it works

 

1. When you call format(s), where s is the format-string, it constructs an object, which parses the format string and look for all directives in it and prepares internal structures for the next step.

 

2. Then, either immediately, as in

 

out << format("%|2$| %|1$|") % 36 % 77 )

 

or later on, as in

 

format fmter("%|2$| %|1$|");

fmter % 36; fmter % 77;

 

you feed variables into the formatter. Those variables are dumped to out, which state is set according to the given formatting options in the format-string -if there are any-, and the format object stores the string results for the last step.

 

3. Once all arguments have been fed you can dump the format object to out, or get its string value by using the str() member function. The result string stays accessible in the format object until another argument is passed, at which time it is reinitialized.

 

// fmter was previously created and fed arguments, it can print the result :

out << fmter ;

 

// You can take the string result :

str s  = fmter.str();

 

// possibly several times :

s = fmter.str( );

 

// You can also do all steps at once :

out << boost::format("%|2$| %|1$|") % 36 % 77;

 

 

4. Optionally, after step 3, you can re-use a format object and restart at step2 :

fmter % 18 % 39;

to format new variables with the same format-string, saving the expensive processing involved at step 1.

 

All in all, the format class translates a format-string (with eventually printf-like directives) into internal operations, and finally returns the result of the formatting, as a string, or directly to out.



This page belongs to the TextTransformer Documentation

Home  Content  German