Skip to content

Commit 3ac86a7

Browse files
committed
fix: Handle escape sequences
Correct the handling of escape sequences in string literals. Don't replace escape sequences in regex expressions, for example, \r or \n. In the parser, these sequences start with \\. Same for \s, don't replace it with a whitespace, since this is also a part of a regex. Handle \\ to avoid that the sequence is escaped and returned as \\\\. (cherry picked from commit d266e80)
1 parent 8eb3bd9 commit 3ac86a7

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

src/main/scala/org/camunda/feel/impl/parser/FeelParser.scala

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -688,22 +688,16 @@ object FeelParser {
688688
}.getOrElse(ConstNull)
689689
}
690690

691-
// replace escaped character with the provided replacement
692691
private def translateEscapes(input: String): String = {
693-
val escapeMap = Map(
694-
"\\b" -> "\b",
695-
"\\t" -> "\t",
696-
"\\n" -> "\n",
697-
"\\f" -> "\f",
698-
"\\r" -> "\r",
699-
"\\\"" -> "\"",
700-
"\\'" -> "'",
701-
"\\s" -> " "
702-
// Add more escape sequences as needed
703-
)
704-
705-
escapeMap.foldLeft(input) { case (result, (escape, replacement)) =>
706-
result.replace(escape, replacement)
707-
}
692+
// replace all escape sequences
693+
input
694+
.replaceAll("(?<!\\\\)\\\\n", "\n") // new line
695+
.replaceAll("(?<!\\\\)\\\\r", "\r") // carriage return
696+
.replaceAll("(?<!\\\\)\\\\t", "\t") // tab
697+
.replaceAll("(?<!\\\\)\\\\b", "\b") // backspace
698+
.replaceAll("(?<!\\\\)\\\\f", "\f") // form feed
699+
.replaceAll("(?<!\\\\)\\\\'", "'") // single quote
700+
.replaceAll("(?<!\\\\)\\\\\"", "\"") // double quote
701+
.replaceAll("\\\\\\\\", "\\\\") // backslash (for regex characters)
708702
}
709703
}

0 commit comments

Comments
 (0)