From 7eba68e62bf2ce3257d0a2609c6492c499447b9a Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Sat, 8 Apr 2023 22:45:12 +0300 Subject: [PATCH] [K/N] Commonize String.regionMatches function As a part of efforts to stabilize K/N stdlib. --- .../src/main/kotlin/kotlin/text/Strings.kt | 3 +- libraries/stdlib/api/js-v1/kotlin.text.kt | 3 ++ libraries/stdlib/api/js/kotlin.text.kt | 3 ++ libraries/stdlib/common/src/kotlin/TextH.kt | 16 +++++++++ .../stdlib/js/src/kotlin/text/stringsCode.kt | 33 +++++++++++++++++-- .../stdlib/jvm/src/kotlin/text/StringsJVM.kt | 3 +- .../wasm/src/kotlin/text/StringsWasm.kt | 17 ++++++++++ 7 files changed, 74 insertions(+), 4 deletions(-) diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/text/Strings.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/text/Strings.kt index 7a8a71489a7..42a119d875a 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/text/Strings.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/text/Strings.kt @@ -185,7 +185,8 @@ public actual fun CharSequence.regionMatches( * @param otherOffset the start offset in the other string of the substring to compare. * @param length the length of the substring to compare. */ -public fun String.regionMatches( +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun String.regionMatches( thisOffset: Int, other: String, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean { if (length < 0 || thisOffset < 0 || otherOffset < 0 diff --git a/libraries/stdlib/api/js-v1/kotlin.text.kt b/libraries/stdlib/api/js-v1/kotlin.text.kt index f430e45028c..8427d46b1cf 100644 --- a/libraries/stdlib/api/js-v1/kotlin.text.kt +++ b/libraries/stdlib/api/js-v1/kotlin.text.kt @@ -689,6 +689,9 @@ public inline fun kotlin.CharSequence.reduceRightOrNull(operation: (kotlin.Char, public fun kotlin.CharSequence.regionMatches(thisOffset: kotlin.Int, other: kotlin.CharSequence, otherOffset: kotlin.Int, length: kotlin.Int, ignoreCase: kotlin.Boolean = ...): kotlin.Boolean +@kotlin.SinceKotlin(version = "1.9") +public fun kotlin.String.regionMatches(thisOffset: kotlin.Int, other: kotlin.String, otherOffset: kotlin.Int, length: kotlin.Int, ignoreCase: kotlin.Boolean = ...): kotlin.Boolean + public fun kotlin.CharSequence.removePrefix(prefix: kotlin.CharSequence): kotlin.CharSequence public fun kotlin.String.removePrefix(prefix: kotlin.CharSequence): kotlin.String diff --git a/libraries/stdlib/api/js/kotlin.text.kt b/libraries/stdlib/api/js/kotlin.text.kt index f430e45028c..8427d46b1cf 100644 --- a/libraries/stdlib/api/js/kotlin.text.kt +++ b/libraries/stdlib/api/js/kotlin.text.kt @@ -689,6 +689,9 @@ public inline fun kotlin.CharSequence.reduceRightOrNull(operation: (kotlin.Char, public fun kotlin.CharSequence.regionMatches(thisOffset: kotlin.Int, other: kotlin.CharSequence, otherOffset: kotlin.Int, length: kotlin.Int, ignoreCase: kotlin.Boolean = ...): kotlin.Boolean +@kotlin.SinceKotlin(version = "1.9") +public fun kotlin.String.regionMatches(thisOffset: kotlin.Int, other: kotlin.String, otherOffset: kotlin.Int, length: kotlin.Int, ignoreCase: kotlin.Boolean = ...): kotlin.Boolean + public fun kotlin.CharSequence.removePrefix(prefix: kotlin.CharSequence): kotlin.CharSequence public fun kotlin.String.removePrefix(prefix: kotlin.CharSequence): kotlin.String diff --git a/libraries/stdlib/common/src/kotlin/TextH.kt b/libraries/stdlib/common/src/kotlin/TextH.kt index 36229aaf74a..c1e248992ad 100644 --- a/libraries/stdlib/common/src/kotlin/TextH.kt +++ b/libraries/stdlib/common/src/kotlin/TextH.kt @@ -311,6 +311,22 @@ expect fun CharSequence.regionMatches( ignoreCase: Boolean = false ): Boolean +/** + * Returns `true` if the specified range in this string is equal to the specified range in another string. + * @param thisOffset the start offset in this string of the substring to compare. + * @param other the string against a substring of which the comparison is performed. + * @param otherOffset the start offset in the other string of the substring to compare. + * @param length the length of the substring to compare. + */ +@SinceKotlin("1.9") +public expect fun String.regionMatches( + thisOffset: Int, + other: String, + otherOffset: Int, + length: Int, + ignoreCase: Boolean = false +): Boolean + /** * A Comparator that orders strings ignoring character case. diff --git a/libraries/stdlib/js/src/kotlin/text/stringsCode.kt b/libraries/stdlib/js/src/kotlin/text/stringsCode.kt index 00e345fd314..bb373053a66 100644 --- a/libraries/stdlib/js/src/kotlin/text/stringsCode.kt +++ b/libraries/stdlib/js/src/kotlin/text/stringsCode.kt @@ -89,9 +89,38 @@ public actual fun String?.equals(other: String?, ignoreCase: Boolean = false): B } +/** + * 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. + * @param other the string against a substring of which the comparison is performed. + * @param otherOffset the start offset in the other char sequence of the substring to compare. + * @param length the length of the substring to compare. + */ @Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") -public actual fun CharSequence.regionMatches(thisOffset: Int, other: CharSequence, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean = - regionMatchesImpl(thisOffset, other, otherOffset, length, ignoreCase) +public actual fun CharSequence.regionMatches( + thisOffset: Int, + other: CharSequence, + otherOffset: Int, + length: Int, + ignoreCase: Boolean = false +): Boolean = regionMatchesImpl(thisOffset, other, otherOffset, length, ignoreCase) + +/** + * Returns `true` if the specified range in this string is equal to the specified range in another string. + * @param thisOffset the start offset in this string of the substring to compare. + * @param other the string against a substring of which the comparison is performed. + * @param otherOffset the start offset in the other string of the substring to compare. + * @param length the length of the substring to compare. + */ +@SinceKotlin("1.9") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun String.regionMatches( + thisOffset: Int, + other: String, + otherOffset: Int, + length: Int, + ignoreCase: Boolean = false +): Boolean = regionMatchesImpl(thisOffset, other, otherOffset, length, ignoreCase) /** diff --git a/libraries/stdlib/jvm/src/kotlin/text/StringsJVM.kt b/libraries/stdlib/jvm/src/kotlin/text/StringsJVM.kt index 81abef23cc6..5b1777f2538 100644 --- a/libraries/stdlib/jvm/src/kotlin/text/StringsJVM.kt +++ b/libraries/stdlib/jvm/src/kotlin/text/StringsJVM.kt @@ -649,7 +649,8 @@ public actual fun CharSequence.regionMatches(thisOffset: Int, other: CharSequenc * @param otherOffset the start offset in the other string of the substring to compare. * @param length the length of the substring to compare. */ -public fun String.regionMatches(thisOffset: Int, other: String, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean = +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun String.regionMatches(thisOffset: Int, other: String, otherOffset: Int, length: Int, ignoreCase: Boolean = false): Boolean = if (!ignoreCase) (this as java.lang.String).regionMatches(thisOffset, other, otherOffset, length) else diff --git a/libraries/stdlib/wasm/src/kotlin/text/StringsWasm.kt b/libraries/stdlib/wasm/src/kotlin/text/StringsWasm.kt index 0e7c600ef1d..753439f502d 100644 --- a/libraries/stdlib/wasm/src/kotlin/text/StringsWasm.kt +++ b/libraries/stdlib/wasm/src/kotlin/text/StringsWasm.kt @@ -501,6 +501,23 @@ actual fun CharSequence.regionMatches( ignoreCase: Boolean ): Boolean = regionMatchesImpl(thisOffset, other, otherOffset, length, ignoreCase) +/** + * Returns `true` if the specified range in this string is equal to the specified range in another string. + * @param thisOffset the start offset in this string of the substring to compare. + * @param other the string against a substring of which the comparison is performed. + * @param otherOffset the start offset in the other string of the substring to compare. + * @param length the length of the substring to compare. + */ +@SinceKotlin("1.9") +@Suppress("ACTUAL_FUNCTION_WITH_DEFAULT_ARGUMENTS") +public actual fun String.regionMatches( + thisOffset: Int, + other: String, + otherOffset: Int, + length: Int, + ignoreCase: Boolean = false +): Boolean = regionMatchesImpl(thisOffset, other, otherOffset, length, ignoreCase) + private val STRING_CASE_INSENSITIVE_ORDER = Comparator { a, b -> a.compareTo(b, ignoreCase = true) } /**