Do not mutate Matcher in MatchResult.next()

Instead of mutating the matcher create a new one when `next()` is called.
This allows getting named groups from that matcher later.

Add lookbehind in matchSequence test to ensure this change doesn't alter
the existing behavior.

Also fixes #KT-20865
This commit is contained in:
Ilya Gorbunov
2018-11-15 17:47:06 +03:00
parent f76c0568ea
commit 8d0d6d1bf3
3 changed files with 33 additions and 10 deletions
@@ -263,7 +263,7 @@ private fun Matcher.matchEntire(input: CharSequence): MatchResult? {
}
private class MatcherMatchResult(private val matcher: Matcher, private val input: CharSequence) : MatchResult {
private val matchResult = matcher.toMatchResult()
private val matchResult: java.util.regex.MatchResult get() = matcher
override val range: IntRange
get() = matchResult.range()
override val value: String
@@ -302,7 +302,7 @@ private class MatcherMatchResult(private val matcher: Matcher, private val input
override fun next(): MatchResult? {
val nextIndex = matchResult.end() + if (matchResult.end() == matchResult.start()) 1 else 0
return if (nextIndex <= input.length) matcher.findNext(nextIndex, input) else null
return if (nextIndex <= input.length) matcher.pattern().matcher(input).findNext(nextIndex, input) else null
}
}