Skip to content

Commit

Permalink
use Strings.toUnixLineSeparator
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoBettini committed Apr 29, 2024
1 parent 4716d46 commit e9228d2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
Expand Up @@ -2201,14 +2201,14 @@ public void testStringLiteralWithUnixEOL_Issue2293() throws Exception {
@Test
public void testStringLiteralWithWindowsEOL_Issue2293() throws Exception {
compilesTo(
"{var s = \"a multiline\r\nstring\"}",
"String s = \"a multiline\\nstring\";");
"{var s = \"a multiline\r\nstring\\r\\nstring\"}",
"String s = \"a multiline\\nstring\\nstring\";");
}

@Test
public void testStringLiteralWithEscapedWindowsEOL_Issue2293() throws Exception {
public void testStringLiteralWithCarriageReturn_Issue2293() throws Exception {
compilesTo(
"{var s = \"a multiline\\\\r\nstring\"}",
"String s = \"a multiline\\\\r\\nstring\";");
"{\"astring\".toString.replace(\"\\r\", \"\");}",
"return \"astring\".toString().replace(\"\\r\", \"\");");
}
}
Expand Up @@ -90,7 +90,7 @@ protected void toJavaExpression(XStringLiteral literal, ITreeAppendable appendab
// 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 normalizationOfLineTerminators = Strings.toUnixLineSeparator(literal.getValue());
String javaString = Strings.convertToJavaString(normalizationOfLineTerminators, useUnicodeEscapes);
appendable.append("\"").append(javaString).append("\"");
}
Expand Down
Expand Up @@ -52,6 +52,7 @@
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.util.CancelIndicator;
import org.eclipse.xtext.util.ReflectionUtil;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.xbase.XAbstractFeatureCall;
import org.eclipse.xtext.xbase.XAssignment;
import org.eclipse.xtext.xbase.XBasicForLoopExpression;
Expand Down Expand Up @@ -324,7 +325,7 @@ protected Object _doEvaluate(XStringLiteral literal, IEvaluationContext context,
return literal.getValue().charAt(0);
}
// see org.eclipse.xtext.xbase.compiler.LiteralsCompiler.toJavaExpression(XStringLiteral, ITreeAppendable, boolean)
return literal.getValue().replace("\r", "");
return Strings.toUnixLineSeparator(literal.getValue());
}

/**
Expand Down

0 comments on commit e9228d2

Please sign in to comment.