You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by he...@apache.org on 2020/11/02 12:00:50 UTC

[commons-jexl] branch master updated: JEXL-336 support some escape control characters

This is an automated email from the ASF dual-hosted git repository.

henrib pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git


The following commit(s) were added to refs/heads/master by this push:
     new 2c7517e  JEXL-336 support some escape control characters
     new 7d5ee87  Merge pull request #32 from hussachai/JEXL-336
2c7517e is described below

commit 2c7517ef24fc92c3c0ab892d8ba5867ddce5b419
Author: Hussachai Puripunpinyo <hu...@workday.com>
AuthorDate: Sat Oct 31 23:37:27 2020 -0700

    JEXL-336 support some escape control characters
---
 .gitignore                                         |  4 ++++
 .../apache/commons/jexl3/parser/StringParser.java  | 14 ++++++++++++--
 .../apache/commons/jexl3/parser/ParserTest.java    | 22 ++++++++++++++++++++++
 3 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore
index 850aaae..3a23372 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,7 @@ target/
 /.classpath
 /.project
 /.settings/
+
+# intellij files
+.idea/
+/*.iml
diff --git a/src/main/java/org/apache/commons/jexl3/parser/StringParser.java b/src/main/java/org/apache/commons/jexl3/parser/StringParser.java
index 6072a3f..1747c1e 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/StringParser.java
+++ b/src/main/java/org/apache/commons/jexl3/parser/StringParser.java
@@ -99,9 +99,19 @@ public class StringParser {
                     // if c is not an escapable character, re-emmit the backslash before it
                     boolean notSeparator = sep == 0 ? c != '\'' && c != '"' : c != sep;
                     if (notSeparator && c != '\\') {
-                        strb.append('\\');
+                        switch (c) {
+                            // http://es5.github.io/x7.html#x7.8.4
+                            case 'b': strb.append('\b'); break; // backspace \u0008
+                            case 't': strb.append('\t'); break; // horizontal tab \u0009
+                            case 'n': strb.append('\n'); break; // line feed \u000A
+                            // We don't support vertical tab. If needed, the unicode (\u000B) should be used instead
+                            case 'f': strb.append('\f'); break; // form feed \u000C
+                            case 'r': strb.append('\r'); break; // carriage return \u000D
+                            default: strb.append('\\').append(c);
+                        }
+                    } else {
+                        strb.append(c);
                     }
-                    strb.append(c);
                 }
                 escape = false;
                 continue;
diff --git a/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java b/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java
index a15b01f..222e14d 100644
--- a/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java
+++ b/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java
@@ -87,4 +87,26 @@ public class ParserTest {
             Assert.assertEquals(id, esc1);
         }
     }
+
+    /**
+     * Test the escaped control characters.
+     */
+    @Test
+    public void testControlCharacters() {
+        // Both '' and "" are valid JEXL string
+        // The array of tuples where the first element is an expected result and the second element is a test string.
+        String[][] strings = new String[][] {
+            new String[] {"a\nb\tc", "'a\nb\tc'"}, // we still honor the actual characters
+            new String[] {"a\nb\tc", "'a\\nb\\tc'"},
+            new String[] {"a\nb\tc", "\"a\\nb\\tc\""},
+            new String[] {"\b\t\n\f\r", "'\\b\\t\\n\\f\\r'"},
+            new String[] {"'hi'", "'\\'hi\\''"},
+            new String[] {"\"hi\"", "'\"hi\"'"},
+            new String[] {"\"hi\"", "'\"hi\"'"},
+        };
+        for(String[] pair: strings) {
+            String output = StringParser.buildString(pair[1], true);
+            Assert.assertEquals(pair[0], output);
+        }
+    }
 }