Rename Regex.hasMatch to containsMatchIn, and add corresponding contains function to CharSequence.

This commit is contained in:
Ilya Gorbunov
2015-10-14 18:46:14 +03:00
parent d1d865aa0f
commit 43bb9347c6
4 changed files with 17 additions and 5 deletions
+5 -2
View File
@@ -59,12 +59,15 @@ public class Regex(pattern: String, options: Set<RegexOption>) {
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()`
@@ -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
@@ -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].
+1 -1
View File
@@ -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!!