Repository: groovy Updated Branches: refs/heads/master b9b69444c -> 12d820627
Avoid compiling regex 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/12d82062 Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/12d82062 Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/12d82062 Branch: refs/heads/master Commit: 12d820627983d92ac24d8a296363d11b0417fa4b Parents: b9b6944 Author: Daniel Sun <sun...@apache.org> Authored: Sat Dec 1 23:00:23 2018 +0800 Committer: Daniel Sun <sun...@apache.org> Committed: Sat Dec 1 23:00:23 2018 +0800 ---------------------------------------------------------------------- .../groovy/parser/antlr4/SemanticPredicates.java | 19 ++++++++++++++----- .../groovy/parser/antlr4/util/StringUtils.java | 8 ++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/groovy/blob/12d82062/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/SemanticPredicates.java ---------------------------------------------------------------------- diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/SemanticPredicates.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/SemanticPredicates.java index 3efb00c..9c1f709 100644 --- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/SemanticPredicates.java +++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/SemanticPredicates.java @@ -26,6 +26,7 @@ import org.antlr.v4.runtime.tree.ParseTree; import java.util.Collections; import java.util.List; import java.util.Set; +import java.util.regex.Pattern; import static org.apache.groovy.parser.antlr4.GroovyParser.ASSIGN; import static org.apache.groovy.parser.antlr4.GroovyParser.BuiltInPrimitiveType; @@ -40,6 +41,8 @@ import static org.apache.groovy.parser.antlr4.GroovyParser.PathExpressionContext import static org.apache.groovy.parser.antlr4.GroovyParser.PostfixExprAltContext; import static org.apache.groovy.parser.antlr4.GroovyParser.PostfixExpressionContext; import static org.apache.groovy.parser.antlr4.GroovyParser.StringLiteral; +import static org.apache.groovy.parser.antlr4.util.StringUtils.matches; + /** * Some semantic predicates for altering the behaviour of the lexer and parser * @@ -47,9 +50,15 @@ import static org.apache.groovy.parser.antlr4.GroovyParser.StringLiteral; * Created on 2016/08/20 */ public class SemanticPredicates { + private static final Pattern NONSPACES_PATTERN = Pattern.compile("\\S+?"); + private static final Pattern LETTER_AND_LEFTCURLY_PATTERN = Pattern.compile("[a-zA-Z_{]"); + private static final Pattern NONSURROGATE_PATTERN = Pattern.compile("[^\u0000-\u007F\uD800-\uDBFF]"); + private static final Pattern SURROGATE_PAIR1_PATTERN = Pattern.compile("[\uD800-\uDBFF]"); + private static final Pattern SURROGATE_PAIR2_PATTERN = Pattern.compile("[\uDC00-\uDFFF]"); + public static boolean isFollowedByWhiteSpaces(CharStream cs) { for (int index = 1, c = cs.LA(index); !('\r' == c || '\n' == c || CharStream.EOF == c); index++, c = cs.LA(index)) { - if (String.valueOf((char) c).matches("\\S+?")) { + if (matches(String.valueOf((char) c), NONSPACES_PATTERN)) { return false; } } @@ -78,11 +87,11 @@ public class SemanticPredicates { String str1 = String.valueOf((char) c1); - if (str1.matches("[a-zA-Z_{]")) { + if (matches(str1, LETTER_AND_LEFTCURLY_PATTERN)) { return true; } - if (str1.matches("[^\u0000-\u007F\uD800-\uDBFF]") + if (matches(str1, NONSURROGATE_PATTERN) && Character.isJavaIdentifierPart(c1)) { return true; } @@ -90,8 +99,8 @@ public class SemanticPredicates { int c2 = cs.LA(2); String str2 = String.valueOf((char) c2); - if (str1.matches("[\uD800-\uDBFF]") - && str2.matches("[\uDC00-\uDFFF]") + if (matches(str1, SURROGATE_PAIR1_PATTERN) + && matches(str2, SURROGATE_PAIR2_PATTERN) && Character.isJavaIdentifierPart(Character.toCodePoint((char) c1, (char) c2))) { return true; http://git-wip-us.apache.org/repos/asf/groovy/blob/12d82062/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 93e10e2..1adc469 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 @@ -27,6 +27,9 @@ import java.util.regex.Pattern; /** * Utilities for handling strings + * + * @author <a href="mailto:realblue...@hotmail.com">Daniel.Sun</a> + * Created on 2016/08/20 */ public class StringUtils { private static final String BACKSLASH = "\\"; @@ -169,6 +172,10 @@ public class StringUtils { return length == quotationLength << 1 ? "" : text.substring(quotationLength, length - quotationLength); } + public static boolean matches(String text, Pattern pattern) { + return pattern.matcher(text).matches(); + } + /** * The modified implementation is based on StringUtils#replace(String text, String searchString, String replacement, int max), Apache commons-lang3-3.6 * @@ -234,6 +241,7 @@ public class StringUtils { * * @param cs the CharSequence to check, may be null * @return {@code true} if the CharSequence is empty or null + * @since 3.0 Changed signature from isEmpty(String) to isEmpty(CharSequence) */ public static boolean isEmpty(final CharSequence cs) { return cs == null || cs.length() == 0;