Skip to content

Instantly share code, notes, and snippets.

@NikolaiRuhe
Last active December 17, 2015 03:49
Show Gist options
  • Save NikolaiRuhe/5546537 to your computer and use it in GitHub Desktop.
Save NikolaiRuhe/5546537 to your computer and use it in GitHub Desktop.
Simple demo code illustrating the advantages of enumerateSubstringsInRange over componentsSeparatedByString.
//
// Created by Nikolai Ruhe on 2013-05-09.
// Copyright (c) 2013 Savoy Software. All rights reserved.
//
#import <Foundation/Foundation.h>
static NSString *replaceLongWords(NSString *originalString, NSString *replaceWord, NSUInteger maxChar)
{
NSMutableString *result = [NSMutableString stringWithCapacity:[originalString length]];
__block NSUInteger location = 0;
[originalString enumerateSubstringsInRange:(NSRange){0, [originalString length]}
options:NSStringEnumerationByWords | NSStringEnumerationLocalized | NSStringEnumerationSubstringNotRequired
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
if (substringRange.length > maxChar) {
NSString *charactersBetweenLongWords = [originalString substringWithRange:(NSRange){ location, substringRange.location - location }];
[result appendString:charactersBetweenLongWords];
[result appendString:replaceWord];
location = substringRange.location + substringRange.length;
}
}];
[result appendString:[originalString substringFromIndex:location]];
return result;
}
static NSString *replaceLongWords2(NSString *originalString, NSString *replaceWord, NSUInteger maxChar)
{
NSMutableArray *array = [[originalString componentsSeparatedByString:@" "] mutableCopy];
for (int i = 0; i < [array count]; i++) {
NSString *str_ = [array objectAtIndex:i];
if ([str_ length] > maxChar)
[array replaceObjectAtIndex:i withObject:replaceWord];
}
return [array componentsJoinedByString:@" "];
}
static NSString *replaceLongWords3(NSString *originalString, NSString *replaceWord, NSUInteger maxChar)
{
NSString *overSixCharsPattern = @"\\b[\\w]{7,}\\b";
NSString *result = [originalString stringByReplacingOccurrencesOfString: overSixCharsPattern
withString: replaceWord
options: NSRegularExpressionSearch
range: NSMakeRange(0, originalString.length)];
return result;
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *text = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.catb.org/jargon/oldversions/jarg447.txt"]
encoding:NSISOLatin1StringEncoding
error:NULL];
{
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
NSString *result = replaceLongWords2(text, @"–", 6);
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
NSLog(@"componentsSeparatedByString: time: %g", end - start);
NSLog(@"result: \"%@\"", [result substringToIndex:[result length] > 1000 ? 1000 : [result length]]);
}
{
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
NSString *result = replaceLongWords(text, @"–", 6);
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
NSLog(@"enumerateSubstringsInRange: time: %g", end - start);
NSLog(@"result: \"%@\"", [result substringToIndex:[result length] > 1000 ? 1000 : [result length]]);
}
{
CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
NSString *result = replaceLongWords3(text, @"–", 6);
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
NSLog(@"stringByReplacingOccurrencesOfString: time: %g", end - start);
NSLog(@"result: \"%@\"", [result substringToIndex:[result length] > 1000 ? 1000 : [result length]]);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment