Skip to content

Commit

Permalink
Merge pull request #933 from camunda/backport-930-to-1.18
Browse files Browse the repository at this point in the history
[Backport 1.18] fix: Handle escape and regex characters
  • Loading branch information
saig0 authored Sep 25, 2024
2 parents 3fa6654 + 1de642c commit b88f97b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 43 deletions.
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)
}
}

}

0 comments on commit b88f97b

Please sign in to comment.