This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 1f3568c  GROOVY-9442: GEP: Support for the new JDK14 string escape 
sequence (\s for single space) (closes #1196, side effect: closes #1190)
1f3568c is described below

commit 1f3568c73c65230600b89bbd8351f5483d9359c6
Author: Paul King <[email protected]>
AuthorDate: Tue Mar 17 10:58:26 2020 +1000

    GROOVY-9442: GEP: Support for the new JDK14 string escape sequence (\s for 
single space) (closes #1196, side effect: closes #1190)
---
 src/antlr/GroovyLexer.g4                           |  2 +-
 src/test/groovy/StringTest.groovy                  | 31 ++++++++++++++++++++++
 .../groovy/parser/antlr4/util/StringUtils.java     |  5 ++--
 3 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/src/antlr/GroovyLexer.g4 b/src/antlr/GroovyLexer.g4
index d13749d..411c366 100644
--- a/src/antlr/GroovyLexer.g4
+++ b/src/antlr/GroovyLexer.g4
@@ -684,7 +684,7 @@ BooleanLiteral
 
 fragment
 EscapeSequence
-    :   Backslash [btnfr"'\\]
+    :   Backslash [btnfrs"'\\]
     |   OctalEscape
     |   UnicodeEscape
     |   DollarEscape
diff --git a/src/test/groovy/StringTest.groovy 
b/src/test/groovy/StringTest.groovy
index 40ce071..6f348d8 100644
--- a/src/test/groovy/StringTest.groovy
+++ b/src/test/groovy/StringTest.groovy
@@ -385,4 +385,35 @@ foo
         assert '    x    '.unexpand() == '    x\t '
         assert '    x    \n'.unexpand() == '    x\t \n'
     }
+
+    void "test JDK14 Escape-S should be replaced by space"() {
+        assert 'ab\scd\s'.size() == 6
+        assert 'ab\scd\s'.bytes  == [97, 98, 32, 99, 100, 32]
+        assert "ab\scd\s".size() == 6
+        assert "ab\scd\s".bytes  == [97, 98, 32, 99, 100, 32]
+    }
+
+    void "test JDK14 Escape-S should not impact slashy or dollar-slashy 
strings"() {
+        assert /ab\scd\s/.size() == 8
+        assert /ab\scd\s/.bytes  == [97, 98, 92, 115, 99, 100, 92, 115]
+        assert $/ab\scd\s/$.size() == 8
+        assert $/ab\scd\s/$.bytes == [97, 98, 92, 115, 99, 100, 92, 115]
+    }
+
+    void "test JDK14 Escape-S multi-line example"() {
+        // control case (existing functionality)
+        String colors = '''\
+            red   \n\
+            green \n\
+            blue  \n\
+        '''.stripIndent(true)
+        assert colors.readLines()*.size() == [6, 6, 6]
+
+        colors = '''\
+            red  \s
+            green\s
+            blue \s
+        '''.stripIndent(true)
+        assert colors.readLines()*.size() == [6, 6, 6]
+    }
 }
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 44cfef2..d4fc339 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
@@ -33,7 +33,7 @@ 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 STANDARD_ESCAPES_PATTERN = 
Pattern.compile("(\\\\*)\\\\([btnfrs\"'])");
        private static final Pattern LINE_ESCAPE_PATTERN = 
Pattern.compile("(\\\\*)\\\\\r?\n");
 
        public static String replaceHexEscapes(String text) {
@@ -73,7 +73,8 @@ public class StringUtils {
                        't', '\t',
                        'n', '\n',
                        'f', '\f',
-                       'r', '\r'
+                       'r', '\r',
+                       's', ' '
        );
 
        public static String replaceStandardEscapes(String text) {

Reply via email to