2014-04-09 22:57:48 +04:00
|
|
|
/* j/5/repg.c
|
|
|
|
**
|
|
|
|
** This file is in the public domain.
|
|
|
|
*/
|
|
|
|
#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
|
|
|
|
u3_cqe_repg(u3_noun lub, u3_noun rad, u3_noun rep)
|
2014-04-09 22:57:48 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
c3_y* lub_y = u3_cr_tape(lub);
|
|
|
|
c3_y* rad_y = u3_cr_tape(rad);
|
|
|
|
c3_y* rep_y = u3_cr_tape(rep);
|
2014-04-09 22:57:48 +04:00
|
|
|
|
2014-04-22 05:22:11 +04:00
|
|
|
|
2014-04-09 22:57:48 +04:00
|
|
|
char* rec = (char*)lub_y;
|
|
|
|
char* end;
|
|
|
|
while(*rec != 0) {
|
|
|
|
if(*rec == '\\') {
|
2014-04-22 05:22:11 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
rec++;
|
|
|
|
}
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
2014-04-22 05:22:11 +04:00
|
|
|
else
|
2014-04-09 22:57:48 +04:00
|
|
|
rec++;
|
|
|
|
}
|
2014-04-22 05:22:11 +04:00
|
|
|
|
2014-04-09 22:57:48 +04:00
|
|
|
cre2_regexp_t * rex;
|
|
|
|
cre2_options_t * opt;
|
2014-04-22 05:22:11 +04:00
|
|
|
|
2014-04-09 22:57:48 +04:00
|
|
|
opt = cre2_opt_new();
|
|
|
|
if (opt) {
|
|
|
|
cre2_opt_set_log_errors(opt, 0);
|
2014-06-10 22:02:33 +04:00
|
|
|
cre2_opt_set_encoding(opt, CRE2_UTF8);
|
2014-04-09 22:57:48 +04:00
|
|
|
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);
|
|
|
|
cre2_string_t matches[1];
|
|
|
|
int ic = 0;
|
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun ret = u3_nul;
|
2014-04-09 22:57:48 +04:00
|
|
|
while (ic <= text_len) {
|
|
|
|
int match = cre2_match(rex, (const char*)rad_y, text_len, ic, text_len, CRE2_ANCHOR_START, matches, 1);
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
if(rad_y[ic])
|
2014-09-06 00:13:24 +04:00
|
|
|
ret = u3_ci_cell((c3_y)rad_y[ic], ret);
|
2014-04-09 22:57:48 +04:00
|
|
|
ic++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
int mlen = matches[0].length;
|
|
|
|
if (mlen == 0) {
|
2014-09-06 00:13:24 +04:00
|
|
|
ret = u3_ckb_weld(u3_ckb_flop(u3_ci_tape((char *) rad_y+ic)), u3_ckb_flop(u3_ci_tape((char *)rep_y)));
|
2014-04-09 22:57:48 +04:00
|
|
|
ic = text_len + 1;
|
|
|
|
}
|
|
|
|
else {
|
2014-09-06 00:13:24 +04:00
|
|
|
ret = u3_ckb_weld(u3_ckb_flop(u3_ci_tape((char *)rep_y)), ret);
|
2014-04-09 22:57:48 +04:00
|
|
|
ic += mlen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cre2_opt_delete(opt);
|
|
|
|
cre2_delete(rex);
|
|
|
|
free(lub_y);
|
|
|
|
free(rad_y);
|
|
|
|
free(rep_y);
|
2014-09-06 00:13:24 +04:00
|
|
|
return u3_ci_cell(u3_nul, u3_ckb_flop(ret));
|
2014-04-09 22:57:48 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Compiling the regular expression failed
|
|
|
|
cre2_opt_delete(opt);
|
|
|
|
cre2_delete(rex);
|
|
|
|
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
|
|
|
}
|
|
|
|
cre2_opt_delete(opt);
|
|
|
|
cre2_delete(rex);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// rex Allocation Error
|
|
|
|
cre2_opt_delete(opt);
|
|
|
|
free(lub_y);
|
|
|
|
free(rad_y);
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_cm_bail(c3__exit);
|
2014-04-09 22:57:48 +04:00
|
|
|
}
|
|
|
|
cre2_opt_delete(opt);
|
|
|
|
}
|
|
|
|
// opt Allocation Error
|
|
|
|
free(lub_y);
|
|
|
|
free(rad_y);
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_cm_bail(c3__exit);
|
|
|
|
return u3_nul;
|
2014-04-09 22:57:48 +04:00
|
|
|
}
|
|
|
|
|
2014-09-06 00:13:24 +04:00
|
|
|
u3_noun
|
|
|
|
u3_cwe_repg(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;
|
|
|
|
u3_noun rep;
|
2014-04-09 22:57:48 +04:00
|
|
|
|
2014-11-06 02:36:30 +03:00
|
|
|
if ( (u3_none == (lub = u3_cr_at(u3_cv_sam_2, cor))) ||
|
|
|
|
(u3_none == (rad = u3_cr_at(u3_cv_sam_6, cor))) ||
|
|
|
|
(u3_none == (rep = u3_cr_at(u3_cv_sam_7, cor))) )
|
2014-04-09 22:57:48 +04:00
|
|
|
{
|
2014-09-06 00:13:24 +04:00
|
|
|
return u3_cm_bail(c3__fail);
|
2014-04-09 22:57:48 +04:00
|
|
|
} else {
|
2014-09-06 00:13:24 +04:00
|
|
|
return u3_cqe_repg(lub, rad, rep);
|
2014-04-09 22:57:48 +04:00
|
|
|
}
|
|
|
|
}
|