I want to get the ranges of members within an NSString built with +[NSString stringWithFormat:]. What is the best way to parse the objective-c format string? I can't just use a C format string parser because of %@. I also need to make sure it supports format orderings: %1$d, %2$@, etc.

For example, with the string built with [NSString stringWithFormat:@"foo %2$@ bar %1$@", @"Heath", @"Borders"], I would ideally like the following NSArray: @[NSMakeRange(15, 5), NSMakeRange(4, 6)]. The first array object corresponds to the first data element in the format string, the second array object to the second data element, etc.

In this case, the API would look something like + (NSString *) stringWithFormatRanges:(NSArray **)outFormatRanges withFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); It would return an NSString just like +[NSString stringWithFormat:], but it would also return an NSArray with NSRanges of each format data element.

-- EDIT --

Given that this question is 3 years old, I'd be happy with a C-only implementation at this point.

Edit
Report