diff --git a/Foundation/CPString.j b/Foundation/CPString.j index f5e9ad007..2a6607dc7 100644 --- a/Foundation/CPString.j +++ b/Foundation/CPString.j @@ -346,10 +346,9 @@ var CPStringRegexSpecialCharacters = [ } /*! - Finds the range of characters in the receiver - where the specified string exists in the given range - of the receiver.The search is subject to the options specified in the - specified mask which can be a combination of: + Finds the range of characters in the receiver where the specified string + exists in the given range of the receiver.The search is subject to the + options specified in the specified mask which can be a combination of:
CPCaseInsensitiveSearch
CPLiteralSearch
@@ -360,8 +359,10 @@ var CPStringRegexSpecialCharacters = [
@param aString the string to search for
@param aMask the options to use in the search
@param aRange the range of the receiver in which to search for
- @return the range of characters in the receiver. If the string was not found,
- or if it was @"", the range will be {CPNotFound, 0}.
+ @return the range of characters in the receiver. The range is relative to
+ the start of the full string and not the passed-in range. If the
+ string was not found, or if it was @"", the range will be
+ {CPNotFound, 0}.
*/
- (CPRange)rangeOfString:(CPString)aString options:(int)aMask range:(CPrange)aRange
{
@@ -389,7 +390,10 @@ var CPStringRegexSpecialCharacters = [
else
location = string.indexOf(aString);
- return CPMakeRange(location, location == CPNotFound ? 0 : aString.length);
+ if (location == CPNotFound)
+ return CPMakeRange(CPNotFound, 0);
+
+ return CPMakeRange(location + (aRange ? aRange.location : 0), aString.length);
}
//Replacing Substrings
diff --git a/Tests/Foundation/CPStringTest.j b/Tests/Foundation/CPStringTest.j
index 171ec53ea..b84b45606 100644
--- a/Tests/Foundation/CPStringTest.j
+++ b/Tests/Foundation/CPStringTest.j
@@ -397,7 +397,26 @@
[self assert:endsTest.length equals:anchoredSuffixRange.length message:"anchored backwards search for whole string (length)"];
anchoredSuffixRange = [endsTest rangeOfString:@"" options:(CPAnchoredSearch | CPCaseInsensitiveSearch | CPBackwardsSearch)];
- [self assert:CPNotFound equals:anchoredSuffixRange.location message:"anchored backwards search for nothing"];
+ [self assert:CPNotFound equals:anchoredSuffixRange.location message:"anchored backwards search for nothing (location)"];
+ [self assert:0 equals:anchoredSuffixRange.length message:"anchored backwards search for nothing (length)"];
+}
+
+- (void)testRangeOfString_options_range
+{
+ var testString = @"In another life you would have made a excellent criminal.",
+ hitRange;
+
+ hitRange = [testString rangeOfString:@"life" options:0 range:CPMakeRange(0, testString.length)];
+ [self assert:11 equals:hitRange.location message:@"search for 'life' in full range (location)"];
+ [self assert:4 equals:hitRange.length message:@"search for 'life' in full range (position)"];
+
+ hitRange = [testString rangeOfString:@"i" options:0 range:CPMakeRange(0, testString.length)];
+ [self assert:12 equals:hitRange.location message:@"search for 'i' in full range (location)"];
+ [self assert:1 equals:hitRange.length message:@"search for 'i' in full range (position)"];
+
+ hitRange = [testString rangeOfString:@"i" options:0 range:CPMakeRange(10, 20)];
+ [self assert:12 equals:hitRange.location message:@"search for 'i' in partial range (location)"];
+ [self assert:1 equals:hitRange.length message:@"search for 'i' in partial range (position)"];
}
@end