Fix String.dropWhile & String.takeWhile

This commit is contained in:
Ilya Ryzhenkov
2014-05-28 15:50:15 +04:00
committed by Andrey Breslav
parent 5b73fba2d4
commit a2a93a3830
3 changed files with 12 additions and 10 deletions
+3 -3
View File
@@ -354,7 +354,7 @@ public fun <T> Stream<T>.dropWhile(predicate: (T) -> Boolean): Stream<T> {
* Returns a list containing all elements except first elements that satisfy the given *predicate*
*/
public inline fun String.dropWhile(predicate: (Char) -> Boolean): String {
for (index in 0..length)
for (index in 0..length - 1)
if (!predicate(get(index))) {
return substring(index)
}
@@ -1240,10 +1240,10 @@ public fun <T> Stream<T>.takeWhile(predicate: (T) -> Boolean): Stream<T> {
* Returns a list containing first elements satisfying the given *predicate*
*/
public inline fun String.takeWhile(predicate: (Char) -> Boolean): String {
for (index in 0..length)
for (index in 0..length - 1)
if (!predicate(get(index))) {
return substring(0, index)
}
return ""
return this
}
+6 -4
View File
@@ -240,8 +240,9 @@ class StringJVMTest {
test fun dropWhile() {
val data = "ab1cd2"
val result = data.dropWhile { it.isJavaLetter() }
assertEquals("1cd2", result)
assertEquals("1cd2", data.dropWhile { it.isJavaLetter() })
assertEquals("", data.dropWhile { true })
assertEquals("ab1cd2", data.dropWhile { false })
}
test fun drop() {
@@ -255,8 +256,9 @@ class StringJVMTest {
test fun takeWhile() {
val data = "ab1cd2"
val result = data.takeWhile { it.isJavaLetter() }
assertEquals("ab", result)
assertEquals("ab", data.takeWhile { it.isJavaLetter() })
assertEquals("", data.takeWhile { false })
assertEquals("ab1cd2", data.takeWhile { true })
}
test fun take() {