Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalization of Line Terminators in Xbase strings #3004

Merged
merged 9 commits into from
Apr 29, 2024
Merged

Conversation

LorenzoBettini
Copy link
Contributor

Closes #2293

@LorenzoBettini
Copy link
Contributor Author

Looks like I haven't gotten the replacement correctly: it also leads to this unwanted difference:

image

I thought this test would check that efa5aa1#diff-7217ccbb0669983e16ca897570a543e7ee1bfb23a156c11fe9425cc5db1536baR2210
but maybe this only checks the input string "\r", while we need to check "\r", that is when the user enters the 2 characters "\r", while I'd like to remove the single character '\r'.

@LorenzoBettini
Copy link
Contributor Author

OK, this fadf9c7 reproduces the problem and fixes it

@LorenzoBettini
Copy link
Contributor Author

@szarnekow the build fails. I guess I haven't override all the needed methods in the custom value converter?

public static class WindowsEOLsAwareSTRINGValueConverter extends STRINGValueConverter {
@Override
public String toValue(String string, INode node) {
return Strings.toUnixLineSeparator(super.toValue(string, node));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an interesting case. I guess we want to treat an explicit \r in the string literal different from a regular windows newline. I've to think about it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cf

	public static void main(String[] args) {
		String s = """
				some\r\nstring""";
		System.out.println(s);
		System.out.println(s.length());
		System.out.println('\r' == s.charAt(4));
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@szarnekow a quick thought: I guess the original string should be normalized before passing it to super, not afterward as I'm doing now

@LorenzoBettini
Copy link
Contributor Author

And the current implementation is still not correct, since we have this result:

	@Test
	public void testStringLiteralWithCarriageReturn3_Issue2293() throws Exception {
		compilesTo(
				"{\"astring\".toString.replaceAll(\"\\r\\n\", \"\\n\");}",
				"return \"astring\".toString().replaceAll(\"\\n\", \"\\n\");");
	}

The "\r" from the original source code is removed, which is wrong!

@LorenzoBettini
Copy link
Contributor Author

Passing the already normalized string to super:

	public static class WindowsEOLsAwareSTRINGValueConverter extends STRINGValueConverter {
		@Override
		public String toValue(String string, INode node) {
			return super.toValue(Strings.toUnixLineSeparator(string), node);
		}
	}

leads to the expected test result:

	@Test
	public void testStringLiteralWithCarriageReturn3_Issue2293() throws Exception {
		compilesTo(
				"{\"astring\".toString.replaceAll(\"\\r\\n\", \"\\n\");}",
				"return \"astring\".toString().replaceAll(\"\\r\\n\", \"\\n\");");
	}

@LorenzoBettini
Copy link
Contributor Author

@szarnekow concerning the failing tests in Xtend, I think I found the culprit: the way I customized the Xbase string converter did not allow for customizations, and the Xtend customizations were not picked anymore. This should do: 9b8cc60

P.S., do you think it's worthwhile to rename the Xtend StringValueConverter to XtendStringValueConverter? That would allow for easier discovery.

@LorenzoBettini
Copy link
Contributor Author

@szarnekow I rebased the PR

This is still to be done if you think it's feasible:

P.S., do you think it's worthwhile to rename the Xtend StringValueConverter to XtendStringValueConverter? That would allow for easier discovery.

@szarnekow
Copy link
Contributor

feasible

Yes

XtendStringValueConverter

Yes

@LorenzoBettini
Copy link
Contributor Author

@szarnekow rebased and renamed the Xtend string value converter.
As for me, this PR would be ready.

@@ -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'");
Copy link
Contributor

@szarnekow szarnekow Apr 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add tests here that use the Java string literal \\r\\n and \\n respectively. These should remain untouched, since they don't rely on the line endings of the compilation unit but use backlash-r and backslash-n respectively.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@szarnekow this one d7c67a3 ?

Yes, we have a duplicated AbstractXbaseEvaluationTest ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I knew that I saw it somewhere ... damn it :)

Thanks!

Copy link
Contributor

@szarnekow szarnekow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a reminder for the N&N

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong line delimiter when enclosing multi-line strings with double quotes.
3 participants