From ac4b20741348d36c3460ef45727854785be68c35 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 31 Mar 2015 18:31:45 +0300 Subject: [PATCH] Add isEmpty, isBlank methods and their nullable and negating couterparts. --- js/js.libraries/src/core/stringsCode.kt | 3 ++ libraries/stdlib/src/kotlin/text/Strings.kt | 42 +++++++++++++++++++ .../stdlib/src/kotlin/text/StringsJVM.kt | 5 +-- libraries/stdlib/test/text/StringTest.kt | 24 +++++++++++ 4 files changed, 71 insertions(+), 3 deletions(-) diff --git a/js/js.libraries/src/core/stringsCode.kt b/js/js.libraries/src/core/stringsCode.kt index 68dc1d15afe..eadadd4e1aa 100644 --- a/js/js.libraries/src/core/stringsCode.kt +++ b/js/js.libraries/src/core/stringsCode.kt @@ -28,6 +28,9 @@ public inline fun String.matches(regex : String) : Boolean { return result != null && result.size() > 0 } + +public fun String.isBlank(): Boolean = length() == 0 || matches("^[\\s\\xA0]+$") + public fun String.equals(anotherString: String, ignoreCase: Boolean = false): Boolean = if (!ignoreCase) this == anotherString diff --git a/libraries/stdlib/src/kotlin/text/Strings.kt b/libraries/stdlib/src/kotlin/text/Strings.kt index d509bb35980..f090bba67ec 100644 --- a/libraries/stdlib/src/kotlin/text/Strings.kt +++ b/libraries/stdlib/src/kotlin/text/Strings.kt @@ -1,6 +1,7 @@ package kotlin import java.util.NoSuchElementException +import kotlin.platform.platformName /** Returns the string with leading and trailing text matching the given string removed */ deprecated("Use removeEnclosing(text, text) or removePrefix(text).removeSuffix(text)") @@ -146,8 +147,49 @@ public fun String.padEnd(length: Int, padChar: Char = ' '): String { } /** Returns true if the string is not null and not empty */ +deprecated("Use isNotNullOrEmpty for nullable strings.") +platformName("isNotEmptyNullable") public fun String?.isNotEmpty(): Boolean = this != null && this.length() > 0 +/** + * Return `true` if this nullable string is either null or empty. + */ +public fun String?.isNullOrEmpty(): Boolean = this == null || this.length() == 0 + +/** + * Return `true` if this nullable string is neither null nor empty. + */ +public fun String?.isNotNullOrEmpty(): Boolean = this != null && this.length() > 0 + +/** + * Returns `true` if this string is empty (contains no characters). + */ +public fun String.isEmpty(): Boolean = length() == 0 + +/** + * Returns `true` if this string is not empty. + */ +public fun String.isNotEmpty(): Boolean = length() > 0 + +// implemented differently in JVM and JS +//public fun String.isBlank(): Boolean = length() == 0 || all { it.isWhitespace() } + + +/** + * Returns `true` if this string is not empty and contains some characters except of whitespace characters. + */ +public fun String.isNotBlank(): Boolean = !isBlank() + +/** + * Returns `true` if this nullable string is either null or empty or consists solely of whitespace characters. + */ +public fun String?.isNullOrBlank(): Boolean = this == null || this.isBlank() + +/** + * Returns `true` if this nullable string is neither null nor empty, and contains some characters except of whitespace characters. + */ +public fun String?.isNotNullOrBlank(): Boolean = this != null && !this.isBlank() + /** * Iterator for characters of given CharSequence */ diff --git a/libraries/stdlib/src/kotlin/text/StringsJVM.kt b/libraries/stdlib/src/kotlin/text/StringsJVM.kt index 17d4f9349f3..b561324acaa 100644 --- a/libraries/stdlib/src/kotlin/text/StringsJVM.kt +++ b/libraries/stdlib/src/kotlin/text/StringsJVM.kt @@ -275,10 +275,9 @@ public fun String.getChars(srcBegin: Int, srcEnd: Int, dst: CharArray, dstBegin: public fun String.intern(): String = (this as java.lang.String).intern() /** - * Returns `true` if this string is empty (contains no characters). + * Returns `true` if this string is empty or consists solely of whitespace characters. */ -public fun String.isEmpty(): Boolean = (this as java.lang.String).isEmpty() - +public fun String.isBlank(): Boolean = length() == 0 || all { it.isWhitespace() } /** * Returns `true` if this string matches the given regular expression. diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index 77183630866..c4ad0f771be 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt @@ -3,8 +3,32 @@ package test.text import kotlin.test.* import org.junit.Test as test +// could not be local inside isEmptyAndBlank, because non-toplevel declarations is not yet supported for JS +class IsEmptyCase(val value: String?, val isNull: Boolean = false, val isEmpty: Boolean = false, val isBlank: Boolean = false) + class StringTest { + test fun isEmptyAndBlank() { + + val cases = listOf( + IsEmptyCase(null, isNull = true), + IsEmptyCase("", isEmpty = true, isBlank = true), + IsEmptyCase(" \r\n\t\u00A0", isBlank = true), + IsEmptyCase(" Some ") + ) + + for (case in cases) { + assertEquals(case.isNull || case.isEmpty, case.value.isNullOrEmpty(), "failed for case '${case.value}'") + assertEquals(case.isNull || case.isBlank, case.value.isNullOrBlank(), "failed for case '${case.value}'") + if (case.value != null) + { + assertEquals(case.isEmpty, case.value.isEmpty(), "failed for case '${case.value}'") + assertEquals(case.isBlank, case.value.isBlank(), "failed for case '${case.value}'") + } + + } + } + test fun startsWithString() { assertTrue("abcd".startsWith("ab")) assertTrue("abcd".startsWith("abcd"))