@@ -35,79 +35,78 @@ static int sd_autolink_issafe(const uint8_t *link, size_t link_len) {
35
35
}
36
36
37
37
static size_t autolink_delim (uint8_t * data , size_t link_end ) {
38
- uint8_t cclose , copen ;
39
38
size_t i ;
39
+ size_t closing = 0 ;
40
+ size_t opening = 0 ;
40
41
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 == '<' ) {
43
45
link_end = i ;
44
46
break ;
47
+ } else if (c == '(' ) {
48
+ opening ++ ;
49
+ } else if (c == ')' ) {
50
+ closing ++ ;
45
51
}
52
+ }
46
53
47
54
while (link_end > 0 ) {
48
- cclose = data [link_end - 1 ];
49
-
50
- switch (cclose ) {
55
+ switch (data [link_end - 1 ]) {
51
56
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
-
76
57
/* Allow any number of matching brackets (as recognised in copen/cclose)
77
58
* at the end of the URL. If there is a greater number of closing
78
59
* brackets than opening ones, we remove one character from the end of
79
60
* the link.
80
61
*
81
62
* Examples (input text => output linked portion):
82
63
*
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)
85
66
*
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)
88
69
*
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)
91
72
*
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))
94
75
*/
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 ;
103
78
}
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 ;
104
96
105
- if ( closing <= opening )
106
- break ;
97
+ while ( new_end > 0 && cmark_isalpha ( data [ new_end ]) )
98
+ new_end -- ;
107
99
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 -- ;
110
104
break ;
105
+ }
106
+
107
+ default :
108
+ return link_end ;
109
+ }
111
110
}
112
111
113
112
return link_end ;
0 commit comments