Created
July 17, 2012 16:21
-
-
Save wokalski/3130403 to your computer and use it in GitHub Desktop.
NSRangeContainsRange - check if NSRange contains the other NSRange :)
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
bool NSRangeContainsRange (NSRange range1, NSRange range2) { | |
BOOL retval = NO; | |
if (range1.location <= range2.location && range1.location+range1.length >= range2.length+range2.location) { | |
retval = YES;; | |
} | |
return retval; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, I see: my previously posted line fails in the case that the second range has length zero and it's location is not within the first range, thanks for pointing that out.
To fix this one would have to add another check (assuming that a range with length zero can actually be considered "contained" in another range):
which is not really simpler than the original solution.
So, I conclude with a different attempt which in my opinion is slightly more readable than the original one:
Note that both solutions will return false whenever
range1.length == 0
, because a range with size zero cannot contain any other range (not even a location). The original implementation will return true for this case ifrange1 == range2
.