Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle duplicates in negated character classes #5623

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -671,28 +671,28 @@ class CudfRegexTranspiler(mode: RegexMode) {
// `[^a\r\n]` => `[^a]`
// `[^\r\n]` => `[^\r\n]`

val linefeedCharsInPattern = components.flatMap {
val distinctComponents = components.distinct
val linefeedCharsInPattern = distinctComponents.flatMap {
case RegexChar(ch) if ch == '\n' || ch == '\r' => Seq(ch)
case RegexEscaped(ch) if ch == 'n' => Seq('\n')
case RegexEscaped(ch) if ch == 'r' => Seq('\r')
case _ => Seq.empty
}

val onlyLinefeedChars = components.length == linefeedCharsInPattern.length

val onlyLinefeedChars = distinctComponents.length == linefeedCharsInPattern.length
val negatedNewlines = Seq('\r', '\n').diff(linefeedCharsInPattern.distinct)

if (onlyLinefeedChars && linefeedCharsInPattern.length == 2) {
// special case for `[^\r\n]` and `[^\\r\\n]`
RegexCharacterClass(negated = true, ListBuffer(components: _*))
RegexCharacterClass(negated = true, ListBuffer(distinctComponents: _*))
} else if (negatedNewlines.isEmpty) {
RegexCharacterClass(negated = true, ListBuffer(components: _*))
RegexCharacterClass(negated = true, ListBuffer(distinctComponents: _*))
} else {
RegexGroup(capture = false,
RegexChoice(
RegexCharacterClass(negated = false,
characters = ListBuffer(negatedNewlines.map(RegexChar): _*)),
RegexCharacterClass(negated = true, ListBuffer(components: _*))))
RegexCharacterClass(negated = true, ListBuffer(distinctComponents: _*))))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,9 @@ class RegularExpressionTranspilerSuite extends FunSuite with Arm {
}

test("compare CPU and GPU: regexp replace negated character class") {
val inputs = Seq("a", "b", "a\nb", "a\r\nb\n\rc\rd")
val inputs = Seq("a", "b", "a\nb", "a\r\nb\n\rc\rd", "\r", "\r\n", "\n")
val patterns = Seq("[^z]", "[^\r]", "[^\n]", "[^\r]", "[^\r\n]",
"[^a\n]", "[^b\r]", "[^bc\r\n]", "[^\\r\\n]")
"[^a\n]", "[^b\r]", "[^bc\r\n]", "[^\\r\\n]", "[^\r\r]", "[^\r\n\r]", "[^\n\n\r\r]")
assertCpuGpuMatchesRegexpReplace(patterns, inputs)
}

Expand Down