Fixed: CPString rangeOfString:options:range returned locations relative to the passed-in range instead of the full string.

This commit is contained in:
Alexander Ljungberg
2011-04-10 21:05:43 -04:00
parent a33dd51d8e
commit 91ef6bf925
2 changed files with 31 additions and 8 deletions
+11 -7
View File
@@ -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:
<pre>
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
+20 -1
View File
@@ -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