[stdlib] Use more idiomatic and performant implementation of isBlank

Also replace expect-actual with a common function because its implementation is no longer different in different platforms.

KT-65590
This commit is contained in:
Ilya Gorbunov
2024-02-06 15:57:42 +01:00
committed by Space Team
parent 6f6496d78a
commit 15375c0fd5
6 changed files with 6 additions and 32 deletions
@@ -114,13 +114,6 @@ public actual fun String.replaceFirst(oldValue: String, newValue: String, ignore
return if (index < 0) this else this.replaceRange(index, index + oldValue.length, newValue)
}
/**
* Returns `true` if this string is empty or consists solely of whitespace characters.
*
* @sample samples.text.Strings.stringIsBlank
*/
public actual fun CharSequence.isBlank(): Boolean = length == 0 || indices.all { this[it].isWhitespace() }
/**
* Returns the substring of this string starting at the [startIndex] and ending right before the [endIndex].
*
@@ -421,7 +421,6 @@ public expect fun String.endsWith(suffix: String, ignoreCase: Boolean = false):
internal expect fun String.nativeIndexOf(ch: Char, fromIndex: Int): Int
internal expect fun String.nativeLastIndexOf(ch: Char, fromIndex: Int): Int
public expect fun CharSequence.isBlank(): Boolean
/**
* Returns `true` if the specified range in this char sequence is equal to the specified range in another char sequence.
* @param thisOffset the start offset in this char sequence of the substring to compare.
@@ -54,13 +54,6 @@ public fun String.matches(regex: String): Boolean {
return result != null && result.size != 0
}
/**
* Returns `true` if this string is empty or consists solely of whitespace characters.
*
* @sample samples.text.Strings.stringIsBlank
*/
public actual fun CharSequence.isBlank(): Boolean = length == 0 || indices.all { this[it].isWhitespace() }
/**
* Returns `true` if this string is equal to [other], optionally ignoring character case.
*
@@ -614,13 +614,6 @@ public actual fun CharSequence?.contentEquals(other: CharSequence?, ignoreCase:
@kotlin.internal.InlineOnly
public inline fun String.intern(): String = (this as java.lang.String).intern()
/**
* Returns `true` if this string is empty or consists solely of whitespace characters.
*
* @sample samples.text.Strings.stringIsBlank
*/
public actual fun CharSequence.isBlank(): Boolean = length == 0 || indices.all { this[it].isWhitespace() }
/**
* Returns the index within this string that is offset from the given [index] by [codePointOffset] code points.
*/
+6 -3
View File
@@ -303,9 +303,12 @@ public inline fun CharSequence.isEmpty(): Boolean = length == 0
@kotlin.internal.InlineOnly
public inline fun CharSequence.isNotEmpty(): Boolean = length > 0
// implemented differently in JVM and JS
//public fun String.isBlank(): Boolean = length() == 0 || all { it.isWhitespace() }
/**
* Returns `true` if this char sequence is empty or consists solely of whitespace characters.
*
* @sample samples.text.Strings.stringIsBlank
*/
public fun CharSequence.isBlank(): Boolean = all { it.isWhitespace() }
/**
* Returns `true` if this char sequence is not empty and contains some characters except whitespace characters.
@@ -499,13 +499,6 @@ public actual fun String.endsWith(suffix: String, ignoreCase: Boolean = false):
// From stringsCode.kt
/**
* Returns `true` if this string is empty or consists solely of whitespace characters.
*
* @sample samples.text.Strings.stringIsBlank
*/
public actual fun CharSequence.isBlank(): Boolean = length == 0 || indices.all { this[it].isWhitespace() }
/**
* Returns `true` if the specified range in this char sequence is equal to the specified range in another char sequence.
* @param thisOffset the start offset in this char sequence of the substring to compare.