From a33dd51d8eee5838fe44b6bce1afb0ed2ed39bcd Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Sun, 10 Apr 2011 20:38:51 -0400 Subject: [PATCH] Fixed: searching a string for @"" with CPString rangeOfString returned results other than CPNotFound. --- Foundation/CPString.j | 6 +++++- Tests/Foundation/CPStringTest.j | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Foundation/CPString.j b/Foundation/CPString.j index 117a82a60..f5e9ad007 100644 --- a/Foundation/CPString.j +++ b/Foundation/CPString.j @@ -361,10 +361,14 @@ var CPStringRegexSpecialCharacters = [ @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, - the \c length of the range will be 0. + or if it was @"", the range will be {CPNotFound, 0}. */ - (CPRange)rangeOfString:(CPString)aString options:(int)aMask range:(CPrange)aRange { + // Searching for @"" always returns CPNotFound. + if (!aString) + return CPMakeRange(CPNotFound, 0); + var string = (aRange == nil) ? self : [self substringWithRange:aRange], location = CPNotFound; diff --git a/Tests/Foundation/CPStringTest.j b/Tests/Foundation/CPStringTest.j index 8066bf933..171ec53ea 100644 --- a/Tests/Foundation/CPStringTest.j +++ b/Tests/Foundation/CPStringTest.j @@ -388,6 +388,16 @@ [self assert:8 equals:unAnchoredSuffixRange.location message:"backwards search for LAG"]; [self assert:CPNotFound equals:anchoredSuffixRange.location message:"anchored backwards search for LAG"]; + + anchoredSuffixRange = [endsTest rangeOfString:@"AGE" options:(CPAnchoredSearch | CPCaseInsensitiveSearch | CPBackwardsSearch)]; + [self assert:9 equals:anchoredSuffixRange.location message:"anchored backwards search for AGE"]; + + anchoredSuffixRange = [endsTest rangeOfString:endsTest options:(CPAnchoredSearch | CPCaseInsensitiveSearch | CPBackwardsSearch)]; + [self assert:0 equals:anchoredSuffixRange.location message:"anchored backwards search for whole string (location)"]; + [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"]; } @end