6

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}

4
  • 1
    are you looking for something that parses format strings valid for the Formatter class, or format strings valid for the C printf? (e.g. supports %*s or %*.*f strings which take 2 or 3 arguments, so you can't just count the number of %s) Commented Apr 1, 2012 at 23:09
  • 1
    the only types of arguments which apply in my case are characters, 32-bit signed integers, 64-bit doubles, and null-terminated strings. Otherwise it should act like c printf, so I guess Formatter isn't exactly right, but it will probably work for my purposes, as the input is coming from a compiler I wrote anyway. Commented Apr 1, 2012 at 23:13
  • Hmm. If you wrote a compiler, can't you write a (relatively) simple grammar parser for whatever variant of 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 Commented Apr 1, 2012 at 23:16
  • the behaviour of the printf opcode is a bit... underspecified. "Does a C-style printf, Does not support 'I64' (wide integer) or 'n' or 'p' (pointer) specifiers". I'm doing this 'for fun' in the context of a compilers class, and I don't really feel like dealing with the complexities of format strings myself. Commented Apr 1, 2012 at 23:32

2 Answers 2

7

Format Strings interpret every % as a placeholder, with literal % being escaped as %%, so it should be as simple as this:

String formatString;
int parameterCount = formatString.replace("%%", "").split("%").length - 1;

This code first removes all escaped (doubled) %, then counts % via split.

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

1 Comment

actually, now that I think about it, I actually need a mapping of parameter # -> parameter type, ugh.
1

Why don't you just use a Regex that is something like %(?:%|[0-9]+([dox])) and examine the format type specifier that way?

There was another SO topic about parsing sprintf format strings with regex's which might give you some more ideas. Unless you specify what features of printf() you want, it's sort of hard to recommend an exact regex.

Or, as I mentioned in my comment, if you're using another compiler tool anyway like ANTLR or Parboiled, use that to deconstruct the format string into appropriate pieces via a simple grammar specification.

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.