Skip to content

Commit

Permalink
Merge pull request #87 from Bit-Quill/dev_doubleQuotes
Browse files Browse the repository at this point in the history
Two single or double quote escapes single or double quote when string is surrounded by same type of quote
  • Loading branch information
MitchellGale authored Jul 15, 2022
2 parents 58cfa88 + 1636f13 commit f1e018c
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,51 @@ public static String unquote(String text, String mark) {

/**
* Unquote Identifier which has " or ' or ` as mark.
* Replace doubled quotes within the string with single version
* @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'

0 comments on commit f1e018c

Please sign in to comment.