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
+14 -1
View File
@@ -359,7 +359,20 @@ private fun RegExp.findNext(input: String, from: Int, nextPattern: RegExp): Matc
}
override fun next(): MatchResult? =
nextPattern.findNext(input, if (range.isEmpty()) range.start + 1 else range.endInclusive + 1, nextPattern)
nextPattern.findNext(input, if (range.isEmpty()) advanceToNextCharacter(range.start) else range.endInclusive + 1, nextPattern)
private fun advanceToNextCharacter(index: Int): Int {
if (index < input.lastIndex) {
val code1 = input.asDynamic().charCodeAt(index).unsafeCast<Int>()
if (code1 in 0xD800..0xDBFF) {
val code2 = input.asDynamic().charCodeAt(index + 1).unsafeCast<Int>()
if (code2 in 0xDC00..0xDFFF) {
return index + 2
}
}
}
return index + 1
}
}
}