diff --git a/js/js.libraries/src/core/stringsCode.kt b/js/js.libraries/src/core/stringsCode.kt index f8af5be3a18..45dcda7195b 100644 --- a/js/js.libraries/src/core/stringsCode.kt +++ b/js/js.libraries/src/core/stringsCode.kt @@ -89,7 +89,7 @@ public fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolea 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()) -deprecated("Use replaceFirst(String, String) instead.") +deprecated("Use replaceFirst(String, String) instead.", ReplaceWith("replaceFirst(oldValue, newValue, ignoreCase = ignoreCase)")) public fun String.replaceFirstLiteral(oldValue: String, newValue: String, ignoreCase: Boolean = false): String = nativeReplace(RegExp(Regex.escape(oldValue), if (ignoreCase) "i" else ""), Regex.escapeReplacement(newValue)) diff --git a/libraries/stdlib/src/kotlin/text/StringsJVM.kt b/libraries/stdlib/src/kotlin/text/StringsJVM.kt index 6100d27dad7..a990d99b124 100644 --- a/libraries/stdlib/src/kotlin/text/StringsJVM.kt +++ b/libraries/stdlib/src/kotlin/text/StringsJVM.kt @@ -76,13 +76,15 @@ public fun String.replaceFirst(oldChar: Char, newChar: Char, ignoreCase: Boolean /** * Returns a new string obtained by replacing the first occurrence of the [oldValue] substring in this string * with the specified [newValue] string. - * - * Soon shall be renamed back to replaceFirst. */ -public fun String.replaceFirstLiteral(oldValue: String, newValue: String, ignoreCase: Boolean = false): String { +public fun String.replaceFirst(oldValue: String, newValue: String, ignoreCase: Boolean = false): String { val index = indexOf(oldValue, ignoreCase = ignoreCase) return if (index < 0) this else this.replaceRange(index, index + oldValue.length(), newValue) } + +deprecated("Use String.replaceFirst instead.", ReplaceWith("replaceFirst(oldValue, newValue, ignoreCase = ignoreCase)")) +public fun String.replaceFirstLiteral(oldValue: String, newValue: String, ignoreCase: Boolean = false): String = replaceFirst(oldValue, newValue, ignoreCase = ignoreCase) + /** * Returns a new string obtained by replacing each substring of this string that matches the given regular expression * with the given [replacement]. @@ -246,11 +248,11 @@ public fun String(stringBuffer: java.lang.StringBuffer): String = java.lang.Stri */ public fun String(stringBuilder: java.lang.StringBuilder): String = java.lang.String(stringBuilder) as String -/** - * Replaces the first substring of this string that matches the given regular expression with the given replacement. - */ -deprecated("Use replaceFirst(Regex, String) or replaceFirstLiteral(String, String) instead.", ReplaceWith("replaceFirst(regex.toRegex(), replacement)")) -public fun String.replaceFirst(regex: String, replacement: String): String = (this as java.lang.String).replaceFirst(regex, replacement) +///** +// * Replaces the first substring of this string that matches the given regular expression with the given replacement. +// */ +//deprecated("Use replaceFirst(Regex, String) or replaceFirstLiteral(String, String) instead.", ReplaceWith("replaceFirst(regex.toRegex(), replacement)")) +//public fun String.replaceFirst(regex: String, replacement: String): String = (this as java.lang.String).replaceFirst(regex, replacement) /** * Returns the character (Unicode code point) at the specified index. diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index d55ac51c03d..0b28ed17228 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt @@ -551,10 +551,10 @@ class StringTest { // doesn't pass in Rhino JS // assertEquals("schrodinger", "schrÖdinger".replaceFirst('ö', 'o', ignoreCase = true)) - assertEquals("Abba${'$'}", input.replaceFirstLiteral("bA", "$")) - assertEquals("Ab${'$'}bA", input.replaceFirstLiteral("bA", "$", ignoreCase = true)) + assertEquals("Abba${'$'}", input.replaceFirst("bA", "$")) + assertEquals("Ab${'$'}bA", input.replaceFirst("bA", "$", ignoreCase = true)) - assertEquals("-test", "test".replaceFirstLiteral("", "-")) + assertEquals("-test", "test".replaceFirst("", "-")) }