speed up UrlFilter

This commit is contained in:
peter
2016-03-05 10:17:24 +01:00
parent e6a5fc8f31
commit 6981a1110f
3 changed files with 16 additions and 2 deletions
@@ -32,6 +32,8 @@ public class UrlFilter implements Filter {
@Nullable
@Override
public Result applyFilter(String line, int entireLength) {
if (!URLUtil.canContainUrl(line)) return null;
int textStartOffset = entireLength - line.length();
Matcher m = URLUtil.URL_PATTERN.matcher(line);
ResultItem item = null;
@@ -47,6 +47,13 @@ public class URLUtil {
private URLUtil() { }
/**
* @return if false, then the line contains no URL; if true, then more heavy {@link #URL_PATTERN} check should be used.
*/
public static boolean canContainUrl(@NotNull String line) {
return line.contains("mailto:") || line.contains("://") || line.contains("www.");
}
/**
* Opens a url stream. The semantics is the sames as {@link URL#openStream()}. The
* separate method is needed, since jar URLs open jars via JarFactory and thus keep them
@@ -86,14 +86,19 @@ public class UrlUtilTest {
private static void doUrlTest(@NotNull final String line, @Nullable final String expectedUrl) {
final Matcher matcher = URLUtil.URL_PATTERN.matcher(line);
boolean found = matcher.find();
if (expectedUrl == null) {
if (matcher.find()) {
if (found) {
fail("No URL expected in [" + line + "], detected: " + matcher.group());
}
return;
}
assertTrue("Expected URL (" + expectedUrl + ") is not detected in [" + line + "]", matcher.find());
if (!URLUtil.canContainUrl(line) && found) {
fail("canContainUrl returns false for " + line);
}
assertTrue("Expected URL (" + expectedUrl + ") is not detected in [" + line + "]", found);
assertEquals("Text: [" + line + "]", expectedUrl, matcher.group());
}