CPString -capitalizedString fixed

Reviewed by me.

[#90 state:resolved responsible:tlrobinson]
This commit is contained in:
Tom Robinson
2008-10-05 18:15:36 -07:00
parent 6b6a6a6f1d
commit bf28b8bbc6
2 changed files with 35 additions and 50 deletions
+6 -13
View File
@@ -465,22 +465,15 @@ var CPStringHashes = new objj_dictionary();
*/
- (CPString)capitalizedString
{
var i = 0,
last = true,
capitalized = self;
for(; i < length; ++i)
var parts = self.split(/\b/g); // split on word boundaries
for (var i = 0; i < parts.length; i++)
{
var character = charAt(i);
if (character == ' ' || character == '\t' || character == '\n') last = true;
if (i == 0 || (/\s$/).test(parts[i-1])) // only capitalize if previous token was whitespace
parts[i] = parts[i].substring(0, 1).toUpperCase() + parts[i].substring(1).toLowerCase();
else
{
if (last) capitalized = capitalized.substr(0, i - 1) + character.toUpperCase() + capitalized.substr(i);
last = false;
}
parts[i] = parts[i].toLowerCase();
}
return capitalized;
return parts.join("");
}
/*
+29 -37
View File
@@ -24,47 +24,39 @@ import <Foundation/CPString.j>
message:"stringByAppendingFormat: expected:" + expectedString + " actual:" + actualString];
}
- (void) testBoolValue
- (void)testBoolValue
{
var testString = @" 090";
[self assertTrue:[testString boolValue] message:"boolValue for the string " + testString + " should return true"];
var testStrings = [
[" 090", YES],
[" YES", YES],
[" true", YES],
[" True", YES],
[" tTR", YES],
[" +98", YES],
[" -98", YES],
[" +08", YES],
[" -98", YES],
[" NO", NO],
[" -N00", NO],
[" 00", NO],
[" -00", NO]
];
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"];
for (var i = 0; i < testStrings.length; i++)
[self assert:[testStrings[i][0] boolValue] equals:testStrings[i][1]];
}
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"];
- (void)testCapitalizedString
{
var testStrings = [
["", ""],
["hElLo wOrLd", "Hello World"],
[" monkey-Cow", " Monkey-cow"],
["tHe QuicK bRowN-Fox JumPed_Over +the LaZy%dog", "The Quick Brown-fox Jumped_over +the Lazy%dog"]
];
for (var i = 0; i < testStrings.length; i++)
[self assert:[testStrings[i][0] capitalizedString] equals:testStrings[i][1]];
}