You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2017/08/29 18:05:28 UTC

groovy git commit: Improve the performance of replacing strings

Repository: groovy
Updated Branches:
  refs/heads/master 4bd92e664 -> 099fbbcea


Improve the performance of replacing strings


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

Branch: refs/heads/master
Commit: 099fbbcea6a6ae3958f1abeadf5d8cfc6799880a
Parents: 4bd92e6
Author: sunlan <su...@apache.org>
Authored: Wed Aug 30 02:05:18 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Wed Aug 30 02:05:18 2017 +0800

----------------------------------------------------------------------
 src/main/antlr/GroovyParser.g4                  |  4 +-
 .../groovy/parser/antlr4/util/StringUtils.java  | 42 ++++++++++++++++----
 2 files changed, 36 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/099fbbce/src/main/antlr/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/src/main/antlr/GroovyParser.g4 b/src/main/antlr/GroovyParser.g4
index 4456a60..b5e4c84 100644
--- a/src/main/antlr/GroovyParser.g4
+++ b/src/main/antlr/GroovyParser.g4
@@ -1250,7 +1250,7 @@ keywords
 rparen
     :   RPAREN
     |
-        // !!!Error Alternative
+        // !!!Error Alternative, impact the performance of parsing
         ~LPAREN
         { require(false, "Missing ')'", -1); }
     ;
@@ -1258,7 +1258,7 @@ rparen
 rbrack
     :   RBRACK
     |
-        // !!!Error Alternative
+        // !!!Error Alternative, impact the performance of parsing
         ~LBRACK
         { require(false, "Missing ']'", -1); }
     ;

http://git-wip-us.apache.org/repos/asf/groovy/blob/099fbbce/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 e200742..ef75471 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
@@ -80,7 +80,7 @@ public class StringUtils {
 							}
 						});
 
-		return result.replace("\\\\", "\\");
+		return replace(result, Maps.of("\\\\", "\\"));
     }
 
 	public static final int NONE_SLASHY = 0;
@@ -92,15 +92,17 @@ public class StringUtils {
 			text = StringUtils.replaceHexEscapes(text);
 			text = StringUtils.replaceLineEscape(text);
 
+			StringBuilder sb = new StringBuilder(text);
 			if (slashyType == SLASHY) {
-				text = text.replace("\\/", "/");
+				replace(sb, Maps.of("\\/", "/"));
 			}
 
 			if (slashyType == DOLLAR_SLASHY) {
-				text = text.replace("$$", "$");
-				text = text.replace("$/", "/");
+				replace(sb, Maps.of("$$", "$"));
+				replace(sb, Maps.of("$/", "/"));
 			}
 
+			text = sb.toString();
 		} else if (slashyType == NONE_SLASHY) {
 			text = StringUtils.replaceEscapes(text);
 		} else {
@@ -111,8 +113,7 @@ public class StringUtils {
 	}
 
 	private static String replaceEscapes(String text) {
-		text = text.replace("\\$", "$");
-
+		text = replace(text, Maps.of("\\$", "$"));
 		text = StringUtils.replaceLineEscape(text);
 
         return StringUtils.replaceStandardEscapes(replaceHexEscapes(replaceOctalEscapes(text)));
@@ -138,16 +139,41 @@ public class StringUtils {
 	}
 
 	public static String removeCR(String text) {
-        return text.replace("\r\n", "\n");
+		return replace(text, Maps.of("\r\n", "\n"));
     }
 
 	public static long countChar(String text, char c) {
 		return text.chars().filter(e -> c == e).count();
 	}
 
+	public static String replace(String str, Map<String, String> replacements) {
+		return replace(new StringBuilder(str), replacements).toString();
+	}
+
+	public static StringBuilder replace(StringBuilder sb, Map<String, String> replacements) {
+
+		for (Map.Entry<String, String> entry : replacements.entrySet()) {
+			String key = entry.getKey();
+			int keyLength = key.length();
+
+			String value = entry.getValue();
+			int valueLength = value.length();
+
+			int start = sb.indexOf(key, 0);
+			while (start > -1) {
+				int end = start + keyLength;
+				int nextSearchStart = start + valueLength;
+				sb.replace(start, end, value);
+				start = sb.indexOf(key, nextSearchStart);
+			}
+		}
+
+		return sb;
+	}
+
 	public static String trimQuotations(String text, int quotationLength) {
 		int length = text.length();
 
-		return length == quotationLength * 2 ? "" : text.substring(quotationLength, length - quotationLength);
+		return length == quotationLength << 1 ? "" : text.substring(quotationLength, length - quotationLength);
 	}
 }
\ No newline at end of file