2nd stage of replaceFirst semantics change: remove deprecated String.replaceFirst(String), rename replaceFirstLiteral to replaceFirst.
This commit is contained in:
@@ -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 =
|
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())
|
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 =
|
public fun String.replaceFirstLiteral(oldValue: String, newValue: String, ignoreCase: Boolean = false): String =
|
||||||
nativeReplace(RegExp(Regex.escape(oldValue), if (ignoreCase) "i" else ""), Regex.escapeReplacement(newValue))
|
nativeReplace(RegExp(Regex.escape(oldValue), if (ignoreCase) "i" else ""), Regex.escapeReplacement(newValue))
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
* Returns a new string obtained by replacing the first occurrence of the [oldValue] substring in this string
|
||||||
* with the specified [newValue] 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)
|
val index = indexOf(oldValue, ignoreCase = ignoreCase)
|
||||||
return if (index < 0) this else this.replaceRange(index, index + oldValue.length(), newValue)
|
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
|
* Returns a new string obtained by replacing each substring of this string that matches the given regular expression
|
||||||
* with the given [replacement].
|
* 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
|
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.
|
// * 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)"))
|
//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)
|
//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.
|
* Returns the character (Unicode code point) at the specified index.
|
||||||
|
|||||||
@@ -551,10 +551,10 @@ class StringTest {
|
|||||||
// doesn't pass in Rhino JS
|
// doesn't pass in Rhino JS
|
||||||
// assertEquals("schrodinger", "schrÖdinger".replaceFirst('ö', 'o', ignoreCase = true))
|
// assertEquals("schrodinger", "schrÖdinger".replaceFirst('ö', 'o', ignoreCase = true))
|
||||||
|
|
||||||
assertEquals("Abba${'$'}", input.replaceFirstLiteral("bA", "$"))
|
assertEquals("Abba${'$'}", input.replaceFirst("bA", "$"))
|
||||||
assertEquals("Ab${'$'}bA", input.replaceFirstLiteral("bA", "$", ignoreCase = true))
|
assertEquals("Ab${'$'}bA", input.replaceFirst("bA", "$", ignoreCase = true))
|
||||||
|
|
||||||
assertEquals("-test", "test".replaceFirstLiteral("", "-"))
|
assertEquals("-test", "test".replaceFirst("", "-"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user