diff --git a/libraries/stdlib/js/src/kotlin/text/regex.kt b/libraries/stdlib/js/src/kotlin/text/regex.kt index 2523ca10ae6..d1a2e10e7b3 100644 --- a/libraries/stdlib/js/src/kotlin/text/regex.kt +++ b/libraries/stdlib/js/src/kotlin/text/regex.kt @@ -58,6 +58,15 @@ public actual class Regex actual constructor(pattern: String, options: Set input.length) { throw IndexOutOfBoundsException("Start index out of bounds: $startIndex, input length: ${input.length}") } - return nativePattern.findNext(input.toString(), startIndex) + return nativePattern.findNext(input.toString(), startIndex, nativePattern) } /** @@ -119,12 +128,8 @@ public actual class Regex actual constructor(pattern: String, options: Set input.length) { throw IndexOutOfBoundsException("index out of bounds: $index, input length: ${input.length}") } - return initStickyPattern().findNext(input.toString(), index) + return initStickyPattern().findNext(input.toString(), index, nativePattern) } @@ -247,7 +252,7 @@ public fun Regex_1(pattern: String): Regex = Regex(pattern, emptySet()) -private fun RegExp.findNext(input: String, from: Int): MatchResult? { +private fun RegExp.findNext(input: String, from: Int, nextPattern: RegExp): MatchResult? { this.lastIndex = from val match = exec(input) if (match == null) return null @@ -278,6 +283,7 @@ private fun RegExp.findNext(input: String, from: Int): MatchResult? { return groupValues_!! } - override fun next(): MatchResult? = this@findNext.findNext(input, if (range.isEmpty()) range.start + 1 else range.endInclusive + 1) + override fun next(): MatchResult? = + nextPattern.findNext(input, if (range.isEmpty()) range.start + 1 else range.endInclusive + 1, nextPattern) } } diff --git a/libraries/stdlib/test/text/RegexTest.kt b/libraries/stdlib/test/text/RegexTest.kt index 404eb4c297e..a217ab8c6f1 100644 --- a/libraries/stdlib/test/text/RegexTest.kt +++ b/libraries/stdlib/test/text/RegexTest.kt @@ -168,6 +168,7 @@ class RegexTest { assertEquals("3c", m.value) assertEquals(3, m.groups.size) assertEquals(listOf("3c", "3", "c"), m.groups.map { it!!.value }) + assertNull(m.next()) } } @@ -179,6 +180,17 @@ class RegexTest { assertEquals("aaaabbbb", regex.matchEntire(input)!!.value) } + @Test fun matchEntireNext() { + val regex = ".*".toRegex() + val input = "abc" + val match = regex.matchEntire(input)!! + assertEquals(input, match.value) + val next = assertNotNull(match.next()) + assertEquals("", next.value) + assertEquals(input.length until input.length, next.range) + assertNull(next.next()) + } + @Test fun matchAt() { val regex = Regex("[a-z][1-5]", RegexOption.IGNORE_CASE) val input = "...a4...B1" @@ -196,6 +208,11 @@ class RegexTest { assertEquals(input.substring(match.range), match.value) } + matches.zipWithNext { (_, m1), (_, m2) -> + assertEquals(m2.range, assertNotNull(m1.next()).range) + } + assertNull(matches.last().second.next()) + for (index in listOf(-1, input.length + 1)) { assertFailsWith { regex.matchAt(input, index) } assertFailsWith { regex.matchesAt(input, index) }