Created
January 20, 2012 22:33
-
-
Save jparise/1650004 to your computer and use it in GitHub Desktop.
NSStringFromIndexSet()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSString * NSStringFromIndexSet(NSIndexSet *indexSet) { | |
NSMutableString *string = nil; | |
NSRange range = NSMakeRange([indexSet firstIndex], 1); | |
while (range.location != NSNotFound) { | |
NSUInteger nextIndex = [indexSet indexGreaterThanIndex:range.location]; | |
while (nextIndex == range.location + range.length) { | |
range.length++; | |
nextIndex = [indexSet indexGreaterThanIndex:nextIndex]; | |
} | |
if (string == nil) { | |
string = [NSMutableString string]; | |
} else if (string.length) { | |
[string appendString:@","]; | |
} | |
if (range.length == 1) { | |
[string appendFormat:@"%u", range.location]; | |
} else { | |
NSUInteger firstIndex = range.location; | |
NSUInteger lastIndex = firstIndex + range.length - 1; | |
[string appendFormat:@"%u-%u", firstIndex, lastIndex]; | |
} | |
range.location = nextIndex; | |
range.length = 1; | |
} | |
return string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment