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

Two single or double quote escapes single or double quote when string is surrounded by same type of quote #696

Merged
merged 37 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3764343
Added integration test for escape character in select statement.
MitchellGale Jun 8, 2022
c952bd4
AOS-637: PoC remove escape characters
Jun 15, 2022
8d8c299
Fixed number of escape characters returned by plugin. Number returned…
MitchellGale Jun 27, 2022
6fa49f4
Addedd test for 'im' in literals.txt.
MitchellGale Jun 28, 2022
d9871ac
Added unit test for unquoteText in StringUtils.java.
MitchellGale Jun 28, 2022
54c3629
Removed old unescapeBackslashes from StringUtils which was replaced b…
MitchellGale Jun 28, 2022
c8aac1a
Removed * from import of Assertions in StringUtilsTest.java.
MitchellGale Jun 28, 2022
44e869b
Fixed indentation. Moved to 2 space indents to replace 4 space indents.
MitchellGale Jun 28, 2022
56c737a
Put static imports before regular imports. Maintained alphabetical or…
MitchellGale Jun 28, 2022
2454d90
Added case for double single quote which converts to a single single …
MitchellGale Jun 28, 2022
1cc549b
Added new line at end of StringUtilsTest.java and added space between…
MitchellGale Jun 28, 2022
621c689
Replaced statement lambda with expression lambda.
MitchellGale Jun 28, 2022
70dada3
Allows for quotes(") and backticks(`) to be around strings in result.
MitchellGale Jun 28, 2022
bf4edec
Added additional test cases to unquoteTest() in StringUtilsTest.java.
MitchellGale Jun 28, 2022
6c8eb88
Added additional test cases to unquoteTest() in StringUtilsTest.java.…
MitchellGale Jun 28, 2022
1f2ab45
Made double single and double double quotes only reduce down to a sin…
MitchellGale Jun 29, 2022
5d0ca9a
Fixed whitespace found before comma in StringUtilsTest.java.
MitchellGale Jun 29, 2022
d0e0468
Added more tests. Removed formatting changes in OpenSearchExecutionEn…
MitchellGale Jun 29, 2022
907f56f
Reverting ProjectOperator.java.
MitchellGale Jun 29, 2022
a144535
Resolved issue with overlapping replacement of quotes (double and sin…
MitchellGale Jun 30, 2022
eae2092
Moved `replace` periods to new line.
MitchellGale Jun 30, 2022
8e213d6
Changed StringUtils.java to use String Builder. Updated some tests.
MitchellGale Jul 4, 2022
c4e9b88
Changed over to fully use string builder and works with escape as wel…
MitchellGale Jul 7, 2022
aa58736
Fixed formatting of StringUtilsTest.java, removed code in comments in…
MitchellGale Jul 7, 2022
d67dd3d
Double quote implemented to convert to single quote when string is su…
MitchellGale Jul 7, 2022
2de1495
Cleaned up redundant code from StringUtils.java.
MitchellGale Jul 7, 2022
4bb7a8d
Removed IT test added.
MitchellGale Jul 12, 2022
2899ee7
Moved whichquote inside unquote text. Removed large portion of code f…
MitchellGale Jul 12, 2022
401d817
Added some additional unit tests. Added branch for case of quoted by …
MitchellGale Jul 13, 2022
16927e8
Fixed test case expected.
MitchellGale Jul 13, 2022
d887809
Reverting changes to OpenSearchExecutionEngine.java.
MitchellGale Jul 14, 2022
e627e5e
Reverting changes to ProjectOperator.java.java.
MitchellGale Jul 14, 2022
1fadb92
Added test cases in literals.txt.
MitchellGale Jul 14, 2022
1a7c927
Added back tick and moved String builder declaration down.
MitchellGale Jul 15, 2022
c799c6a
Added better description regarding unquoteText function in regards to…
MitchellGale Jul 22, 2022
e7dd5a1
Merge branch 'main' of https://github.com/opensearch-project/sql into…
MitchellGale Jul 22, 2022
ba86654
Fixed line length of comments to not exceed 100 characters
MitchellGale Jul 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,54 @@ public static String unquote(String text, String mark) {

/**
* Unquote Identifier which has " or ' or ` as mark.
* Strings quoted by ' or " with two of these quotes appearing next to each other in the quote
* acts as an escape
* Example: 'Test''s' will result in 'Test's', similar with those single quotes being replaced
* with double.
* @param text string
* @return An unquoted string whose outer pair of (single/double/back-tick) quotes have been
* removed
*/
public static String unquoteText(String text) {
if (isQuoted(text, "\"") || isQuoted(text, "'") || isQuoted(text, "`")) {
return text.substring(1, text.length() - 1);

if (text.length() < 2) {
return text;
}

char enclosingQuote;
char firstChar = text.charAt(0);
char lastChar = text.charAt(text.length() - 1);

if (firstChar == lastChar
&& (firstChar == '\''
|| firstChar == '"'
|| firstChar == '`')) {
enclosingQuote = firstChar;
} else {
return text;
}

if (enclosingQuote == '`') {
return text.substring(1, text.length() - 1);
}

char currentChar;
char nextChar;

StringBuilder textSB = new StringBuilder();

// Ignores first and last character as they are the quotes that should be removed
for (int chIndex = 1; chIndex < text.length() - 1; chIndex++) {
currentChar = text.charAt(chIndex);
nextChar = text.charAt(chIndex + 1);
if (currentChar == enclosingQuote
&& nextChar == currentChar) {
chIndex++;
}
textSB.append(currentChar);
}

return textSB.toString();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.opensearch.sql.common.utils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.opensearch.sql.common.utils.StringUtils.unquoteText;

import org.junit.jupiter.api.Test;

class StringUtilsTest {
@Test
void unquoteTest() {
assertEquals("test", unquoteText("test"));
assertEquals("test", unquoteText("'test'"));
assertEquals("test", unquoteText("`test`"));

assertEquals("test'", unquoteText("'test'''"));
assertEquals("test\"", unquoteText("\"test\"\"\""));

assertEquals("te``st", unquoteText("'te``st'"));
assertEquals("te``st", unquoteText("\"te``st\""));
assertEquals("te``st", unquoteText("`te``st`"));

assertEquals("te'st", unquoteText("'te''st'"));
assertEquals("te''st", unquoteText("\"te''st\""));
assertEquals("te''st", unquoteText("`te''st`"));

assertEquals("te\"\"st", unquoteText("'te\"\"st'"));
assertEquals("te\"st", unquoteText("\"te\"\"st\""));
assertEquals("te\"\"st", unquoteText("`te\"\"st`"));

assertEquals("''", unquoteText("''''''"));
assertEquals("\"\"", unquoteText("\"\"\"\"\"\""));
assertEquals("````", unquoteText("``````"));

assertEquals("test'", unquoteText("'test''"));

assertEquals("", unquoteText(""));
assertEquals("'", unquoteText("'"));
assertEquals("`", unquoteText("`"));
assertEquals("\"", unquoteText("\""));

assertEquals("hello'", unquoteText("'hello''"));
assertEquals("don't", unquoteText("'don't'"));
assertEquals("hello`", unquoteText("`hello``"));
assertEquals("don\"t", unquoteText("\"don\"t\""));

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ true
2147483647
-2147483648
2147483648
-2147483649
-2147483649
'im'
'i''m'
'i""m'