Skip to content

Commit

Permalink
Reverted string utils
Browse files Browse the repository at this point in the history
Signed-off-by: Mitchell Gale <Mitchell.Gale@improving.com>
  • Loading branch information
MitchellGale committed Aug 22, 2023
1 parent 5afb99f commit 6a14bbd
Showing 1 changed file with 42 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/


package org.opensearch.sql.common.utils;

import com.google.common.base.Strings;
Expand All @@ -11,38 +12,52 @@

public class StringUtils {
/**
* Unquote Identifier which has " or ' as mark. Strings quoted by ' or " with two of these quotes
* appearing next to each other in the quote acts as an escape<br>
* Unquote any string with mark specified.
* @param text string
* @param mark quotation mark
* @return An unquoted string whose outer pair of (single/double/back-tick) quotes have been
* removed
*/
public static String unquote(String text, String mark) {
if (isQuoted(text, mark)) {
return text.substring(mark.length(), text.length() - mark.length());
}
return text;
}

/**
* 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 quote. Supports escaping quotes (single/double) and escape characters using the `\`
* characters.
*
* with double.
* @param text string
* @return An unquoted string whose outer pair of (single/double) quotes have been removed
* @return An unquoted string whose outer pair of (single/double/back-tick) quotes have been
* removed
*/
public static String unquoteText(String text) {

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

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

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

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

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

char currentChar;
char nextChar;

Expand All @@ -52,22 +67,21 @@ public static String unquoteText(String text) {
for (int chIndex = 1; chIndex < text.length() - 1; chIndex++) {
currentChar = text.charAt(chIndex);
nextChar = text.charAt(chIndex + 1);

if ((currentChar == '\\' && (nextChar == '"' || nextChar == '\\' || nextChar == '\''))
|| (currentChar == nextChar && currentChar == enclosingQuote)) {
if (currentChar == enclosingQuote
&& nextChar == currentChar) {
chIndex++;
currentChar = nextChar;
}
textSB.append(currentChar);
}

return textSB.toString();
}

/**
* Unquote Identifier which has ` as mark.
*
* @param identifier identifier that possibly enclosed by double quotes or back ticks
* @return An unquoted string whose outer pair of (double/back-tick) quotes have been removed
* @return An unquoted string whose outer pair of (double/back-tick) quotes have been
* removed
*/
public static String unquoteIdentifier(String identifier) {
if (isQuoted(identifier, "`")) {
Expand All @@ -78,15 +92,16 @@ public static String unquoteIdentifier(String identifier) {
}

/**
* Returns a formatted string using the specified format string and arguments, as well as the
* {@link Locale#ROOT} locale.
* Returns a formatted string using the specified format string and
* arguments, as well as the {@link Locale#ROOT} locale.
*
* @param format format string
* @param args arguments referenced by the format specifiers in the format string
* @param args arguments referenced by the format specifiers in the format string
* @return A formatted string
* @throws IllegalFormatException If a format string contains an illegal syntax, a format
* specifier that is incompatible with the given arguments, insufficient arguments given the
* format string, or other illegal conditions.
* specifier that is incompatible with the given arguments,
* insufficient arguments given the format string, or other
* illegal conditions.
* @see java.lang.String#format(Locale, String, Object...)
*/
public static String format(final String format, Object... args) {
Expand All @@ -96,4 +111,4 @@ public static String format(final String format, Object... args) {
private static boolean isQuoted(String text, String mark) {
return !Strings.isNullOrEmpty(text) && text.startsWith(mark) && text.endsWith(mark);
}
}
}

0 comments on commit 6a14bbd

Please sign in to comment.