Uniform implementation of String.replace and String.matches(Regex) across JVM and JS.

Deprecated String.matches(String) and String.replaceAll methods.
This commit is contained in:
Ilya Gorbunov
2015-04-09 21:23:14 +03:00
parent 01ace84070
commit 0b03caf3e7
5 changed files with 61 additions and 16 deletions
-4
View File
@@ -54,10 +54,6 @@ public fun CharSequence.isEmpty(): Boolean = noImpl
// TODO: internal
// native because we need to escape newValue
//native("replace")
//public fun String.nativeReplace(oldValue: String, newValue: String): String = noImpl
// TODO: internal
native("replace")
public fun String.nativeReplace(pattern: RegExp, replacement: String): String = noImpl
+6 -4
View File
@@ -1,5 +1,7 @@
package kotlin.js
import kotlin.text.Regex
// TODO: make internal
public inline fun String.nativeIndexOf(ch : Char, fromIndex : Int) : Int = nativeIndexOf(ch.toString(), fromIndex)
public inline fun String.nativeLastIndexOf(ch : Char, fromIndex : Int) : Int = nativeLastIndexOf(ch.toString(), fromIndex)
@@ -80,8 +82,8 @@ public inline fun String.decapitalize(): String {
}
public fun String.replace(oldValue: String, newValue: String): String =
nativeReplace(RegExp(kotlin.text.Regex.escape(oldValue),"g"), kotlin.text.Regex.escapeReplacement(newValue))
public fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String =
nativeReplace(RegExp(Regex.escape(oldValue), if (ignoreCase) "gi" else "g"), Regex.escapeReplacement(newValue))
public fun String.replace(oldChar: Char, newChar: Char): String =
nativeReplace(RegExp(kotlin.text.Regex.escape(oldChar.toString()),"g"), newChar.toString())
public fun String.replace(oldChar: Char, newChar: Char, ignoreCase: Boolean = false): String =
nativeReplace(RegExp(Regex.escape(oldChar.toString()), if (ignoreCase) "gi" else "g"), newChar.toString())