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
+21 -4
View File
@@ -14,12 +14,14 @@ import java.util.regex.Pattern
class RegexJVMTest {
@Test fun matchGroups() {
val input = "1a 2b 3c"
val regex = "(\\d)(\\w)".toRegex()
val input = "1a 2b 3"
val regex = "(\\d)(\\w)?".toRegex()
val matches = regex.findAll(input).toList()
assertTrue(matches.all { it.groups.size == 3 })
val m1 = matches[0]
val (m1, m2, m3) = matches
assertEquals("1a", m1.groups[0]?.value)
assertEquals(0..1, m1.groups[0]?.range)
assertEquals("1", m1.groups[1]?.value)
@@ -27,13 +29,28 @@ class RegexJVMTest {
assertEquals("a", m1.groups[2]?.value)
assertEquals(1..1, m1.groups[2]?.range)
val m2 = matches[1]
assertEquals("2", m2.groups[1]?.value)
assertEquals(3..3, m2.groups[1]?.range)
assertEquals("b", m2.groups[2]?.value)
assertEquals(4..4, m2.groups[2]?.range)
assertEquals("3", m3.groups[1]?.value)
assertNull(m3.groups[2])
}
@Test fun matchSequenceWithLookbehind() {
val input = "123_000 456 789"
val pattern = "(?<=^|\\s)\\d+_?".toRegex()
val matches = pattern.findAll(input)
val values = matches.map { it.value }
val expected = listOf("123_", "456", "789")
assertEquals(expected, values.toList())
assertEquals(expected, values.toList(), "running match sequence second time")
assertEquals(expected.drop(1), pattern.findAll(input, startIndex = 3).map { it.value }.toList())
assertEquals(listOf(0..3, 8..10, 12..14), matches.map { it.range }.toList())
}
private fun compareRegex(expected: Regex, actual: Regex) = compare(expected, actual) {
propertyEquals(Regex::pattern)