I'm writing an emulator for a machine with a 'printf' opcode, and while I'm aware of the Formatter class which will probably be good enough for actually formatting the strings, I need a way to count the number of arguments which are consumed by the printf call.
Off the top of my head, I could probably do something with a regex to count the number of '%'s, but I'm not too familiar with format strings, so I might not count properly... (excluding escaped ones, etc)
edit: I actually need the number of parameter, along with a mapping of parameter # to parameter type, so, for example, "hello %s %+.3i" would give {0 -> String, 1 -> Integer}
%*sor%*.*fstrings which take 2 or 3 arguments, so you can't just count the number of %s)printf()you want to support, and use the abstract syntax tree to generate the argument count? more work than a simple library call, but if you have tools like ANTLR or whatever to do the parsing, I would think it wouldn't be that hard, and you could make sure your program works correctly. The only reason I bring this up is that a full printf parsing isn't so easy. If you can live without the variable-width syntax, that simplifies it quite a bit, and you could probably do it with Regex