Moved JVM-specific String.repeat method to StringsJVM.kt

This commit is contained in:
Evgeny Gerashchenko
2012-11-19 13:29:17 +04:00
parent 7ccded5f6d
commit be7989af36
4 changed files with 21 additions and 24 deletions
-15
View File
@@ -63,18 +63,3 @@ public inline fun String.count(predicate: (Char) -> Boolean): Int {
}
return answer
}
/**
* Repeats a given string n times.
* When n < 0, IllegalArgumentException is thrown.
* @includeFunctionBody ../../test/StringTest.kt repeat
*/
public inline fun String.repeat(n: Int): String {
require(n >= 0, { "Cannot repeat string $n times" })
var sb = StringBuilder()
for (i in 1..n) {
sb.append(this)
}
return sb.toString()
}
+14
View File
@@ -192,3 +192,17 @@ public inline fun String.decapitalize(): String {
return if (notEmpty() && charAt(0).isUpperCase()) substring(0, 1).toLowerCase() + substring(1) else this
}
/**
* Repeats a given string n times.
* When n < 0, IllegalArgumentException is thrown.
* @includeFunctionBody ../../test/StringTest.kt repeat
*/
public inline fun String.repeat(n: Int): String {
require(n >= 0, { "Cannot repeat string $n times" })
var sb = StringBuilder()
for (i in 1..n) {
sb.append(this)
}
return sb.toString()
}
+7 -1
View File
@@ -65,5 +65,11 @@ class StringJVMTest {
assertEquals(s, list[0])
}
test fun repeat() {
fails{ "foo".repeat(-1) }
assertEquals("", "foo".repeat(0))
assertEquals("foo", "foo".repeat(1))
assertEquals("foofoo", "foo".repeat(2))
assertEquals("foofoofoo", "foo".repeat(3))
}
}
-8
View File
@@ -49,12 +49,4 @@ class StringTest {
assertEquals("uRL", "URL".decapitalize())
}
test fun repeat() {
fails{ "foo".repeat(-1) }
assertEquals("", "foo".repeat(0))
assertEquals("foo", "foo".repeat(1))
assertEquals("foofoo", "foo".repeat(2))
assertEquals("foofoofoo", "foo".repeat(3))
}
}