Declare Common MatchGroupCollection.get(name) extension function
diff --git a/libraries/stdlib/api/js-v1/kotlin.text.kt b/libraries/stdlib/api/js-v1/kotlin.text.kt
index f430e45..56e135a 100644
--- a/libraries/stdlib/api/js-v1/kotlin.text.kt
+++ b/libraries/stdlib/api/js-v1/kotlin.text.kt
@@ -1349,6 +1349,8 @@
public abstract operator fun get(index: kotlin.Int): kotlin.text.MatchGroup?
}
+@kotlin.Deprecated(message = "Use MatchGroupCollection instead.", replaceWith = kotlin.ReplaceWith(expression = "MatchGroupCollection", imports = {}))
+@kotlin.DeprecatedSinceKotlin(warningSince = "1.8")
@kotlin.SinceKotlin(version = "1.1")
public interface MatchNamedGroupCollection : kotlin.text.MatchGroupCollection {
public abstract operator fun get(name: kotlin.String): kotlin.text.MatchGroup?
diff --git a/libraries/stdlib/api/js/kotlin.text.kt b/libraries/stdlib/api/js/kotlin.text.kt
index f430e45..56e135a 100644
--- a/libraries/stdlib/api/js/kotlin.text.kt
+++ b/libraries/stdlib/api/js/kotlin.text.kt
@@ -1349,6 +1349,8 @@
public abstract operator fun get(index: kotlin.Int): kotlin.text.MatchGroup?
}
+@kotlin.Deprecated(message = "Use MatchGroupCollection instead.", replaceWith = kotlin.ReplaceWith(expression = "MatchGroupCollection", imports = {}))
+@kotlin.DeprecatedSinceKotlin(warningSince = "1.8")
@kotlin.SinceKotlin(version = "1.1")
public interface MatchNamedGroupCollection : kotlin.text.MatchGroupCollection {
public abstract operator fun get(name: kotlin.String): kotlin.text.MatchGroup?
diff --git a/libraries/stdlib/jdk8/src/kotlin/text/RegexExtensions.kt b/libraries/stdlib/jdk8/src/kotlin/text/RegexExtensions.kt
deleted file mode 100644
index 3809bb7..0000000
--- a/libraries/stdlib/jdk8/src/kotlin/text/RegexExtensions.kt
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * 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.
- */
-
-@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
-@file:JvmName("RegexExtensionsJDK8Kt")
-@file:kotlin.jvm.JvmPackageName("kotlin.text.jdk8")
-package kotlin.text
-
-/**
- * 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.2")
-public operator fun MatchGroupCollection.get(name: String): MatchGroup? {
- val namedGroups = this as? MatchNamedGroupCollection
- ?: throw UnsupportedOperationException("Retrieving groups by name is not supported on this platform.")
-
- return namedGroups[name]
-}
diff --git a/libraries/stdlib/js/src/kotlin/text/regex.kt b/libraries/stdlib/js/src/kotlin/text/regex.kt
index ffb5280..fdf5927 100644
--- a/libraries/stdlib/js/src/kotlin/text/regex.kt
+++ b/libraries/stdlib/js/src/kotlin/text/regex.kt
@@ -39,8 +39,9 @@
* @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.
*/
+@Suppress("DEPRECATION")
@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.")
@@ -353,6 +354,7 @@
override val value: String
get() = match[0]!!
+ @Suppress("DEPRECATION")
override val groups: MatchGroupCollection = object : MatchNamedGroupCollection, AbstractCollection<MatchGroup?>() {
override val size: Int get() = match.length
override fun iterator(): Iterator<MatchGroup?> = indices.asSequence().map { this[it] }.iterator()
diff --git a/libraries/stdlib/jvm/src/kotlin/text/regex/Regex.kt b/libraries/stdlib/jvm/src/kotlin/text/regex/Regex.kt
index f92c985..a47ab00 100644
--- a/libraries/stdlib/jvm/src/kotlin/text/regex/Regex.kt
+++ b/libraries/stdlib/jvm/src/kotlin/text/regex/Regex.kt
@@ -355,6 +355,7 @@
override val value: String
get() = matchResult.group()
+ @Suppress("DEPRECATION")
override val groups: MatchGroupCollection = object : MatchNamedGroupCollection, AbstractCollection<MatchGroup?>() {
override val size: Int get() = matchResult.groupCount() + 1
override fun isEmpty(): Boolean = false
diff --git a/libraries/stdlib/jvm/src/kotlin/text/regex/RegexExtensions.kt b/libraries/stdlib/jvm/src/kotlin/text/regex/RegexExtensions.kt
new file mode 100644
index 0000000..bf3fb91
--- /dev/null
+++ b/libraries/stdlib/jvm/src/kotlin/text/regex/RegexExtensions.kt
@@ -0,0 +1,26 @@
+/*
+ * 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
+
+/**
+ * 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.
+ */
+@Suppress("DEPRECATION")
+@SinceKotlin("1.2")
+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.")
+
+ return namedGroups[name]
+}
diff --git a/libraries/stdlib/native-wasm/src/kotlin/text/Regex.kt b/libraries/stdlib/native-wasm/src/kotlin/text/Regex.kt
index fe97a00..95a490b 100644
--- a/libraries/stdlib/native-wasm/src/kotlin/text/Regex.kt
+++ b/libraries/stdlib/native-wasm/src/kotlin/text/Regex.kt
@@ -75,8 +75,9 @@
* @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.
*/
+@Suppress("DEPRECATION")
@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/native-wasm/src/kotlin/text/regex/MatchResultImpl.kt b/libraries/stdlib/native-wasm/src/kotlin/text/regex/MatchResultImpl.kt
index 14cadb6..618491a 100644
--- a/libraries/stdlib/native-wasm/src/kotlin/text/regex/MatchResultImpl.kt
+++ b/libraries/stdlib/native-wasm/src/kotlin/text/regex/MatchResultImpl.kt
@@ -91,6 +91,7 @@
* Groups are indexed from 1 to `groupCount` and group with the index 0 corresponds to the entire match.
*/
// Create one object or several ones?
+ @Suppress("DEPRECATION")
override val groups: MatchGroupCollection = object: MatchNamedGroupCollection, AbstractCollection<MatchGroup?>() {
override val size: Int
get() = this@MatchResultImpl.groupCount
diff --git a/libraries/stdlib/src/kotlin/text/regex/MatchResult.kt b/libraries/stdlib/src/kotlin/text/regex/MatchResult.kt
index 2aa3cb5..403841e 100644
--- a/libraries/stdlib/src/kotlin/text/regex/MatchResult.kt
+++ b/libraries/stdlib/src/kotlin/text/regex/MatchResult.kt
@@ -30,6 +30,8 @@
/**
* Extends [MatchGroupCollection] by introducing a way to get matched groups by name, when regex supports it.
*/
+@Deprecated("Use MatchGroupCollection instead.", ReplaceWith("MatchGroupCollection"))
+@DeprecatedSinceKotlin(warningSince = "1.8")
@SinceKotlin("1.1")
public interface MatchNamedGroupCollection : MatchGroupCollection {
/**
diff --git a/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt b/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt
index 735c0b7..bee56b6 100644
--- a/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt
+++ b/libraries/stdlib/src/kotlin/text/regex/RegexExtensions.kt
@@ -25,3 +25,14 @@
*/
@kotlin.internal.InlineOnly
public inline fun String.toRegex(options: Set<RegexOption>): 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.8")
+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 5326baf..524d28e 100644
--- a/libraries/stdlib/test/text/RegexTest.kt
+++ b/libraries/stdlib/test/text/RegexTest.kt
@@ -179,7 +179,7 @@
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)
@@ -195,14 +195,14 @@
@Test fun matchOptionalNamedGroup() {
"(?<hi>hi)|(?<bye>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<IllegalArgumentException> { 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)
@@ -211,14 +211,14 @@
"(?<hi>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<IllegalArgumentException> { 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<IllegalArgumentException> { byeGroups["bye"] }
@@ -274,7 +274,7 @@
"(?<title>\\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!"))
}
@@ -284,6 +284,14 @@
testInvalidBackReference(BackReferenceHandling.notYetDefinedNamedGroup, pattern = "a\\k<first>(?<first>a)")
}
+ @Suppress("DEPRECATION")
+ @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 ->