Regex.matchAt/matchesAt #KT-34021

This commit is contained in:
Ilya Gorbunov
2021-07-07 06:52:58 +03:00
committed by teamcityserver
parent d99d25e51e
commit 28a0698463
9 changed files with 147 additions and 6 deletions
@@ -1,6 +1,6 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
* Copyright 2010-2021 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.
*/
package kotlin.text
@@ -150,6 +150,12 @@ public actual class Regex internal constructor(internal val nativePattern: Patte
/** Indicates whether the regular expression can find at least one match in the specified [input]. */
actual fun containsMatchIn(input: CharSequence): Boolean = find(input) != null
@SinceKotlin("1.5")
@ExperimentalStdlibApi
public actual fun matchesAt(input: CharSequence, index: Int): Boolean =
// TODO: expand and simplify
matchAt(input, index) != null
/**
* Returns the first match of a regular expression in the [input], beginning at the specified [startIndex].
*
@@ -197,6 +203,23 @@ public actual class Regex internal constructor(internal val nativePattern: Patte
*/
actual fun matchEntire(input: CharSequence): MatchResult?= doMatch(input, Mode.MATCH)
@SinceKotlin("1.5")
@ExperimentalStdlibApi
public actual fun matchAt(input: CharSequence, index: Int): MatchResult? {
if (index < 0 || index > input.length) {
throw IndexOutOfBoundsException("index is out of bounds: $index, input length: ${input.length}")
}
val matchResult = MatchResultImpl(input, this)
matchResult.mode = Mode.FIND
matchResult.startIndex = index
val matches = startNode.matches(index, input, matchResult) >= 0
if (!matches) {
return null
}
matchResult.finalizeMatch()
return matchResult
}
private fun processReplacement(match: MatchResult, replacement: String): String {
val result = StringBuilder(replacement.length)
var escaped = false