Skip to content

Commit

Permalink
implemented morphing algorithm and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomknig committed Jun 13, 2014
1 parent 085f1cd commit a00d180
Show file tree
Hide file tree
Showing 5 changed files with 417 additions and 39 deletions.
8 changes: 8 additions & 0 deletions Classes/NSString+Morphing.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

#import <Foundation/Foundation.h>

#define kTOMSDictionaryKeyMergedString @"mergedString"
#define kTOMSDictionaryKeyAdditionRanges @"additionRanges"
#define kTOMSDictionaryKeyDeletionRanges @"deletionRanges"

@interface NSString (Morphing)

- (NSDictionary *)toms_mergeIntoString:(NSString *)string;

- (NSDictionary *)toms_mergeIntoString:(NSString *)string lookAheadRadius:(NSUInteger)lookAheadRadius;

@end
97 changes: 97 additions & 0 deletions Classes/NSString+Morphing.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,101 @@

@implementation NSString (Morphing)

- (NSDictionary *)toms_mergeIntoString:(NSString *)string
{
return [self toms_mergeIntoString:string
lookAheadRadius:6];
}

- (NSDictionary *)toms_mergeIntoString:(NSString *)string
lookAheadRadius:(NSUInteger)lookAheadRadius
{
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];

NSMutableString *mergeString = [[NSMutableString alloc] init];
NSMutableArray *additionRanges = [[NSMutableArray alloc] init];
NSMutableArray *deletionRanges = [[NSMutableArray alloc] init];

__block int startLocation, endLocation;
__block int ownIdx = 0, numberOfInsertions = 0;

[self toms_enumerateCharacters:^(BOOL *stopOwnEnumeration, const unichar ownChar, NSUInteger index){
startLocation = -1;
endLocation = -1;

[string toms_enumerateCharacters:^(BOOL *stopAlienEnumeration, const unichar alienChar, NSUInteger alienIdx){
if (ownIdx <= alienIdx++) {
if (startLocation < 0) {
startLocation = ownIdx;
}

if (ownChar == alienChar) {
endLocation = alienIdx;

[mergeString appendString:[string substringWithRange:NSMakeRange(startLocation, endLocation - startLocation)]];
ownIdx = alienIdx;
*stopAlienEnumeration = YES;
}

if (alienIdx - ownIdx >= lookAheadRadius) {
*stopAlienEnumeration = YES;
}
}
}];

if (endLocation >= 0) {
if (endLocation - startLocation - 1 > 0) {
NSRange deletionRange = NSMakeRange(startLocation + numberOfInsertions, endLocation - startLocation - 1);
[deletionRanges addObject:[NSValue valueWithRange:deletionRange]];
}
} else {
NSRange additionRange = NSMakeRange(mergeString.length, 1);
[additionRanges addObject:[NSValue valueWithRange:additionRange]];
[mergeString appendFormat:@"%c", ownChar];
++numberOfInsertions;
}
}];


NSUInteger alienStringLength = string.length;
if (ownIdx < alienStringLength) {
NSUInteger deletionLength = alienStringLength - ownIdx;
NSRange deletionRange = NSMakeRange(mergeString.length, deletionLength);
[deletionRanges addObject:[NSValue valueWithRange:deletionRange]];
[mergeString appendString:[string substringWithRange:NSMakeRange(ownIdx, deletionLength)]];
}

result[kTOMSDictionaryKeyMergedString] = mergeString;
result[kTOMSDictionaryKeyAdditionRanges] = additionRanges;
result[kTOMSDictionaryKeyDeletionRanges] = deletionRanges;

return result;
}


-(void)toms_enumerateCharacters:(void(^)(BOOL *stop, const unichar aChar, NSUInteger index))enumerationBlock
{
const unichar *chars = CFStringGetCharactersPtr((__bridge CFStringRef)self);
BOOL stop = NO;

if (chars != NULL) {
NSUInteger index = 0;
while (*chars && !stop) {
enumerationBlock(&stop, *chars, index);
chars++;
index++;
}
} else {
SEL sel = @selector(characterAtIndex:);
unichar (*charAtIndex)(id, SEL, NSUInteger) = (typeof(charAtIndex)) [self methodForSelector:sel];
for (NSUInteger i = 0; i < self.length; i++) {
const unichar c = charAtIndex(self, sel, i);
enumerationBlock(&stop, c, i);
if (stop) {
break;
}
}
}
}

@end
1 change: 1 addition & 0 deletions Classes/TOMSMorphingLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import <UIKit/UIKit.h>
#import "NSString+Morphing.h"

@interface TOMSMorphingLabel : UILabel

Expand Down

This file was deleted.

Loading

0 comments on commit a00d180

Please sign in to comment.