From 288e0b3c7e4502de2e9a8f76bec5639d1ddcaf72 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 17 Jan 2017 19:01:02 +0300 Subject: [PATCH] Allow to use Regex matches CharSequence and CharSequence matches Regex in infix form #KT-14279 --- js/js.libraries/src/core/regex.kt | 2 +- libraries/stdlib/common/src/kotlin/TextH.kt | 2 +- libraries/stdlib/src/kotlin/text/Strings.kt | 2 +- libraries/stdlib/src/kotlin/text/regex/Regex.kt | 2 +- libraries/stdlib/test/text/RegexTest.kt | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/js/js.libraries/src/core/regex.kt b/js/js.libraries/src/core/regex.kt index 16b3272269d..0ad7f68f2bd 100644 --- a/js/js.libraries/src/core/regex.kt +++ b/js/js.libraries/src/core/regex.kt @@ -52,7 +52,7 @@ public class Regex(pattern: String, options: Set) { private val nativePattern: RegExp = RegExp(pattern, options.map { it.value }.joinToString(separator = "") + "g") /** Indicates whether the regular expression matches the entire [input]. */ - public fun matches(input: CharSequence): Boolean { + public infix fun matches(input: CharSequence): Boolean { nativePattern.reset() val match = nativePattern.exec(input.toString()) return match != null && match.index == 0 && nativePattern.lastIndex == input.length diff --git a/libraries/stdlib/common/src/kotlin/TextH.kt b/libraries/stdlib/common/src/kotlin/TextH.kt index c3f67cb0411..c58829b48d4 100644 --- a/libraries/stdlib/common/src/kotlin/TextH.kt +++ b/libraries/stdlib/common/src/kotlin/TextH.kt @@ -44,7 +44,7 @@ header class Regex { constructor(pattern: String, options: Set) fun matchEntire(input: CharSequence): MatchResult? - fun matches(input: CharSequence): Boolean + infix fun matches(input: CharSequence): Boolean fun containsMatchIn(input: CharSequence): Boolean fun replace(input: CharSequence, replacement: String): String fun replace(input: CharSequence, transform: (MatchResult) -> CharSequence): String diff --git a/libraries/stdlib/src/kotlin/text/Strings.kt b/libraries/stdlib/src/kotlin/text/Strings.kt index f127507dab4..aebb84c74e1 100644 --- a/libraries/stdlib/src/kotlin/text/Strings.kt +++ b/libraries/stdlib/src/kotlin/text/Strings.kt @@ -677,7 +677,7 @@ public inline fun CharSequence.replaceFirst(regex: Regex, replacement: String): * Returns `true` if this char sequence matches the given regular expression. */ @kotlin.internal.InlineOnly -public inline fun CharSequence.matches(regex: Regex): Boolean = regex.matches(this) +public inline infix fun CharSequence.matches(regex: Regex): Boolean = regex.matches(this) /** * Implementation of [regionMatches] for CharSequences. diff --git a/libraries/stdlib/src/kotlin/text/regex/Regex.kt b/libraries/stdlib/src/kotlin/text/regex/Regex.kt index 38b864f24a0..e652a9c1d6b 100644 --- a/libraries/stdlib/src/kotlin/text/regex/Regex.kt +++ b/libraries/stdlib/src/kotlin/text/regex/Regex.kt @@ -116,7 +116,7 @@ internal constructor(private val nativePattern: Pattern) { public val options: Set = fromInt(nativePattern.flags()) /** Indicates whether the regular expression matches the entire [input]. */ - public fun matches(input: CharSequence): Boolean = nativePattern.matcher(input).matches() + public infix fun matches(input: CharSequence): Boolean = nativePattern.matcher(input).matches() /** Indicates whether the regular expression can find at least one match in the specified [input]. */ public fun containsMatchIn(input: CharSequence): Boolean = nativePattern.matcher(input).find() diff --git a/libraries/stdlib/test/text/RegexTest.kt b/libraries/stdlib/test/text/RegexTest.kt index d2e850dfe92..fdd670fd64d 100644 --- a/libraries/stdlib/test/text/RegexTest.kt +++ b/libraries/stdlib/test/text/RegexTest.kt @@ -11,8 +11,8 @@ class RegexTest { val p = "\\d+".toRegex() val input = "123 456 789" - assertFalse(input.matches(p)) - assertFalse(p.matches(input)) + assertFalse(input matches p) + assertFalse(p matches input) assertTrue(p in input)