Skip to content

Commit 9c30a1e

Browse files
committed
Fix unexpected link end with unfinished delimiter pairs (#5)
This: http://example.org/"_(foo) Was extracted like this: http://example.org/"_(foo The problem was that all delimiter pairs were checked in one go, instead of just when the delimiter actually occurred. This also changes behavior for unbalanced brackets: A lone closing bracket immediately terminates a link now.
1 parent 9a6c3f9 commit 9c30a1e

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,49 +77,75 @@ private int findLast(CharSequence input, int beginIndex) {
7777
case ':':
7878
case ';':
7979
// These may be part of an URL but not at the end
80-
continue loop;
80+
break;
8181
case '/':
8282
// This may be part of an URL and at the end, but not if the previous character can't be the end of an URL
83-
if (last != i - 1) {
84-
continue loop;
83+
if (last == i - 1) {
84+
last = i;
8585
}
8686
break;
8787
case '(':
8888
round++;
8989
break;
9090
case ')':
9191
round--;
92+
if (round >= 0) {
93+
last = i;
94+
} else {
95+
// More closing than opening brackets, stop now
96+
break loop;
97+
}
9298
break;
9399
case '[':
94100
square++;
95101
break;
96102
case ']':
97103
square--;
104+
if (square >= 0) {
105+
last = i;
106+
} else {
107+
// More closing than opening brackets, stop now
108+
break loop;
109+
}
98110
break;
99111
case '{':
100112
curly++;
101113
break;
102114
case '}':
103115
curly--;
116+
if (curly >= 0) {
117+
last = i;
118+
} else {
119+
// More closing than opening brackets, stop now
120+
break loop;
121+
}
104122
break;
105123
case '<':
106124
angle++;
107125
break;
108126
case '>':
109127
angle--;
128+
if (angle >= 0) {
129+
last = i;
130+
} else {
131+
// More closing than opening brackets, stop now
132+
break loop;
133+
}
110134
break;
111135
case '"':
112136
doubleQuote = !doubleQuote;
137+
if (!doubleQuote) {
138+
last = i;
139+
}
113140
break;
114141
case '\'':
115142
singleQuote = !singleQuote;
143+
if (!singleQuote) {
144+
last = i;
145+
}
116146
break;
117147
default:
118148
last = i;
119-
continue loop;
120-
}
121-
if (round >= 0 && square >= 0 && curly >= 0 && angle >= 0 && !doubleQuote && !singleQuote) {
122-
last = i;
123149
}
124150
}
125151
return last;

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,23 @@ public void matchingPunctuationTricky() {
113113
assertLinked("(http://example.org/).", "(|http://example.org/|).");
114114
assertLinked("(http://example.org/.)", "(|http://example.org/|.)");
115115
assertLinked("http://example.org/>", "|http://example.org/|>");
116-
// not sure about these:
117-
assertLinked("http://example.org/(", "|http://example.org/(|");
116+
// not sure about these
117+
assertLinked("http://example.org/(", "|http://example.org/|(");
118+
assertLinked("http://example.org/(.", "|http://example.org/|(.");
118119
assertLinked("http://example.org/]()", "|http://example.org/|]()");
119120
}
120121

122+
@Test
123+
public void quotes() {
124+
assertLinked("http://example.org/\"_(foo)", "|http://example.org/\"_(foo)|");
125+
assertLinked("http://example.org/\"_(foo)\"", "|http://example.org/\"_(foo)\"|");
126+
assertLinked("http://example.org/\"\"", "|http://example.org/\"\"|");
127+
assertLinked("http://example.org/\"\"\"", "|http://example.org/\"\"|\"");
128+
assertLinked("http://example.org/\".", "|http://example.org/|\".");
129+
assertLinked("http://example.org/\"a", "|http://example.org/\"a|");
130+
assertLinked("http://example.org/it's", "|http://example.org/it's|");
131+
}
132+
121133
@Test
122134
public void html() {
123135
assertLinked("http://example.org\">", "|http://example.org|\">");
@@ -143,6 +155,7 @@ public void slash() {
143155
public void multiple() {
144156
assertLinked("http://one.org/ http://two.org/", "|http://one.org/| |http://two.org/|");
145157
assertLinked("http://one.org/ : http://two.org/", "|http://one.org/| : |http://two.org/|");
158+
assertLinked("(http://one.org/)(http://two.org/)", "(|http://one.org/|)(|http://two.org/|)");
146159
}
147160

148161
@Test

0 commit comments

Comments
 (0)