1

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...

2
  • 3
    Your high-performance program rapidly greets the user? Commented Jul 17, 2009 at 19:42
  • @GMan hahahahaha. Im still asking myself that question too Commented Jul 17, 2009 at 20:03

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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:

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.