String literals in Java 7: version 1.2

rssh at gradsoft.com.ua rssh at gradsoft.com.ua
Wed Mar 4 23:52:17 PST 2009


Summary of changes:
 - methods for setting LF type in strings instead suffixes.
 - add line-termination escape sequence. (as in C and groovy)

Proposal itself:

AUTHOR(s): Ruslan Shevchenko, Jeremy Manson (if agree), Reinier
Zwitserloot (if agree)

OVERVIEW:

FEATURE SUMMARY:
new string literals in java language:
* multiline string literals.
* string literals without escape processing.

MAJOR ADVANTAGE:
Possibility more elegant to code strings from  other languages, such as
sql constructions or inline xml (for multiline strings) or regular
expressions
(for string literals without escape processing).

MAJOR DISADVANTAGE
I don't know

ALTERNATIVES:

For multiline strings use operations and concatenation methods, such as:

  String,contact("Multiline \n",
                 "string ");
 or
  String bigString="First line\n"+
                   "second line"

For unescaped ('row') stings - use escaping of ordinary java string.


EXAMPLES

SIMPLE EXAMPLE:

Multiline string:

  StringBuilder sb = new StringBuilder();
  sb.append("""select a from Area a, CountryCodes cc
                where
                   cc.isoCode='UA'
                  and
                   a.owner = cc.country
              """);
  if (question.getAreaName()!=null) {
     sb.append("""and
                  a.name like ?
               """);
     sqlParams.setString(++i,question.getAreaName());
  }

 instead:
  StringBuilder sb = new StringBuilder();
  sb.append("select a from Area a, CountryCodes cc\n");
  sb.append("where cc.isoCode='UA'\n");
  sb.append("and a.owner=cc.country'\n");
  if (question.getAreaName()!=null) {
     sb.append("and a.name like ?");
     sqlParams.setString(++i,question.getAreaName());
  }

 String platformDepended="""q
 """.nativeLf();
 is 'q\n' if compiled on Unix and 'q\n\r' if compiled on Windows.

 String platformIndepended="""q
 """;
 is always "q\n".

 String platformIndepended="""q
 """U.unixLf();
 is the same.

 String platformIndepended="""
 """.windowsLf();
 is always '\r\n'.

Unescaped String:

 String myParrern=''..*\.*'';
 instead
 String myParrern="\..*\\.*";

 String fname=''C:\Program Files\My Program\Configuration'';
 instead
 String myParrern="C:\\Program Files\\My Program\\Configuration";


ADVANCED EXAMPLE:

 String empty="""
 """;
 is empty.

 String foo = """
     bar
                 baz
       bla
     qux";

is equal to: String foo = "bar\n            baz\n  bla\nqux";

 String foo = """
    foo
 bar""";
 is a compile-time error.

 String manyQuotes="""\"""\"\"\"""";
 is """""

  String s = """I'm  long string in groovy stile wi\
  th \\ at end of line""";
 is:
  I'm  long string in groovy stile with \ at end of line

  String s = ''I'm  long string in groovy stile wi\
  th \\ at end of line'';
 is:
 I'm  long string in groovy stile wi\
 th \\ at end of line

DETAILS:

Multiline strings are part of program text, which begin and ends by three
double quotes.

I. e. grammar in 3.10.5 of JLS can be extented as:

<pre>
MultilineStringLiteral:
        """ MultilineStringCharacters/opt """

MultilineStringCharacters:
        MultilineStringCharacter
        MultilineStringCharacters  (MultilineStringCharacter but not ")
        (MultilineStringCharacters but not "") "

MultilineStringCharacter:
        InputCharacter but not \
        EscapeSequence
        LineTermination
        EolEscapeSequence

EolEscapeSequence: \ LineTermination.

</pre>


Unescaped strings are part of program text, which begin and ends by two
single quotes.


<pre>
 RowStringLiteral:
                   '' RowInputCharacters/opt ''

 RowInputCharacters:
                      ' (InputCharacter but not ')
                     |
                      (InputCharacter but not ') '
                     |
                      LineTermination
</pre>

 Methods for replacing line termination sequences in string to native
format of host platform, and to well-known unix/windows formats
must be added to standard library.


COMPILATION:

Handling of multiline strings:

Text within """ brackets processed in next way:

1. splitted to sequence of lines by line termination symbols.
2. sequence \LineTermination at the end of line is erased and such line
cause line be concatenated with next line in one.
  (if nextline exists, otherwise - throw compile-trime error)
3. escape sequences in each line are processed exactly as in ordinary Java
strings.
4. elimination of leading whitespaces are processed in next way:
  - at first determinate sequence of whitespace symbols (exclude
LineTermination, i.e. ST, HP, FF) at first nonempty line in sequence.
    let's call it 'leading whitespace sequence'
  - all next lines must start with same leading whitespace sequence,
otherwise compile-time error is thrown.
  - whitespace processing erase such leading sequence from resulting lines
5. set of lines after erasing of leading whitespace sequence is
concatenated, with LF (i. e. '\n') line-termination sequences between two
neighbour lines,
   regardless of host system


Handling of row strings:
Text within '' brackets processed in next way:
1. splitted to sequence of lines by line termination symbols.
2. set of lines after erasing of leading whitespace sequence is
concatenated, with '\n' line-termination sequences between two neighbour
lines,

No escape processing, no leading whitespace elimination are performed for
receiving of resulting string value.

new strings literals created and used in .class files exactly as ordinary
strings.


TESTING:
 add new  strings literals to test-cases for all combinations of finished
and unfinished escape sequences and quotes.


LIBRARY SUPPORT:
 Add to String next methods:

 s.platformLf() - returns string which replace all line-termination
sequences in s by value of system property 'line.separator'
 s.unixLf() - returns string which replace all line-termination sequences
in s by '\n'
 s.windowsLf() - returns string which replace all line-termination
sequences in s by '\r\n'

REFLECTIVE APIS: None

OTHER CHANGES: None

MIGRATION: None

COMPABILITY
 None

REFERENCES

Sun bug database
 http://bugs.sun.com/view_bug.do?bug_id=4165111
 http://bugs.sun.com/view_bug.do?bug_id=4472509

Multiline strings proposal in project Kijaro by  by Jacek Furmankiewicz
 http://docs.google.com/View?docid=d36kv8n_32g9zj7pdd








More information about the coin-dev mailing list