shrub/jets/e/rexp.c

139 lines
3.1 KiB
C
Raw Normal View History

2014-04-09 22:57:48 +04:00
/* j/5/rexp.c
**
*/
#include "all.h"
2014-09-04 07:10:43 +04:00
2014-04-09 22:57:48 +04:00
#include "cre2.h"
#include <string.h>
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3qe_rexp(u3_noun lub, u3_noun rad)
2014-04-09 22:57:48 +04:00
{
2014-11-06 03:20:01 +03:00
c3_y* lub_y = u3r_tape(lub);
c3_y* rad_y = u3r_tape(rad);
2014-04-09 22:57:48 +04:00
2014-09-06 00:13:24 +04:00
u3k(lub);
2014-11-06 22:13:57 +03:00
int lub_l = u3kb_lent(lub);
2014-04-10 01:22:08 +04:00
if (lub_l != strlen((char *)lub_y)) {
2014-04-09 22:57:48 +04:00
free(lub_y);
free(rad_y);
2014-09-06 00:13:24 +04:00
return u3_nul;
2014-04-09 22:57:48 +04:00
}
char* rec = (char*)lub_y;
char* end;
while(*rec != 0) {
if(*rec > 127) {
free(lub_y);
free(rad_y);
2014-09-06 00:13:24 +04:00
return u3_nul;
2014-04-09 22:57:48 +04:00
}
else if(*rec == '\\') {
rec++;
2014-04-09 22:57:48 +04:00
switch (*rec) {
case 'P':
case 'p':
free(lub_y);
free(rad_y);
2014-09-06 00:13:24 +04:00
return u3_nul;
2014-04-09 22:57:48 +04:00
case 'Q':
end = strstr(rec, "\\E");
if(end == NULL) rec += strlen(rec) - 1;
else rec = end;
}
}
else if(*rec == '(') {
rec++;
if(*rec == '?') {
rec++;
if(*rec != ':') {
free(lub_y);
free(rad_y);
2014-09-06 00:13:24 +04:00
return u3_nul;
2014-04-09 22:57:48 +04:00
}
rec++;
}
}
else
2014-04-09 22:57:48 +04:00
rec++;
}
2014-04-09 22:57:48 +04:00
cre2_regexp_t * rex;
cre2_options_t * opt;
2014-04-09 22:57:48 +04:00
opt = cre2_opt_new();
if (opt) {
cre2_opt_set_log_errors(opt, 0);
cre2_opt_set_encoding(opt, CRE2_UTF8);
cre2_opt_set_perl_classes(opt, 1);
cre2_opt_set_one_line(opt, 1);
cre2_opt_set_longest_match(opt, 1);
rex = cre2_new((const char *)lub_y, strlen((char *)lub_y), opt);
if (rex) {
if (!cre2_error_code(rex)) {
int text_len = strlen((char *)rad_y);
int captures = cre2_num_capturing_groups(rex);
cre2_string_t matches[captures+1];
int match = cre2_match(rex, (const char*)rad_y, text_len, 0, text_len, CRE2_UNANCHORED, matches, captures+1);
2014-04-09 22:57:48 +04:00
if (!match) {
// No matches
cre2_opt_delete(opt);
cre2_delete(rex);
free(lub_y);
2014-04-09 22:57:48 +04:00
free(rad_y);
2014-11-06 03:20:01 +03:00
return u3i_cell(u3_nul, u3_nul);
2014-04-09 22:57:48 +04:00
}
2014-09-06 00:13:24 +04:00
u3_noun map = u3_nul;
2014-04-09 22:57:48 +04:00
int i;
for (i = 0; i < captures+1; i++) {
char * buf = malloc(matches[i].length + 1);
memcpy(buf, matches[i].data, matches[i].length);
buf[matches[i].length] = 0;
2014-11-06 22:13:57 +03:00
map = u3kdb_put(map, i, u3i_tape(buf));
2014-04-09 22:57:48 +04:00
free(buf);
}
cre2_opt_delete(opt);
cre2_delete(rex);
free(lub_y);
2014-04-09 22:57:48 +04:00
free(rad_y);
2014-11-06 03:20:01 +03:00
return u3i_cell(u3_nul, u3i_cell(u3_nul, map));
2014-04-09 22:57:48 +04:00
}
else {
// Compiling the regular expression failed
cre2_opt_delete(opt);
cre2_delete(rex);
free(lub_y);
2014-04-09 22:57:48 +04:00
free(rad_y);
2014-09-06 00:13:24 +04:00
return u3_nul;
2014-04-09 22:57:48 +04:00
}
cre2_delete(rex);
}
cre2_opt_delete(opt);
}
free(lub_y);
free(rad_y);
2014-11-06 03:20:01 +03:00
u3m_bail(c3__exit);
2014-09-06 00:13:24 +04:00
return u3_nul;
2014-04-09 22:57:48 +04:00
}
2014-09-06 00:13:24 +04:00
u3_noun
2014-11-06 22:13:57 +03:00
u3we_rexp(u3_noun cor)
2014-04-09 22:57:48 +04:00
{
2014-09-06 00:13:24 +04:00
u3_noun lub;
u3_noun rad;
2014-04-09 22:57:48 +04:00
2014-11-18 00:56:51 +03:00
if ( (u3_none == (lub = u3r_at(u3x_sam_2, cor))) ||
(u3_none == (rad = u3r_at(u3x_sam_3, cor))) )
2014-04-09 22:57:48 +04:00
{
2014-11-06 03:20:01 +03:00
return u3m_bail(c3__fail);
2014-04-09 22:57:48 +04:00
} else {
2014-11-06 22:13:57 +03:00
return u3qe_rexp(lub, rad);
2014-04-09 22:57:48 +04:00
}
}