public
Last active

Simple demo code illustrating the advantages of enumerateSubstringsInRange over componentsSeparatedByString.

  • Download Gist
gistfile1.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
//
// 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;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.