Skip to content

Commit

Permalink
Issue checkstyle#3476: Improve escaped unicode chars detection for Av…
Browse files Browse the repository at this point in the history
…oidEscapedUnicodeCharacters
  • Loading branch information
Dziman committed Oct 6, 2016
1 parent 7b5f86a commit a58c9ef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public class AvoidEscapedUnicodeCharactersCheck
Pattern.compile("^((\\\\u)[a-fA-F0-9]{4}"
+ "||\\\\b|\\\\t|\\\\n|\\\\f|\\\\r|\\\\|\"|\')+$");

/** Regular expression for escaped backslash. */
private static final Pattern ESCAPED_BACKSLASH = Pattern.compile("\\\\\\\\");

/** Regular expression for non-printable unicode chars. */
private static final Pattern NON_PRINTABLE_CHARS = Pattern.compile("\\\\u1680|\\\\u2028"
+ "|\\\\u2029|\\\\u205(f|F)|\\\\u3000|\\\\u2007|\\\\u2000|\\\\u200(a|A)"
Expand Down Expand Up @@ -242,7 +245,9 @@ && isOnlyUnicodeValidChars(literal, NON_PRINTABLE_CHARS))) {
* @return true if literal has Unicode chars.
*/
private static boolean hasUnicodeChar(String literal) {
return UNICODE_REGEXP.matcher(literal).find();
final String literalWithoutEscapedBackslashes =
ESCAPED_BACKSLASH.matcher(literal).replaceAll("");
return UNICODE_REGEXP.matcher(literalWithoutEscapedBackslashes).find();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void testDefault() throws Exception {
"93: " + getCheckMessage(MSG_KEY),
"94: " + getCheckMessage(MSG_KEY),
"98: " + getCheckMessage(MSG_KEY),
"104: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputAvoidEscapedUnicodeCharacters.java"), expected);
}
Expand Down Expand Up @@ -127,6 +128,7 @@ public void testAllowEscapesForControlCharacterSet() throws Exception {
"92: " + getCheckMessage(MSG_KEY),
"94: " + getCheckMessage(MSG_KEY),
"98: " + getCheckMessage(MSG_KEY),
"104: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputAvoidEscapedUnicodeCharacters.java"), expected);
}
Expand Down Expand Up @@ -156,6 +158,7 @@ public void testAllowByTailComment() throws Exception {
"82: " + getCheckMessage(MSG_KEY),
"92: " + getCheckMessage(MSG_KEY),
"98: " + getCheckMessage(MSG_KEY),
"104: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputAvoidEscapedUnicodeCharacters.java"), expected);
}
Expand Down Expand Up @@ -209,6 +212,7 @@ public void allowNonPrintableEscapes() throws Exception {
"93: " + getCheckMessage(MSG_KEY),
"94: " + getCheckMessage(MSG_KEY),
"98: " + getCheckMessage(MSG_KEY),
"104: " + getCheckMessage(MSG_KEY),
};
verify(checkConfig, getPath("InputAvoidEscapedUnicodeCharacters.java"), expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ private static String abbreviate(TimeUnit unit) {
throw new AssertionError();
}
}

static final String WHITESPACE_TABLE = ""
+ "\u2002\u3000\r\u0085\u200A\u2005\u2000\u3000\\"
+ "\u2029\u000B\u3000\u2008\u2003\u205F\u3000\u1680"
+ "\u0009\u0020\u2006\u2001\u202F\u00A0\u000C\u2009"
+ "\u3000\u2004\u3000\u3000\u2028\n\u2007\u3000";

public boolean matches(char c) {
switch (c) {
case '\t':
Expand Down Expand Up @@ -96,4 +96,10 @@ void foo() {
}
}
private String unitAbbrev9 = "\u03bcs"; /* comment */ int i;

private String notAUnicodeEscaped1 = "\\u1234";

private String notAUnicodeEscaped2 = "\\\\u1234";

private String onlyEscaped = "\\\u1234";
}

0 comments on commit a58c9ef

Please sign in to comment.