title: Parse Objective-C printf Format Strings?
body: <p>I want to get the ranges of members within an <code>NSString</code> built with <code>+[NSString stringWithFormat:]</code>. What is the best way to parse the objective-c format string? I can't just use a C format string parser because of <code>%@</code>. I also need to make sure it supports format orderings: <code>%1$d</code>, <code>%2$@</code>, etc.</p>
  
  <p>For example, with the string built with <code>[NSString stringWithFormat:@"foo %2$@ bar %1$@", @"Heath", @"Borders"]</code>, I would ideally like the following <code>NSArray</code>: <code>@[NSMakeRange(15, 5), NSMakeRange(4, 6)]</code>. The first array object corresponds to the first data element in the format string, the second array object to the second data element, etc.</p>
  
  <p>In this case, the API would look something like <code>+ (NSString *) stringWithFormatRanges:(NSArray **)outFormatRanges withFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);</code> It would return an <code>NSString</code> just like <code>+[NSString stringWithFormat:]</code>, but it would also return an <code>NSArray</code> with <code>NSRange</code>s of each format data element.</p>
  
  <p><strong>-- EDIT --</strong></p>
  
  <p>Given that this question is 3 years old, I'd be happy with a C-only implementation at this point.</p>