2nd stage of replaceFirst semantics change: remove deprecated String.replaceFirst(String), rename replaceFirstLiteral to replaceFirst.

This commit is contained in:
Ilya Gorbunov
2015-06-24 19:07:26 +03:00
parent 249106647c
commit d20d8e2106
3 changed files with 14 additions and 12 deletions
+1 -1
View File
@@ -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))
+10 -8
View File
@@ -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.
+3 -3
View File
@@ -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("", "-"))
}