mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
speed up UrlFilter
This commit is contained in:
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user