Use regexp to skip schema matches not preceded by allowed separator
This commit is contained in:
parent
98974a7699
commit
cf9c3d078e
1 changed files with 6 additions and 19 deletions
|
@ -13,7 +13,8 @@ import android.text.TextUtils;
|
||||||
public class UriLinkifier {
|
public class UriLinkifier {
|
||||||
private static final Pattern URI_SCHEME;
|
private static final Pattern URI_SCHEME;
|
||||||
private static final Map<String, UriParser> SUPPORTED_URIS;
|
private static final Map<String, UriParser> SUPPORTED_URIS;
|
||||||
private static final String SCHEME_SEPARATOR = " (";
|
private static final String SCHEME_SEPARATORS = " (";
|
||||||
|
private static final String ALLOWED_SEPARATORS_PATTERN = "(?:^|[" + SCHEME_SEPARATORS + "])";
|
||||||
|
|
||||||
static {
|
static {
|
||||||
SUPPORTED_URIS = new HashMap<>();
|
SUPPORTED_URIS = new HashMap<>();
|
||||||
|
@ -24,7 +25,8 @@ public class UriLinkifier {
|
||||||
SUPPORTED_URIS.put("rtsp:", httpParser);
|
SUPPORTED_URIS.put("rtsp:", httpParser);
|
||||||
|
|
||||||
String allSchemes = TextUtils.join("|", SUPPORTED_URIS.keySet());
|
String allSchemes = TextUtils.join("|", SUPPORTED_URIS.keySet());
|
||||||
URI_SCHEME = Pattern.compile(allSchemes, Pattern.CASE_INSENSITIVE);
|
String pattern = ALLOWED_SEPARATORS_PATTERN + "(" + allSchemes + ")";
|
||||||
|
URI_SCHEME = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -33,18 +35,12 @@ public class UriLinkifier {
|
||||||
Matcher matcher = URI_SCHEME.matcher(text);
|
Matcher matcher = URI_SCHEME.matcher(text);
|
||||||
|
|
||||||
while (matcher.find(currentPos)) {
|
while (matcher.find(currentPos)) {
|
||||||
int startPos = matcher.start();
|
int startPos = matcher.start(1);
|
||||||
|
|
||||||
String textBeforeMatch = text.substring(currentPos, startPos);
|
String textBeforeMatch = text.substring(currentPos, startPos);
|
||||||
outputBuffer.append(textBeforeMatch);
|
outputBuffer.append(textBeforeMatch);
|
||||||
|
|
||||||
if (!isPrecededByValidSeparator(textBeforeMatch)) {
|
String scheme = matcher.group(1).toLowerCase(Locale.US);
|
||||||
outputBuffer.append(text.charAt(startPos));
|
|
||||||
currentPos = startPos + 1;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
String scheme = matcher.group().toLowerCase(Locale.US);
|
|
||||||
UriParser parser = SUPPORTED_URIS.get(scheme);
|
UriParser parser = SUPPORTED_URIS.get(scheme);
|
||||||
int newPos = parser.linkifyUri(text, startPos, outputBuffer);
|
int newPos = parser.linkifyUri(text, startPos, outputBuffer);
|
||||||
|
|
||||||
|
@ -64,13 +60,4 @@ public class UriLinkifier {
|
||||||
String textAfterLastMatch = text.substring(currentPos);
|
String textAfterLastMatch = text.substring(currentPos);
|
||||||
outputBuffer.append(textAfterLastMatch);
|
outputBuffer.append(textAfterLastMatch);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isPrecededByValidSeparator(String textBeforeMatch) {
|
|
||||||
if (textBeforeMatch.isEmpty()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
String characterBeforeMatch = textBeforeMatch.substring(textBeforeMatch.length() - 1);
|
|
||||||
return SCHEME_SEPARATOR.contains(characterBeforeMatch);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue