Skip to content

Commit

Permalink
Issue checkstyle#3700: Control Characters are not skipped with google…
Browse files Browse the repository at this point in the history
…_checks config
  • Loading branch information
Luolc committed Mar 8, 2017
1 parent 8e64917 commit 531f7ac
Show file tree
Hide file tree
Showing 4 changed files with 65,674 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public Object fooString()
String fakeUnicode = "asd\tsasd";
String fakeUnicode2 = "\\u23\\u123i\\u";
String content = "";
return "\ufeff" + content ; // byte order mark ok
/*byte order mark ok*/return "\ufeff" + content ;
}

public Object fooChar()
{
/*warn*/char unitAbbrev2 = '\u03bc';
char unitAbbrev3 = '\u03bc'; // Greek letter mu, "s" ok
String content = "";
return '\ufeff' + content; // byte order mark ok
/*byte order mark ok*/return '\ufeff' + content;
}

public void multiplyString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public class AvoidEscapedUnicodeCharactersCheck

/** Regular expression Unicode control characters. */
private static final Pattern UNICODE_CONTROL = Pattern.compile("\\\\(u|U)"
+ "(00[0-1][0-1A-Fa-f]|00[8-9][0-9A-Fa-f]|034(f|F)|070(f|F)"
+ "|180(e|E)|200[b-fB-F]|202[b-eB-E]|206[0-4a-fA-F]"
+ "(00[0-1][0-9A-Fa-f]|00[8-9][0-9A-Fa-f]|00(a|A)(d|D)|034(f|F)|070(f|F)"
+ "|180(e|E)|200[b-fB-F]|202[a-eA-E]|206[0-4a-fA-F]"
+ "|[fF]{3}[9a-bA-B]|[fF][eE][fF]{2})");

/** Regular expression for all escaped chars. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.IntStream;

import org.junit.Test;

Expand All @@ -32,6 +34,109 @@
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

public class AvoidEscapedUnicodeCharactersCheckTest extends BaseCheckTestSupport {

private static final int[] C0_CONTROL_CHARACTER_INDICES = {
0x0000,
0x0001,
0x0002,
0x0003,
0x0004,
0x0005,
0x0006,
0x0007,
0x0008,
0x0009,
0x000a,
0x000b,
0x000c,
0x000d,
0x000e,
0x000f,
0x0010,
0x0011,
0x0012,
0x0013,
0x0014,
0x0015,
0x0016,
0x0017,
0x0018,
0x0019,
0x001a,
0x001b,
0x001c,
0x001d,
0x001e,
0x001f,
};

private static final int[] C1_CONTROL_CHARACTER_INDICES = {
0x0080,
0x0081,
0x0082,
0x0083,
0x0084,
0x0085,
0x0086,
0x0087,
0x0088,
0x0089,
0x008a,
0x008b,
0x008c,
0x008d,
0x008e,
0x008f,
0x0090,
0x0091,
0x0092,
0x0093,
0x0094,
0x0095,
0x0096,
0x0097,
0x0098,
0x0099,
0x009a,
0x009b,
0x009c,
0x009d,
0x009e,
0x009f,
};

private static final int[] OTHER_CONTROL_CHARACTER_INDICES = {
0x00ad,
0x034f,
0x070f,
0x180e,
0x200b,
0x200c,
0x200d,
0x200e,
0x200f,
0x202a,
0x202b,
0x202c,
0x202d,
0x202e,
0x2060,
0x2061,
0x2062,
0x2063,
0x2064,
0x206a,
0x206b,
0x206c,
0x206d,
0x206e,
0x206f,
0xfeff,
0xfff9,
0xfffa,
0xfffb,
};

@Override
protected String getPath(String filename) throws IOException {
return super.getPath("checks" + File.separator + filename);
Expand Down Expand Up @@ -224,4 +329,26 @@ public void testGetAcceptableTokens() {
final int[] expected = {TokenTypes.STRING_LITERAL, TokenTypes.CHAR_LITERAL };
assertArrayEquals(expected, actual);
}

@Test
public void testAllowEscapesForControlCharacterSetForAllCharacters() throws Exception {
final DefaultConfiguration checkConfig =
createCheckConfig(AvoidEscapedUnicodeCharactersCheck.class);
checkConfig.addAttribute("allowEscapesForControlCharacters", "true");

final int indexOfStartLineInInputFile = 6;
final String message = getCheckMessage(MSG_KEY);
final String[] expected = IntStream.rangeClosed(0, 0xffff)
.parallel()
.filter(val -> !isControlCharacter(val))
.mapToObj(msg -> indexOfStartLineInInputFile + msg + ": " + message)
.toArray(String[]::new);
verify(checkConfig, getPath("InputAllEscapedUnicodeCharacters.java"), expected);
}

private static boolean isControlCharacter(final int character) {
return Arrays.binarySearch(C0_CONTROL_CHARACTER_INDICES, character) >= 0
|| Arrays.binarySearch(C1_CONTROL_CHARACTER_INDICES, character) >= 0
|| Arrays.binarySearch(OTHER_CONTROL_CHARACTER_INDICES, character) >= 0;
}
}
Loading

0 comments on commit 531f7ac

Please sign in to comment.