Do not use String.replace() 34/64434/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 16 Sep 2017 08:28:49 +0000 (10:28 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 18 Oct 2017 09:54:52 +0000 (11:54 +0200)
This forces a Pattern compilation, which we do not really want.
Pre-compile the patterns instead.

Change-Id: Ibcb9f911f4a4089f375264b75ce2f5d9c5f46447
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 359c773d8bffa2caba25cdaf2561ede71fe90736)

yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/Utils.java

index 677b9b3456c77a00a227c1023a9b48cf71c0d15b..d9ef0107c1449023dee4be748cfb192d831b5c41 100644 (file)
@@ -58,6 +58,10 @@ public final class Utils {
     private static final String YANG_XPATH_FUNCTIONS_STRING =
             "(re-match|deref|derived-from(-or-self)?|enum-value|bit-is-set)(\\()";
     private static final Pattern YANG_XPATH_FUNCTIONS_PATTERN = Pattern.compile(YANG_XPATH_FUNCTIONS_STRING);
+    private static final Pattern ESCAPED_DQUOT = Pattern.compile("\\\"", Pattern.LITERAL);
+    private static final Pattern ESCAPED_BACKSLASH = Pattern.compile("\\\\", Pattern.LITERAL);
+    private static final Pattern ESCAPED_LF = Pattern.compile("\\n", Pattern.LITERAL);
+    private static final Pattern ESCAPED_TAB = Pattern.compile("\\t", Pattern.LITERAL);
 
     private static final ThreadLocal<XPathFactory> XPATH_FACTORY = new ThreadLocal<XPathFactory>() {
         @Override
@@ -215,8 +219,13 @@ public final class Utils {
                  * in the inner string and trim the result.
                  */
                 checkDoubleQuotedString(innerStr, yangVersion, ref);
-                sb.append(innerStr.replace("\\\"", "\"").replace("\\\\", "\\").replace("\\n", "\n")
-                        .replace("\\t", "\t"));
+                sb.append(ESCAPED_TAB.matcher(
+                    ESCAPED_LF.matcher(
+                        ESCAPED_BACKSLASH.matcher(
+                            ESCAPED_DQUOT.matcher(innerStr).replaceAll("\\\""))
+                        .replaceAll("\\\\"))
+                    .replaceAll("\\\n"))
+                    .replaceAll("\\\t"));
             } else if (firstChar == '\'' && lastChar == '\'') {
                 /*
                  * According to RFC6020 a single quote character cannot occur in