Optimize trivial cases of CharSequence.repeat. Provide CharSequence.repeat for JS.

#KT-3064 Fixed
This commit is contained in:
Ilya Gorbunov
2016-02-21 06:43:56 +03:00
parent 7a43d62408
commit f564adfdd4
4 changed files with 51 additions and 17 deletions
+21
View File
@@ -75,6 +75,27 @@ public inline fun String.decapitalize(): String {
return if (isNotEmpty()) substring(0, 1).toLowerCase() + substring(1) else this
}
/**
* Returns a string containing this char sequence repeated [n] times.
* @throws [IllegalArgumentException] when n < 0.
*/
public fun CharSequence.repeat(n: Int): String {
require (n >= 0) { "Count 'n' must be non-negative, but was $n." }
return when (n) {
0 -> ""
1 -> this.toString()
else -> {
var result = ""
if (!isEmpty()) {
val s = this.toString()
for (i in 1..n) {
result += s
}
}
return result
}
}
}
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))