From 0323c7531a390e9d858ea57810d50ce4a828f35d Mon Sep 17 00:00:00 2001 From: Mitchell Livingston Date: Thu, 7 Mar 2013 23:55:31 +0000 Subject: [PATCH] Simplify betterComponentsSeparatedByCharactersInSet: --- macosx/NSStringAdditions.h | 2 +- macosx/NSStringAdditions.m | 29 +++++++++++++++-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/macosx/NSStringAdditions.h b/macosx/NSStringAdditions.h index 7cbf8325e..202a70502 100644 --- a/macosx/NSStringAdditions.h +++ b/macosx/NSStringAdditions.h @@ -45,6 +45,6 @@ - (NSComparisonResult) compareNumeric: (NSString *) string; //simple compare method for strings with numbers (works for IP addresses) -- (NSArray *) betterComponentsSeparatedByCharactersInSet: (NSCharacterSet *) separator; //like betterComponentsSeparatedByCharactersInSet:, but excludes blank values +- (NSArray *) betterComponentsSeparatedByCharactersInSet: (NSCharacterSet *) separators; //like componentsSeparatedByCharactersInSet:, but excludes blank values @end diff --git a/macosx/NSStringAdditions.m b/macosx/NSStringAdditions.m index 841dc0101..b1cf62a80 100644 --- a/macosx/NSStringAdditions.m +++ b/macosx/NSStringAdditions.m @@ -209,30 +209,31 @@ return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]]; } -- (NSArray *) betterComponentsSeparatedByCharactersInSet: (NSCharacterSet *) separator +- (NSArray *) betterComponentsSeparatedByCharactersInSet: (NSCharacterSet *) separators { NSMutableArray * components = [NSMutableArray array]; - NSUInteger i = 0; - while (i < [self length]) + NSCharacterSet * includededCharSet = [separators invertedSet]; + NSUInteger index = 0; + const NSUInteger fullLength = [self length]; + do { - const NSRange range = [self rangeOfCharacterFromSet: separator options: 0 range: NSMakeRange(i, [self length]-i)]; + const NSUInteger start = [self rangeOfCharacterFromSet: includededCharSet options: 0 range: NSMakeRange(index, fullLength - index)].location; + if (start == NSNotFound) + break; - if (range.location == NSNotFound) + const NSRange endRange = [self rangeOfCharacterFromSet: separators options: 0 range: NSMakeRange(start, fullLength - start)]; + if (endRange.location == NSNotFound) { - [components addObject: [self substringFromIndex: i]]; + [components addObject: [self substringFromIndex: start]]; break; } - else if (range.location != i) - { - const NSUInteger length = range.location - i; - [components addObject: [self substringWithRange: NSMakeRange(i, length)]]; - - i += length; - } - i += range.length; + [components addObject: [self substringWithRange: NSMakeRange(start, endRange.location - start)]]; + + index = NSMaxRange(endRange); } + while (YES); return components; } -- 2.40.0