Skip to content

Commit

Permalink
Normalization of Line Terminators in Xbase strings
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Apr 29, 2024
1 parent 385e329 commit 52b1616
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
Expand Up @@ -909,7 +909,7 @@ public abstract class AbstractXbaseEvaluationTest extends Assert {
}

@Test public void testStringLiteral_03() throws Exception {
assertEvaluatesTo("lite\r\nr\\al", "'lite\r\nr\\\\al'");
assertEvaluatesTo("lite\nr\\al", "'lite\r\nr\\\\al'");
}

@Test public void testStringLiteral_04() throws Exception {
Expand Down
Expand Up @@ -912,7 +912,7 @@ public abstract class AbstractXbaseEvaluationTest extends Assert {
}

@Test public void testStringLiteral_03() throws Exception {
assertEvaluatesTo("lite\r\nr\\al", "'lite\r\nr\\\\al'");
assertEvaluatesTo("lite\nr\\al", "'lite\r\nr\\\\al'");
}

@Test public void testStringLiteral_04() throws Exception {
Expand Down
Expand Up @@ -2190,4 +2190,25 @@ public void testLibIssue60() throws Exception {
"}\n" +
"return _xblockexpression;\n");
}

@Test
public void testStringLiteralWithUnixEOL_Issue2293() throws Exception {
compilesTo(
"{var s = \"a multiline\nstring\"}",
"String s = \"a multiline\\nstring\";");
}

@Test
public void testStringLiteralWithWindowsEOL_Issue2293() throws Exception {
compilesTo(
"{var s = \"a multiline\r\nstring\"}",
"String s = \"a multiline\\nstring\";");
}

@Test
public void testStringLiteralWithEscapedWindowsEOL_Issue2293() throws Exception {
compilesTo(
"{var s = \"a multiline\\\\r\nstring\"}",
"String s = \"a multiline\\\\r\\nstring\";");
}
}
Expand Up @@ -86,7 +86,12 @@ protected void toJavaExpression(XStringLiteral literal, ITreeAppendable appendab
String javaString = Strings.convertToJavaString(literal.getValue(), useUnicodeEscapes);
appendable.append("Character.valueOf('").append(javaString).append("')");
} else {
String javaString = Strings.convertToJavaString(literal.getValue(), useUnicodeEscapes);
// Avoid Windows EOL characters from the original parsed text:
// this would result in different generated Java files in Windows
// see https://github.com/eclipse/xtext/issues/2293
// This is aligned with Java text blocks' "Normalization of Line Terminators"
String normalizationOfLineTerminators = literal.getValue().replace("\r", "");
String javaString = Strings.convertToJavaString(normalizationOfLineTerminators, useUnicodeEscapes);
appendable.append("\"").append(javaString).append("\"");
}
}
Expand Down

0 comments on commit 52b1616

Please sign in to comment.