Skip to content

Instantly share code, notes, and snippets.

@jparise
Created January 20, 2012 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jparise/1650004 to your computer and use it in GitHub Desktop.
Save jparise/1650004 to your computer and use it in GitHub Desktop.
NSStringFromIndexSet()
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