From 71febc875fdcc5993eac2eb15993ca34bde7a1c4 Mon Sep 17 00:00:00 2001 From: Daniel Sun Date: Tue, 14 Jan 2025 23:27:40 +0900 Subject: [PATCH] Use efficient `replaceAll` instead --- .../parser/antlr4/util/StringUtils.java | 66 ++++++++++--------- 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java b/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java index 1c80045e5b6..f4d431b53cd 100644 --- a/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java +++ b/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java @@ -18,14 +18,13 @@ */ package org.apache.groovy.parser.antlr4.util; -import groovy.lang.Closure; import org.apache.groovy.util.Maps; import java.util.Map; +import java.util.function.Function; +import java.util.regex.Matcher; import java.util.regex.Pattern; -import static org.codehaus.groovy.runtime.StringGroovyMethods.replaceAll; - /** * Utilities for handling strings * @@ -57,17 +56,30 @@ public static String replaceEscapes(String text, int slashyType) { public static String replaceHexEscapes(String text) { if (!text.contains(BACKSLASH)) return text; - return replaceAll((CharSequence) text, HEX_ESCAPES_PATTERN, REPLACE_HEX_ESCAPES); + return replaceAll(text, HEX_ESCAPES_PATTERN, m -> { + final String _0 = m.group(0), _1 = m.group(1), _2 = m.group(2); + if (isLengthOdd(_1)) return _0; + return _1 + new String(Character.toChars(Integer.parseInt(_2, 16))); + }); } public static String replaceOctalEscapes(String text) { if (!text.contains(BACKSLASH)) return text; - return replaceAll((CharSequence) text, OCTAL_ESCAPES_PATTERN, REPLACE_OCTAL_ESCAPES); + return replaceAll(text, OCTAL_ESCAPES_PATTERN, m -> { + final String _0 = m.group(0), _1 = m.group(1), _2 = m.group(2); + if (isLengthOdd(_1)) return _0; + return _1 + new String(Character.toChars(Integer.parseInt(_2, 8))); + }); } public static String replaceStandardEscapes(String text) { if (!text.contains(BACKSLASH)) return text; - final String result = replaceAll((CharSequence) text, STANDARD_ESCAPES_PATTERN, REPLACE_STANDARD_ESCAPES); + final String result = replaceAll(text, STANDARD_ESCAPES_PATTERN, m -> { + final String _0 = m.group(0), _1 = m.group(1), _2 = m.group(2); + if (isLengthOdd(_1)) return _0; + final Character character = STANDARD_ESCAPES.get(_2.charAt(0)); + return _1 + (character != null ? character : _2); + }); return replace(result,"\\\\", "\\"); } @@ -166,10 +178,25 @@ private static String replaceEscapes(String text) { private static String replaceLineEscape(String text) { if (!text.contains(BACKSLASH)) return text; - text = replaceAll((CharSequence) text, LINE_ESCAPE_PATTERN, REPLACE_LINE_ESCAPE); + text = replaceAll(text, LINE_ESCAPE_PATTERN, m -> { + final String _0 = m.group(0), _1 = m.group(1); + if (isLengthOdd(_1)) return _0; + return _1; + }); return text; } + private static String replaceAll(final CharSequence input, final Pattern pattern, final Function replaceFunction) { + final Matcher matcher = pattern.matcher(input); + final StringBuilder result = new StringBuilder(input.length() + matcher.groupCount() * 8); + while (matcher.find()) { + final String replacement = replaceFunction.apply(matcher); + matcher.appendReplacement(result, Matcher.quoteReplacement(replacement)); + } + matcher.appendTail(result); + return result.toString(); + } + private static boolean isLengthOdd(String str) { return null != str && str.length() % 2 == 1; } @@ -193,29 +220,4 @@ private static boolean isLengthOdd(String str) { 'r', '\r', 's', ' ' ); - private static final Closure REPLACE_STANDARD_ESCAPES = new Closure(null, null) { - String doCall(String _0, String _1, String _2) { - if (isLengthOdd(_1)) return _0; - final Character character = STANDARD_ESCAPES.get(_2.charAt(0)); - return _1 + (character != null ? character : _2); - } - }; - private static final Closure REPLACE_OCTAL_ESCAPES = new Closure(null, null) { - String doCall(String _0, String _1, String _2) { - if (isLengthOdd(_1)) return _0; - return _1 + new String(Character.toChars(Integer.parseInt(_2, 8))); - } - }; - private static final Closure REPLACE_HEX_ESCAPES = new Closure(null, null) { - String doCall(String _0, String _1, String _2) { - if (isLengthOdd(_1)) return _0; - return _1 + new String(Character.toChars(Integer.parseInt(_2, 16))); - } - }; - private static final Closure REPLACE_LINE_ESCAPE = new Closure(null, null) { - String doCall(String _0, String _1) { - if (isLengthOdd(_1)) return _0; - return _1; - } - }; }