Skip to content

Commit 78f7620

Browse files
author
Tammo Freese
committed
Performance: Use NSLiteralSearch for speedup, reimplemented stringByNumberingFormatPlaceholders using regex
1 parent 1ce28f5 commit 78f7620

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

Core/Source/NSString+DTLocalizableStringScanner.m

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,34 @@ @implementation NSString (DTStringFileParser)
1212

1313
- (NSString *)stringByNumberingFormatPlaceholders
1414
{
15-
NSMutableString *tmpString = [NSMutableString string];
15+
static dispatch_once_t onceToken;
16+
static NSRegularExpression *matchNonEscapedPercent = nil;
17+
dispatch_once(&onceToken, ^{
18+
matchNonEscapedPercent = [NSRegularExpression regularExpressionWithPattern:@"(?<=[^%]|^)(?:(?:%%)*)(%)(?:[^%]|$)" options:0 error:NULL];
19+
});
1620

17-
NSScanner *scanner = [NSScanner scannerWithString:self];
18-
scanner.charactersToBeSkipped = nil;
19-
20-
NSUInteger placeholderCount = 0;
21+
__block NSMutableString *tmpString = nil;
22+
__block NSUInteger placeholderCount = 0;
23+
__block NSUInteger lastLocation = 0;
2124

22-
while (![scanner isAtEnd])
23-
{
24-
// scan until percent
25-
NSString *part = nil;
26-
if ([scanner scanUpToString:@"%" intoString:&part])
27-
{
28-
[tmpString appendString:part];
29-
}
30-
31-
if ([scanner scanString:@"%" intoString:NULL])
32-
{
33-
// scan for escaped percent
34-
if ([scanner scanString:@"%" intoString:NULL])
35-
{
36-
[tmpString appendString:@"%%"];
37-
}
38-
else
39-
{
40-
// just insert the number
41-
placeholderCount++;
42-
[tmpString appendFormat:@"%%%d$", placeholderCount];
25+
[matchNonEscapedPercent enumerateMatchesInString:self options:0 range:NSMakeRange(0, [self length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop) {
26+
placeholderCount++;
27+
NSUInteger currentLocation = [match rangeAtIndex:1].location;
28+
if (placeholderCount >= 2) {
29+
if (placeholderCount == 2) {
30+
tmpString = [NSMutableString string];
31+
[tmpString appendString:[self substringToIndex:lastLocation + 1]];
32+
[tmpString appendString:@"1$"];
4333
}
34+
[tmpString appendString:[self substringWithRange:NSMakeRange(lastLocation + 1, currentLocation - lastLocation)]];
35+
[tmpString appendFormat:@"%d$", placeholderCount];
4436
}
45-
}
37+
lastLocation = currentLocation;
38+
}];
4639

47-
// only number if there is more than one placeholder
48-
if (placeholderCount>1)
40+
if (placeholderCount > 1)
4941
{
42+
[tmpString appendString:[self substringWithRange:NSMakeRange(lastLocation + 1, [self length] - (lastLocation + 1))]];
5043
return tmpString;
5144
}
5245
else
@@ -199,7 +192,7 @@ - (NSString *)stringByRemovingSlashEscapes
199192
- (NSString *)stringByDecodingUnicodeSequences
200193
{
201194

202-
if ([self rangeOfString:@"\\"].location == NSNotFound) {
195+
if ([self rangeOfString:@"\\" options:NSLiteralSearch].location == NSNotFound) {
203196
return [self copy];
204197
}
205198

@@ -313,7 +306,7 @@ - (NSString *)stringByDecodingUnicodeSequences
313306

314307
- (NSString *)stringByReplacingSlashEscapes
315308
{
316-
if ([self rangeOfString:@"\\"].location == NSNotFound) {
309+
if ([self rangeOfString:@"\\" options:NSLiteralSearch].location == NSNotFound) {
317310
return [self copy];
318311
}
319312

0 commit comments

Comments
 (0)