Skip to content

Commit 663ddea

Browse files
committed
Stop recognizing "abc://foo" in "1abc://foo"
A digit doesn't feel enough like a separator, it's more like an invalid scheme.
1 parent 3859fc7 commit 663ddea

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/main/java/org/nibor/autolink/internal/UrlScanner.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,22 @@ public LinkSpan scan(CharSequence input, int triggerIndex, int rewindIndex) {
3131
// See "scheme" in RFC 3986
3232
private int findFirst(CharSequence input, int beginIndex, int rewindIndex) {
3333
int first = -1;
34+
int digit = -1;
3435
for (int i = beginIndex; i >= rewindIndex; i--) {
3536
char c = input.charAt(i);
3637
if (Scanners.isAlpha(c)) {
3738
first = i;
38-
} else if (!schemeNonAlpha(c)) {
39+
} else if (Scanners.isDigit(c)) {
40+
digit = i;
41+
} else if (!schemeSpecial(c)) {
3942
break;
4043
}
4144
}
45+
if (first > 0 && first - 1 == digit) {
46+
// We don't want to extract "abc://foo" out of "1abc://foo".
47+
// ".abc://foo" and others are ok though, as they feel more like separators.
48+
first = -1;
49+
}
4250
return first;
4351
}
4452

@@ -101,14 +109,13 @@ private int findLast(CharSequence input, int beginIndex) {
101109
return last;
102110
}
103111

104-
private boolean schemeNonAlpha(char c) {
112+
private static boolean schemeSpecial(char c) {
105113
switch (c) {
106114
case '+':
107115
case '-':
108116
case '.':
109117
return true;
110118
}
111-
return Scanners.isDigit(c);
119+
return false;
112120
}
113-
114121
}

src/test/java/org/nibor/autolink/AutolinkUrlTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ public void schemes() {
4545
assertNotLinked("+://foo");
4646
assertNotLinked("-://foo");
4747
assertNotLinked(".://foo");
48+
assertNotLinked("1abc://foo");
4849
assertLinked("a://foo", "|a://foo|");
4950
assertLinked("a123://foo", "|a123://foo|");
51+
assertLinked("a123b://foo", "|a123b://foo|");
5052
assertLinked("a+b://foo", "|a+b://foo|");
5153
assertLinked("a-b://foo", "|a-b://foo|");
5254
assertLinked("a.b://foo", "|a.b://foo|");

0 commit comments

Comments
 (0)