From 4e8a929769c9a4db70ef578944e3db9658bd6517 Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Thu, 21 Aug 2008 23:35:34 +0000 Subject: [PATCH] Monotone-Parent: 1af347fe96abc17587172e207fcbf80e98d7f6f8 Monotone-Revision: 81291ebd761827191a007b02ee603cd1f31fc8a3 Monotone-Author: flachapelle@inverse.ca Monotone-Date: 2008-08-21T23:35:34 Monotone-Branch: ca.inverse.sogo --- SoObjects/SOGo/NSArray+Utilities.h | 2 +- SoObjects/SOGo/NSArray+Utilities.m | 3 +- SoObjects/SOGo/NSString+Utilities.m | 85 +++++++++++++++-------------- 3 files changed, 45 insertions(+), 45 deletions(-) diff --git a/SoObjects/SOGo/NSArray+Utilities.h b/SoObjects/SOGo/NSArray+Utilities.h index e65bef886..96825c3d5 100644 --- a/SoObjects/SOGo/NSArray+Utilities.h +++ b/SoObjects/SOGo/NSArray+Utilities.h @@ -55,7 +55,7 @@ - (void) addObjectUniquely: (id) object; - (void) addRange: (NSRange) newRange; -- (BOOL) hasRangeIntersection: (NSRange) testRange withOffset: (unsigned int) offset; +- (BOOL) hasRangeIntersection: (NSRange) testRange; @end diff --git a/SoObjects/SOGo/NSArray+Utilities.m b/SoObjects/SOGo/NSArray+Utilities.m index c96654ecd..7cb57f193 100644 --- a/SoObjects/SOGo/NSArray+Utilities.m +++ b/SoObjects/SOGo/NSArray+Utilities.m @@ -206,7 +206,7 @@ [self addObject: NSStringFromRange (newRange)]; } -- (BOOL) hasRangeIntersection: (NSRange) testRange withOffset: (unsigned int) offset +- (BOOL) hasRangeIntersection: (NSRange) testRange { NSEnumerator *ranges; NSString *currentRangeString; @@ -221,7 +221,6 @@ while (!response && currentRangeString) { currentRange = NSRangeFromString (currentRangeString); - currentRange.location = currentRange.location + offset; if (NSLocationInRange (testRange.location, currentRange) || NSLocationInRange (NSMaxRange (testRange), currentRange)) response = YES; diff --git a/SoObjects/SOGo/NSString+Utilities.m b/SoObjects/SOGo/NSString+Utilities.m index 6fbb3d7d4..db7ec95cd 100644 --- a/SoObjects/SOGo/NSString+Utilities.m +++ b/SoObjects/SOGo/NSString+Utilities.m @@ -195,9 +195,11 @@ static NSMutableCharacterSet *urlStartChars = nil; prefix: (NSString *) prefix inRanges: (NSMutableArray *) ranges { - NSRange httpRange, currentURL, rest; - NSString *urlText, *newUrlText; - unsigned int length, matchLength, offset; + NSEnumerator *enumRanges; + NSMutableArray *newRanges; + NSRange matchRange, currentUrlRange, rest; + NSString *urlText, *newUrlText, *range; + unsigned int length, matchLength; int startLocation; if (!urlStartChars) @@ -207,56 +209,55 @@ static NSMutableCharacterSet *urlStartChars = nil; @"ABCDEFGHIJKLMNOPQRSTUVWXYZ" @"0123456789:@"]; } + newRanges = [NSMutableArray new]; matchLength = [match length]; - httpRange = [selfCopy rangeOfString: match]; - if (httpRange.location != NSNotFound) + rest.location = -1; + + matchRange = [selfCopy rangeOfString: match]; + while (matchRange.location != NSNotFound) { - offset = 0; - startLocation = httpRange.location; - while (startLocation > -1 + startLocation = matchRange.location; + while (startLocation > rest.location && [urlStartChars characterIsMember: [selfCopy characterAtIndex: startLocation]]) startLocation--; - httpRange.location = startLocation + 1; - } - while (httpRange.location != NSNotFound) - { - currentURL = [selfCopy _rangeOfURLInRange: httpRange]; - if ([ranges hasRangeIntersection: httpRange withOffset: offset]) - rest.location = NSMaxRange(httpRange); + matchRange.location = startLocation + 1; + + currentUrlRange = [selfCopy _rangeOfURLInRange: matchRange]; + + if ([ranges hasRangeIntersection: currentUrlRange]) + rest.location = NSMaxRange(currentUrlRange); else { - currentURL = [selfCopy _rangeOfURLInRange: httpRange]; - urlText = [selfCopy substringFromRange: currentURL]; - if ([urlText length] > matchLength) - { - if ([urlText hasPrefix: prefix]) prefix = @""; - - newUrlText = [NSString stringWithFormat: @"%@", - prefix, urlText, urlText]; - [selfCopy replaceCharactersInRange: currentURL - withString: newUrlText]; - currentURL - = NSMakeRange (currentURL.location, [newUrlText length]); - [ranges addRange: currentURL]; - offset = offset + 9 + [prefix length]; - } - rest.location = NSMaxRange(currentURL); + if (currentUrlRange.length > matchLength) + [newRanges addRange: currentUrlRange]; + rest.location = NSMaxRange(currentUrlRange); } length = [selfCopy length]; rest.length = length - rest.location; - httpRange = [selfCopy rangeOfString: match - options: 0 range: rest]; - if (httpRange.location != NSNotFound) - { - startLocation = httpRange.location; - while (startLocation > -1 - && [urlStartChars characterIsMember: - [selfCopy characterAtIndex: startLocation]]) - startLocation--; - httpRange.location = startLocation + 1; - } + matchRange = [selfCopy rangeOfString: match + options: 0 range: rest]; + } + + // Make the substitutions, in reverse order + enumRanges = [newRanges reverseObjectEnumerator]; + range = [enumRanges nextObject]; + while (range) + { + currentUrlRange = NSRangeFromString(range); + urlText = [selfCopy substringFromRange: currentUrlRange]; + if ([urlText hasPrefix: prefix]) prefix = @""; + newUrlText = [NSString stringWithFormat: @"%@", + prefix, urlText, urlText]; + [selfCopy replaceCharactersInRange: currentUrlRange + withString: newUrlText]; + + // Add range for further substitutions + currentUrlRange = NSMakeRange (currentUrlRange.location, [newUrlText length]); + [ranges addRange: currentUrlRange]; + + range = [enumRanges nextObject]; } }