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 2018/05/16 08:41:27 UTC

[commons-jexl] branch master updated: [JEXL]: - preparing to add 'escaped' identifiers

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 8afe559  [JEXL]: - preparing to add 'escaped' identifiers
8afe559 is described below

commit 8afe5595058c727b64b10decaa49bb5a72acc977
Author: henrib <>
AuthorDate: Wed May 16 10:40:19 2018 +0200

    [JEXL]:
    - preparing to add 'escaped' identifiers
---
 .../apache/commons/jexl3/parser/StringParser.java  | 67 ++++++++++++++++++++++
 1 file changed, 67 insertions(+)

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 8cb4b9d..375c1dd 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/StringParser.java
+++ b/src/main/java/org/apache/commons/jexl3/parser/StringParser.java
@@ -205,5 +205,72 @@ public class StringParser {
         strb.append(delim);
         return strb.toString();
     }
+    
+    /**
+     * Remove escape char ('\') from an identifier.
+     * @param str the identifier escaped string, ie with a backslash before space, quote, double-quote and backslash
+     * @return the string with no '\\' character
+     */
+    public static String unescapeIdentifier(String str) {
+        StringBuilder strb = null;
+        boolean esc = false;
+        if (str != null) {
+            int n = 0;
+            int last = str.length();
+            while (n < last) {
+                char c = str.charAt(n);
+                if (esc) {
+                    if (strb == null) {
+                        strb = new StringBuilder(last);
+                        strb.append(str.substring(0, n));
+                    } else {
+                        strb.append(c);
+                    }
+                    esc = false;
+                } else if (c == '\\') {
+                    esc = true;
+                } else if (strb != null) {
+                    strb.append(c);
+                }
+                n += 1;
+            }
+        }
+        return strb == null ? str : strb.toString();
+    }
 
+    /**
+     * Adds a escape char ('\') where needed in a string form of an ide
+     * @param str the identifier un-escaped string
+     * @return the string with added  backslash character before space, quote, double-quote and backslash
+     */
+    public static String escapeIdentifier(String str) {
+        StringBuilder strb = null;
+        if (str != null) {
+            int n = 0;
+            int last = str.length();
+            while (n < last) {
+                char c = str.charAt(n);
+                switch (c) {
+                    case ' ':
+                    case '\'':
+                    case '"':
+                    case '\\': {
+                        if (strb == null) {
+                            strb = new StringBuilder(last);
+                            strb.append(str.substring(0, n));
+                        }
+                        strb.append('\\');
+                        strb.append(c);
+                        break;
+                    }
+                    default:
+                        if (strb != null) {
+                            strb.append(c);
+                        }
+                }
+                n += 1;
+            }
+        }
+        return strb == null ? str : strb.toString();
+    }
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
henrib@apache.org.