Elffuss BraidHub

kikocis/java-driver public

JAVA-1262: Use ParseUtils for quoting & unquoting (#728)

Metadata.handleId and quote use String.replace or replaceAll, which each
create a lot of garbage as they compile a Pattern each time.  Instead
use ParseUtils.quote and unquote for quoting and unquoting.

Adds doubleQuote, unDoubleQuote, and isDoubleQuoted as analogs to the
single quoted counterparts in ParseUtils.
Changeset06afebc26b362d2442cc4477633bee3e444b5749815205ff7ff7129ac6ce6450
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2016-08-31 13:36 -05:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-08-31 11:36 -07:00
Parents846b89ef8105

Browse the files at this changeset

4 files changed

modified changelog/README.md +1 −0
@@ -6,6 +6,7 @@
66 - [improvement] JAVA-1261: Throw error when attempting to page in I/O thread.
77 - [bug] JAVA-1258: Regression: Mapper cannot map a materialized view after JAVA-1126.
88 - [bug] JAVA-1101: Batch and BatchStatement should consider inner statements to determine query idempotence
9+- [improvement] JAVA-1262: Use ParseUtils for quoting & unquoting.
910
1011
1112 ### 3.0.3
modified driver-core/src/main/java/com/datastax/driver/core/Metadata.java +2 −9
@@ -135,14 +135,7 @@
135135 return id.toLowerCase();
136136
137137 // Check if it's enclosed in quotes. If it is, remove them and unescape internal double quotes
138- if (!id.isEmpty() && id.charAt(0) == '"' && id.charAt(id.length() - 1) == '"')
139- return id.substring(1, id.length() - 1).replaceAll("\"\"", "\"");
140-
141- // Otherwise, just return the id.
142- // Note that this is a bit at odds with the rules explained above, because the client can pass an
143- // identifier that contains special characters, without the need to quote it.
144- // Still it's better to be lenient here rather than throwing an exception.
145- return id;
138+ return ParseUtils.unDoubleQuote(id);
146139 }
147140
148141 // Escape a CQL3 identifier based on its value as read from the schema
@@ -217,7 +210,7 @@
217210 * or even {@link Cluster#connect(String)}.
218211 */
219212 public static String quote(String id) {
220- return '"' + id.replace("\"", "\"\"") + '"';
213+ return ParseUtils.doubleQuote(id);
221214 }
222215
223216 /**
modified driver-core/src/main/java/com/datastax/driver/core/ParseUtils.java +141 −25
@@ -232,7 +232,7 @@
232232 * by single quotes, and {@code false} otherwise.
233233 */
234234 public static boolean isQuoted(String value) {
235- return value != null && value.length() > 1 && value.charAt(0) == '\'' && value.charAt(value.length() - 1) == '\'';
235+ return isQuoted(value, '\'');
236236 }
237237
238238 /**
@@ -243,7 +243,7 @@
243243 * @return The quoted string.
244244 */
245245 public static String quote(String value) {
246- return '\'' + replaceChar(value, '\'', "''") + '\'';
246+ return quote(value, '\'');
247247 }
248248
249249 /**
@@ -254,9 +254,41 @@
254254 * @return The unquoted string.
255255 */
256256 public static String unquote(String value) {
257- if (!isQuoted(value))
258- return value;
259- return value.substring(1, value.length() - 1).replace("''", "'");
257+ return unquote(value, '\'');
258+ }
259+
260+ /**
261+ * Return {@code true} if the given string is surrounded
262+ * by double quotes, and {@code false} otherwise.
263+ *
264+ * @param value The string to inspect.
265+ * @return {@code true} if the given string is surrounded
266+ * by double quotes, and {@code false} otherwise.
267+ */
268+ public static boolean isDoubleQuoted(String value) {
269+ return isQuoted(value, '\"');
270+ }
271+
272+ /**
273+ * Double quote the given string; double quotes are escaped.
274+ * If the given string is null, this method returns a quoted empty string ({@code ""}).
275+ *
276+ * @param value The value to double quote.
277+ * @return The double quoted string.
278+ */
279+ public static String doubleQuote(String value) {
280+ return quote(value, '"');
281+ }
282+
283+ /**
284+ * Unquote the given string if it is double quoted; double quotes are unescaped.
285+ * If the given string is not double quoted, it is returned without any modification.
286+ *
287+ * @param value The string to un-double quote.
288+ * @return The un-double quoted string.
289+ */
290+ public static String unDoubleQuote(String value) {
291+ return unquote(value, '"');
260292 }
261293
262294 /**
@@ -428,45 +460,129 @@
428460 }
429461
430462 /**
431- * Simple method to replace a single character.
432- * {@code String.replace()} is a bit too inefficient (see JAVA-67).
433- * This methods is specially useful for escaping single quotes
434- * in CQL strings:
435- * <p/>
436- * <pre>{@code
437- * ParseUtils.replaceChar(value, '\'', "''");
438- * }</pre>
463+ * Return {@code true} if the given string is surrounded
464+ * by the quote character given, and {@code false} otherwise.
465+ *
466+ * @param value The string to inspect.
467+ * @return {@code true} if the given string is surrounded
468+ * by the quote character, and {@code false} otherwise.
469+ */
470+ private static boolean isQuoted(String value, char quoteChar) {
471+ return value != null && value.length() > 1
472+ && value.charAt(0) == quoteChar && value.charAt(value.length() - 1) == quoteChar;
473+ }
474+
475+ /**
476+ * @param quoteChar " or '
477+ * @return A quoted empty string.
478+ */
479+ private static String emptyQuoted(char quoteChar) {
480+ // don't handle non quote characters, this is done so that these are interned and don't create
481+ // repeated empty quoted strings.
482+ assert quoteChar == '"' || quoteChar == '\'';
483+ if (quoteChar == '"')
484+ return "\"\"";
485+ else
486+ return "''";
487+ }
488+
489+
490+ /**
491+ * Quotes text and escapes any existing quotes in the text.
492+ * {@code String.replace()} is a bit too inefficient (see JAVA-67, JAVA-1262).
439493 *
440- * @param text The text.
441- * @param search The character to search for.
442- * @param replacement The replacement.
443- * @return The text with all occurrences of {@code search} replaced with {@code replacement}.
494+ * @param text The text.
495+ * @param quoteChar The character to use as a quote.
496+ * @return The text with surrounded in quotes with all existing quotes escaped with (i.e. ' becomes '')
444497 */
445- private static String replaceChar(String text, char search, String replacement) {
498+ private static String quote(String text, char quoteChar) {
446499 if (text == null || text.isEmpty())
447- return "";
500+ return emptyQuoted(quoteChar);
448501
449502 int nbMatch = 0;
450503 int start = -1;
451504 do {
452- start = text.indexOf(search, start + 1);
505+ start = text.indexOf(quoteChar, start + 1);
453506 if (start != -1)
454507 ++nbMatch;
455508 } while (start != -1);
456509
510+ // no quotes found that need to be escaped, simply surround in quotes and return.
457511 if (nbMatch == 0)
512+ return quoteChar + text + quoteChar;
513+
514+ // 2 for beginning and end quotes.
515+ // length for original text
516+ // nbMatch for escape characters to add to quotes to be escaped.
517+ int newLength = 2 + text.length() + nbMatch;
518+ char[] result = new char[newLength];
519+ result[0] = quoteChar;
520+ result[newLength - 1] = quoteChar;
521+ int newIdx = 1;
522+ for (int i = 0; i < text.length(); i++) {
523+ char c = text.charAt(i);
524+ if (c == quoteChar) {
525+ // escape quote with another occurrence.
526+ result[newIdx++] = c;
527+ result[newIdx++] = c;
528+ } else {
529+ result[newIdx++] = c;
530+ }
531+ }
532+ return new String(result);
533+ }
534+
535+ /**
536+ * Unquotes text and unescapes non surrounding quotes.
537+ * {@code String.replace()} is a bit too inefficient (see JAVA-67, JAVA-1262).
538+ *
539+ * @param text The text
540+ * @param quoteChar The character to use as a quote.
541+ * @return The text with surrounding quotes removed and non surrounding quotes unescaped (i.e. '' becomes ')
542+ */
543+ private static String unquote(String text, char quoteChar) {
544+ if (!isQuoted(text, quoteChar))
458545 return text;
459546
460- int newLength = text.length() + nbMatch * (replacement.length() - 1);
547+ if (text.length() == 2)
548+ return "";
549+
550+ String search = emptyQuoted(quoteChar);
551+ int nbMatch = 0;
552+ int start = -1;
553+ do {
554+ start = text.indexOf(search, start + 2);
555+ // ignore the second to last character occurrence, as the last character is a quote.
556+ if (start != -1 && start != text.length() - 2)
557+ ++nbMatch;
558+ } while (start != -1);
559+
560+ // no escaped quotes found, simply remove surrounding quotes and return.
561+ if (nbMatch == 0)
562+ return text.substring(1, text.length() - 1);
563+
564+ // length of the new string will be its current length - the number of occurrences.
565+ int newLength = text.length() - nbMatch - 2;
461566 char[] result = new char[newLength];
462567 int newIdx = 0;
463- for (int i = 0; i < text.length(); i++) {
568+ // track whenever a quoteChar is encountered and the previous character is not a quoteChar.
569+ boolean firstFound = false;
570+ for (int i = 1; i < text.length() - 1; i++) {
464571 char c = text.charAt(i);
465- if (c == search) {
466- for (int r = 0; r < replacement.length(); r++)
467- result[newIdx++] = replacement.charAt(r);
572+ if (c == quoteChar) {
573+ if (firstFound) {
574+ // The previous character was a quoteChar, don't add this to result, this action in
575+ // effect removes consecutive quotes.
576+ firstFound = false;
577+ } else {
578+ // found a quoteChar and the previous character was not a quoteChar, include in result.
579+ firstFound = true;
580+ result[newIdx++] = c;
581+ }
468582 } else {
583+ // non quoteChar encountered, include in result.
469584 result[newIdx++] = c;
585+ firstFound = false;
470586 }
471587 }
472588 return new String(result);
modified driver-core/src/test/java/com/datastax/driver/core/ParseUtilsTest.java +45 −0
@@ -43,6 +43,9 @@
4343 assertThat(ParseUtils.unquote("'foo'")).isEqualTo("foo");
4444 assertThat(ParseUtils.unquote(" 'foo' ")).isEqualTo(" 'foo' "); // considered unquoted
4545 assertThat(ParseUtils.unquote("'''foo'''")).isEqualTo("'foo'");
46+ assertThat(ParseUtils.unquote("'''")).isEqualTo("'");
47+ assertThat(ParseUtils.unquote("''foo'")).isEqualTo("'foo");
48+ assertThat(ParseUtils.unquote("'foo''")).isEqualTo("foo'");
4649 }
4750
4851 @Test(groups = "unit")
@@ -60,4 +63,46 @@
6063 assertThat(ParseUtils.isQuoted("'''foo'''")).isTrue();
6164 }
6265
66+ @Test(groups = "unit")
67+ public void testDoubleQuote() {
68+ assertThat(ParseUtils.doubleQuote(null)).isEqualTo("\"\"");
69+ assertThat(ParseUtils.doubleQuote("")).isEqualTo("\"\"");
70+ assertThat(ParseUtils.doubleQuote(" ")).isEqualTo("\" \"");
71+ assertThat(ParseUtils.doubleQuote("foo")).isEqualTo("\"foo\"");
72+ assertThat(ParseUtils.doubleQuote(" \"foo\" ")).isEqualTo("\" \"\"foo\"\" \"");
73+ }
74+
75+ @Test(groups = "unit")
76+ public void testDoubleUnquote() {
77+ assertThat(ParseUtils.unDoubleQuote(null)).isNull();
78+ assertThat(ParseUtils.unDoubleQuote("")).isEqualTo("");
79+ assertThat(ParseUtils.unDoubleQuote(" ")).isEqualTo(" ");
80+ assertThat(ParseUtils.unDoubleQuote("\"")).isEqualTo("\""); // malformed string left untouched
81+ assertThat(ParseUtils.unDoubleQuote("foo")).isEqualTo("foo");
82+ assertThat(ParseUtils.unDoubleQuote("\"\"")).isEqualTo("");
83+ assertThat(ParseUtils.unDoubleQuote("\" \"")).isEqualTo(" ");
84+ assertThat(ParseUtils.unDoubleQuote("\"foo")).isEqualTo("\"foo"); // malformed string left untouched
85+ assertThat(ParseUtils.unDoubleQuote("\"foo\"")).isEqualTo("foo");
86+ assertThat(ParseUtils.unDoubleQuote(" \"foo\" ")).isEqualTo(" \"foo\" "); // considered unquoted
87+ assertThat(ParseUtils.unDoubleQuote("\"\"\"foo\"\"\"")).isEqualTo("\"foo\"");
88+ assertThat(ParseUtils.unDoubleQuote("\"\"\"")).isEqualTo("\"");
89+ assertThat(ParseUtils.unDoubleQuote("\"\"foo\"")).isEqualTo("\"foo");
90+ assertThat(ParseUtils.unDoubleQuote("\"foo\"\"")).isEqualTo("foo\"");
91+ }
92+
93+ @Test(groups = "unit")
94+ public void testIsDoubleQuoted() {
95+ assertThat(ParseUtils.isDoubleQuoted(null)).isFalse();
96+ assertThat(ParseUtils.isDoubleQuoted("")).isFalse();
97+ assertThat(ParseUtils.isDoubleQuoted(" ")).isFalse();
98+ assertThat(ParseUtils.isDoubleQuoted("\"")).isFalse(); // malformed string considered unquoted
99+ assertThat(ParseUtils.isDoubleQuoted("foo")).isFalse();
100+ assertThat(ParseUtils.isDoubleQuoted("\"\"")).isTrue();
101+ assertThat(ParseUtils.isDoubleQuoted("\" \"")).isTrue();
102+ assertThat(ParseUtils.isDoubleQuoted("\"foo")).isFalse(); // malformed string considered unquoted
103+ assertThat(ParseUtils.isDoubleQuoted("\"foo\"")).isTrue();
104+ assertThat(ParseUtils.isDoubleQuoted(" \"foo\" ")).isFalse(); // considered unquoted
105+ assertThat(ParseUtils.isDoubleQuoted("\"\"\"foo\"\"\"")).isTrue();
106+ }
107+
63108 }