mirror of
https://github.com/transmission/transmission.git
synced 2025-12-20 02:18:42 +00:00
Simplify betterComponentsSeparatedByCharactersInSet:
This commit is contained in:
@@ -45,6 +45,6 @@
|
|||||||
|
|
||||||
- (NSComparisonResult) compareNumeric: (NSString *) string; //simple compare method for strings with numbers (works for IP addresses)
|
- (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
|
@end
|
||||||
|
|||||||
@@ -209,30 +209,31 @@
|
|||||||
return [self compare: string options: comparisonOptions range: NSMakeRange(0, [self length]) locale: [NSLocale currentLocale]];
|
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];
|
NSMutableArray * components = [NSMutableArray array];
|
||||||
|
|
||||||
NSUInteger i = 0;
|
NSCharacterSet * includededCharSet = [separators invertedSet];
|
||||||
while (i < [self length])
|
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;
|
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;
|
return components;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user