-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Allows unclosed quote characters to exist in comments (#4993)
* fix: Allows quote characters to exist in comments, e.g. SELECT -- It's a comment
- Loading branch information
1 parent
13b0455
commit fd65021
Showing
6 changed files
with
305 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
ksqldb-cli/src/main/java/io/confluent/ksql/cli/console/UnclosedQuoteChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2020 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.cli.console; | ||
|
||
/** | ||
* Checks to see if the line is in the middle of a quote. Unlike | ||
* org.jline.reader.impl.DefaultParser, this is comment aware. | ||
*/ | ||
public final class UnclosedQuoteChecker { | ||
private static final String COMMENT = "--"; | ||
|
||
private UnclosedQuoteChecker() {} | ||
|
||
// CHECKSTYLE_RULES.OFF: CyclomaticComplexity | ||
public static boolean isUnclosedQuote(final String line) { | ||
// CHECKSTYLE_RULES.ON: CyclomaticComplexity | ||
int quoteStart = -1; | ||
for (int i = 0; i < line.length(); ++i) { | ||
if (quoteStart < 0 && isQuoteChar(line, i)) { | ||
quoteStart = i; | ||
} else if (quoteStart >= 0 && isTwoQuoteStart(line, i) && !isEscaped(line, i)) { | ||
// Together, two quotes are effectively an escaped quote and don't act as a quote character. | ||
// Skip the next quote char, since it's coupled with the first. | ||
i++; | ||
} else if (quoteStart >= 0 && isQuoteChar(line, i) && !isEscaped(line, i)) { | ||
quoteStart = -1; | ||
} | ||
} | ||
final int commentInd = line.indexOf(COMMENT); | ||
if (commentInd < 0) { | ||
return quoteStart >= 0; | ||
} else if (quoteStart < 0) { | ||
return false; | ||
} else { | ||
return commentInd > quoteStart; | ||
} | ||
} | ||
|
||
private static boolean isQuoteChar(final String line, final int ind) { | ||
final char c = line.charAt(ind); | ||
if (c == '\'') { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private static boolean isEscaped(final String line, final int ind) { | ||
if (ind == 0) { | ||
return false; | ||
} | ||
final char c = line.charAt(ind - 1); | ||
if (c == '\\' && !isEscaped(line, ind - 1)) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
// Technically, it is sufficient to ensure that quotes are paired up to answer the "unclosed" | ||
// question. It's still clearer to differentiate between the two-quote escaped quote and two | ||
// back-to-back strings (e.g. 'ab''cd' is really evaluated as "ab'cd" rather than 'ab' and 'cd'), | ||
// so we explicitly check for that case here. | ||
private static boolean isTwoQuoteStart(final String line, final int ind) { | ||
if (ind + 1 >= line.length()) { | ||
return false; | ||
} | ||
return isQuoteChar(line, ind) && isQuoteChar(line, ind + 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
ksqldb-cli/src/test/java/io/confluent/ksql/cli/console/UnclosedQuoteCheckerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
/* | ||
* Copyright 2020 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (the "License"); you may not use | ||
* this file except in compliance with the License. You may obtain a copy of the | ||
* License at | ||
* | ||
* http://www.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.cli.console; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import org.junit.Test; | ||
|
||
public class UnclosedQuoteCheckerTest { | ||
|
||
@Test | ||
public void shouldFindUnclosedQuote() { | ||
// Given: | ||
final String line = "some line 'this is in a quote"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(true)); | ||
} | ||
|
||
@Test | ||
public void shouldFindUnclosedQuote_commentCharsInside() { | ||
// Given: | ||
final String line = "some line 'this is in a quote -- not a comment"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(true)); | ||
} | ||
|
||
@Test | ||
public void shouldNotFindUnclosedQuote_commentCharsInside() { | ||
// Given: | ||
final String line = "some line 'this is in a quote -- not a comment'"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(false)); | ||
} | ||
|
||
@Test | ||
public void shouldFindUnclosedQuote_escaped() { | ||
// Given: | ||
final String line = "some line 'this is in a quote\\'"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(true)); | ||
} | ||
|
||
@Test | ||
public void shouldFindUnclosedQuote_escapedEscape() { | ||
// Given: | ||
final String line = "some line 'this is in a quote\\\\'"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(false)); | ||
} | ||
|
||
@Test | ||
public void shouldFindUnclosedQuote_escapedThree() { | ||
// Given: | ||
final String line = "some line 'this is in a quote\\\'"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(true)); | ||
} | ||
|
||
@Test | ||
public void shouldFindUnclosedQuote_twoQuote() { | ||
// Given: | ||
final String line = "some line 'this is in a quote''"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(true)); | ||
} | ||
|
||
@Test | ||
public void shouldNotFindUnclosedQuote_endsQuote() { | ||
// Given: | ||
final String line = "some line 'this is in a quote'"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(false)); | ||
} | ||
|
||
@Test | ||
public void shouldNotFindUnclosedQuote_endsNonQuote() { | ||
// Given: | ||
final String line = "some line 'this is in a quote' more"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(false)); | ||
} | ||
|
||
@Test | ||
public void shouldNotFindUnclosedQuote_containsDoubleQuote() { | ||
// Given: | ||
final String line = "some line 'this is \"in\" a quote'"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(false)); | ||
} | ||
|
||
@Test | ||
public void shouldNotFindUnclosedQuote_containsUnclosedDoubleQuote() { | ||
// Given: | ||
final String line = "some line 'this is \"in a quote'"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(false)); | ||
} | ||
|
||
@Test | ||
public void shouldNotFindUnclosedQuote_escaped() { | ||
// Given: | ||
final String line = "some line 'this is in a quote\\''"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(false)); | ||
} | ||
|
||
@Test | ||
public void shouldNotFindUnclosedQuote_twoQuote() { | ||
// Given: | ||
final String line = "some line 'this is in a quote'''"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(false)); | ||
} | ||
|
||
@Test | ||
public void shouldFindUnclosedQuote_manyQuote() { | ||
// Given: | ||
final String line = "some line 'this is in a quote''''"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(true)); | ||
} | ||
|
||
@Test | ||
public void shouldNotFindUnclosedQuote_manyQuote() { | ||
// Given: | ||
final String line = "some line 'this is in a quote'''''"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(false)); | ||
} | ||
|
||
@Test | ||
public void shouldNotFindUnclosedQuote_escapedAndMultipleQuotes() { | ||
// Given: | ||
final String line = "some line 'this is in a quote\\''''"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(false)); | ||
} | ||
|
||
@Test | ||
public void shouldNotFindUnclosedQuote_inComment() { | ||
// Given: | ||
final String line = "some line -- 'this is in a comment"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(false)); | ||
} | ||
|
||
@Test | ||
public void shouldNotFindUnclosedQuote_onlyComment() { | ||
// Given: | ||
final String line = "some line -- this is a comment"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(false)); | ||
} | ||
|
||
@Test | ||
public void shouldNotFindUnclosedQuote_commentAfterQuote() { | ||
// Given: | ||
final String line = "some line 'quoted text' -- this is a comment"; | ||
|
||
// Then: | ||
assertThat(UnclosedQuoteChecker.isUnclosedQuote(line), is(false)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters