Fix String.dropWhile & String.takeWhile
This commit is contained in:
committed by
Andrey Breslav
parent
5b73fba2d4
commit
a2a93a3830
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -113,7 +113,7 @@ fun filtering(): List<GenericFunction> {
|
||||
returns(Strings) { "String" }
|
||||
body(Strings) {
|
||||
"""
|
||||
for (index in 0..length)
|
||||
for (index in 0..length - 1)
|
||||
if (!predicate(get(index))) {
|
||||
return substring(index)
|
||||
}
|
||||
@@ -161,11 +161,11 @@ fun filtering(): List<GenericFunction> {
|
||||
returns(Strings) { "String" }
|
||||
body(Strings) {
|
||||
"""
|
||||
for (index in 0..length)
|
||||
for (index in 0..length - 1)
|
||||
if (!predicate(get(index))) {
|
||||
return substring(0, index)
|
||||
}
|
||||
return ""
|
||||
return this
|
||||
"""
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user