Revert using regex Pattern in String.replace

Use String.indexOf(..., ignoreCase) instead in all branches to preserve
compatibility with behavior before 1.4.20 that used String.split which
essentially relied on that String.indexOf

#KT-43745 Fixed
This commit is contained in:
Ilya Gorbunov
2020-12-04 11:49:05 +03:00
parent 5167d69b7c
commit 149bcc2d22
2 changed files with 17 additions and 11 deletions
@@ -77,17 +77,7 @@ public actual fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boole
*/
@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS")
public actual fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String {
if (ignoreCase) {
val matcher = Pattern.compile(oldValue, Pattern.LITERAL or Pattern.CASE_INSENSITIVE).matcher(this)
if (!matcher.find()) return this
val stringBuilder = StringBuilder()
var i = 0
do {
stringBuilder.append(this, i, matcher.start()).append(newValue)
i = matcher.end()
} while (matcher.find())
return stringBuilder.append(this, i, length).toString()
} else {
run {
var occurrenceIndex: Int = indexOf(oldValue, 0, ignoreCase)
// FAST PATH: no match
if (occurrenceIndex < 0) return this