Skip to content

Commit

Permalink
UnicodeEscape: exempt the very specific pattern of \u005Cu, as there'…
Browse files Browse the repository at this point in the history
…s no other way to write a literal "\u" outside string literals.

I suppose we could urge towards "\\u" for string literals, such that \u005Cu is only necessary in comments, but it doesn't seem worthwhile.

PiperOrigin-RevId: 402924410
  • Loading branch information
graememorgan authored and Error Prone Team committed Oct 13, 2021
1 parent 4747528 commit d2764b9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ private UnicodeScanner(String source, VisitorState state) {
public void scan() {
for (; position < source.length(); processCharacter()) {
if (isUnicode && isBanned(currentCharacter)) {
if (currentCharacter == '\\' && peek() == 'u') {
continue;
}
state.reportMatch(
describeMatch(
new FixedPosition(state.getPath().getCompilationUnit(), position),
Expand Down Expand Up @@ -97,6 +100,11 @@ private void nextCharacter() {
currentCharacter = source.charAt(position);
}
}

/** Returns the next character, or {@code 0} if we're at the end of the file. */
private char peek() {
return position + 1 < source.length() ? source.charAt(position + 1) : 0;
}
}

private static boolean isBanned(char c) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,15 @@ public void everythingObfuscated() {
.addOutputLines("A.java", "class A{}")
.doTest(TEXT_MATCH);
}

@Test
public void escapedLiteralBackslashU() {
refactoring
.addInputLines(
"A.java", //
"/** \\u005Cu */",
"class A {}")
.expectUnchanged()
.doTest(TEXT_MATCH);
}
}

0 comments on commit d2764b9

Please sign in to comment.