mirror of
https://github.com/carp-lang/Carp.git
synced 2024-09-11 05:25:28 +03:00
core: pattern functions are now prefixed
This commit is contained in:
parent
dac3bad33a
commit
fa41129ff3
@ -31,7 +31,7 @@
|
||||
#define CAP_UNFINISHED (-1)
|
||||
#define CAP_NONE (-2)
|
||||
|
||||
typedef struct MatchState {
|
||||
typedef struct PatternMatchState {
|
||||
string src_init; /* init of source string */
|
||||
string src_end; /* end ('\0') of source string */
|
||||
string p_end; /* end ('\0') of pattern */
|
||||
@ -41,10 +41,10 @@ typedef struct MatchState {
|
||||
string init;
|
||||
ptrdiff_t len;
|
||||
} capture[CARP_MAXCAPTURES];
|
||||
} MatchState;
|
||||
} PatternMatchState;
|
||||
|
||||
/* recursive function */
|
||||
string match(MatchState *ms, string s, string p);
|
||||
string Pattern_internal_match(PatternMatchState *ms, string s, string p);
|
||||
|
||||
/* maximum recursion depth for 'match' */
|
||||
#if !defined(MAXCCALLS)
|
||||
@ -62,7 +62,7 @@ int carp_regerror(const char* fmt, ...) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int check_capture(MatchState *ms, int l) {
|
||||
int Pattern_internal_check_capture(PatternMatchState *ms, int l) {
|
||||
l -= '1';
|
||||
if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) {
|
||||
return carp_regerror("invalid capture index %c%d", C_ESC, l + 1);
|
||||
@ -70,7 +70,7 @@ int check_capture(MatchState *ms, int l) {
|
||||
return l;
|
||||
}
|
||||
|
||||
int capture_to_close(MatchState *ms) {
|
||||
int Pattern_internal_capture_to_close(PatternMatchState *ms) {
|
||||
int level = ms->level;
|
||||
for (level--; level>=0; level--) {
|
||||
if (ms->capture[level].len == CAP_UNFINISHED) return level;
|
||||
@ -78,7 +78,7 @@ int capture_to_close(MatchState *ms) {
|
||||
return carp_regerror("invalid pattern capture");
|
||||
}
|
||||
|
||||
string classend(MatchState *ms, string p) {
|
||||
string Pattern_internal_classend(PatternMatchState *ms, string p) {
|
||||
switch (*p++) {
|
||||
case C_ESC: {
|
||||
if (p == ms->p_end) carp_regerror("malformed pattern (ends with '%c')", C_ESC);
|
||||
@ -98,7 +98,7 @@ string classend(MatchState *ms, string p) {
|
||||
}
|
||||
}
|
||||
|
||||
int match_class(int c, int cl) {
|
||||
int Pattern_internal_match_class(int c, int cl) {
|
||||
int res;
|
||||
switch (tolower(cl)) {
|
||||
case 'a' : res = isalpha(c); break;
|
||||
@ -116,7 +116,7 @@ int match_class(int c, int cl) {
|
||||
return (islower(cl) ? res : !res);
|
||||
}
|
||||
|
||||
int matchbracketclass(int c, string p, string ec) {
|
||||
int Pattern_internal_matchbracketclass(int c, string p, string ec) {
|
||||
int sig = 1;
|
||||
if (*(p+1) == '^') {
|
||||
sig = 0;
|
||||
@ -125,7 +125,7 @@ int matchbracketclass(int c, string p, string ec) {
|
||||
while (++p < ec) {
|
||||
if (*p == C_ESC) {
|
||||
p++;
|
||||
if (match_class(c, uchar(*p))) return sig;
|
||||
if (Pattern_internal_match_class(c, uchar(*p))) return sig;
|
||||
}
|
||||
else if ((*(p+1) == '-') && (p+2 < ec)) {
|
||||
p+=2;
|
||||
@ -136,22 +136,22 @@ int matchbracketclass(int c, string p, string ec) {
|
||||
return !sig;
|
||||
}
|
||||
|
||||
int singlematch(MatchState *ms, string s, string p,
|
||||
string ep) {
|
||||
int Pattern_internal_singlematch(PatternMatchState *ms, string s, string p,
|
||||
string ep) {
|
||||
if (s >= ms->src_end) {
|
||||
return 0;
|
||||
} else {
|
||||
int c = uchar(*s);
|
||||
switch (*p) {
|
||||
case '.': return 1; /* matches any char */
|
||||
case C_ESC: return match_class(c, uchar(*(p+1)));
|
||||
case '[': return matchbracketclass(c, p, ep-1);
|
||||
case C_ESC: return Pattern_internal_match_class(c, uchar(*(p+1)));
|
||||
case '[': return Pattern_internal_matchbracketclass(c, p, ep-1);
|
||||
default: return (uchar(*p) == c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string matchbalance(MatchState *ms, string s, string p) {
|
||||
string Pattern_internal_matchbalance(PatternMatchState *ms, string s, string p) {
|
||||
if (p >= ms->p_end - 1) carp_regerror("malformed pattern (missing arguments to '%cb')", C_ESC);
|
||||
if (*s != *p) {
|
||||
return NULL;
|
||||
@ -171,52 +171,54 @@ string matchbalance(MatchState *ms, string s, string p) {
|
||||
return NULL; /* string ends out of balance */
|
||||
}
|
||||
|
||||
string max_expand(MatchState *ms, string s, string p,
|
||||
string ep) {
|
||||
string Pattern_internal_max_expand(PatternMatchState *ms, string s, string p,
|
||||
string ep) {
|
||||
ptrdiff_t i = 0; /* counts maximum expand for item */
|
||||
while (singlematch(ms, s + i, p, ep)) i++;
|
||||
while (Pattern_internal_singlematch(ms, s + i, p, ep)) i++;
|
||||
/* keeps trying to match with the maximum repetitions */
|
||||
while (i>=0) {
|
||||
string res = match(ms, (s+i), ep+1);
|
||||
string res = Pattern_internal_match(ms, (s+i), ep+1);
|
||||
if (res) return res;
|
||||
i--; /* else didn't match; reduce 1 repetition to try again */
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
string min_expand(MatchState *ms, string s, string p,
|
||||
string ep) {
|
||||
string Pattern_internal_min_expand(PatternMatchState *ms, string s, string p,
|
||||
string ep) {
|
||||
for (;;) {
|
||||
string res = match(ms, s, ep+1);
|
||||
string res = Pattern_internal_match(ms, s, ep+1);
|
||||
if (res) return res;
|
||||
else if (singlematch(ms, s, p, ep)) s++; /* try with one more repetition */
|
||||
else if (Pattern_internal_singlematch(ms, s, p, ep)) s++; /* try with one more repetition */
|
||||
else return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
string start_capture(MatchState *ms, string s, string p,
|
||||
int what) {
|
||||
string Pattern_internal_start_capture(PatternMatchState *ms, string s, string p,
|
||||
int what) {
|
||||
string res;
|
||||
int level = ms->level;
|
||||
if (level >= CARP_MAXCAPTURES) carp_regerror("too many captures");
|
||||
ms->capture[level].init = s;
|
||||
ms->capture[level].len = what;
|
||||
ms->level = level+1;
|
||||
if (!(res=match(ms, s, p))) ms->level--; /* undo capture on failed match */
|
||||
if (!(res=Pattern_internal_match(ms, s, p))) ms->level--; /* undo capture on failed match */
|
||||
return res;
|
||||
}
|
||||
|
||||
string end_capture(MatchState *ms, string s, string p) {
|
||||
int l = capture_to_close(ms);
|
||||
string Pattern_internal_end_capture(PatternMatchState *ms, string s, string p) {
|
||||
int l = Pattern_internal_capture_to_close(ms);
|
||||
string res;
|
||||
ms->capture[l].len = s - ms->capture[l].init; /* close capture */
|
||||
if (!(res = match(ms, s, p))) ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
|
||||
if (!(res=Pattern_internal_match(ms, s, p))) {
|
||||
ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
string match_capture (MatchState *ms, string s, int l) {
|
||||
string Pattern_internal_match_capture(PatternMatchState *ms, string s, int l) {
|
||||
size_t len;
|
||||
l = check_capture(ms, l);
|
||||
l = Pattern_internal_check_capture(ms, l);
|
||||
len = ms->capture[l].len;
|
||||
if ((size_t)(ms->src_end-s) >= len && !memcmp(ms->capture[l].init, s, len)) {
|
||||
return s+len;
|
||||
@ -224,18 +226,19 @@ string match_capture (MatchState *ms, string s, int l) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
string match(MatchState *ms, string s, string p) {
|
||||
string Pattern_internal_match(PatternMatchState *ms, string s, string p) {
|
||||
if (ms->matchdepth-- == 0) carp_regerror("pattern too complex");
|
||||
init: /* using goto's to optimize tail recursion */
|
||||
if (p != ms->p_end) { /* end of pattern? */
|
||||
switch (*p) {
|
||||
case '(': { /* start capture */
|
||||
if (*(p + 1) == ')') s = start_capture(ms, s, p + 2, CAP_NONE);
|
||||
else s = start_capture(ms, s, p + 1, CAP_UNFINISHED);
|
||||
if (*(p + 1) == ')') s = Pattern_internal_start_capture(ms, s, p + 2,
|
||||
CAP_NONE);
|
||||
else s = Pattern_internal_start_capture(ms, s, p + 1, CAP_UNFINISHED);
|
||||
break;
|
||||
}
|
||||
case ')': { /* end capture */
|
||||
s = end_capture(ms, s, p + 1);
|
||||
s = Pattern_internal_end_capture(ms, s, p + 1);
|
||||
break;
|
||||
}
|
||||
case '$': {
|
||||
@ -246,7 +249,7 @@ string match(MatchState *ms, string s, string p) {
|
||||
case C_ESC: { /* escaped sequences not in the format class[*+?-]? */
|
||||
switch (*(p + 1)) {
|
||||
case 'b': { /* balanced string? */
|
||||
s = matchbalance(ms, s, p + 2);
|
||||
s = Pattern_internal_matchbalance(ms, s, p + 2);
|
||||
if (s) {
|
||||
p += 4; goto init; /* return match(ms, s, p + 4); */
|
||||
} /* else fail (s == NULL) */
|
||||
@ -256,10 +259,10 @@ string match(MatchState *ms, string s, string p) {
|
||||
string ep; char previous;
|
||||
p += 2;
|
||||
if (*p != '[') carp_regerror("missing '[' after '%cf' in pattern", C_ESC);
|
||||
ep = classend(ms, p); /* points to what is next */
|
||||
ep = Pattern_internal_classend(ms, p); /* points to what is next */
|
||||
previous = (s == ms->src_init) ? '\0' : *(s - 1);
|
||||
if (!matchbracketclass(uchar(previous), p, ep - 1) &&
|
||||
matchbracketclass(uchar(*s), p, ep - 1)) {
|
||||
if (!Pattern_internal_matchbracketclass(uchar(previous), p, ep - 1) &&
|
||||
Pattern_internal_matchbracketclass(uchar(*s), p, ep - 1)) {
|
||||
p = ep; goto init; /* return match(ms, s, ep); */
|
||||
}
|
||||
s = NULL; /* match failed */
|
||||
@ -279,7 +282,7 @@ string match(MatchState *ms, string s, string p) {
|
||||
case '0': case '1': case '2': case '3':
|
||||
case '4': case '5': case '6': case '7':
|
||||
case '8': case '9': { /* capture results (\0-\9)? */
|
||||
s = match_capture(ms, s, uchar(*(p + 1)));
|
||||
s = Pattern_internal_match_capture(ms, s, uchar(*(p + 1)));
|
||||
if (s) {
|
||||
p += 2; goto init; /* return match(ms, s, p + 2) */
|
||||
}
|
||||
@ -290,9 +293,9 @@ string match(MatchState *ms, string s, string p) {
|
||||
break;
|
||||
}
|
||||
default: dflt: { /* pattern class plus optional suffix */
|
||||
string ep = classend(ms, p); /* points to optional suffix */
|
||||
string ep = Pattern_internal_classend(ms, p); /* points to optional suffix */
|
||||
/* does not match at least once? */
|
||||
if (!singlematch(ms, s, p, ep)) {
|
||||
if (!Pattern_internal_singlematch(ms, s, p, ep)) {
|
||||
if (*ep == '*' || *ep == '?' || *ep == '-') { /* accept empty? */
|
||||
p = ep + 1; goto init; /* return match(ms, s, ep + 1); */
|
||||
}
|
||||
@ -304,7 +307,7 @@ string match(MatchState *ms, string s, string p) {
|
||||
switch (*ep) { /* handle optional suffix */
|
||||
case '?': { /* optional */
|
||||
string res;
|
||||
if ((res = match(ms, s + 1, ep + 1))) {
|
||||
if ((res = Pattern_internal_match(ms, s + 1, ep + 1))) {
|
||||
s = res;
|
||||
} else {
|
||||
p = ep + 1; goto init; /* else return match(ms, s, ep + 1); */
|
||||
@ -315,10 +318,10 @@ string match(MatchState *ms, string s, string p) {
|
||||
s++; /* 1 match already done */
|
||||
/* FALLTHROUGH */
|
||||
case '*': /* 0 or more repetitions */
|
||||
s = max_expand(ms, s, p, ep);
|
||||
s = Pattern_internal_max_expand(ms, s, p, ep);
|
||||
break;
|
||||
case '-': /* 0 or more repetitions (minimum) */
|
||||
s = min_expand(ms, s, p, ep);
|
||||
s = Pattern_internal_min_expand(ms, s, p, ep);
|
||||
break;
|
||||
default: /* no suffix */
|
||||
s++; p = ep; goto init; /* return match(ms, s + 1, ep); */
|
||||
@ -332,8 +335,7 @@ string match(MatchState *ms, string s, string p) {
|
||||
return s;
|
||||
}
|
||||
|
||||
string lmemfind(string s1, size_t l1, string s2,
|
||||
size_t l2) {
|
||||
string Pattern_internal_lmemfind(string s1, size_t l1, string s2, size_t l2) {
|
||||
if (l2 == 0) return s1; /* empty strings are everywhere */
|
||||
if (l2 > l1) return NULL; /* avoids a negative 'l1' */
|
||||
string init; /* to search for a '*s2' inside 's1' */
|
||||
@ -361,37 +363,39 @@ string String_copy_len(string s, int len) {
|
||||
return ptr;
|
||||
}
|
||||
|
||||
Array push_string(Array a, string s, int i, int len) {
|
||||
Array Array_push_string(Array a, string s, int i, int len) {
|
||||
((string*)a.data)[i] = String_copy_len(s, len);
|
||||
return a;
|
||||
}
|
||||
|
||||
Array push_onecapture(MatchState *ms, int i, string s, string e,
|
||||
Array captures) {
|
||||
Array Pattern_internal_push_onecapture(PatternMatchState *ms, int i, string s,
|
||||
string e, Array captures) {
|
||||
if (i >= ms->level) {
|
||||
if (!i) return push_string(captures, s, i, ms->capture[i].len); /* add whole match */
|
||||
if (!i) return Array_push_string(captures, s, i, ms->capture[i].len); /* add whole match */
|
||||
else carp_regerror("invalid capture index %cd", C_ESC, i + 1);
|
||||
}
|
||||
else {
|
||||
ptrdiff_t l = ms->capture[i].len;
|
||||
if (l == CAP_UNFINISHED) carp_regerror("unfinished capture");
|
||||
else if (l != CAP_NONE) return push_string(captures, ms->capture[i].init, i, ms->capture[i].len);
|
||||
else if (l != CAP_NONE) return Array_push_string(captures, ms->capture[i].init,
|
||||
i, ms->capture[i].len);
|
||||
}
|
||||
return captures;
|
||||
}
|
||||
|
||||
Array push_captures(MatchState *ms, string s, string e) {
|
||||
Array Pattern_internal_push_captures(PatternMatchState *ms, string s, string e) {
|
||||
int i;
|
||||
int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
|
||||
Array res;
|
||||
res.len = nlevels;
|
||||
res.data = CARP_MALLOC(nlevels*sizeof(string));
|
||||
for (i = 0; i < nlevels; i++) push_onecapture(ms, i, s, e, res);
|
||||
for (i = 0; i < nlevels; i++) Pattern_internal_push_onecapture(ms, i, s, e,
|
||||
res);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* check whether pattern has no special characters */
|
||||
int nospecials(string p, size_t l) {
|
||||
int Pattern_internal_nospecials(string p, size_t l) {
|
||||
size_t upto = 0;
|
||||
do {
|
||||
if (strpbrk(p + upto, SPECIALS)) return 0; /* pattern has a special character */
|
||||
@ -400,15 +404,15 @@ int nospecials(string p, size_t l) {
|
||||
return 1; /* no special chars found */
|
||||
}
|
||||
|
||||
void prepstate(MatchState *ms, string s, size_t ls, string p,
|
||||
size_t lp) {
|
||||
void Pattern_internal_prepstate(PatternMatchState *ms, string s, size_t ls,
|
||||
string p, size_t lp) {
|
||||
ms->matchdepth = MAXCCALLS;
|
||||
ms->src_init = s;
|
||||
ms->src_end = s + ls;
|
||||
ms->p_end = p + lp;
|
||||
}
|
||||
|
||||
void reprepstate(MatchState *ms) {
|
||||
void Pattern_internal_reprepstate(PatternMatchState *ms) {
|
||||
ms->level = 0;
|
||||
assert(ms->matchdepth == MAXCCALLS);
|
||||
}
|
||||
@ -419,23 +423,23 @@ int Pattern_find(string* p, string* s) {
|
||||
int lstr = strlen(str);
|
||||
int lpat = strlen(pat);
|
||||
/* explicit request or no special characters? */
|
||||
if (nospecials(pat, lpat)) {
|
||||
if (Pattern_internal_nospecials(pat, lpat)) {
|
||||
/* do a plain search */
|
||||
string s2 = lmemfind(str, lstr, pat, lpat);
|
||||
string s2 = Pattern_internal_lmemfind(str, lstr, pat, lpat);
|
||||
if (!s2) return -1;
|
||||
return s2-str;
|
||||
}
|
||||
MatchState ms;
|
||||
PatternMatchState ms;
|
||||
string s1 = str;
|
||||
int anchor = (*pat == '^');
|
||||
if (anchor) {
|
||||
pat++; lpat--; /* skip anchor character */
|
||||
}
|
||||
prepstate(&ms, str, lstr, pat, lpat);
|
||||
Pattern_internal_prepstate(&ms, str, lstr, pat, lpat);
|
||||
do {
|
||||
string res;
|
||||
reprepstate(&ms);
|
||||
if ((res=match(&ms, s1, pat))) return s1 - str;
|
||||
Pattern_internal_reprepstate(&ms);
|
||||
if ((res=Pattern_internal_match(&ms, s1, pat))) return s1 - str;
|
||||
} while (s1++ < ms.src_end && !anchor);
|
||||
return -1;
|
||||
}
|
||||
@ -445,17 +449,19 @@ Array Pattern_match(string* p, string* s) {
|
||||
string pat = *p;
|
||||
int lstr = strlen(str);
|
||||
int lpat = strlen(pat);
|
||||
MatchState ms;
|
||||
PatternMatchState ms;
|
||||
string s1 = str;
|
||||
int anchor = (*pat == '^');
|
||||
if (anchor) {
|
||||
pat++; lpat--; /* skip anchor character */
|
||||
}
|
||||
prepstate(&ms, str, lstr, pat, lpat);
|
||||
Pattern_internal_prepstate(&ms, str, lstr, pat, lpat);
|
||||
do {
|
||||
string res;
|
||||
reprepstate(&ms);
|
||||
if ((res=match(&ms, s1, pat))) return push_captures(&ms, s1, res);
|
||||
Pattern_internal_reprepstate(&ms);
|
||||
if ((res=Pattern_internal_match(&ms, s1, pat))) {
|
||||
return Pattern_internal_push_captures(&ms, s1, res);
|
||||
}
|
||||
} while (s1++ < ms.src_end && !anchor);
|
||||
Array a;
|
||||
a.len = 0;
|
||||
@ -468,17 +474,17 @@ string Pattern_match_MINUS_str(string* p, string* s) {
|
||||
string pat = *p;
|
||||
int lstr = strlen(str);
|
||||
int lpat = strlen(pat);
|
||||
MatchState ms;
|
||||
PatternMatchState ms;
|
||||
string s1 = str;
|
||||
int anchor = (*pat == '^');
|
||||
if (anchor) {
|
||||
pat++; lpat--; /* skip anchor character */
|
||||
}
|
||||
prepstate(&ms, str, lstr, pat, lpat);
|
||||
Pattern_internal_prepstate(&ms, str, lstr, pat, lpat);
|
||||
do {
|
||||
string res;
|
||||
reprepstate(&ms);
|
||||
if ((res=match(&ms, s1, pat))) {
|
||||
Pattern_internal_reprepstate(&ms);
|
||||
if ((res=Pattern_internal_match(&ms, s1, pat))) {
|
||||
int start = (s1 - str) + 1;
|
||||
int end = res - str + 1;
|
||||
int len = end - start;
|
||||
@ -492,35 +498,36 @@ string Pattern_match_MINUS_str(string* p, string* s) {
|
||||
}
|
||||
|
||||
/* state for 'gmatch' */
|
||||
typedef struct GMatchState {
|
||||
typedef struct PatternGMatchState {
|
||||
string src; /* current position */
|
||||
string pat; /* pattern */
|
||||
string lastmatch; /* end of last match */
|
||||
MatchState ms; /* match state */
|
||||
} GMatchState;
|
||||
PatternMatchState ms; /* match state */
|
||||
} PatternGMatchState;
|
||||
|
||||
typedef struct GMatchRes {
|
||||
typedef struct PatternGMatchRes {
|
||||
bool valid;
|
||||
Array data;
|
||||
} GMatchRes;
|
||||
} PatternGMatchRes;
|
||||
|
||||
|
||||
GMatchRes gmatch_aux(GMatchState* gm) {
|
||||
PatternGMatchRes Pattern_internal_gmatch_aux(PatternGMatchState* gm) {
|
||||
string src;
|
||||
Array a;
|
||||
for (src = gm->src; src <= gm->ms.src_end; src++) {
|
||||
string e;
|
||||
reprepstate(&gm->ms);
|
||||
if ((e = match(&gm->ms, src, gm->pat)) && e != gm->lastmatch) {
|
||||
Pattern_internal_reprepstate(&gm->ms);
|
||||
if ((e = Pattern_internal_match(&gm->ms, src, gm->pat)) &&
|
||||
e != gm->lastmatch) {
|
||||
gm->src = gm->lastmatch = e;
|
||||
a = push_captures(&gm->ms, src, e);
|
||||
return (GMatchRes){.valid=true, .data=a};
|
||||
a = Pattern_internal_push_captures(&gm->ms, src, e);
|
||||
return (PatternGMatchRes){.valid=true, .data=a};
|
||||
}
|
||||
}
|
||||
return (GMatchRes){.valid=false, .data=a}; /* not found */
|
||||
return (PatternGMatchRes){.valid=false, .data=a}; /* not found */
|
||||
}
|
||||
|
||||
Array push_back(Array res, Array tmp) {
|
||||
Array Array_push_back(Array res, Array tmp) {
|
||||
res.len++;
|
||||
res.data = realloc(res.data, res.len*sizeof(Array));
|
||||
((Array*)res.data)[res.len-1] = tmp;
|
||||
@ -532,21 +539,21 @@ Array Pattern_global_MINUS_match(string* p, string* s) {
|
||||
string pat = *p;
|
||||
int lstr = strlen(str);
|
||||
int lpat = strlen(pat);
|
||||
GMatchState gm;
|
||||
prepstate(&gm.ms, str, lstr, pat, lpat);
|
||||
PatternGMatchState gm;
|
||||
Pattern_internal_prepstate(&gm.ms, str, lstr, pat, lpat);
|
||||
gm.src = str; gm.pat = pat; gm.lastmatch = NULL;
|
||||
Array res;
|
||||
res.len = 0;
|
||||
res.data = NULL;
|
||||
GMatchRes tmp = gmatch_aux(&gm);
|
||||
PatternGMatchRes tmp = Pattern_internal_gmatch_aux(&gm);
|
||||
while (tmp.valid) {
|
||||
res = push_back(res, tmp.data);
|
||||
tmp = gmatch_aux(&gm);
|
||||
res = Array_push_back(res, tmp.data);
|
||||
tmp = Pattern_internal_gmatch_aux(&gm);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
string add_char(string a, char b) {
|
||||
string Pattern_internal_add_char(string a, char b) {
|
||||
if (!a) {
|
||||
string buffer = CARP_MALLOC(2);
|
||||
snprintf(buffer, 2, "%c", b);
|
||||
@ -560,21 +567,24 @@ string add_char(string a, char b) {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
string add_value(MatchState *ms, string res, string src, string e, string tr) {
|
||||
string Pattern_internal_add_value(PatternMatchState *ms, string res, string src,
|
||||
string e, string tr) {
|
||||
size_t l, i;
|
||||
l = strlen(tr);
|
||||
for (i = 0; i < l; i++) {
|
||||
if (tr[i] != C_ESC) res = add_char(res, tr[i]);
|
||||
if (tr[i] != C_ESC) res = Pattern_internal_add_char(res, tr[i]);
|
||||
else {
|
||||
i++; /* skip ESC */
|
||||
if (!isdigit(uchar(tr[i]))) {
|
||||
if (tr[i] != C_ESC) carp_regerror( "invalid use of '%c' in replacement string", C_ESC);
|
||||
res = add_char(res, tr[i]);
|
||||
if (tr[i] != C_ESC) {
|
||||
carp_regerror( "invalid use of '%c' in replacement string", C_ESC);
|
||||
}
|
||||
res = Pattern_internal_add_char(res, tr[i]);
|
||||
}
|
||||
else if (tr[i] == '0') res = String_append(res, src);
|
||||
else {
|
||||
Array a = {.len = 0, .data = NULL};
|
||||
push_onecapture(ms, tr[i] - '1', src, e, a);
|
||||
Pattern_internal_push_onecapture(ms, tr[i] - '1', src, e, a);
|
||||
res = String_append(res, ((string*)a.data)[0]); /* add capture to accumulated result */
|
||||
}
|
||||
}
|
||||
@ -591,21 +601,21 @@ string Pattern_substitute(string* p, string *s, string *t, int ns) {
|
||||
string lastmatch = NULL; /* end of last match */
|
||||
int anchor = (*pat == '^');
|
||||
string res = NULL;
|
||||
MatchState ms;
|
||||
PatternMatchState ms;
|
||||
int n = 0;
|
||||
if (anchor) {
|
||||
pat++; lpat--; /* skip anchor character */
|
||||
}
|
||||
prepstate(&ms, str, lstr, pat, lpat);
|
||||
Pattern_internal_prepstate(&ms, str, lstr, pat, lpat);
|
||||
while (n < ns || ns == -1) {
|
||||
string e;
|
||||
reprepstate(&ms); /* (re)prepare state for new match */
|
||||
if ((e = match(&ms, str, pat)) && e != lastmatch) { /* match? */
|
||||
Pattern_internal_reprepstate(&ms); /* (re)prepare state for new match */
|
||||
if ((e = Pattern_internal_match(&ms, str, pat)) && e != lastmatch) { /* match? */
|
||||
n++;
|
||||
res = add_value(&ms, res, str, e, tr); /* add replacement to buffer */
|
||||
res = Pattern_internal_add_value(&ms, res, str, e, tr); /* add replacement to buffer */
|
||||
str = lastmatch = e;
|
||||
}
|
||||
else if (str < ms.src_end) res = add_char(res, *str++);
|
||||
else if (str < ms.src_end) res = Pattern_internal_add_char(res, *str++);
|
||||
else break; /* end of subject */
|
||||
if (anchor) break;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user