Repository: groovy
Updated Branches:
  refs/heads/master c6c375334 -> 72c7c2ea1


Avoid compiling regex pattern repeatedly for better performance


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/72c7c2ea
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/72c7c2ea
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/72c7c2ea

Branch: refs/heads/master
Commit: 72c7c2ea10f7b08b5208db2ce7b4123398adce8a
Parents: c6c3753
Author: Daniel Sun <sun...@apache.org>
Authored: Thu Nov 1 22:56:38 2018 +0800
Committer: Daniel Sun <sun...@apache.org>
Committed: Thu Nov 1 22:57:54 2018 +0800

----------------------------------------------------------------------
 .../groovy/parser/antlr4/util/StringUtils.java   | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/72c7c2ea/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java
----------------------------------------------------------------------
diff --git 
a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java
 
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java
index b674734..93e10e2 100644
--- 
a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java
+++ 
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java
@@ -30,14 +30,17 @@ import java.util.regex.Pattern;
  */
 public class StringUtils {
        private static final String BACKSLASH = "\\";
+       private static final Pattern HEX_ESCAPES_PATTERN = 
Pattern.compile("(\\\\*)\\\\u([0-9abcdefABCDEF]{4})");
+       private static final Pattern OCTAL_ESCAPES_PATTERN = 
Pattern.compile("(\\\\*)\\\\([0-3]?[0-7]?[0-7])");
+       private static final Pattern STANDARD_ESCAPES_PATTERN = 
Pattern.compile("(\\\\*)\\\\([btnfr\"'])");
+       private static final Pattern LINE_ESCAPE_PATTERN = 
Pattern.compile("(\\\\*)\\\\\r?\n");
 
        public static String replaceHexEscapes(String text) {
                if (!text.contains(BACKSLASH)) {
                        return text;
                }
 
-               Pattern p = 
Pattern.compile("(\\\\*)\\\\u([0-9abcdefABCDEF]{4})");
-               return StringGroovyMethods.replaceAll((CharSequence) text, p, 
new Closure<Void>(null, null) {
+               return StringGroovyMethods.replaceAll((CharSequence) text, 
HEX_ESCAPES_PATTERN, new Closure<Void>(null, null) {
                        Object doCall(String _0, String _1, String _2) {
                                if (isLengthOdd(_1)) {
                                        return _0;
@@ -53,8 +56,7 @@ public class StringUtils {
                        return text;
                }
 
-               Pattern p = Pattern.compile("(\\\\*)\\\\([0-3]?[0-7]?[0-7])");
-               return StringGroovyMethods.replaceAll((CharSequence) text, p, 
new Closure<Void>(null, null) {
+               return StringGroovyMethods.replaceAll((CharSequence) text, 
OCTAL_ESCAPES_PATTERN, new Closure<Void>(null, null) {
                        Object doCall(String _0, String _1, String _2) {
                                if (isLengthOdd(_1)) {
                                        return _0;
@@ -78,9 +80,7 @@ public class StringUtils {
                        return text;
                }
 
-               Pattern p = Pattern.compile("(\\\\*)\\\\([btnfr\"'])");
-
-               String result = StringGroovyMethods.replaceAll((CharSequence) 
text, p, new Closure<Void>(null, null) {
+               String result = StringGroovyMethods.replaceAll((CharSequence) 
text, STANDARD_ESCAPES_PATTERN, new Closure<Void>(null, null) {
                        Object doCall(String _0, String _1, String _2) {
                                if (isLengthOdd(_1)) {
                                        return _0;
@@ -138,8 +138,7 @@ public class StringUtils {
                        return text;
                }
 
-               Pattern p = Pattern.compile("(\\\\*)\\\\\r?\n");
-               text = StringGroovyMethods.replaceAll((CharSequence) text, p, 
new Closure<Void>(null, null) {
+               text = StringGroovyMethods.replaceAll((CharSequence) text, 
LINE_ESCAPE_PATTERN, new Closure<Void>(null, null) {
                        Object doCall(String _0, String _1) {
                                if (isLengthOdd(_1)) {
                                        return _0;
@@ -208,7 +207,7 @@ public class StringUtils {
                increase = (increase < 0 ? 0 : increase) * 16;
                final StringBuilder buf = new StringBuilder(text.length() + 
increase);
                while (end != INDEX_NOT_FOUND) {
-                       buf.append(text.substring(start, 
end)).append(replacement);
+                       buf.append(text, start, end).append(replacement);
                        start = end + replLength;
                        end = text.indexOf(searchString, start);
                }

Reply via email to