Permalink
Browse files

Merge branch 'master' of git@github.com:280north/cappuccino

  • Loading branch information...
2 parents 5cb9f5f + a7bd5e1 commit d4b9c4c5e1eae114e864a2209e964671ed1ed7a1 @tolmasky tolmasky committed May 27, 2009
Showing with 22 additions and 3 deletions.
  1. +13 −1 Foundation/CPString.j
  2. +1 −1 Foundation/Rakefile
  3. +8 −1 Tests/Foundation/CPStringTest.j
View
14 Foundation/CPString.j
@@ -58,6 +58,12 @@ CPNumericSearch = 64;
var CPStringHashes = new objj_dictionary();
+var CPStringRegexSpecialCharacters = [
+ '/', '.', '*', '+', '?', '|', '$', '^',
+ '(', ')', '[', ']', '{', '}', '\\'
+ ],
+ CPStringRegexEscapeExpression = new RegExp("(\\" + CPStringRegexSpecialCharacters.join("|\\") + ")", 'g');
+
/*!
@ingroup foundation
@class CPString
@@ -359,6 +365,11 @@ var CPStringHashes = new objj_dictionary();
//Replacing Substrings
+- (CPString)stringByEscapingRegexControlCharacters
+{
+ return self.replace(CPStringRegexEscapeExpression, "\\$1");
+}
+
/*!
Returns a new string in which all occurrences of a target string in the reciever are replaced by
another given string.
@@ -368,7 +379,7 @@ var CPStringHashes = new objj_dictionary();
- (CPString)stringByReplacingOccurrencesOfString:(CPString)target withString:(CPString)replacement
{
- return self.replace(new RegExp(target, "g"), replacement);
+ return self.replace(new RegExp([target stringByEscapingRegexControlCharacters], "g"), replacement);
}
/*
@@ -385,6 +396,7 @@ var CPStringHashes = new objj_dictionary();
var start = substring(0, searchRange.location),
stringSegmentToSearch = substr(searchRange.location, searchRange.length),
end = substring(searchRange.location + searchRange.length, self.length),
+ target = [target stringByEscapingRegexControlCharacters],
regExp;
if (options & CPCaseInsensitiveSearch)
View
2 Foundation/Rakefile
@@ -18,7 +18,7 @@ ObjectiveJ::BundleTask.new(:Foundation) do |t|
t.author = '280 North, Inc.'
t.email = 'feedback @nospam@ 280north.com'
t.summary = 'Foundation classes for Cappuccino'
- t.sources = FileList['*.j']
+ t.sources = FileList['**/*.j']
t.resources = FileList['Resources/*'].to_a
t.license = ObjectiveJ::License::LGPL_v2_1
t.build_path = $BUILD_PATH
View
9 Tests/Foundation/CPStringTest.j
@@ -11,9 +11,16 @@
[self assertTrue:(expectedString === actualString)
message:"stringByAppendingFormat: expected:" + expectedString + " actual:" + actualString];
+}
+
+- (void)testStringByReplacingWithRegexCharacters
+{
+ var stringToTest = "${foo} {foo}",
+ result = [stringToTest stringByReplacingOccurrencesOfString:"${foo}" withString:"BAR"];
-
+ [self assert:result equals:"BAR {foo}"];
}
+
- (void)testStringByAppendingFormat
{
var format = @"%d X %d = %d",

0 comments on commit d4b9c4c

Please sign in to comment.