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.16] fix: Handle escape and regex characters #935

Merged
merged 5 commits into from
Sep 26, 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 @@ -688,22 +688,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 @@ -16,100 +16,125 @@
*/
package org.camunda.feel.impl.interpreter

import org.camunda.feel.impl.FeelIntegrationTest
import org.camunda.feel.impl.{EvaluationResultMatchers, FeelEngineTest, FeelIntegrationTest}
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

/** @author
* Philipp Ossler
*/
class InterpreterStringExpressionTest extends AnyFlatSpec with Matchers with FeelIntegrationTest {
class InterpreterStringExpressionTest
extends AnyFlatSpec
with Matchers
with FeelEngineTest
with EvaluationResultMatchers
with TableDrivenPropertyChecks {

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

eval(""" "a" + "b" """) should be(ValString("ab"))
evaluateExpression(""" "a" + "b" """) should returnResult("ab")
}

it should "compare with '='" in {

eval(""" "a" = "a" """) should be(ValBoolean(true))
eval(""" "a" = "b" """) should be(ValBoolean(false))
evaluateExpression(""" "a" = "a" """) should returnResult(true)
evaluateExpression(""" "a" = "b" """) should returnResult(false)
}

it should "compare with '!='" in {

eval(""" "a" != "a" """) should be(ValBoolean(false))
eval(""" "a" != "b" """) should be(ValBoolean(true))
evaluateExpression(""" "a" != "a" """) should returnResult(false)
evaluateExpression(""" "a" != "b" """) should returnResult(true)
}

it should "compare with '<'" in {

eval(""" "a" < "b" """) should be(ValBoolean(true))
eval(""" "b" < "a" """) should be(ValBoolean(false))
evaluateExpression(""" "a" < "b" """) should returnResult(true)
evaluateExpression(""" "b" < "a" """) should returnResult(false)
}

it should "compare with '<='" in {

eval(""" "a" <= "a" """) should be(ValBoolean(true))
eval(""" "b" <= "a" """) should be(ValBoolean(false))
evaluateExpression(""" "a" <= "a" """) should returnResult(true)
evaluateExpression(""" "b" <= "a" """) should returnResult(false)
}

it should "compare with '>'" in {

eval(""" "b" > "a" """) should be(ValBoolean(true))
eval(""" "a" > "b" """) should be(ValBoolean(false))
evaluateExpression(""" "b" > "a" """) should returnResult(true)
evaluateExpression(""" "a" > "b" """) should returnResult(false)
}

it should "compare with '>='" in {

eval(""" "b" >= "b" """) should be(ValBoolean(true))
eval(""" "a" >= "b" """) should be(ValBoolean(false))
evaluateExpression(""" "b" >= "b" """) should returnResult(true)
evaluateExpression(""" "a" >= "b" """) should returnResult(false)
}

it should "compare with null" in {

eval(""" "a" = null """) should be(ValBoolean(false))
eval(""" null = "a" """) should be(ValBoolean(false))
eval(""" "a" != null """) should be(ValBoolean(true))
evaluateExpression(""" "a" = null """) should returnResult(false)
evaluateExpression(""" null = "a" """) should returnResult(false)
evaluateExpression(""" "a" != null """) should returnResult(true)
}

it should "return not escaped characters" in {

eval(""" "Hello\nWorld" """) should be(ValString("Hello\nWorld"))
eval(" x ", Map("x" -> "Hello\nWorld")) should be(ValString("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")
)

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

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

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

eval(""" "Hello\"World" """) should be(ValString("Hello\"World"))
eval(" x ", Map("x" -> "Hello\"World")) should be(ValString("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 {

eval(s""" "a $notEscapeChar b" """) should be(
ValString(
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)
}
}

}
Loading