This commit is contained in:
Vitor Santos Costa 2018-05-16 02:58:37 +01:00
parent 67a88a196d
commit d7c4ac95e0
2 changed files with 123 additions and 123 deletions

View File

@ -23,19 +23,19 @@ static char SccsId[] = "%W% %G%";
*
* @namespace prolog
*
*/
*/
/**
* @defgroup Predicates_on_Atoms Predicates on Atoms and Strings
* @ingroup builtins
* @{
*
* @brief The following predicates are used to manipulate atoms, strings, lists of
* codes and lists of chars:
*
* @brief The following predicates are used to manipulate atoms, strings, lists
* of codes and lists of chars:
*
* \toc
*
*/
*/
#define HAS_CACHE_REGS 1
/*
@ -713,18 +713,17 @@ static Int number_chars(USES_REGS1) {
/* ARG1 unbound */
Term t = Deref(ARG2);
Term tf = Yap_ListToNumber(t PASS_REGS);
if (tf)
{
pop_text_stack(l);
return Yap_unify(ARG1, tf);
}
if (tf) {
pop_text_stack(l);
return Yap_unify(ARG1, tf);
}
LOCAL_ActiveError->errorRawTerm = 0;
Yap_ThrowExistingError();
LOCAL_ActiveError->errorRawTerm = 0;
Yap_ThrowExistingError();
return false;
}
return true;
return true;
}
/** @pred number_atom(? _I_,? _A_){te
@ -1326,8 +1325,7 @@ restart_aux:
}
while (t1 != TermNil) {
inpv[i].type = YAP_STRING_ATOM,
inpv[i].val.t = HeadOfTerm(t1);
inpv[i].type = YAP_STRING_ATOM, inpv[i].val.t = HeadOfTerm(t1);
i++;
t1 = TailOfTerm(t1);
}
@ -1365,12 +1363,11 @@ restart_aux:
if (*tailp != TermNil) {
LOCAL_Error_TYPE = TYPE_ERROR_LIST;
} else {
seq_tv_t *inpv = (seq_tv_t *)malloc(n * sizeof(seq_tv_t)), out;
seq_tv_t *inpv = (seq_tv_t *)Malloc(n * sizeof(seq_tv_t));
seq_tv_t *out = (seq_tv_t *)Malloc( sizeof(seq_tv_t));
int i = 0;
if (!inpv) {
LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP;
free(inpv);
goto error;
}
@ -1380,15 +1377,14 @@ restart_aux:
i++;
t1 = TailOfTerm(t1);
}
out.type = YAP_STRING_STRING;
if (!Yap_Concat_Text(n, inpv, &out PASS_REGS)) {
free(inpv);
out->type = YAP_STRING_STRING;
if (!Yap_Concat_Text(n, inpv, out PASS_REGS)) {
goto error;
}
free(inpv);
if (out.val.t) {
if (out->val.t) {
bool rc = Yap_unify(ARG2, out->val.t);
pop_text_stack(l);
return Yap_unify(ARG2, out.val.t);
return rc;
}
}
error:
@ -1417,17 +1413,18 @@ restart_aux:
if (*tailp != TermNil) {
LOCAL_Error_TYPE = TYPE_ERROR_LIST;
} else {
seq_tv_t *inpv = (seq_tv_t *)malloc(n * sizeof(seq_tv_t)), out;
seq_tv_t *inpv = (seq_tv_t *)Malloc(n * sizeof(seq_tv_t));
seq_tv_t *out = (seq_tv_t *)Malloc(sizeof(seq_tv_t));
int i = 0;
Atom at;
if (n == 1) {
bool rc = Yap_unify(ARG2, HeadOfTerm(t1));
pop_text_stack(l);
return Yap_unify(ARG2, HeadOfTerm(t1));
return rc;
}
if (!inpv) {
LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP;
free(inpv);
goto error;
}
@ -1439,16 +1436,15 @@ restart_aux:
i++;
t1 = TailOfTerm(t1);
}
out.type = YAP_STRING_ATOM;
if (!Yap_Concat_Text(n, inpv, &out PASS_REGS)) {
free(inpv);
out->type = YAP_STRING_ATOM;
if (!Yap_Concat_Text(n, inpv, out PASS_REGS)) {
goto error;
}
free(inpv);
at = out.val.a;
pop_text_stack(l);
at = out->val.a;
if (at) {
pop_text_stack(l);
return Yap_unify(ARG2, MkAtomTerm(at));
bool rc = Yap_unify(ARG2, MkAtomTerm(at));
return rc;
}
}
error:
@ -1457,7 +1453,6 @@ error:
goto restart_aux;
}
{
pop_text_stack(l);
return FALSE;
}
}
@ -1473,13 +1468,12 @@ restart_aux:
if (*tailp != TermNil) {
LOCAL_Error_TYPE = TYPE_ERROR_LIST;
} else {
seq_tv_t *inpv = (seq_tv_t *)malloc(n * sizeof(seq_tv_t)), out;
seq_tv_t *inpv = (seq_tv_t *)Malloc(n * sizeof(seq_tv_t)), out;
int i = 0;
Atom at;
if (!inpv) {
LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP;
free(inpv);
goto error;
}
@ -1492,10 +1486,8 @@ restart_aux:
}
out.type = YAP_STRING_STRING;
if (!Yap_Concat_Text(n, inpv, &out PASS_REGS)) {
free(inpv);
goto error;
}
free(inpv);
at = out.val.a;
if (at) {
pop_text_stack(l);
@ -1525,13 +1517,12 @@ restart_aux:
if (*tailp != TermNil) {
LOCAL_Error_TYPE = TYPE_ERROR_LIST;
} else {
seq_tv_t *inpv = (seq_tv_t *)malloc((n * 2 - 1) * sizeof(seq_tv_t)), out;
seq_tv_t *inpv = (seq_tv_t *)Malloc((n * 2 - 1) * sizeof(seq_tv_t)), out;
int i = 0;
Atom at;
if (!inpv) {
LOCAL_Error_TYPE = RESOURCE_ERROR_HEAP;
free(inpv);
goto error;
}
@ -1548,10 +1539,8 @@ restart_aux:
}
out.type = YAP_STRING_STRING;
if (!Yap_Concat_Text(2 * n - 1, inpv, &out PASS_REGS)) {
free(inpv);
goto error;
}
free(inpv);
at = out.val.a;
if (at) {
pop_text_stack(l);
@ -2301,8 +2290,8 @@ static Int cont_sub_atomic(USES_REGS1) {
}
} else if (mask & SUB_ATOM_HAS_SIZE) {
Term nat = build_new_atomic(mask, p, minv, len PASS_REGS);
if (nat == 0)
Yap_ThrowExistingError();
if (nat == 0)
Yap_ThrowExistingError();
Yap_unify(ARG2, MkIntegerTerm(minv));
Yap_unify(ARG4, MkIntegerTerm(after));
Yap_unify(ARG5, nat);
@ -2313,8 +2302,8 @@ static Int cont_sub_atomic(USES_REGS1) {
} else if (mask & SUB_ATOM_HAS_MIN) {
after = sz - (minv + len);
Term nat = build_new_atomic(mask, p, minv, len PASS_REGS);
if (nat == 0)
Yap_ThrowExistingError();
if (nat == 0)
Yap_ThrowExistingError();
Yap_unify(ARG3, MkIntegerTerm(len));
Yap_unify(ARG4, MkIntegerTerm(after));
Yap_unify(ARG5, nat);
@ -2325,8 +2314,8 @@ static Int cont_sub_atomic(USES_REGS1) {
} else if (mask & SUB_ATOM_HAS_AFTER) {
len = sz - (minv + after);
Term nat = build_new_atomic(mask, p, minv, len PASS_REGS);
if (nat == 0)
Yap_ThrowExistingError();
if (nat == 0)
Yap_ThrowExistingError();
Yap_unify(ARG2, MkIntegerTerm(minv));
Yap_unify(ARG3, MkIntegerTerm(len));
Yap_unify(ARG5, nat);
@ -2336,8 +2325,8 @@ static Int cont_sub_atomic(USES_REGS1) {
}
} else {
Term nat = build_new_atomic(mask, p, minv, len PASS_REGS);
if (nat == 0)
Yap_ThrowExistingError();
if (nat == 0)
Yap_ThrowExistingError();
Yap_unify(ARG2, MkIntegerTerm(minv));
Yap_unify(ARG3, MkIntegerTerm(len));
Yap_unify(ARG4, MkIntegerTerm(after));
@ -2483,25 +2472,24 @@ static Int sub_atomic(bool sub_atom, bool sub_string USES_REGS) {
(SUB_ATOM_HAS_MIN | SUB_ATOM_HAS_VAL | SUB_ATOM_HAS_AFTER)) {
const unsigned char *sm;
if (sub_atom)
sm = RepAtom(AtomOfTerm(tout))->UStrOfAE;
sm = RepAtom(AtomOfTerm(tout))->UStrOfAE;
else
sm = UStringOfTerm(tout);
sm = UStringOfTerm(tout);
if (mask & SUB_ATOM_HAS_SIZE) {
if (len != strlen_utf8(sm) ) {
cut_fail();
} else {
len = strlen_utf8(sm);
}
if (len != strlen_utf8(sm)) {
cut_fail();
} else {
len = strlen_utf8(sm);
}
}
if (sz != minv+len+after) {
cut_fail();
if (sz != minv + len + after) {
cut_fail();
}
return do_cut(check_sub_string_at(
minv, p, sm, len));
return do_cut(check_sub_string_at(minv, p, sm, len));
} else if ((mask & (SUB_ATOM_HAS_MIN | SUB_ATOM_HAS_VAL)) ==
(SUB_ATOM_HAS_MIN | SUB_ATOM_HAS_VAL)) {
if (! Yap_unify(ARG4,MkIntegerTerm(sz-minv-len)) )
cut_fail();
(SUB_ATOM_HAS_MIN | SUB_ATOM_HAS_VAL)) {
if (!Yap_unify(ARG4, MkIntegerTerm(sz - minv - len)))
cut_fail();
if (sub_atom)
return do_cut(check_sub_string_at(
minv, p, RepAtom(AtomOfTerm(tout))->UStrOfAE, len));
@ -2509,13 +2497,14 @@ static Int sub_atomic(bool sub_atom, bool sub_string USES_REGS) {
return do_cut(check_sub_string_at(minv, p, UStringOfTerm(tout), len));
} else if ((mask & (SUB_ATOM_HAS_AFTER | SUB_ATOM_HAS_VAL)) ==
(SUB_ATOM_HAS_AFTER | SUB_ATOM_HAS_VAL)) {
if (! Yap_unify(ARG2,MkIntegerTerm(sz-after-len)) )
cut_fail();
if (!Yap_unify(ARG2, MkIntegerTerm(sz - after - len)))
cut_fail();
if (sub_atom) {
return do_cut(check_sub_string_bef(
sz - after, p, RepAtom(AtomOfTerm(tout))->UStrOfAE));
} else {
return do_cut(check_sub_string_bef(sz - after, p, UStringOfTerm(tout)));}
return do_cut(check_sub_string_bef(sz - after, p, UStringOfTerm(tout)));
}
} else if ((mask & (SUB_ATOM_HAS_MIN | SUB_ATOM_HAS_SIZE)) ==
(SUB_ATOM_HAS_MIN | SUB_ATOM_HAS_SIZE)) {
if (minv + len + after > sz) {

121
C/text.c
View File

@ -32,7 +32,9 @@ inline static size_t min_size(size_t i, size_t j) { return (i < j ? i : j); }
#endif
#if !defined(HAVE_STPCPY) && !defined(__APPLE__)
inline static void* __stpcpy(void * i, const void * j) { return strcpy(i,j)+strlen(j);}
inline static void *__stpcpy(void *i, const void *j) {
return strcpy(i, j) + strlen(j);
}
#define stpcpy __stpcpy
#endif
@ -89,7 +91,7 @@ void *pop_output_text_stack__(int i, const void *export) {
while (p) {
struct mblock *np = p->next;
if (p + 1 == export) {
size_t sz = p->sz-sizeof(struct mblock) ;
size_t sz = p->sz - sizeof(struct mblock);
memcpy(p, p + 1, sz);
export = p;
} else {
@ -185,19 +187,19 @@ void *Realloc(void *pt, size_t sz USES_REGS) {
return o + 1;
}
/**
* Export a local memory object as a RO object to the outside world, that is, recovering as much storage as one can.
* Export a local memory object as a RO object to the outside world, that is,
* recovering as much storage as one can.
* @param pt pointer to object
* @return new object
*/
const void *MallocExportAsRO(const void *pt USES_REGS) {
struct mblock *old = (void *)pt, *o = old-1;
if (old == NULL)
return NULL;
struct mblock *old = (void *)pt, *o = old - 1;
if (old == NULL)
return NULL;
size_t sz = o->sz;
release_block(o);
memcpy((void*)o, pt,sz);
memcpy((void *)o, pt, sz);
return realloc((void *)o, sz);
}
@ -258,8 +260,7 @@ static void *codes2buf(Term t0, void *b0, bool *get_codes USES_REGS) {
}
Int code = IntegerOfTerm(hd);
if (code < 0) {
Yap_ThrowError(TYPE_ERROR_CHARACTER_CODE, hd,
"scanning list of codes");
Yap_ThrowError(TYPE_ERROR_CHARACTER_CODE, hd, "scanning list of codes");
return NULL;
}
length += put_utf8(ar, code);
@ -439,8 +440,10 @@ unsigned char *Yap_readText(seq_tv_t *inp USES_REGS) {
}
LOCAL_ActiveError->errorRawTerm = inp->val.t;
}
if (LOCAL_Error_TYPE != YAP_NO_ERROR)
if (LOCAL_Error_TYPE != YAP_NO_ERROR) {
pop_text_stack(lvl);
return NULL;
}
if (IsAtomTerm(inp->val.t) && inp->type & YAP_STRING_ATOM) {
// this is a term, extract to a buffer, and representation is wide
@ -451,8 +454,10 @@ unsigned char *Yap_readText(seq_tv_t *inp USES_REGS) {
memset(o, 0, 4);
return pop_output_text_stack(lvl, o);
}
if (inp->type & YAP_STRING_WITH_BUFFER)
if (inp->type & YAP_STRING_WITH_BUFFER) {
pop_text_stack(lvl);
return at->UStrOfAE;
}
size_t sz = strlen(at->StrOfAE);
inp->type |= YAP_STRING_IN_TMP;
void *o = Malloc(sz + 1);
@ -472,7 +477,7 @@ unsigned char *Yap_readText(seq_tv_t *inp USES_REGS) {
return (unsigned char *)UStringOfTerm(inp->val.t);
inp->type |= YAP_STRING_IN_TMP;
size_t sz = strlen(s);
char *o = BaseMalloc(sz + 1);
char *o = Malloc(sz + 1);
strcpy(o, s);
return pop_output_text_stack(lvl, o);
}
@ -480,17 +485,20 @@ unsigned char *Yap_readText(seq_tv_t *inp USES_REGS) {
(YAP_STRING_CODES | YAP_STRING_ATOMS)) &&
IsPairOrNilTerm(inp->val.t)) {
// Yap_DebugPlWriteln(inp->val.t);
return Yap_ListToBuffer(NULL, inp->val.t, inp PASS_REGS);
return pop_output_text_stack(lvl,
Yap_ListToBuffer(NULL, inp->val.t, inp PASS_REGS) );
// this is a term, extract to a sfer, and representation is wide
}
if (inp->type & YAP_STRING_CODES && IsPairOrNilTerm(inp->val.t)) {
// Yap_DebugPlWriteln(inp->val.t);
return Yap_ListOfCodesToBuffer(NULL, inp->val.t, inp PASS_REGS);
return pop_output_text_stack(lvl,
Yap_ListOfCodesToBuffer(NULL, inp->val.t, inp PASS_REGS));
// this is a term, extract to a sfer, and representation is wide
}
if (inp->type & YAP_STRING_ATOMS && IsPairOrNilTerm(inp->val.t)) {
// Yap_DebugPlWriteln(inp->val.t);
return Yap_ListOfAtomsToBuffer(NULL, inp->val.t, inp PASS_REGS);
return pop_output_text_stack(lvl,
Yap_ListOfAtomsToBuffer(NULL, inp->val.t, inp PASS_REGS));
// this is a term, extract to a buffer, and representation is wide
}
if (inp->type & YAP_STRING_INT && IsIntegerTerm(inp->val.t)) {
@ -508,6 +516,7 @@ unsigned char *Yap_readText(seq_tv_t *inp USES_REGS) {
char *s;
// Yap_DebugPlWriteln(inp->val.t);
if (!Yap_FormatFloat(FloatOfTerm(inp->val.t), &s, 1024)) {
pop_text_stack(lvl);
return NULL;
}
return pop_output_text_stack(lvl, s);
@ -525,7 +534,7 @@ unsigned char *Yap_readText(seq_tv_t *inp USES_REGS) {
#endif
if (inp->type & YAP_STRING_TERM) {
// Yap_DebugPlWriteln(inp->val.t);
char *s = (char *) Yap_TermToBuffer(inp->val.t, ENC_ISO_UTF8, 0);
char *s = (char *)Yap_TermToBuffer(inp->val.t, ENC_ISO_UTF8, 0);
return inp->val.uc = pop_output_text_stack(lvl, s);
}
if (inp->type & YAP_STRING_CHARS) {
@ -562,7 +571,7 @@ static Term write_strings(unsigned char *s0, seq_tv_t *out USES_REGS) {
Term t = init_tstring(PASS_REGS1);
LOCAL_TERM_ERROR(t, 2 * max);
unsigned char *buf = buf_from_tstring(HR);
if (max==0)
if (max == 0)
buf[0] = '\0';
else
strcpy((char *)buf, s);
@ -689,7 +698,7 @@ void *write_buffer(unsigned char *s0, seq_tv_t *out USES_REGS) {
size_t leng = strlen((char *)s0);
size_t min = 0, max = leng;
if (out->enc == ENC_ISO_UTF8) {
if ( out->val.uc == NULL) { // this should always be the case
if (out->val.uc == NULL) { // this should always be the case
out->val.uc = BaseMalloc(leng + 1);
strcpy(out->val.c, (char *)s0);
} else if (out->val.uc != s0) {
@ -760,19 +769,19 @@ static Term write_number(unsigned char *s, seq_tv_t *out,
bool error_on USES_REGS) {
Term t;
LOCAL_delay = !error_on;
t = Yap_StringToNumberTerm((char *)s, &out->enc,error_on);
LOCAL_delay = false;
return t;
t = Yap_StringToNumberTerm((char *)s, &out->enc, error_on);
LOCAL_delay = false;
return t;
}
static Term string_to_term(void *s, seq_tv_t *out USES_REGS) {
Term o;
yap_error_descriptor_t new_error;
bool mdnew = Yap_pushErrorContext(true, &new_error);
o = out->val.t = Yap_BufferToTerm(s, TermNil);
Yap_popErrorContext(mdnew, true);
bool mdnew = Yap_pushErrorContext(true, &new_error);
o = out->val.t = Yap_BufferToTerm(s, TermNil);
Yap_popErrorContext(mdnew, true);
return o;
return o;
}
bool write_Text(unsigned char *inp, seq_tv_t *out USES_REGS) {
@ -878,7 +887,7 @@ static size_t downcase(void *s0, seq_tv_t *out USES_REGS) {
bool Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS) {
unsigned char *buf;
bool rc;
/*
//printf(stderr, "[ %d ", n++) ;
if (inp->type & (YAP_STRING_TERM|YAP_STRING_ATOM|YAP_STRING_ATOMS_CODES
@ -896,34 +905,34 @@ bool Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS) {
return 0L;
}
if (buf[0]) {
size_t leng = strlen_utf8(buf);
if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) {
if (out->max < leng) {
const unsigned char *ptr = skip_utf8(buf, out->max);
size_t diff = (ptr - buf);
char *nbuf = Malloc(diff + 1);
memcpy(nbuf, buf, diff);
nbuf[diff] = '\0';
leng = diff;
size_t leng = strlen_utf8(buf);
if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) {
if (out->max < leng) {
const unsigned char *ptr = skip_utf8(buf, out->max);
size_t diff = (ptr - buf);
char *nbuf = Malloc(diff + 1);
memcpy(nbuf, buf, diff);
nbuf[diff] = '\0';
leng = diff;
}
// else if (out->type & YAP_STRING_NCHARS &&
// const unsigned char *ptr = skip_utf8(buf)
}
// else if (out->type & YAP_STRING_NCHARS &&
// const unsigned char *ptr = skip_utf8(buf)
}
if (out->type & (YAP_STRING_UPCASE | YAP_STRING_DOWNCASE)) {
if (out->type & YAP_STRING_UPCASE) {
if (!upcase(buf, out)) {
pop_text_stack(l);
return false;
if (out->type & (YAP_STRING_UPCASE | YAP_STRING_DOWNCASE)) {
if (out->type & YAP_STRING_UPCASE) {
if (!upcase(buf, out)) {
pop_text_stack(l);
return false;
}
}
if (out->type & YAP_STRING_DOWNCASE) {
if (!downcase(buf, out)) {
pop_text_stack(l);
return false;
}
}
}
if (out->type & YAP_STRING_DOWNCASE) {
if (!downcase(buf, out)) {
pop_text_stack(l);
return false;
}
}
}
}
rc = write_Text(buf, out PASS_REGS);
/* fprintf(stderr, " -> ");
@ -989,9 +998,10 @@ bool Yap_Concat_Text(int tot, seq_tv_t inp[], seq_tv_t *out USES_REGS) {
void **bufv;
unsigned char *buf;
int i, j;
//int lvl = push_text_stack();
bufv = Malloc(tot * sizeof(unsigned char *));
if (!bufv) {
//pop_text_stack(lvl);
return NULL;
}
for (i = 0, j = 0; i < tot; i++) {
@ -999,6 +1009,7 @@ bool Yap_Concat_Text(int tot, seq_tv_t inp[], seq_tv_t *out USES_REGS) {
unsigned char *nbuf = Yap_readText(inp + i PASS_REGS);
if (!nbuf) {
//pop_text_stack(lvl);
return NULL;
}
// if (!nbuf[0])
@ -1014,6 +1025,7 @@ bool Yap_Concat_Text(int tot, seq_tv_t inp[], seq_tv_t *out USES_REGS) {
buf = concat(tot, bufv PASS_REGS);
}
bool rc = write_Text(buf, out PASS_REGS);
//pop_text_stack( lvl );
return rc;
}
@ -1080,8 +1092,8 @@ bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp,
return false;
}
buf0 = slice(0, u_l0, buf PASS_REGS);
buf0 = pop_output_text_stack(lvl, buf0);
bool rc = write_Text(buf0, outv PASS_REGS);
pop_text_stack(lvl);
return rc;
}
}
@ -1095,8 +1107,8 @@ bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp,
if (i > 0 && cuts[i] == 0)
break;
void *bufi = slice(next, cuts[i], buf PASS_REGS);
bufi = pop_output_text_stack(lvl, bufi);
if (!write_Text(bufi, outv + i PASS_REGS)) {
pop_text_stack(lvl);
return false;
}
}
@ -1105,7 +1117,6 @@ bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp,
return true;
}
/**
* Convert from a predicate structure to an UTF-8 string of the form
*