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

[Backport 1.18] fix: Handle escape and regex characters #933

Merged
merged 4 commits into from
Sep 25, 2024
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
26 changes: 10 additions & 16 deletions src/main/scala/org/camunda/feel/impl/parser/FeelParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -694,22 +694,16 @@ object FeelParser {
}.getOrElse(ConstNull)
}

// replace escaped character with the provided replacement
private def translateEscapes(input: String): String = {
val escapeMap = Map(
"\\b" -> "\b",
"\\t" -> "\t",
"\\n" -> "\n",
"\\f" -> "\f",
"\\r" -> "\r",
"\\\"" -> "\"",
"\\'" -> "'",
"\\s" -> " "
// Add more escape sequences as needed
)

escapeMap.foldLeft(input) { case (result, (escape, replacement)) =>
result.replace(escape, replacement)
}
// replace all escape sequences
input
.replaceAll("(?<!\\\\)\\\\n", "\n") // new line
.replaceAll("(?<!\\\\)\\\\r", "\r") // carriage return
.replaceAll("(?<!\\\\)\\\\t", "\t") // tab
.replaceAll("(?<!\\\\)\\\\b", "\b") // backspace
.replaceAll("(?<!\\\\)\\\\f", "\f") // form feed
.replaceAll("(?<!\\\\)\\\\'", "'") // single quote
.replaceAll("(?<!\\\\)\\\\\"", "\"") // double quote
.replaceAll("\\\\\\\\", "\\\\") // backslash (for regex characters)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.camunda.feel.impl.{EvaluationResultMatchers, FeelEngineTest, FeelInte
import org.camunda.feel.syntaxtree._
import org.scalatest.matchers.should.Matchers
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.prop.TableDrivenPropertyChecks

import scala.collection.immutable.Map

Expand All @@ -30,7 +31,8 @@ class InterpreterStringExpressionTest
extends AnyFlatSpec
with Matchers
with FeelEngineTest
with EvaluationResultMatchers {
with EvaluationResultMatchers
with TableDrivenPropertyChecks {

"A string" should "concatenates to another String" in {

Expand Down Expand Up @@ -80,40 +82,59 @@ class InterpreterStringExpressionTest
evaluateExpression(""" "a" != null """) should returnResult(true)
}

it should "return not escaped characters" in {

evaluateExpression(""" "Hello\nWorld" """) should returnResult("Hello\nWorld")
evaluateExpression(" x ", Map("x" -> "Hello\nWorld")) should returnResult("Hello\nWorld")
private val escapeSequences = Table(
("Character", "Expected", "Display name"),
('\n', '\n', "new line"),
('\r', '\r', "carriage return"),
('\t', '\t', "tab"),
('\b', '\b', "backspace"),
('\f', '\f', "form feed"),
('\'', '\'', "single quote"),
("\\\"", '"', "double quote"),
("\\\\", '\\', "backslash")
)

evaluateExpression(""" "Hello\rWorld" """) should returnResult("Hello\rWorld")
evaluateExpression(" x ", Map("x" -> "Hello\rWorld")) should returnResult("Hello\rWorld")
it should "contains an escape sequence" in {
forEvery(escapeSequences) { (character, expected, _) =>
val expectedString = s"a $expected b"

evaluateExpression(""" "Hello\'World" """) should returnResult("Hello\'World")
evaluateExpression(" x ", Map("x" -> "Hello\'World")) should returnResult("Hello\'World")
evaluateExpression(s" \"a $character b\" ") should returnResult(expectedString)
evaluateExpression("char", Map("char" -> expectedString)) should returnResult(expectedString)
}
}

evaluateExpression(""" "Hello\tWorld" """) should returnResult("Hello\tWorld")
evaluateExpression(" x ", Map("x" -> "Hello\tWorld")) should returnResult("Hello\tWorld")
private val unicodeCharacters = Table(
("Character", "Display name"),
('\u269D', "\\u269D"),
("\\U101EF", "\\U101EF")
)

evaluateExpression(""" "Hello\"World" """) should returnResult("Hello\"World")
evaluateExpression(" x ", Map("x" -> "Hello\"World")) should returnResult("Hello\"World")
it should "contains unicode characters" in {
forEvery(unicodeCharacters) { (character, _) =>
evaluateExpression(s" \"a $character b\" ") should returnResult(s"a $character b")
}
}

List(
" \' ",
" \\ ",
" \n ",
" \r ",
" \t ",
""" \u269D """,
""" \U101EF """
private val regexCharacters = Table(
("Character", "Display name"),
("\\s", "\\s"),
("\\S", "\\S"),
("\\d", "\\d"),
("\\w", "\\w"),
("\\R", "\\R"),
("\\h", "\\h"),
("\\v", "\\v"),
("\\\n", "\\n"),
("\\\r", "\\r")
)
.foreach { notEscapeChar =>
it should s"contains a not escape sequence ($notEscapeChar)" in {

evaluateExpression(s""" "a $notEscapeChar b" """) should returnResult(
s"""a $notEscapeChar b"""
)
}
it should "contains a regex character" in {
forEvery(regexCharacters) { (character, _) =>
val expectedString = s"a $character b"

evaluateExpression(s" \"a $character b\" ") should returnResult(expectedString)
evaluateExpression("char", Map("char" -> expectedString)) should returnResult(expectedString)
}
}

}