views:

207

answers:

3

Usually I use streams for formatting stuff however in this case ?I don't know the format until runtime.

I want to be able to take something like the following format string:
Hello {0}! Your last login was on {1,date:dd/mm/yy}.
...and feed in the variables "Fire Lancer" and 1247859223, and end up with the following formatted string:
Hello Fire Lancer! Your last login was on 17/07/09.

In other languages I use there is built in support for this kind of thing, eg pythons format string method, however in c++ there doesn't seem to be any such functionality, accept the C print methods which are not very safe.

Also this is for a high performance program, so whatever solution I use needs to parse the format string once and store it (eg mayby a Parse method that returns a FormatString object with a Format(string) method), not reparse the string every time the format method is called...

A: 

boost::format will do the positional arguments portion, but not the date formatting...

bdonlan
+1  A: 

Boost Formatting does that for you:

http://www.boost.org/doc/libs/1_39_0/libs/format/doc/format.html

Check out this question and answer for examples of usage:

Martin York
+2  A: 

Your format string looks very much like those used in ICU MessageFormat. Did you consider using it?

Éric Malenfant