KT-51908 Common MatchGroupCollection.get(name) extension function

This commit is contained in:
Abduqodiri Qurbonzoda
2022-09-21 16:02:34 +03:00
committed by Space Team
parent 448e9fc5e7
commit 175566fe56
5 changed files with 29 additions and 23 deletions
+1 -1
View File
@@ -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.")
@@ -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.")
@@ -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.")
@@ -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<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.9")
public expect operator fun MatchGroupCollection.get(name: String): MatchGroup?
+13 -6
View File
@@ -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>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)
@@ -218,14 +218,14 @@ class RegexTest {
"(?<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"] }
@@ -283,7 +283,7 @@ class RegexTest {
"(?<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!"))
}
@@ -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 ->