Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-9442

GEP: Support for the new JDK14 string escape sequence (\s for single space)

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 3.0.3
    • GEP
    • None

    Description

      The goal is to add \s as an additional escape sequence for single and double quoted strings.

      assert 'ab\scd\s'.bytes == [97, 98, 32, 99, 100, 32]
      

      Relevant section from the Java issue tracker (JEP 368: Text Blocks (Second Preview)):

      This isn't a breaking change since:

      • illegal escape sequences are currently an error for single and triple (multi-line) single/double quoted strings
      • slashy and dollar slashy strings ignore backslash escape sequences (apart from special cases)

      The use case cited in Java is to allow explicit indication of spaces at the end of a line in a multi-line string. This is then explicit for human readers or editors that might auto-remove trailing spaces. It becomes more important in recent versions of Java with auto-removal of incidental whitespace in multi-line strings.

      Note: I don't want to pre-empt implementation details, but I imagine in Groovy's case (since we don't auto-remove incidental whitespace) we will do translation during parsing and won't have the same rule as per Java above about translating after incidental whitespace removal.

      So, we could do this now (shown for Groovy 3):

      String colors = '''\
          red   \n\
          green \n\
          blue  \n\
      '''.stripIndent(true)
      assert colors.readLines()*.size() == [6, 6, 6]
      

      After this change, you could also do:

      String colors = '''\
          red  \s
          green\s
          blue \s
      '''.stripIndent(true)
      assert colors.readLines()*.size() == [6, 6, 6]
      

      The only -ve I can see is regex users who might get regex \s mixed up with this new escape. But we already have that issue for \b (backspace in normal string vs word boundary in regex), so this is almost a non-issue.

      See also: Existing Groovy escape sequences

      Attachments

        1. screenshot-1.png
          43 kB
          Paul King

        Issue Links

          Activity

            People

              paulk Paul King
              paulk Paul King
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 40m
                  40m