diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java index 9ae615e3b9..b59651111e 100644 --- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java +++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java @@ -2350,17 +2350,17 @@ public ConstantExpression visitStringLiteral(StringLiteralContext ctx) { if (text.startsWith("'''") || text.startsWith("\"\"\"")) { text = StringUtils.removeCR(text); // remove CR in the multiline string - text = text.length() == 6 ? "" : text.substring(3, text.length() - 3); + text = StringUtils.trimQuotations(text, 3); } else if (text.startsWith("'") || text.startsWith("/") || text.startsWith("\"")) { if (text.startsWith("/")) { // the slashy string can span rows, so we have to remove CR for it text = StringUtils.removeCR(text); // remove CR in the multiline string } - text = text.length() == 2 ? "" : text.substring(1, text.length() - 1); + text = StringUtils.trimQuotations(text, 1); } else if (text.startsWith("$/")) { text = StringUtils.removeCR(text); - text = text.length() == 4 ? "" : text.substring(2, text.length() - 2); + text = StringUtils.trimQuotations(text, 2); } //handle escapes. 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 58ac588458..e20074227c 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 @@ -22,7 +22,6 @@ import org.apache.groovy.util.Maps; import org.codehaus.groovy.runtime.StringGroovyMethods; -import java.util.HashMap; import java.util.Map; import java.util.regex.Pattern; @@ -145,4 +144,10 @@ public static String removeCR(String text) { public static long countChar(String text, char c) { return text.chars().filter(e -> c == e).count(); } + + public static String trimQuotations(String text, int quotationLength) { + int length = text.length(); + + return length == quotationLength * 2 ? "" : text.substring(quotationLength, length - quotationLength); + } } \ No newline at end of file