mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-11-09 19:50:26 +03:00
Backport from xs.
This commit is contained in:
parent
8ac32f0d81
commit
bd2a07691d
6
html.c
6
html.c
@ -21,7 +21,7 @@ d_char *not_really_markdown(char *content, d_char **f_content)
|
|||||||
/* global changes */
|
/* global changes */
|
||||||
{
|
{
|
||||||
/* backticks */
|
/* backticks */
|
||||||
xs *ml = xs_regex_matchall(wrk, "`[^`]+`");
|
xs *ml = xs_regex_match(wrk, "`[^`]+`");
|
||||||
p = ml;
|
p = ml;
|
||||||
|
|
||||||
while (xs_list_iter(&p, &v)) {
|
while (xs_list_iter(&p, &v)) {
|
||||||
@ -34,7 +34,7 @@ d_char *not_really_markdown(char *content, d_char **f_content)
|
|||||||
|
|
||||||
{
|
{
|
||||||
/* double asterisks */
|
/* double asterisks */
|
||||||
xs *ml = xs_regex_matchall(wrk, "\\*\\*[^\\*]+\\*\\*");
|
xs *ml = xs_regex_match(wrk, "\\*\\*[^\\*]+\\*\\*");
|
||||||
p = ml;
|
p = ml;
|
||||||
|
|
||||||
while (xs_list_iter(&p, &v)) {
|
while (xs_list_iter(&p, &v)) {
|
||||||
@ -47,7 +47,7 @@ d_char *not_really_markdown(char *content, d_char **f_content)
|
|||||||
|
|
||||||
{
|
{
|
||||||
/* single asterisks */
|
/* single asterisks */
|
||||||
xs *ml = xs_regex_matchall(wrk, "\\*[^\\*]+\\*");
|
xs *ml = xs_regex_match(wrk, "\\*[^\\*]+\\*");
|
||||||
p = ml;
|
p = ml;
|
||||||
|
|
||||||
while (xs_list_iter(&p, &v)) {
|
while (xs_list_iter(&p, &v)) {
|
||||||
|
51
xs_regex.h
51
xs_regex.h
@ -4,21 +4,23 @@
|
|||||||
|
|
||||||
#define _XS_REGEX_H
|
#define _XS_REGEX_H
|
||||||
|
|
||||||
d_char *xs_regex_match(char *str, char *rx, int count);
|
d_char *xs_regex_split_n(const char *str, const char *rx, int count);
|
||||||
#define xs_regex_matchall(str, rx) xs_regex_match(str, rx, 0xfffffff)
|
#define xs_regex_split(str, rx) xs_regex_split_n(str, rx, 0xfffffff)
|
||||||
|
d_char *xs_regex_match_n(const char *str, const char *rx, int count);
|
||||||
|
#define xs_regex_match(str, rx) xs_regex_match_n(str, rx, 0xfffffff)
|
||||||
|
|
||||||
#ifdef XS_IMPLEMENTATION
|
#ifdef XS_IMPLEMENTATION
|
||||||
|
|
||||||
#include <regex.h>
|
#include <regex.h>
|
||||||
|
|
||||||
d_char *xs_regex_match(char *str, char *rx, int count)
|
d_char *xs_regex_split_n(const char *str, const char *rx, int count)
|
||||||
/* returns a list with upto count matches */
|
/* splits str by regex */
|
||||||
{
|
{
|
||||||
regex_t re;
|
regex_t re;
|
||||||
regmatch_t rm;
|
regmatch_t rm;
|
||||||
d_char *list = NULL;
|
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
char *p;
|
d_char *list = NULL;
|
||||||
|
const char *p;
|
||||||
|
|
||||||
if (regcomp(&re, rx, REG_EXTENDED))
|
if (regcomp(&re, rx, REG_EXTENDED))
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -26,22 +28,51 @@ d_char *xs_regex_match(char *str, char *rx, int count)
|
|||||||
list = xs_list_new();
|
list = xs_list_new();
|
||||||
|
|
||||||
while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) {
|
while (count > 0 && !regexec(&re, (p = str + offset), 1, &rm, offset > 0 ? REG_NOTBOL : 0)) {
|
||||||
/* add the first part */
|
/* add first the leading part of the string */
|
||||||
list = xs_list_append_m(list, p + rm.rm_so, rm.rm_eo - rm.rm_so);
|
list = xs_list_append_m(list, p, rm.rm_so);
|
||||||
|
|
||||||
/* add the asciiz */
|
|
||||||
list = xs_str_cat(list, "");
|
list = xs_str_cat(list, "");
|
||||||
|
|
||||||
|
/* add now the matched text as the separator */
|
||||||
|
list = xs_list_append_m(list, p + rm.rm_so, rm.rm_eo - rm.rm_so);
|
||||||
|
list = xs_str_cat(list, "");
|
||||||
|
|
||||||
|
/* move forward */
|
||||||
offset += rm.rm_eo;
|
offset += rm.rm_eo;
|
||||||
|
|
||||||
count--;
|
count--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* add the rest of the string */
|
||||||
|
list = xs_list_append(list, p);
|
||||||
|
|
||||||
regfree(&re);
|
regfree(&re);
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
d_char *xs_regex_match_n(const char *str, const char *rx, int count)
|
||||||
|
/* returns a list with upto count matches */
|
||||||
|
{
|
||||||
|
d_char *list = xs_list_new();
|
||||||
|
xs *split = NULL;
|
||||||
|
char *p, *v;
|
||||||
|
int n = 0;
|
||||||
|
|
||||||
|
/* split */
|
||||||
|
p = split = xs_regex_split_n(str, rx, count);
|
||||||
|
|
||||||
|
/* now iterate to get only the 'separators' (odd ones) */
|
||||||
|
while (xs_list_iter(&p, &v)) {
|
||||||
|
if (n & 0x1)
|
||||||
|
list = xs_list_append(list, v);
|
||||||
|
|
||||||
|
n++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
#endif /* XS_IMPLEMENTATION */
|
#endif /* XS_IMPLEMENTATION */
|
||||||
|
|
||||||
#endif /* XS_REGEX_H */
|
#endif /* XS_REGEX_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user