diff --git a/common-ktx/src/main/java/co/nimblehq/common/extensions/StringExt.kt b/common-ktx/src/main/java/co/nimblehq/common/extensions/StringExt.kt index df8332d..d1d4728 100644 --- a/common-ktx/src/main/java/co/nimblehq/common/extensions/StringExt.kt +++ b/common-ktx/src/main/java/co/nimblehq/common/extensions/StringExt.kt @@ -2,6 +2,10 @@ package co.nimblehq.common.extensions import androidx.core.util.PatternsCompat import java.util.* +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.contract + +private val THAI_REGEX = "([\\u0E00-\\u0E7F]+)".toRegex() /** * Check if the string not null or empty. @@ -9,7 +13,13 @@ import java.util.* * * @return true if this nullable char sequence is NOT either null or empty */ -fun String?.isNotNullOrEmpty(): Boolean = !this.isNullOrEmpty() +@OptIn(ExperimentalContracts::class) +fun String?.isNotNullOrEmpty(): Boolean { + contract { + returns(true) implies (this@isNotNullOrEmpty != null) + } + return !this.isNullOrEmpty() +} /** * Check if the string not null or blank. @@ -18,7 +28,13 @@ fun String?.isNotNullOrEmpty(): Boolean = !this.isNullOrEmpty() * @return true if this nullable char sequence is NOT either null or empty or consists solely of * whitespace */ -fun String?.isNotNullOrBlank(): Boolean = !this.isNullOrBlank() +@OptIn(ExperimentalContracts::class) +fun String?.isNotNullOrBlank(): Boolean { + contract { + returns(true) implies (this@isNotNullOrBlank != null) + } + return !this.isNullOrBlank() +} /** * Eliminate the given character then titleize. @@ -68,6 +84,5 @@ fun String.isEmailValid(): Boolean { * @return true if this string is Thai */ fun String.isThai(): Boolean { - val thaiRegex = "([\\u0E00-\\u0E7F]+)".toRegex() - return if (isEmpty() || isBlank()) false else all { thaiRegex.matches(it.toString()) } + return isNotEmpty() && all { THAI_REGEX.matches(it.toString()) } }