Skip to content

fix: preserve unrecognized escape sequences in string literals - #454

Open
chenjunwenhao wants to merge 1 commit into
alibaba:mainfrom
chenjunwenhao:fix/issue-335-string-escape
Open

fix: preserve unrecognized escape sequences in string literals#454
chenjunwenhao wants to merge 1 commit into
alibaba:mainfrom
chenjunwenhao:fix/issue-335-string-escape

Conversation

@chenjunwenhao

Copy link
Copy Markdown
Contributor

Problem

QLStringUtils.parseStringEscapeStartEnd() silently drops both the backslash and the following character for unrecognized escape sequences like \d, \w, \s, etc.

For example, the string literal '(\d*)ch' produces (*)ch instead of (\d*)ch, which breaks regex patterns embedded in string literals (reported in #335).

Root Cause

In QLStringUtils.java, the escape state's switch statement only handles recognized escape characters (b, t, n, f, r, ", ', \, $). There is no default case, so when an unrecognized character follows a backslash, neither the backslash nor the character is appended to the result.

Fix

Added a default case to the escape state switch that preserves the backslash and the character as-is:

default:
    result.append('\\');
    result.append(cur);
    break;

This means:

  • \d\d (preserved for regex use)
  • \w\w (preserved for regex use)
  • \n → newline (still works as before)
  • \\\ (still works as before)

Changes

  1. QLStringUtils.java — Added default case in escape state switch
  2. QLStringUtilsTest.java — New unit test class with 20+ test cases covering recognized, unrecognized, and mixed escape sequences
  3. string_escape.ql — Extended test suite with unrecognized escape and regex pattern tests
  4. README-source.adoc / README-EN-source.adoc — Added "String Escape Sequences" documentation section listing all supported escapes

Testing

  • 20+ new JUnit tests for QLStringUtils covering all recognized escapes, all common unrecognized escapes (\d, \w, \s, \D, \W, \S, \(, \., \*, \+, \1), complex regex patterns, and mixed scenarios
  • Extended string_escape.ql test suite with regex pattern assertions

…ba#335)

QLStringUtils.parseStringEscapeStartEnd() silently dropped both the
backslash and the following character for unrecognized escape sequences
like \d, \w, \s. For example, '(\d*)ch' would become (*)ch instead of
(\d*)ch, breaking regex patterns in string literals.

Added a default case to the escape state switch that preserves the
backslash and character as-is. Also added unit tests for QLStringUtils
and extended the test suite with regex pattern escape tests.

Documentation updated in both README-source.adoc and README-EN-source.adoc
with a complete list of supported escape sequences.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant