From 7e3c4074e4f881d2a92fed1c79eef9d1c6539ce1 Mon Sep 17 00:00:00 2001 From: daboe01 Date: Tue, 30 Jun 2020 20:52:34 +0200 Subject: [PATCH] =?UTF-8?q?New:=20replaceOccurrencesOfString:(CPString)tar?= =?UTF-8?q?get=20withString:(CPString=E2=80=A6=20(#2900)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New: replaceOccurrencesOfString:(CPString)target withString:(CPString)replacement options:(int)options range:(CSRange)searchRange --- Foundation/CPAttributedString.j | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Foundation/CPAttributedString.j b/Foundation/CPAttributedString.j index b43b43d94..6d74c0312 100755 --- a/Foundation/CPAttributedString.j +++ b/Foundation/CPAttributedString.j @@ -887,6 +887,30 @@ var CPAttributedStringStringKey = "CPAttributedStringString", */ @implementation CPMutableAttributedString : CPAttributedString +/*! + Replaces all occurrences of the target string in a given range with a replacement string. Returns the number of replacements. + */ + +- (CPUInteger)replaceOccurrencesOfString:(CPString)target + withString:(CPString)replacement + options:(CPStringCompareOptions)options + range:(CSRange)searchRange +{ + var foundRange, + searchRangeDynamic = searchRange ? CPMakeRangeCopy(searchRange) : CPMakeRange(0, [_string length]), + searchRangeLengthOffset = [replacement length] - [target length]; + + for (var replacements = 0; + (foundRange = [_string rangeOfString:target options:options range:searchRangeDynamic]), foundRange.location != CPNotFound; + replacements++) + { + [self replaceCharactersInRange:foundRange withString:replacement]; + searchRangeDynamic.length += searchRangeLengthOffset; + } + + return replacements; +} + @end var isEqual = function(a, b)