@@ -35,79 +35,78 @@ static int sd_autolink_issafe(const uint8_t *link, size_t link_len) {
3535}
3636
3737static size_t autolink_delim (uint8_t * data , size_t link_end ) {
38- uint8_t cclose , copen ;
3938 size_t i ;
39+ size_t closing = 0 ;
40+ size_t opening = 0 ;
4041
41- for (i = 0 ; i < link_end ; ++ i )
42- if (data [i ] == '<' ) {
42+ for (i = 0 ; i < link_end ; ++ i ) {
43+ const uint8_t c = data [i ];
44+ if (c == '<' ) {
4345 link_end = i ;
4446 break ;
47+ } else if (c == '(' ) {
48+ opening ++ ;
49+ } else if (c == ')' ) {
50+ closing ++ ;
4551 }
52+ }
4653
4754 while (link_end > 0 ) {
48- cclose = data [link_end - 1 ];
49-
50- switch (cclose ) {
55+ switch (data [link_end - 1 ]) {
5156 case ')' :
52- copen = '(' ;
53- break ;
54- default :
55- copen = 0 ;
56- }
57-
58- if (strchr ("?!.,:*_~'\"" , data [link_end - 1 ]) != NULL )
59- link_end -- ;
60-
61- else if (data [link_end - 1 ] == ';' ) {
62- size_t new_end = link_end - 2 ;
63-
64- while (new_end > 0 && cmark_isalpha (data [new_end ]))
65- new_end -- ;
66-
67- if (new_end < link_end - 2 && data [new_end ] == '&' )
68- link_end = new_end ;
69- else
70- link_end -- ;
71- } else if (copen != 0 ) {
72- size_t closing = 0 ;
73- size_t opening = 0 ;
74- i = 0 ;
75-
7657 /* Allow any number of matching brackets (as recognised in copen/cclose)
7758 * at the end of the URL. If there is a greater number of closing
7859 * brackets than opening ones, we remove one character from the end of
7960 * the link.
8061 *
8162 * Examples (input text => output linked portion):
8263 *
83- * http://www.pokemon.com/Pikachu_(Electric)
84- * => http://www.pokemon.com/Pikachu_(Electric)
64+ * http://www.pokemon.com/Pikachu_(Electric)
65+ * => http://www.pokemon.com/Pikachu_(Electric)
8566 *
86- * http://www.pokemon.com/Pikachu_((Electric)
87- * => http://www.pokemon.com/Pikachu_((Electric)
67+ * http://www.pokemon.com/Pikachu_((Electric)
68+ * => http://www.pokemon.com/Pikachu_((Electric)
8869 *
89- * http://www.pokemon.com/Pikachu_(Electric))
90- * => http://www.pokemon.com/Pikachu_(Electric)
70+ * http://www.pokemon.com/Pikachu_(Electric))
71+ * => http://www.pokemon.com/Pikachu_(Electric)
9172 *
92- * http://www.pokemon.com/Pikachu_((Electric))
93- * => http://www.pokemon.com/Pikachu_((Electric))
73+ * http://www.pokemon.com/Pikachu_((Electric))
74+ * => http://www.pokemon.com/Pikachu_((Electric))
9475 */
95-
96- while (i < link_end ) {
97- if (data [i ] == copen )
98- opening ++ ;
99- else if (data [i ] == cclose )
100- closing ++ ;
101-
102- i ++ ;
76+ if (closing <= opening ) {
77+ return link_end ;
10378 }
79+ closing -- ;
80+ link_end -- ;
81+ break ;
82+ case '?' :
83+ case '!' :
84+ case '.' :
85+ case ',' :
86+ case ':' :
87+ case '*' :
88+ case '_' :
89+ case '~' :
90+ case '\'' :
91+ case '"' :
92+ link_end -- ;
93+ break ;
94+ case ';' : {
95+ size_t new_end = link_end - 2 ;
10496
105- if ( closing <= opening )
106- break ;
97+ while ( new_end > 0 && cmark_isalpha ( data [ new_end ]) )
98+ new_end -- ;
10799
108- link_end -- ;
109- } else
100+ if (new_end < link_end - 2 && data [new_end ] == '&' )
101+ link_end = new_end ;
102+ else
103+ link_end -- ;
110104 break ;
105+ }
106+
107+ default :
108+ return link_end ;
109+ }
111110 }
112111
113112 return link_end ;
0 commit comments