c42f / tinyformat

Minimal, type safe printf replacement library for C++
531 stars 75 forks source link

Usage question: How to NOT display a parameter? #55

Closed ninnghazad closed 5 years ago

ninnghazad commented 5 years ago

Consider:

tfm::format(
   _(
     "%1$s the pig!",
     "%1$s all of the %2$d pigs!",
     number_of_pigs
     ),
   "Floop",
   number_of_pigs
)

where _() is i18n macro which returns translated singular/plural string depending on number_of_pigs. In the case of a single pig, i would want a sentence not containing the number 1. Now this won't work because

Not enough conversion specifiers in format string

Tried using zero-width specifier like %1$00d or using %n to hide the 1. Is there a way to hide a parameter?

c42f commented 5 years ago

Good question, I think this is a bug!

The printf specification doesn't prohibit this, though has the weird restriction that you can only ignore the tail of the argument list:

When numbered argument specifications are used, specifying the Nth argument requires that all the leading arguments, from the first to the (N-1)th, are specified in the format string.

However I think we can relax this requirement to just allow the arguments to be referenced any number of times in any order when using numbered arguments.