runtime(java): Recognise string templates (#14150)
As this is encouraged in the referenced JEPs, "to visually
distinguish a string template from a string literal, and
a text block template from a text block", the default
colours for java\%[Debug]StrTempl are made distinct from
java\%[Debug]String.
According to §3.2 Lexical Translations (JLS, c. 1996 or any
more recent version), line terminators, white space, and
comments are discarded before tokens are accepted. Since
a template expression comprises a template processor, a dot,
and a template, it may be visually appealing to break up
its head across a few lines whenever its tail already spans
multiple lines. Curiously, no allowance for it is made in
the distributed tests for OpenJDK 21; the proposed regexp
patterns take in consideration a line terminator and white
space after a dot.
References:
https://openjdk.org/jeps/430 (Preview)
https://openjdk.org/jeps/459 (Second Preview)
https://openjdk.org/jeps/465
Signed-off-by: Aliaksei Budavei <0x000c70@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
2024-03-09 20:03:11 +03:00
|
|
|
|
class StringTests // JDK 21+ (--enable-preview --release 21).
|
2024-03-03 17:51:01 +03:00
|
|
|
|
{
|
|
|
|
|
static {
|
|
|
|
|
String s1 = "A quick brown fox jumps over the lazy dog";
|
|
|
|
|
String s2 = "\"Woof\s!\"";
|
|
|
|
|
String s3 = """
|
|
|
|
|
A\s\
|
|
|
|
|
quick \
|
|
|
|
|
brown\s\
|
|
|
|
|
fox \
|
|
|
|
|
jumps\s\
|
|
|
|
|
over \
|
|
|
|
|
the\s\
|
|
|
|
|
lazy \
|
|
|
|
|
dog""";
|
|
|
|
|
String s4 = """
|
|
|
|
|
"Woof\s!\"""";
|
|
|
|
|
String s5 = """
|
|
|
|
|
String s3 = \"""
|
|
|
|
|
A\\s\\
|
|
|
|
|
quick \\
|
|
|
|
|
brown\\s\\
|
|
|
|
|
fox \\
|
|
|
|
|
jumps\\s\\
|
|
|
|
|
over \\
|
|
|
|
|
the\\s\\
|
|
|
|
|
lazy \\
|
|
|
|
|
dog\""";""";
|
|
|
|
|
|
|
|
|
|
// There are SPACE, FF, HT, CR, and LF after """.
|
|
|
|
|
String empty = """
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
System.out.println("""
|
|
|
|
|
"
|
|
|
|
|
""
|
|
|
|
|
""\u005c"
|
|
|
|
|
""\u005c""
|
|
|
|
|
""\"\u0022\u0022
|
|
|
|
|
""\"""\u005c\u0022
|
|
|
|
|
""\"""\""
|
|
|
|
|
""\"""\""\"""");
|
runtime(java): Recognise string templates (#14150)
As this is encouraged in the referenced JEPs, "to visually
distinguish a string template from a string literal, and
a text block template from a text block", the default
colours for java\%[Debug]StrTempl are made distinct from
java\%[Debug]String.
According to §3.2 Lexical Translations (JLS, c. 1996 or any
more recent version), line terminators, white space, and
comments are discarded before tokens are accepted. Since
a template expression comprises a template processor, a dot,
and a template, it may be visually appealing to break up
its head across a few lines whenever its tail already spans
multiple lines. Curiously, no allowance for it is made in
the distributed tests for OpenJDK 21; the proposed regexp
patterns take in consideration a line terminator and white
space after a dot.
References:
https://openjdk.org/jeps/430 (Preview)
https://openjdk.org/jeps/459 (Second Preview)
https://openjdk.org/jeps/465
Signed-off-by: Aliaksei Budavei <0x000c70@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
2024-03-09 20:03:11 +03:00
|
|
|
|
|
|
|
|
|
System.out.println(STR."""
|
|
|
|
|
"
|
|
|
|
|
\{"\"\""}
|
|
|
|
|
\{"\"\""}\{"\u005c\u0022"}
|
|
|
|
|
\{"\"\""}\{"\u005c\u0022"}"
|
|
|
|
|
\{"\"\""}\"\{"\u005c\u0022\u005c\u0022"}
|
|
|
|
|
\{"\"\""}\"\{"\"\""}\{"\u005c\u0022"}
|
|
|
|
|
\{"\"\""}\"\{"\"\""}\""
|
|
|
|
|
\{"\"\""}\"\{"\"\""}\""\""""); // JDK 21+.
|
|
|
|
|
|
|
|
|
|
String woof = "Woof", dog = "dog", fox = "fox";
|
|
|
|
|
|
|
|
|
|
String s6 = STR
|
|
|
|
|
."A quick brown \{fox} jumps over the lazy \{dog}";
|
|
|
|
|
String s7 = STR.process(StringTemplate.RAW
|
|
|
|
|
."\"\{woof}\s!\"");
|
|
|
|
|
String s8 = STR."""
|
|
|
|
|
A\s\
|
|
|
|
|
quick \
|
|
|
|
|
brown\s\
|
|
|
|
|
\{fox} \
|
|
|
|
|
jumps\s\
|
|
|
|
|
over \
|
|
|
|
|
the\s\
|
|
|
|
|
lazy \
|
|
|
|
|
\{dog}""";
|
|
|
|
|
String s9 = STR.process(StringTemplate.RAW
|
|
|
|
|
.
|
|
|
|
|
"""
|
|
|
|
|
"\{woof}\s!\"""");
|
|
|
|
|
String s10 = java.util.FormatProcessor.FMT
|
|
|
|
|
. "%-14s\{"A\s" + STR . "quick" + "brown"}%s\{fox} "
|
|
|
|
|
+ java.util.FormatProcessor.FMT
|
|
|
|
|
. "%-20s\{"jumps\sover the\s"
|
|
|
|
|
+ STR . "lazy"}%s\{dog}";
|
|
|
|
|
String s11 = STR."""
|
|
|
|
|
\"\{ // A nested comment.
|
|
|
|
|
(new java.util.function.Function<String, String>() {
|
|
|
|
|
public String apply(String bay) { return bay; };
|
|
|
|
|
}).apply(woof)
|
|
|
|
|
}\s!\"""";
|
|
|
|
|
String s12 = java.util.FormatProcessor.FMT
|
|
|
|
|
."""
|
|
|
|
|
%-14s\{STR."""
|
|
|
|
|
A\s\
|
|
|
|
|
\{ "quick" } \
|
|
|
|
|
brown"""}\
|
|
|
|
|
%s\{ fox } \
|
|
|
|
|
%-20s\{STR."""
|
|
|
|
|
jumps\s\
|
|
|
|
|
over \
|
|
|
|
|
the\s\
|
|
|
|
|
\{ "lazy" } """}\
|
|
|
|
|
%s\{ dog }""";
|
|
|
|
|
String s13 = STR
|
|
|
|
|
."\"\{ /* A nested comment. */
|
|
|
|
|
((java.util.function.Function<String, String>) bay -> bay)
|
|
|
|
|
.apply(woof)
|
|
|
|
|
}\s!\"";
|
2024-03-03 17:51:01 +03:00
|
|
|
|
}
|
|
|
|
|
}
|