Skip to content

@NikolaiRuhe /gist:5546537
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.