Repository navigation
Handles Escape Characters in String Literals - #81
MitchellGale wants to merge 23 commits into
Conversation
Signed-off-by: Andrew Carbonetto <[email protected]>
… is now expected number. 'I\'m' -> I'm 'I\\''m' -> I\'m 'I\\\'m' -> I\'m Signed-off-by: mitchellg <[email protected]>
Signed-off-by: mitchellg <[email protected]>
Signed-off-by: mitchellg <[email protected]>
…y unquoteText. Signed-off-by: mitchellg <[email protected]>
Signed-off-by: mitchellg <[email protected]>
Signed-off-by: mitchellg <[email protected]>
…der in StringUtilsTest.java Signed-off-by: mitchellg <[email protected]>
…quote. Signed-off-by: mitchellg <[email protected]>
… static and non-static imports Signed-off-by: mitchellg <[email protected]>
MaxKsyunz
left a comment
There was a problem hiding this comment.
Please make sure all the PR checks pass [*].
[*] Integration tests against ODFE 1.13 are an exception. They are very flaky and are more likely to fail than not.
| if (isQuoted(text, "`")) { | ||
| return text.substring(1, text.length() - 1).replace("\\`", "`").replace("\\\\", "\\"); | ||
| } | ||
| return text; |
There was a problem hiding this comment.
Is it possible to reach this line?
Maybe we need to throw an error instead (RuntimeError?)
There was a problem hiding this comment.
It'll reach it in the case where unquoteText is called when text doesn't need to be unquoted (or using quotes not supported, in which case it won't "unquote it"). I think it's a needed case.
There was a problem hiding this comment.
So we're calling unquote string on a string that doesn't need unquoting...? That sounds wrong ;)
There was a problem hiding this comment.
It is being called on every thing. String that needed to be unquoted would be unquoted, the rest should left unchanged.
| @Test | ||
| void unquoteTest() { | ||
| assertEquals("test\\'s", unquoteText("'test\\\\'s'")); | ||
| assertEquals("test\\'s", unquoteText("'test\\\\\\'s'")); |
There was a problem hiding this comment.
Unsure... so what is happening is java is reducing the four \ into two, and the six \ into three.
Once it's inside the function, it's a string that has 'test\\'s', which it converts into test's, no obligation for there to be an odd number of escape characters because it doesn't need to use them at least..
Once it's inside the function, it's a string that has 'test\\\'s', which it converts into test\'s.
I think it's behaving appropriately.
There was a problem hiding this comment.
It seems like ' by itself won't throw an error. Maybe this is also because the parser will throw a syntax error before ever reaching this staging in the code... so practically speaking, this is fine.
But... then this should be properly described in the method documentation. Otherwise, this function may be misused.
Signed-off-by: mitchellg <[email protected]>
Signed-off-by: mitchellg <[email protected]>
Signed-off-by: mitchellg <[email protected]>
… Added case for double, double quote being converted to single, double quote. Signed-off-by: mitchellg <[email protected]>
…gle of their respective type when the string is quoted by the same type. Added some unit tests to test for that condition in StringUtilsTest.java. Signed-off-by: mitchellg <[email protected]>
Signed-off-by: mitchellg <[email protected]>
|
|
||
| while (plan.hasNext()) { | ||
| result.add(plan.next()); | ||
| ExprValue resultItem = plan.next(); |
There was a problem hiding this comment.
Why is this block of code necessary? (lines 44-58)
| if (isQuoted(text, "\"")) { | ||
| text = text.replace("\"\"", "\""); | ||
| text = text.substring(1, text.length() - 1).replace("\\\"", "\"").replace("\\\\", "\\"); | ||
| return text; | ||
| } | ||
| if (isQuoted(text, "'")) { | ||
| text = text.replace("''", "'"); | ||
| text = text.substring(1, text.length() - 1).replace("\\'", "'").replace("\\\\", "\\"); | ||
| return text; |
There was a problem hiding this comment.
Strings like these: ""the cake" is lie", ''the cake' is lie' would be converted into the cake" is lie/the cake' is lie. I think expected output there is "the cake" is lie/'the cake' is lie.
We shouldn't collapse two '/" chars in the beginning and in the end into '/" char.
@MaxKsyunz, what do you think?
|
|
||
| // Tests case of unquoted string sent to unquoteText function | ||
| assertEquals("test", unquoteText("test")); | ||
| } |
There was a problem hiding this comment.
More tests for god of tests!
Tests with backtick `, tests with combined ' and ", mixes of '/"/\ and '/"/\ in the beginning and in the end of line wrapped by '/"/`/none and so on.
| return explain(node, context, explainNode -> | ||
| explainNode.setDescription(ImmutableMap.of("request", node.explain())) | ||
| ); |
There was a problem hiding this comment.
Regarding the switch from
return explain(node, context, explainNode -> { explainNode.setDescription(ImmutableMap.of("request", node.explain())); });
to return explain(node, context, explainNode -> explainNode.setDescription(ImmutableMap.of("request", node.explain())) );
It was giving me a warning about "Warning:(60, 58) Statement lambda can be replaced with expression lambda".
There was a problem hiding this comment.
That is an IDEA warning/suggestion. We can ignore them unless they in code we're working on.
…gine.java. Signed-off-by: mitchellg <[email protected]>
Codecov Report
@@ Coverage Diff @@
## integ-escapecharacter #81 +/- ##
===========================================================
- Coverage 97.66% 97.66% -0.01%
Complexity 2743 2743
===========================================================
Files 268 268
Lines 6772 6771 -1
Branches 435 435
===========================================================
- Hits 6614 6613 -1
Misses 157 157
Partials 1 1
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
Signed-off-by: mitchellg <[email protected]>
| if (isQuoted(text, "\"") || isQuoted(text, "'") || isQuoted(text, "`")) { | ||
| return text.substring(1, text.length() - 1); | ||
| } else { | ||
|
|
There was a problem hiding this comment.
A few things can be improved here (lines 36-50).
Currently, the input string is scanned multiple times (3) and temporary strings (3) are allocated in each scenario.
A more efficient implementation is possible by scanning the input string once and using StringBuilder to allocate only one string. In pseudocode it is something like:
IF input_string IsQuoteBy $QUOTE
FOREACH char in input_string[1, LENGTH - 1):
IF char is '\' ADD next char to StringBuilder
ELSE ADD char to StringBuilder
I don't think we even need to check which quote character the string is surrounded by.
See this StackOverflow post and this method for reference.
This would also avoid the repeated code pattern -- if quoted by $QUOTE then ... .
There was a problem hiding this comment.
@MaxKsyunz Can you please review my updated code for StringUtils.java? It does not use StringBuilder, but it resolves other issues. If you still recommend I use StringBuilder, I will figure it out and use it (thanks for the documentation).
There was a problem hiding this comment.
If you want to reduce the number of temporary strings, we should consider using match replaceAll with a pattern. https://www.geeksforgeeks.org/matcher-replaceallstring-method-in-java-with-examples/
If we only want a single isQuoted call (which returns the quoted character) we could reduce this from 3 IF statements, to 2. But the backticks is a unique case that doesn't reduce double-backticks to a single backtick.
…gle). Signed-off-by: mitchellg <[email protected]>
Signed-off-by: mitchellg <[email protected]>
|
|
||
| if (isQuoted(text, "\"")) { | ||
| return text.substring(1, text.length() - 1) | ||
| .replace("\\\"", "\"") |
There was a problem hiding this comment.
Please consider using something like replaceAll instead of trying to cover each of these cases individually. We shouldn't need to consider both \\ and \\\\ and \\\\\\ and ...etc
Signed-off-by: mitchellg <[email protected]>
…l as double quotes. Signed-off-by: mitchellg <[email protected]>
… StringUtils.java. Signed-off-by: mitchellg <[email protected]>
|
Closed until consideration for 3.0 release. |
Issue: #76 Signed-off-by: Navneet Verma <[email protected]>
Description
Correctly handles escape characters in string literals. No longer returns all escape characters passed in.
Issues Resolved
Resolves Issue 297(Unable to escape characters for string literals[BUG]).
Check List
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.