ext/rinku/autolink.c in rinku-2.0.2 vs ext/rinku/autolink.c in rinku-2.0.3
- old
+ new
@@ -50,11 +50,11 @@
}
static bool
autolink_delim(const uint8_t *data, struct autolink_pos *link)
{
- uint8_t cclose, copen = 0;
+ int32_t cclose, copen = 0;
size_t i;
for (i = link->start; i < link->end; ++i)
if (data[i] == '<') {
link->end = i;
@@ -86,20 +86,13 @@
}
if (link->end == link->start)
return false;
- cclose = data[link->end - 1];
+ cclose = utf8proc_rewind(data, link->end);
+ copen = utf8proc_open_paren_character(cclose);
- switch (cclose) {
- case '"': copen = '"'; break;
- case '\'': copen = '\''; break;
- case ')': copen = '('; break;
- case ']': copen = '['; break;
- case '}': copen = '{'; break;
- }
-
if (copen != 0) {
/* Try to close the final punctuation sign in this link; if
* there's more closing than opening punctuation symbols in the
* URL, we conservatively remove one closing punctuation from
* the end of the URL.
@@ -122,19 +115,24 @@
size_t closing = 0;
size_t opening = 0;
size_t i = link->start;
while (i < link->end) {
- if (data[i] == copen)
+ int32_t c = utf8proc_next(data, &i);
+ if (c == copen)
opening++;
- else if (data[i] == cclose)
+ else if (c == cclose)
closing++;
-
- i++;
}
- if (closing > opening)
- link->end--;
+ if (copen == cclose) {
+ if (opening > 0)
+ utf8proc_back(data, &link->end);
+ }
+ else {
+ if (closing > opening)
+ utf8proc_back(data, &link->end);
+ }
}
return true;
}