KT-49721 Avoid matching from the same position after a zero-width match

In JS, RegExp can return a match before the specified start index,
if it has matched at that index, but it is in the middle of a surrogate
pair. Account for that when advancing to the next position after
a zero-width match so that it doesn't get to the middle of SP.
This commit is contained in:
Ilya Gorbunov
2021-11-18 12:57:25 +03:00
committed by Space
parent cebe25ff39
commit 4386a800c8
7 changed files with 46 additions and 5 deletions
+18
View File
@@ -7,6 +7,7 @@
package test.text
import test.regexSplitUnicodeCodePointHandling
import test.supportsNamedCapturingGroup
import kotlin.test.*
@@ -354,6 +355,11 @@ class RegexTest {
testSplitEquals("".split(""), "", emptyMatch)
testSplitEquals(
if (regexSplitUnicodeCodePointHandling) listOf("", "\uD83D\uDE04", "\uD801", "") else listOf("", "\uD83D", "\uDE04", "\uD801", ""),
"\uD83D\uDE04\uD801", emptyMatch
)
val emptyMatchBeforeT = "(?=t)".toRegex()
testSplitEquals(listOf("", "tes", "t"), input, emptyMatchBeforeT)
@@ -397,4 +403,16 @@ class RegexTest {
assertFailsWith<NoSuchElementException> { splits.next() }
}
@Test fun findAllEmoji() {
val input = "\uD83D\uDE04\uD801x"
val regex = ".".toRegex()
val matches = regex.findAll(input).toList()
val values = matches.map { it.value }
val ranges = matches.map { it.range }
assertEquals(listOf("\uD83D\uDE04", "\uD801", "x"), values)
assertEquals(listOf(0..1, 2..2, 3..3), ranges)
}
}