regexp core-dump (obs from Ryszard Szopa)
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@2163 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
parent
fa73e89807
commit
84c15fda0b
14
docs/yap.tex
14
docs/yap.tex
@ -9813,19 +9813,19 @@ according to options @var{Opts}. The options may be:
|
||||
be treated as lower case during the matching process.
|
||||
@end itemize
|
||||
|
||||
@item regexp(+@var{RegExp},+@var{String},+@var{Opts},@var{SubMatchVars})
|
||||
@item regexp(+@var{RegExp},+@var{String},+@var{Opts},?@var{SubMatchVars})
|
||||
@findex regexp/4
|
||||
@snindex regexp/4
|
||||
@cnindex regexp/4
|
||||
|
||||
Match regular expression @var{RegExp} to input string @var{String}
|
||||
according to options @var{Opts}. The variable @var{SubMatchVars} should
|
||||
be originally a list of unbound variables all will contain a sequence of
|
||||
matches, that is, the head of @var{SubMatchVars} will contain the
|
||||
characters in @var{String} that matched the leftmost parenthesized
|
||||
subexpression within @var{RegExp}, the next head of list will contain
|
||||
the characters that matched the next parenthesized subexpression to the
|
||||
right in @var{RegExp}, and so on.
|
||||
be originally unbound or a list of unbound variables all will contain a
|
||||
sequence of matches, that is, the head of @var{SubMatchVars} will
|
||||
contain the characters in @var{String} that matched the leftmost
|
||||
parenthesized subexpression within @var{RegExp}, the next head of list
|
||||
will contain the characters that matched the next parenthesized
|
||||
subexpression to the right in @var{RegExp}, and so on.
|
||||
|
||||
The options may be:
|
||||
@itemize @bullet
|
||||
|
@ -88,12 +88,13 @@ static int regexp(void)
|
||||
char *buf, *sbuf;
|
||||
regex_t reg;
|
||||
int out;
|
||||
long int nmatch = YAP_IntOfTerm(YAP_ARG7);
|
||||
size_t nmatch;
|
||||
regmatch_t *pmatch;
|
||||
long int tout;
|
||||
int yap_flags = YAP_IntOfTerm(YAP_ARG5);
|
||||
int regcomp_flags = REG_EXTENDED;
|
||||
|
||||
|
||||
if ((buf = (char *)YAP_AllocSpaceFromYap(buflen)) == NULL) {
|
||||
/* early exit */
|
||||
return(FALSE);
|
||||
@ -106,8 +107,15 @@ static int regexp(void)
|
||||
if (yap_flags & 1)
|
||||
regcomp_flags |= REG_ICASE;
|
||||
/* cool, now I have my string in the buffer, let's have some fun */
|
||||
if (yap_regcomp(®,buf, regcomp_flags) != 0)
|
||||
if (yap_regcomp(®,buf, regcomp_flags) != 0) {
|
||||
YAP_FreeSpaceFromYap(buf);
|
||||
return(FALSE);
|
||||
}
|
||||
if (YAP_IsVarTerm(YAP_ARG7)) {
|
||||
nmatch = reg.re_nsub;
|
||||
} else {
|
||||
nmatch = YAP_IntOfTerm(YAP_ARG7);
|
||||
}
|
||||
if ((sbuf = (char *)YAP_AllocSpaceFromYap(sbuflen)) == NULL) {
|
||||
/* early exit */
|
||||
yap_regfree(®);
|
||||
@ -121,36 +129,37 @@ static int regexp(void)
|
||||
YAP_FreeSpaceFromYap(sbuf);
|
||||
return(FALSE);
|
||||
}
|
||||
pmatch = YAP_AllocSpaceFromYap(sizeof(regmatch_t)*nmatch);
|
||||
out = yap_regexec(®,sbuf,(int)nmatch,pmatch,0);
|
||||
pmatch = YAP_AllocSpaceFromYap(sizeof(regmatch_t)*(nmatch+1));
|
||||
out = yap_regexec(®,sbuf,nmatch+1,pmatch,0);
|
||||
if (out == 0) {
|
||||
/* match succeed, let's fill the match in */
|
||||
long int i;
|
||||
YAP_Term TNil = YAP_MkAtomTerm(YAP_LookupAtom("[]"));
|
||||
YAP_Functor FDiff = YAP_MkFunctor(YAP_LookupAtom("-"),2);
|
||||
|
||||
tout = YAP_ARG6;
|
||||
for (i = 0; i < nmatch; i++) {
|
||||
tout = TNil;
|
||||
for (i = nmatch-1; i >= 0; --i) {
|
||||
int j;
|
||||
YAP_Term t = TNil;
|
||||
|
||||
if (pmatch[i].rm_so == -1) break;
|
||||
if (yap_flags & 2) {
|
||||
YAP_Term to[2];
|
||||
to[0] = YAP_MkIntTerm(pmatch[i].rm_so);
|
||||
to[1] = YAP_MkIntTerm(pmatch[i].rm_eo);
|
||||
t = YAP_MkApplTerm(FDiff,2,to);
|
||||
} else {
|
||||
for (j = pmatch[i].rm_eo-1; j >= pmatch[i].rm_so; j--) {
|
||||
t = YAP_MkPairTerm(YAP_MkIntTerm(sbuf[j]),t);
|
||||
if (pmatch[i].rm_so != -1) {
|
||||
if (yap_flags & 2) {
|
||||
YAP_Term to[2];
|
||||
to[0] = YAP_MkIntTerm(pmatch[i].rm_so);
|
||||
to[1] = YAP_MkIntTerm(pmatch[i].rm_eo);
|
||||
t = YAP_MkApplTerm(FDiff,2,to);
|
||||
} else {
|
||||
for (j = pmatch[i].rm_eo-1; j >= pmatch[i].rm_so; j--) {
|
||||
t = YAP_MkPairTerm(YAP_MkIntTerm(sbuf[j]),t);
|
||||
}
|
||||
}
|
||||
tout = YAP_MkPairTerm(t,tout);
|
||||
}
|
||||
YAP_Unify(t,YAP_HeadOfTerm(tout));
|
||||
tout = YAP_TailOfTerm(tout);
|
||||
}
|
||||
out = !YAP_Unify(tout, YAP_ARG6);
|
||||
}
|
||||
else if (out != REG_NOMATCH) {
|
||||
return(FALSE);
|
||||
out = 0;
|
||||
}
|
||||
yap_regfree(®);
|
||||
YAP_FreeSpaceFromYap(buf);
|
||||
|
@ -39,8 +39,7 @@ regexp(RegExp, String, Opts, OUT) :-
|
||||
% OUT must be bound to a list of unbound variables.
|
||||
% Check this and count how many.
|
||||
%
|
||||
check_out(V,_,_,G) :- var(V), !,
|
||||
throw(error(instantiation_error,G)).
|
||||
check_out(V,_,_,G) :- var(V), !.
|
||||
check_out([],I,I,_) :- !.
|
||||
check_out([V|L],I0,IF,G) :- !,
|
||||
(nonvar(V) -> throw(error(type_error(variable,V),G)) ; true),
|
||||
|
Reference in New Issue
Block a user