@@ -30,8 +30,7 @@ @implementation DTLocalizableStringScanner
3030
3131@synthesize entryFoundCallback=_entryFoundCallback;
3232
33- - (id )initWithContentsOfURL : (NSURL *)url encoding : (NSStringEncoding )encoding validMacros : (NSDictionary *)validMacros validMacroRegex : (NSRegularExpression *)validMacroRegex
34-
33+ - (id )initWithContentsOfURL : (NSURL *)url encoding : (NSStringEncoding )encoding validMacros : (NSDictionary *)validMacros
3534{
3635 self = [super init ];
3736
@@ -50,9 +49,13 @@ - (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)encoding val
5049 _currentIndex = 0 ;
5150
5251 _url = [url copy ]; // to have a reference later
52+
5353 _validMacros = validMacros;
54- _validMacroRegex = validMacroRegex;
55-
54+
55+ // build regex to find macro words
56+ NSString *innerPatternPart = [[validMacros allKeys ] componentsJoinedByString: @" |" ];
57+ NSString *pattern = [NSString stringWithFormat: @" \\ b(%@ )\\ b" , innerPatternPart];
58+ _validMacroRegex = [NSRegularExpression regularExpressionWithPattern: pattern options: 0 error: NULL ];
5659 }
5760
5861 return self;
@@ -70,10 +73,13 @@ - (void)main
7073{
7174 @autoreleasepool
7275 {
73- [_validMacroRegex enumerateMatchesInString: _charactersAsString options: 0 range: NSMakeRange (0 , [_charactersAsString length ]) usingBlock: ^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
74- NSRange matchRange = [match range ];
75- [self _processMacroAtRange: matchRange];
76- }];
76+ [_validMacroRegex enumerateMatchesInString: _charactersAsString
77+ options: 0 range: NSMakeRange (0 , [_charactersAsString length ])
78+ usingBlock: ^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop)
79+ {
80+ NSRange matchRange = [match range ];
81+ [self _processMacroAtRange: matchRange];
82+ }];
7783 }
7884}
7985
@@ -192,85 +198,87 @@ - (NSString *)_scanParameter
192198- (BOOL )_processMacroAtRange : (NSRange )range
193199{
194200 NSString *macroName = [_charactersAsString substringWithRange: range];
195-
201+
196202 _currentIndex = range.location + range.length ;
197-
198- NSMutableArray *parameters = [[NSMutableArray alloc ] initWithCapacity: 10 ];
199-
200-
201- // skip any whitespace between here and the (
202- [self _scanWhitespace ];
203+
204+ NSMutableArray *parameters = [[NSMutableArray alloc ] initWithCapacity: 10 ];
205+
206+
207+ // skip any whitespace between here and the (
208+ [self _scanWhitespace ];
209+
210+ if (_characters[_currentIndex] == ' (' )
211+ {
212+ // read the opening parenthesis
213+ _currentIndex++;
203214
204- if (_characters[ _currentIndex] == ' ( ' )
215+ while ( _currentIndex < _stringLength )
205216 {
206- // read the opening parenthesis
207- _currentIndex++;
217+ // skip any leading whitespace
218+ [self _scanWhitespace ];
219+
220+ // scan a parameter
221+ NSString *parameter = [self _scanParameter ];
208222
209- while (_currentIndex < _stringLength )
223+ if (parameter )
210224 {
211- // skip any leading whitespace
212- [self _scanWhitespace ];
225+ // we found one!
226+ // single slash unicode sequences need to be decoded on reading
227+ [parameters addObject: [parameter stringByDecodingUnicodeSequences ]];
213228
214- // scan a parameter
215- NSString *parameter = [self _scanParameter ];
229+ // skip any trailing whitespace
230+ [self _scanWhitespace ];
216231
217- if (parameter)
232+ if (_characters[_currentIndex] == ' ,' )
233+ {
234+ // consume the comma, but loop again
235+ _currentIndex++;
236+ }
237+ else if (_characters[_currentIndex] == ' )' )
218238 {
219- // we found one!
220- // single slash unicode sequences need to be decoded on reading
221- [parameters addObject: [parameter stringByDecodingUnicodeSequences ]];
222-
223- // skip any trailing whitespace
224- [self _scanWhitespace ];
225-
226- if (_characters[_currentIndex] == ' ,' )
227- {
228- // consume the comma, but loop again
229- _currentIndex++;
230- }
231- else if (_characters[_currentIndex] == ' )' )
232- {
233- // comsume the closing paren and break
234- _currentIndex++;
235- break ;
236- }
237- else
238- {
239- // some other character = not syntactically valid = exit
240- return NO ;
241- }
239+ // comsume the closing paren and break
240+ _currentIndex++;
241+ break ;
242242 }
243243 else
244244 {
245- // we were unable to scan a valid parameter
246- // therefore something must be wrong and we should exit
245+ // some other character = not syntactically valid = exit
247246 return NO ;
248247 }
248+ }
249+ else
250+ {
251+ // we were unable to scan a valid parameter
252+ // therefore something must be wrong and we should exit
253+ return NO ;
249254 }
250255 }
256+ }
257+
258+ NSArray *expectedParameters = [_validMacros objectForKey: macroName];
259+ if ([expectedParameters count ] == [parameters count ])
260+ {
261+ // hooray, we successfully scanned!
251262
252- NSArray *expectedParameters = [_validMacros objectForKey: macroName ];
253- if ([expectedParameters count ] == [parameters count ])
263+ DTLocalizableStringEntry *entry = [[DTLocalizableStringEntry alloc ] init ];
264+ for ( NSUInteger i = 0 ; i < [parameters count ]; ++i )
254265 {
255- // hooray, we successfully scanned!
256-
257- DTLocalizableStringEntry *entry = [[DTLocalizableStringEntry alloc ] init ];
258- for (NSUInteger i = 0 ; i < [parameters count ]; ++i)
259- {
260- NSString *property = [expectedParameters objectAtIndex: i];
261- NSString *value = [parameters objectAtIndex: i];
262- [entry setValue: value forKey: property];
263- }
264-
265- if (_entryFoundCallback)
266- {
267- _entryFoundCallback (entry);
268- }
269-
270- return YES ;
271- } else {
272- NSLog (@" mismaatch" );
266+ NSString *property = [expectedParameters objectAtIndex: i];
267+ NSString *value = [parameters objectAtIndex: i];
268+ [entry setValue: value forKey: property];
269+ }
270+
271+ if (_entryFoundCallback)
272+ {
273+ _entryFoundCallback (entry);
273274 }
275+
276+ return YES ;
277+ }
278+ else
279+ {
280+ NSLog (@" mismatch of parameters for %@ macro" , macroName);
281+ }
274282
275283 return NO ;
276284}
0 commit comments