From 43bb9347c66d38083587714f0ec096481ef0808d Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 14 Oct 2015 18:46:14 +0300 Subject: [PATCH] Rename Regex.hasMatch to containsMatchIn, and add corresponding contains function to CharSequence. --- js/js.libraries/src/core/regex.kt | 7 +++++-- libraries/stdlib/src/kotlin/text/Strings.kt | 6 ++++++ libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt | 7 +++++-- libraries/stdlib/test/text/RegexTest.kt | 2 +- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/js/js.libraries/src/core/regex.kt b/js/js.libraries/src/core/regex.kt index 6508cd16153..c14114d8264 100644 --- a/js/js.libraries/src/core/regex.kt +++ b/js/js.libraries/src/core/regex.kt @@ -59,12 +59,15 @@ public class Regex(pattern: String, options: Set) { return match != null && (match as RegExpMatch).index == 0 && nativePattern.lastIndex == input.length() } - /** Indicates whether the regular expression can find at least a match in the specified [input]. */ - public fun hasMatch(input: CharSequence): Boolean { + /** Indicates whether the regular expression can find at least one match in the specified [input]. */ + public fun containsMatchIn(input: CharSequence): Boolean { nativePattern.reset() return nativePattern.test(input.toString()) } + @Deprecated("Use containsMatchIn() or 'in' operator instead.", ReplaceWith("this in input")) + public fun hasMatch(input: CharSequence): Boolean = containsMatchIn(input) + /** Returns the first match of a regular expression in the [input], beginning at the specified [startIndex]. * * @param startIndex An index to start search with, by default 0. Must be not less than zero and not greater than `input.length()` diff --git a/libraries/stdlib/src/kotlin/text/Strings.kt b/libraries/stdlib/src/kotlin/text/Strings.kt index 398f227c831..126b0339405 100644 --- a/libraries/stdlib/src/kotlin/text/Strings.kt +++ b/libraries/stdlib/src/kotlin/text/Strings.kt @@ -777,6 +777,12 @@ public operator fun String.contains(seq: CharSequence, ignoreCase: Boolean = fal public operator fun String.contains(char: Char, ignoreCase: Boolean = false): Boolean = indexOf(char, ignoreCase = ignoreCase) >= 0 +/** + * Returns `true` if this string contains at least one match of the specified regular expression [regex]. + */ +public operator fun CharSequence.contains(regex: Regex): Boolean = regex.containsMatchIn(this) + + // rangesDelimitedBy diff --git a/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt b/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt index 2ef1deb471f..a52024f207d 100644 --- a/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt +++ b/libraries/stdlib/src/kotlin/text/regex/RegexJVM.kt @@ -114,8 +114,11 @@ public class Regex internal constructor(private val nativePattern: Pattern) { /** Indicates whether the regular expression matches the entire [input]. */ public fun matches(input: CharSequence): Boolean = nativePattern.matcher(input).matches() - /** Indicates whether the regular expression can find at least a match in the specified [input]. */ - public fun hasMatch(input: CharSequence): Boolean = nativePattern.matcher(input).find() + /** 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() + + @Deprecated("Use containsMatchIn() or 'in' operator instead.", ReplaceWith("this in input")) + public fun hasMatch(input: CharSequence): Boolean = containsMatchIn(input) /** * Returns the first match of a regular expression in the [input], beginning at the specified [startIndex]. diff --git a/libraries/stdlib/test/text/RegexTest.kt b/libraries/stdlib/test/text/RegexTest.kt index 21a6515ac7e..b224299336c 100644 --- a/libraries/stdlib/test/text/RegexTest.kt +++ b/libraries/stdlib/test/text/RegexTest.kt @@ -14,7 +14,7 @@ class RegexTest { assertFalse(input.matches(p)) assertFalse(p.matches(input)) - assertTrue(p.hasMatch(input)) + assertTrue(p in input) val first = p.find(input) assertTrue(first != null); first!!