From 175566fe56df0eb4225378b851765dfe09efad1d Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Wed, 21 Sep 2022 16:02:34 +0300 Subject: [PATCH] KT-51908 Common MatchGroupCollection.get(name) extension function --- libraries/stdlib/js/src/kotlin/text/regex.kt | 2 +- .../src/kotlin/text/regex}/RegexExtensions.kt | 18 +++--------------- .../native-wasm/src/kotlin/text/Regex.kt | 2 +- .../src/kotlin/text/regex/RegexExtensions.kt | 11 +++++++++++ libraries/stdlib/test/text/RegexTest.kt | 19 +++++++++++++------ 5 files changed, 29 insertions(+), 23 deletions(-) rename libraries/stdlib/{jdk8/src/kotlin/text => jvm/src/kotlin/text/regex}/RegexExtensions.kt (52%) diff --git a/libraries/stdlib/js/src/kotlin/text/regex.kt b/libraries/stdlib/js/src/kotlin/text/regex.kt index ffb5280d807..2a089bfff1b 100644 --- a/libraries/stdlib/js/src/kotlin/text/regex.kt +++ b/libraries/stdlib/js/src/kotlin/text/regex.kt @@ -40,7 +40,7 @@ public actual data class MatchGroup(actual val value: String) * for example, when it's not supported by the current platform. */ @SinceKotlin("1.7") -public operator fun MatchGroupCollection.get(name: String): MatchGroup? { +public actual operator fun MatchGroupCollection.get(name: String): MatchGroup? { val namedGroups = this as? MatchNamedGroupCollection ?: throw UnsupportedOperationException("Retrieving groups by name is not supported on this platform.") diff --git a/libraries/stdlib/jdk8/src/kotlin/text/RegexExtensions.kt b/libraries/stdlib/jvm/src/kotlin/text/regex/RegexExtensions.kt similarity index 52% rename from libraries/stdlib/jdk8/src/kotlin/text/RegexExtensions.kt rename to libraries/stdlib/jvm/src/kotlin/text/regex/RegexExtensions.kt index 3809bb7f292..42f82138b04 100644 --- a/libraries/stdlib/jdk8/src/kotlin/text/RegexExtensions.kt +++ b/libraries/stdlib/jvm/src/kotlin/text/regex/RegexExtensions.kt @@ -1,20 +1,8 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ -@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") @file:JvmName("RegexExtensionsJDK8Kt") @file:kotlin.jvm.JvmPackageName("kotlin.text.jdk8") package kotlin.text @@ -28,7 +16,7 @@ package kotlin.text * for example, when it's not supported by the current platform. */ @SinceKotlin("1.2") -public operator fun MatchGroupCollection.get(name: String): MatchGroup? { +public actual operator fun MatchGroupCollection.get(name: String): MatchGroup? { val namedGroups = this as? MatchNamedGroupCollection ?: throw UnsupportedOperationException("Retrieving groups by name is not supported on this platform.") diff --git a/libraries/stdlib/native-wasm/src/kotlin/text/Regex.kt b/libraries/stdlib/native-wasm/src/kotlin/text/Regex.kt index 0ea5a58039c..15644a8ab75 100644 --- a/libraries/stdlib/native-wasm/src/kotlin/text/Regex.kt +++ b/libraries/stdlib/native-wasm/src/kotlin/text/Regex.kt @@ -68,7 +68,7 @@ public actual data class MatchGroup(actual val value: String, val range: IntRang * for example, when it's not supported by the current platform. */ @SinceKotlin("1.7") -public operator fun MatchGroupCollection.get(name: String): MatchGroup? { +public actual operator fun MatchGroupCollection.get(name: String): MatchGroup? { val namedGroups = this as? MatchNamedGroupCollection ?: throw UnsupportedOperationException("Retrieving groups by name is not supported on this platform.") diff --git a/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt b/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt index 735c0b7f6b2..ef36de79ff7 100644 --- a/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt +++ b/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt @@ -25,3 +25,14 @@ public inline fun String.toRegex(option: RegexOption): Regex = Regex(this, optio */ @kotlin.internal.InlineOnly public inline fun String.toRegex(options: Set): Regex = Regex(this, options) + +/** + * Returns a named group with the specified [name]. + * + * @return An instance of [MatchGroup] if the group with the specified [name] was matched or `null` otherwise. + * @throws IllegalArgumentException if there is no group with the specified [name] defined in the regex pattern. + * @throws UnsupportedOperationException if this match group collection doesn't support getting match groups by name, + * for example, when it's not supported by the current platform. + */ +@SinceKotlin("1.9") +public expect operator fun MatchGroupCollection.get(name: String): MatchGroup? \ No newline at end of file diff --git a/libraries/stdlib/test/text/RegexTest.kt b/libraries/stdlib/test/text/RegexTest.kt index 39524582dd0..10dc9891f7c 100644 --- a/libraries/stdlib/test/text/RegexTest.kt +++ b/libraries/stdlib/test/text/RegexTest.kt @@ -182,7 +182,7 @@ class RegexTest { val match = regex.find(input)!! assertEquals(listOf("Austin, TX: 123", "Austin", "TX", "123"), match.groupValues) - val namedGroups = match.groups as MatchNamedGroupCollection + val namedGroups = match.groups assertEquals(4, namedGroups.size) assertEquals("Austin", namedGroups["city"]?.value) assertEquals("TX", namedGroups["state"]?.value) @@ -202,14 +202,14 @@ class RegexTest { "(?hi)|(?bye)".toRegex(RegexOption.IGNORE_CASE).let { regex -> val hiMatch = regex.find("Hi!")!! - val hiGroups = hiMatch.groups as MatchNamedGroupCollection + val hiGroups = hiMatch.groups assertEquals(3, hiGroups.size) assertEquals("Hi", hiGroups["hi"]?.value) assertEquals(null, hiGroups["bye"]) assertFailsWith { hiGroups["hello"] } val byeMatch = regex.find("bye...")!! - val byeGroups = byeMatch.groups as MatchNamedGroupCollection + val byeGroups = byeMatch.groups assertEquals(3, byeGroups.size) assertEquals(null, byeGroups["hi"]) assertEquals("bye", byeGroups["bye"]?.value) @@ -218,14 +218,14 @@ class RegexTest { "(?hi)|bye".toRegex(RegexOption.IGNORE_CASE).let { regex -> val hiMatch = regex.find("Hi!")!! - val hiGroups = hiMatch.groups as MatchNamedGroupCollection + val hiGroups = hiMatch.groups assertEquals(2, hiGroups.size) assertEquals("Hi", hiGroups["hi"]?.value) assertFailsWith { hiGroups["bye"] } // Named group collection consisting of a single 'null' group value val byeMatch = regex.find("bye...")!! - val byeGroups = byeMatch.groups as MatchNamedGroupCollection + val byeGroups = byeMatch.groups assertEquals(2, byeGroups.size) assertEquals(null, byeGroups["hi"]) assertFailsWith { byeGroups["bye"] } @@ -283,7 +283,7 @@ class RegexTest { "(?\\w+), yes \\k<title>".toRegex().let { regex -> val match = regex.find("Do you copy? Sir, yes Sir!")!! assertEquals("Sir, yes Sir", match.value) - assertEquals("Sir", (match.groups as MatchNamedGroupCollection)["title"]?.value) + assertEquals("Sir", match.groups["title"]?.value) assertNull(regex.find("Do you copy? Sir, yes I do!")) } @@ -293,6 +293,13 @@ class RegexTest { testInvalidBackReference(BackReferenceHandling.notYetDefinedNamedGroup, pattern = "a\\k<first>(?<first>a)") } + @Test fun matchNamedGroupCollection() { + val regex = "(?<hi>hi)".toRegex(RegexOption.IGNORE_CASE) + val hiMatch = regex.find("Hi!")!! + val hiGroups = hiMatch.groups as MatchNamedGroupCollection + assertEquals("Hi", hiGroups["hi"]?.value) + } + private fun testInvalidBackReference(option: HandlingOption, pattern: String, input: CharSequence = "aaaa", matchValue: String = "aa") { when (option) { HandlingOption.IGNORE_BACK_REFERENCE_EXPRESSION ->