diff --git a/Foundation/CPString.j b/Foundation/CPString.j
index 691a9ee1b..d98b64a42 100644
--- a/Foundation/CPString.j
+++ b/Foundation/CPString.j
@@ -128,14 +128,15 @@ var CPStringHashes = new objj_dictionary();
{
if (!format)
[CPException raise:CPInvalidArgumentException
- reason:"stringWithString: the format can't be 'nil'"];
+ reason:"initWithFormat: the format can't be 'nil'"];
self = sprintf.apply(this, Array.prototype.slice.call(arguments, 2));
return self;
}
/*
- Creates a new string using C printf-style formatting. First argument should be a constant format string, like ' "float val = %f" ', remaining arguments should be the variables to print the values of, comma-separated.
+ Creates a new string using C printf-style formatting. First argument should be a constant format string,
+ like ' "float val = %f" ', remaining arguments should be the variables to print the values of, comma-separated.
@param format the format to be used, printf-style
@return the initialized length will be 0.
@@ -269,9 +287,7 @@ var CPStringHashes = new objj_dictionary();
*/
- (CPRange)rangeOfString:(CPString)aString
{
- var location = indexOf(aString);
-
- return CPMakeRange(location, location == CPNotFound ? 0 : aString.length);
+ return [self rangeOfString:aString options:0];
}
/*
@@ -279,13 +295,13 @@ var CPStringHashes = new objj_dictionary();
where the specified string exists. The search
is subject to the options specified in the
specified mask which can be a combination of:
-
-CPCaseInsensitiveSearch -CPLiteralSearch -CPBackwardsSearch -CPAnchoredSearch -CPNumericSearch -+
+ CPCaseInsensitiveSearch + CPLiteralSearch + CPBackwardsSearch + CPAnchoredSearch + CPNumericSearch +@param aString the string to search for @param aMask the options to use in the search @return the range of characters in the receiver. If the string was not found, @@ -293,23 +309,78 @@ CPNumericSearch */ - (CPRange)rangeOfString:(CPString)aString options:(int)aMask { - var string = self, + return [self rangeOfString:aString options:aMask range:nil]; +} + +/* + 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 + CPBackwardsSearch + CPAnchoredSearch + CPNumericSearch ++ @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, + the
length of the range will be 0.
+*/
+- (CPRange)rangeOfString:(CPString)aString options:(int)aMask range:(CPrange)aRange
+{
+ var string = (aRange == nil) ? self : [self substringWithRange:aRange],
location = CPNotFound;
-
+
if (aMask & CPCaseInsensitiveSearch)
{
string = string.toLowerCase();
aString = aString.toLowerCase();
}
-
- if (CPBackwardsSearch) location = lastIndexOf(aString, aMask & CPAnchoredSearch ? length - aString.length : 0);
- else if (aMask & CPAnchoredSearch) location = substr(0, aString.length).indexOf(aString) != CPNotFound ? 0 : CPNotFound;
- else location = indexOf(aString);
-
+
+ if (aMask & CPBackwardsSearch)
+ location = string.lastIndexOf(aString, aMask & CPAnchoredSearch ? length - aString.length : 0);
+ else if (aMask & CPAnchoredSearch)
+ location = string.substr(0, aString.length).indexOf(aString) != CPNotFound ? 0 : CPNotFound;
+ else
+ location = string.indexOf(aString);
+
return CPMakeRange(location, location == CPNotFound ? 0 : aString.length);
}
+//Replacing Substrings
+
+/*
+ Returns a new string in which all occurrences of a target string in the reciever are replaced by
+ another given string.
+ @param target The string to replace.
+ @param replacement the string with which to replace the target
+*/
+
+- (CPString)stringByReplacingOccurrencesOfString:(CPString)target withString:(CPString)replacement
+{
+ return self.replace(new RegExp(target, "g"), replacement);
+}
+
+/*
+- (CPString)stringByReplacingOccurrencesOfString:(CPString)target withString:(CPString)replacement options:(int)options range:(CPRange)searchRange
+{
+ //TODO :Vijay implement the method
+
+}
+
+- (CPString)stringByReplacingCharactersInRange:(CPRange)range withString:(CPString)replacement
+{
+ //TODO :Vijay implement the method
+}
+*/
+
+
// Identifying and comparing strings
+
/*
Compares the receiver to the specified string.
@param aString the string with which to compare
@@ -433,7 +504,18 @@ CPNumericSearch
*/
- (double)doubleValue
{
- return eval(self);
+ return parseFloat(self, 10);
+}
+/*
+ Returns YES on encountering one of "Y", "y", "T", "t", or
+ a digit 1-9. Returns NO otherwise. This method skips the initial
+ whitespace characters, +,- followed by Zeroes.
+*/
+
+- (BOOL)boolValue
+{
+ var replaceRegExp = new RegExp("^\\s*[\\+,\\-]*0*");
+ return RegExp("^[Y,y,t,T,1-9]").test(self.replace(replaceRegExp, ''));
}
/*
@@ -441,7 +523,7 @@ CPNumericSearch
*/
- (float)floatValue
{
- return eval(self);
+ return parseFloat(self, 10);
}
/*
@@ -449,12 +531,12 @@ CPNumericSearch
*/
- (int)intValue
{
- return parseInt(self);
+ return parseInt(self, 10);
}
/*
Returns an the path components of this string. This
- method assumes that the string's contents is a '/'
+ method assumes that the string's content is a '/'
separated file system path.
*/
- (CPArray)pathComponents
@@ -472,6 +554,11 @@ CPNumericSearch
return substr(lastIndexOf('.') + 1);
}
+/*
+ Returns the last component of this string.
+ This method assumes that the string's content is a '/'
+ separated file system path.
+*/
- (CPString)lastPathComponent
{
var components = [self pathComponents];
@@ -484,8 +571,8 @@ CPNumericSearch
*/
- (CPString)stringByDeletingLastPathComponent
{
- // FIMXE: this is wrong: a/a/ returns a/a/.
- return substr(0, lastIndexOf('/')+1);
+ // FIXME: this is wrong: a/a/ returns a/a/.
+ return substr(0, lastIndexOf('/') + 1);
}
- (CPString)stringByStandardizingPath
@@ -509,11 +596,11 @@ String.prototype.isa = CPString;
var sprintfFormatRegex = new RegExp("([^%]+|%[\\+\\-\\ \\#0]*[0-9\\*]*(.[0-9\\*]+)?[hlL]?[cdieEfgGosuxXpn%])", "g");
var sprintfTagRegex = new RegExp("(%)([\\+\\-\\ \\#0]*)([0-9\\*]*)((.[0-9\\*]+)?)([hlL]?)([cdieEfgGosuxXpn%])");
-/**
+/*
Creates a new string using C printf-style formatting. First argument should be a constant format string, like ' "float val = %f" ', remaining arguments should be the variables to print the values of, comma-separated.
@param format the format to be used, printf-style
@return the initialized CPString
- */
+*/
function sprintf(format)
{
var format = arguments[0],
diff --git a/Tests/Foundation/CPStringTest.j b/Tests/Foundation/CPStringTest.j
new file mode 100644
index 000000000..289663cdc
--- /dev/null
+++ b/Tests/Foundation/CPStringTest.j
@@ -0,0 +1,72 @@
+import
+
+@implementation CPStringTest : OJTestCase
+
+
+- (void)testStringByReplacingOccurrencesOfStringWithString
+{
+ var expectedString = @"hello world. A new world!";
+ var dummyString = @"hello woold. A new woold!";
+ var actualString = [dummyString stringByReplacingOccurrencesOfString:@"woold" withString:@"world"];
+ [self assertTrue:(expectedString === actualString)
+ message:"stringByAppendingFormat: expected:" + expectedString + " actual:" + actualString];
+
+
+
+}
+- (void)testStringByAppendingFormat
+{
+ var format = @"%d X %d = %d";
+ var expectedString = "2 X 3 = 6";
+ var dummyString = @"";
+ var actualString = [dummyString stringByAppendingFormat:format ,2 ,3 ,6];
+ [self assertTrue:(expectedString === actualString)
+ message:"stringByAppendingFormat: expected:" + expectedString + " actual:" + actualString];
+
+}
+- (void) testBoolValue
+{
+ var testString = @" 090";
+ [self assertTrue:[testString boolValue] message:"boolValue for the string " + testString + " should return true"];
+
+ testString = @" YES";
+ [self assertTrue:[testString boolValue] message:"boolValue for the string " + testString + " should return true"];
+
+ testString = @" true";
+ [self assertTrue:[testString boolValue] message:"boolValue for the string " + testString + " should return true"];
+
+ testString = @" True";
+ [self assertTrue:[testString boolValue] message:"boolValue for the string " + testString + " should return true"];
+
+ testString = @" tTR";
+ [self assertTrue:[testString boolValue] message:"boolValue for the string " + testString + " should return true"];
+
+ testString = @" +98";
+ [self assertTrue:[testString boolValue] message:"boolValue for the string " + testString + " should return true"];
+
+ testString = @" -98";
+ [self assertTrue:[testString boolValue] message:"boolValue for the string " + testString + " should return true"];
+
+ testString = @" +08";
+ [self assertTrue:[testString boolValue] message:"boolValue for the string " + testString + " should return true"];
+
+ testString = @" -98";
+ [self assertTrue:[testString boolValue] message:"boolValue for the string " + testString + " should return true"];
+
+ testString = @" NO";
+ [self assertFalse:[testString boolValue] message:"boolValue for the string " + testString + " should return false"];
+
+ testString = @" -N00";
+ [self assertFalse:[testString boolValue] message:"boolValue for the string " + testString + " should return false"];
+
+ testString = @" 00";
+ [self assertFalse:[testString boolValue] message:"boolValue for the string " + testString + " should return false"];
+
+ testString = @" -00";
+ [self assertFalse:[testString boolValue] message:"boolValue for the string " + testString + " should return false"];
+
+}
+
+
+
+@end