RuleDatabase { @Nullable static String parseLine(String line) { int endOfLine = line.indexOf('#'); if (endOfLine == -1) endOfLine = line.length(); while (endOfLine > 0 && Character.isWhitespace(line.charAt(endOfLine - 1))) endOfLine--; if (endOfLine <= 0) return null; int startOfHost = 0; if (line.regionMatches(0, "127.0.0.1", 0, 9) && (endOfLine <= 9 || Character.isWhitespace(line.charAt(9)))) startOfHost += 10; else if (line.regionMatches(0, "::1", 0, 3) && (endOfLine <= 3 || Character.isWhitespace(line.charAt(3)))) startOfHost += 4; else if (line.regionMatches(0, "0.0.0.0", 0, 7) && (endOfLine <= 7 || Character.isWhitespace(line.charAt(7)))) startOfHost += 8; while (startOfHost < endOfLine && Character.isWhitespace(line.charAt(startOfHost))) startOfHost++; for (int i = startOfHost; i < endOfLine; i++) { if (Character.isWhitespace(line.charAt(i))) return null; } if (startOfHost >= endOfLine) return null; return line.substring(startOfHost, endOfLine).toLowerCase(Locale.ENGLISH); } RuleDatabase(); static RuleDatabase getInstance(); boolean isBlocked(String host); synchronized void initialize(Context context);  }
@Test public void testParseLine() throws Exception { assertEquals("example.com", RuleDatabase.parseLine("0.0.0.0 example.com")); assertEquals("example.com", RuleDatabase.parseLine("127.0.0.1 example.com")); assertEquals("example.com", RuleDatabase.parseLine("::1 example.com")); assertEquals("example.com", RuleDatabase.parseLine("example.com")); assertEquals("example.com", RuleDatabase.parseLine("example.com # foo")); assertEquals("example.com", RuleDatabase.parseLine("0.0.0.0 example.com # foo")); assertEquals("example.com", RuleDatabase.parseLine("::1 example.com # foo")); assertEquals("example.com", RuleDatabase.parseLine("example.cOm")); assertEquals("example.com", RuleDatabase.parseLine("127.0.0.1 example.cOm")); assertEquals("example.com", RuleDatabase.parseLine("::1 example.cOm")); assertNull(RuleDatabase.parseLine(" 127.0.0.1 example.com")); assertEquals("127.0.0.1.example.com", RuleDatabase.parseLine("127.0.0.1.example.com ")); assertEquals("::1.example.com", RuleDatabase.parseLine("::1.example.com ")); assertEquals("0.0.0.0.example.com", RuleDatabase.parseLine("0.0.0.0.example.com ")); assertEquals("example.com", RuleDatabase.parseLine("127.0.0.1 example.com ")); assertEquals("example.com", RuleDatabase.parseLine("127.0.0.1 example.com\t")); assertEquals("example.com", RuleDatabase.parseLine("127.0.0.1 example.com ")); assertEquals("example.com", RuleDatabase.parseLine("127.0.0.1\t example.com ")); assertEquals("example.com", RuleDatabase.parseLine("::1\t example.com ")); assertNull(RuleDatabase.parseLine("127.0.0.1 ")); assertNull(RuleDatabase.parseLine("127.0.0.1")); assertNull(RuleDatabase.parseLine("0.0.0.0")); assertNull(RuleDatabase.parseLine("0.0.0.0 ")); assertNull(RuleDatabase.parseLine("::1 ")); assertNull(RuleDatabase.parseLine("::1")); assertNull(RuleDatabase.parseLine("invalid example.com")); assertNull(RuleDatabase.parseLine("invalid\texample.com")); assertNull(RuleDatabase.parseLine("invalid long line")); assertNull(RuleDatabase.parseLine("# comment line")); assertNull(RuleDatabase.parseLine("")); assertNull(RuleDatabase.parseLine("\t")); assertNull(RuleDatabase.parseLine(" ")); }