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
+17 -6
View File
@@ -457,14 +457,25 @@ public fun String.decapitalize(): String {
* @sample test.text.StringJVMTest.repeat
*/
public fun CharSequence.repeat(n: Int): String {
if (n < 0)
throw IllegalArgumentException("Value should be non-negative, but was $n")
require (n >= 0) { "Count 'n' must be non-negative, but was $n." }
val sb = StringBuilder(n * length)
for (i in 1..n) {
sb.append(this)
return when (n) {
0 -> ""
1 -> this.toString()
else -> {
when (length) {
0 -> ""
1 -> this[0].let { char -> String(CharArray(n) { char }) }
else -> {
val sb = StringBuilder(n * length)
for (i in 1..n) {
sb.append(this)
}
sb.toString()
}
}
}
}
return sb.toString()
}