From bc7859fc2e3aa0823919a362b136243274905588 Mon Sep 17 00:00:00 2001 From: Jake MacMullin Date: Thu, 9 Oct 2008 16:47:13 +1100 Subject: [PATCH] Implemented missing methods on CPString: stringByReplacingOccurrencesOfString:target withString:replacement options:options range:searchRange stringByReplacingCharactersInRange:range withString:replacement Signed-off-by: Jake MacMullin --- Foundation/CPString.j | 29 ++++++++++++++++++++++++++--- 1 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Foundation/CPString.j b/Foundation/CPString.j index 24a904f..764e946 100644 --- a/Foundation/CPString.j +++ b/Foundation/CPString.j @@ -366,17 +366,40 @@ var CPStringHashes = new objj_dictionary(); } /* + Returns a new string in which all occurrences of a target string in a specified range of the receiver + are replaced by another given string. + @param target The string to replace + @param replacement the string with which to replace the
target
+    @param options A mask of options to use when comparing 
target
 with the receiver. Pass 0 to specify no options
+    @param searchRange The range in the receiver in which to search for 
target
.
+*/
+
 - (CPString)stringByReplacingOccurrencesOfString:(CPString)target withString:(CPString)replacement options:(int)options range:(CPRange)searchRange
 {
-    //TODO :Vijay implement the method
+    var start = substring(0, searchRange.location),
+        stringSegmentToSearch = substr(searchRange.location, searchRange.length),
+        end = substring(searchRange.location + searchRange.length, self.length),
+        regExp;
+
+    if (options & CPCaseInsensitiveSearch)
+        regExp = new RegExp(target, "gi"); 
+    else
+        regExp = new RegExp(target, "g");
 
+    return start + '' + stringSegmentToSearch.replace(regExp, replacement) + '' + end;
 }
 
+/*
+   Returns a new string in which the characters in a specified range of the receiver 
+   are replaced by a given string.
+   @param range A range of characters in the receiver.
+   @param replacement The string with which to replace the characters in 
range
. +*/ + - (CPString)stringByReplacingCharactersInRange:(CPRange)range withString:(CPString)replacement { - //TODO :Vijay implement the method + return('' + substring(0, range.location) + replacement + substring(range.location + range.length, self.length)); } -*/ // Identifying and comparing strings -- 1.6.0.2