Ignore:
Timestamp:
Mar 7, 2013, 11:55:31 PM (9 years ago)
Author:
livings124
Message:

Simplify betterComponentsSeparatedByCharactersInSet:

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/NSStringAdditions.m

    r13510 r14045  
    210210}
    211211
    212 - (NSArray *) betterComponentsSeparatedByCharactersInSet: (NSCharacterSet *) separator
     212- (NSArray *) betterComponentsSeparatedByCharactersInSet: (NSCharacterSet *) separators
    213213{
    214214    NSMutableArray * components = [NSMutableArray array];
    215215   
    216     NSUInteger i = 0;
    217     while (i < [self length])
    218     {
    219         const NSRange range = [self rangeOfCharacterFromSet: separator options: 0 range: NSMakeRange(i, [self length]-i)];
    220        
    221         if (range.location == NSNotFound)
     216    NSCharacterSet * includededCharSet = [separators invertedSet];
     217    NSUInteger index = 0;
     218    const NSUInteger fullLength = [self length];
     219    do
     220    {
     221        const NSUInteger start = [self rangeOfCharacterFromSet: includededCharSet options: 0 range: NSMakeRange(index, fullLength - index)].location;
     222        if (start == NSNotFound)
     223            break;
     224       
     225        const NSRange endRange = [self rangeOfCharacterFromSet: separators options: 0 range: NSMakeRange(start, fullLength - start)];
     226        if (endRange.location == NSNotFound)
    222227        {
    223             [components addObject: [self substringFromIndex: i]];
     228            [components addObject: [self substringFromIndex: start]];
    224229            break;
    225230        }
    226         else if (range.location != i)
    227         {
    228             const NSUInteger length = range.location - i;
    229             [components addObject: [self substringWithRange: NSMakeRange(i, length)]];
    230            
    231             i += length;
    232         }
    233        
    234         i += range.length;
    235     }
     231       
     232        [components addObject: [self substringWithRange: NSMakeRange(start, endRange.location - start)]];
     233       
     234        index = NSMaxRange(endRange);
     235    }
     236    while (YES);
    236237   
    237238    return components;
Note: See TracChangeset for help on using the changeset viewer.