Skip to content

Instantly share code, notes, and snippets.

@magneticrob
Last active February 17, 2016 16:15
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 magneticrob/39fdc2aa212a6595f0a7 to your computer and use it in GitHub Desktop.
Save magneticrob/39fdc2aa212a6595f0a7 to your computer and use it in GitHub Desktop.
- (NSArray *)inbetweenRangesForArrayOfRanges:(NSArray *)arrayOfRanges forString:(NSString *)string
{
NSMutableArray *ranges = @[].mutableCopy;
for (int i = 0; i < arrayOfRanges.count; i++)
{
// grab the first range
NSRange currentRange = [arrayOfRanges[i] rangeValue];
// add it to our return object
[ranges addObject:[NSValue valueWithRange:currentRange]];
// if the first range starts at 0, or if the length of the range is the entire string, bail out
if (currentRange.location != 0 || currentRange.length == string.length)
{
if (i == 0) // first URL starts later in the string
{
// Make the range up in between loc 0 and the start of the first range
NSRange newRange = NSMakeRange(0, currentRange.location - 1);
[ranges addObject:[NSValue valueWithRange:newRange]];
}
else // otherwise lets get going
{
NSRange previousRange = [arrayOfRanges[i - 1] rangeValue];
NSUInteger index = (previousRange.location + 1) + previousRange.length;
NSUInteger length = currentRange.location - (previousRange.length + 1);
NSRange newRange = NSMakeRange(index, length);
[ranges addObject:[NSValue valueWithRange:newRange]];
}
}
}
return ranges;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment