This commit is contained in:
Vitor Santos Costa 2017-09-19 14:23:20 +01:00
parent 27fdeb81cc
commit 4d1c031051

141
C/text.c
View File

@ -271,8 +271,9 @@ st += put_utf8(st,ch);
return -TYPE_ERROR_LIST; return -TYPE_ERROR_LIST;
} }
st[0] = '\0'; st[0] = '\0';
Malloc((st - st0) + 1);
*tailp = l; *tailp = l;
*atoms = length;
length = (st-st0);
return length; return length;
} }
@ -294,9 +295,9 @@ static unsigned char *latin2utf8(seq_tv_t *inp, size_t *lengp) {
} }
*pt++ = '\0'; *pt++ = '\0';
return buf; return buf;
} }
static unsigned char *wchar2utf8(seq_tv_t *inp, size_t *lengp) { static unsigned char *wchar2utf8(seq_tv_t *inp, size_t *lengp) {
*lengp = wcslen(inp->val.w); *lengp = wcslen(inp->val.w);
size_t sz = *lengp * 4; size_t sz = *lengp * 4;
wchar_t *b0 = inp->val.w; wchar_t *b0 = inp->val.w;
@ -308,11 +309,11 @@ static unsigned char *wchar2utf8(seq_tv_t *inp, size_t *lengp) {
pt += put_utf8(pt, ch); pt += put_utf8(pt, ch);
*pt++ = '\0'; *pt++ = '\0';
return buf; return buf;
} }
static void *slice(size_t min, size_t max, const unsigned char *buf USES_REGS); static void *slice(size_t min, size_t max, const unsigned char *buf USES_REGS);
static unsigned char *to_buffer(unsigned char *buf, Term t, seq_tv_t *inp, static unsigned char *to_buffer(unsigned char *buf, Term t, seq_tv_t *inp,
bool *widep, Int *atoms, bool *widep, Int *atoms,
size_t *lenp USES_REGS) { size_t *lenp USES_REGS) {
CELL *r = NULL; CELL *r = NULL;
@ -329,31 +330,31 @@ static unsigned char *to_buffer(unsigned char *buf, Term t, seq_tv_t *inp,
} }
*lenp = n; *lenp = n;
return bufc; return bufc;
} }
static unsigned char *Yap_ListOfCodesToBuffer(unsigned char *buf, Term t, static unsigned char *Yap_ListOfCodesToBuffer(unsigned char *buf, Term t,
seq_tv_t *inp, bool *widep, seq_tv_t *inp, bool *widep,
size_t *lenp USES_REGS) { size_t *lenp USES_REGS) {
Int atoms = 1; // we only want lists of atoms Int atoms = 1; // we only want lists of atoms
return to_buffer(buf, t, inp, widep, &atoms, lenp PASS_REGS); return to_buffer(buf, t, inp, widep, &atoms, lenp PASS_REGS);
} }
static unsigned char *Yap_ListOfAtomsToBuffer(unsigned char *buf, Term t, static unsigned char *Yap_ListOfAtomsToBuffer(unsigned char *buf, Term t,
seq_tv_t *inp, bool *widep, seq_tv_t *inp, bool *widep,
size_t *lenp USES_REGS) { size_t *lenp USES_REGS) {
Int atoms = 2; // we only want lists of integer codes Int atoms = 2; // we only want lists of integer codes
return to_buffer(buf, t, inp, widep, &atoms, lenp PASS_REGS); return to_buffer(buf, t, inp, widep, &atoms, lenp PASS_REGS);
} }
static unsigned char *Yap_ListToBuffer(unsigned char *buf, Term t, static unsigned char *Yap_ListToBuffer(unsigned char *buf, Term t,
seq_tv_t *inp, bool *widep, seq_tv_t *inp, bool *widep,
size_t *lenp USES_REGS) { size_t *lenp USES_REGS) {
Int atoms = 0; // we accept both types of lists. Int atoms = 0; // we accept both types of lists.
return to_buffer(buf, t, inp, widep, &atoms, lenp PASS_REGS); return to_buffer(buf, t, inp, widep, &atoms, lenp PASS_REGS);
} }
#if USE_GEN_TYPE_ERROR #if USE_GEN_TYPE_ERROR
static yap_error_number gen_type_error(int flags) { static yap_error_number gen_type_error(int flags) {
if ((flags & (YAP_STRING_STRING | YAP_STRING_ATOM | YAP_STRING_INT | if ((flags & (YAP_STRING_STRING | YAP_STRING_ATOM | YAP_STRING_INT |
YAP_STRING_FLOAT | YAP_STRING_ATOMS_CODES | YAP_STRING_BIG)) == YAP_STRING_FLOAT | YAP_STRING_ATOMS_CODES | YAP_STRING_BIG)) ==
(YAP_STRING_STRING | YAP_STRING_ATOM | YAP_STRING_INT | YAP_STRING_FLOAT | (YAP_STRING_STRING | YAP_STRING_ATOM | YAP_STRING_INT | YAP_STRING_FLOAT |
@ -374,12 +375,12 @@ static yap_error_number gen_type_error(int flags) {
if (flags & (YAP_STRING_CODES | YAP_STRING_ATOMS)) if (flags & (YAP_STRING_CODES | YAP_STRING_ATOMS))
return TYPE_ERROR_LIST; return TYPE_ERROR_LIST;
return TYPE_ERROR_NUMBER; return TYPE_ERROR_NUMBER;
} }
#endif #endif
// static int cnt; // static int cnt;
unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp) { unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp) {
unsigned char *s0 = NULL; unsigned char *s0 = NULL;
bool wide; bool wide;
@ -513,9 +514,9 @@ unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp) {
return wchar2utf8(inp, lengp); return wchar2utf8(inp, lengp);
} }
return NULL; return NULL;
} }
static Term write_strings(unsigned char *s0, seq_tv_t *out, static Term write_strings(unsigned char *s0, seq_tv_t *out,
size_t leng USES_REGS) { size_t leng USES_REGS) {
size_t min = 0, max = leng; size_t min = 0, max = leng;
@ -544,9 +545,9 @@ static Term write_strings(unsigned char *s0, seq_tv_t *out,
out->val.t = t; out->val.t = t;
return out->val.t; return out->val.t;
} }
static Term write_atoms(void *s0, seq_tv_t *out, size_t leng USES_REGS) { static Term write_atoms(void *s0, seq_tv_t *out, size_t leng USES_REGS) {
Term t = AbsPair(HR); Term t = AbsPair(HR);
size_t sz = 0; size_t sz = 0;
size_t max = leng; size_t max = leng;
@ -595,9 +596,9 @@ static Term write_atoms(void *s0, seq_tv_t *out, size_t leng USES_REGS) {
} }
out->val.t = t; out->val.t = t;
return (t); return (t);
} }
static Term write_codes(void *s0, seq_tv_t *out, size_t leng USES_REGS) { static Term write_codes(void *s0, seq_tv_t *out, size_t leng USES_REGS) {
Term t = AbsPair(HR); Term t = AbsPair(HR);
size_t sz = 0; size_t sz = 0;
size_t max = leng; size_t max = leng;
@ -641,9 +642,9 @@ static Term write_codes(void *s0, seq_tv_t *out, size_t leng USES_REGS) {
} }
out->val.t = t; out->val.t = t;
return (t); return (t);
} }
static Atom write_atom(void *s0, seq_tv_t *out, size_t leng USES_REGS) { static Atom write_atom(void *s0, seq_tv_t *out, size_t leng USES_REGS) {
unsigned char *s = s0; unsigned char *s = s0;
int32_t ch; int32_t ch;
if (leng == 0) { if (leng == 0) {
@ -657,9 +658,9 @@ static Atom write_atom(void *s0, seq_tv_t *out, size_t leng USES_REGS) {
memcpy(buf, s0, n + 1); memcpy(buf, s0, n + 1);
return Yap_ULookupAtom(buf); return Yap_ULookupAtom(buf);
} }
} }
size_t write_buffer(unsigned char *s0, seq_tv_t *out, size_t leng USES_REGS) { size_t write_buffer(unsigned char *s0, seq_tv_t *out, size_t leng USES_REGS) {
size_t min = 0, max = leng, room_end; size_t min = 0, max = leng, room_end;
if (out->enc == ENC_ISO_UTF8) { if (out->enc == ENC_ISO_UTF8) {
room_end = strlen((char *)s0) + 1; room_end = strlen((char *)s0) + 1;
@ -725,29 +726,29 @@ size_t write_buffer(unsigned char *s0, seq_tv_t *out, size_t leng USES_REGS) {
room_end = -1; room_end = -1;
} }
return room_end; return room_end;
} }
static size_t write_length(const unsigned char *s0, seq_tv_t *out, static size_t write_length(const unsigned char *s0, seq_tv_t *out,
size_t leng USES_REGS) { size_t leng USES_REGS) {
return leng; return leng;
} }
static Term write_number(unsigned char *s, seq_tv_t *out, int size, static Term write_number(unsigned char *s, seq_tv_t *out, int size,
bool error_on USES_REGS) { bool error_on USES_REGS) {
Term t; Term t;
int i = push_text_stack(); int i = push_text_stack();
t = Yap_StringToNumberTerm((char *)s, &out->enc, error_on); t = Yap_StringToNumberTerm((char *)s, &out->enc, error_on);
pop_text_stack(i); pop_text_stack(i);
return t; return t;
} }
static Term string_to_term(void *s, seq_tv_t *out, size_t leng USES_REGS) { static Term string_to_term(void *s, seq_tv_t *out, size_t leng USES_REGS) {
Term o; Term o;
o = out->val.t = Yap_BufferToTerm(s, strlen(s) + 1, TermNil); o = out->val.t = Yap_BufferToTerm(s, strlen(s) + 1, TermNil);
return o; return o;
} }
bool write_Text(unsigned char *inp, seq_tv_t *out, size_t leng USES_REGS) { bool write_Text(unsigned char *inp, seq_tv_t *out, size_t leng USES_REGS) {
/* we know what the term is */ /* we know what the term is */
if (out->type == 0) { if (out->type == 0) {
return true; return true;
@ -816,9 +817,9 @@ bool write_Text(unsigned char *inp, seq_tv_t *out, size_t leng USES_REGS) {
default: { return true; } default: { return true; }
} }
return false; return false;
} }
static size_t upcase(void *s0, seq_tv_t *out USES_REGS) { static size_t upcase(void *s0, seq_tv_t *out USES_REGS) {
unsigned char *s = s0; unsigned char *s = s0;
while (*s) { while (*s) {
@ -829,9 +830,9 @@ static size_t upcase(void *s0, seq_tv_t *out USES_REGS) {
s += put_utf8(s, chr); s += put_utf8(s, chr);
} }
return true; return true;
} }
static size_t downcase(void *s0, seq_tv_t *out USES_REGS) { static size_t downcase(void *s0, seq_tv_t *out USES_REGS) {
unsigned char *s = s0; unsigned char *s = s0;
while (*s) { while (*s) {
@ -842,9 +843,9 @@ static size_t downcase(void *s0, seq_tv_t *out USES_REGS) {
s += put_utf8(s, chr); s += put_utf8(s, chr);
} }
return true; return true;
} }
bool Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS) { bool Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS) {
unsigned char *buf; unsigned char *buf;
bool rc; bool rc;
@ -857,7 +858,7 @@ bool Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS) {
else if (inp->type & YAP_STRING_WCHARS) fprintf(stderr,"S %S\n", inp->val else if (inp->type & YAP_STRING_WCHARS) fprintf(stderr,"S %S\n", inp->val
.w); .w);
else fprintf(stderr,"s %s\n", inp->val.c); else fprintf(stderr,"s %s\n", inp->val.c);
*/ */
// cnt++; // cnt++;
buf = Yap_readText(inp, &leng PASS_REGS); buf = Yap_readText(inp, &leng PASS_REGS);
if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) { if (out->type & (YAP_STRING_NCHARS | YAP_STRING_TRUNC)) {
@ -903,10 +904,10 @@ bool Yap_CVT_Text(seq_tv_t *inp, seq_tv_t *out USES_REGS) {
fprintf(stderr, "%s", out->val.c); fprintf(stderr, "%s", out->val.c);
fprintf(stderr, "\n]\n"); */ fprintf(stderr, "\n]\n"); */
return rc; return rc;
} }
static unsigned char *concat(int n, void *sv[] USES_REGS) { static unsigned char *concat(int n, void *sv[] USES_REGS) {
void *buf; void *buf;
unsigned char *buf0; unsigned char *buf0;
size_t room = 0; size_t room = 0;
@ -926,9 +927,9 @@ static unsigned char *concat(int n, void *sv[] USES_REGS) {
#endif #endif
} }
return buf0; return buf0;
} }
static void *slice(size_t min, size_t max, const unsigned char *buf USES_REGS) { static void *slice(size_t min, size_t max, const unsigned char *buf USES_REGS) {
unsigned char *nbuf = Malloc((max - min) * 4 + 1); unsigned char *nbuf = Malloc((max - min) * 4 + 1);
const unsigned char *ptr = skip_utf8(buf, min); const unsigned char *ptr = skip_utf8(buf, min);
unsigned char *nptr = nbuf; unsigned char *nptr = nbuf;
@ -940,11 +941,11 @@ static void *slice(size_t min, size_t max, const unsigned char *buf USES_REGS) {
} }
nptr[0] = '\0'; nptr[0] = '\0';
return nbuf; return nbuf;
} }
// //
// Out must be an atom or a string // Out must be an atom or a string
bool Yap_Concat_Text(int tot, seq_tv_t inp[], seq_tv_t *out USES_REGS) { bool Yap_Concat_Text(int tot, seq_tv_t inp[], seq_tv_t *out USES_REGS) {
void **bufv; void **bufv;
unsigned char *buf; unsigned char *buf;
int i; int i;
@ -968,10 +969,10 @@ bool Yap_Concat_Text(int tot, seq_tv_t inp[], seq_tv_t *out USES_REGS) {
bool rc = write_Text(buf, out, strlen_utf8(buf) PASS_REGS); bool rc = write_Text(buf, out, strlen_utf8(buf) PASS_REGS);
return rc; return rc;
} }
// //
bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp, bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp,
seq_tv_t outv[] USES_REGS) { seq_tv_t outv[] USES_REGS) {
const unsigned char *buf; const unsigned char *buf;
size_t b_l, u_l; size_t b_l, u_l;
@ -1038,24 +1039,24 @@ bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp,
} }
return true; return true;
} }
/** /**
* Function to convert a generic text term (string, atom, list of codes, list * Function to convert a generic text term (string, atom, list of codes, list
of< of<
atoms) into a buff atoms) into a buff
er. er.
* *
* @param t the term * @param t the term
* @param buf the buffer, if NULL a buffer is malloced, and the user should * @param buf the buffer, if NULL a buffer is malloced, and the user should
reclai it reclai it
* @param len buffer size * @param len buffer size
* @param enc encoding (UTF-8 is strongly recommended) * @param enc encoding (UTF-8 is strongly recommended)
* *
* @return the buffer, or NULL in case of failure. If so, Yap_Error may be * @return the buffer, or NULL in case of failure. If so, Yap_Error may be
called. called.
*/ */
const char *Yap_TextTermToText(Term t, char *buf, size_t len, encoding_t enc) { const char *Yap_TextTermToText(Term t, char *buf, size_t len, encoding_t enc) {
CACHE_REGS CACHE_REGS
seq_tv_t inp, out; seq_tv_t inp, out;
inp.val.t = t; inp.val.t = t;
@ -1077,9 +1078,9 @@ const char *Yap_TextTermToText(Term t, char *buf, size_t len, encoding_t enc) {
if (!Yap_CVT_Text(&inp, &out PASS_REGS)) if (!Yap_CVT_Text(&inp, &out PASS_REGS))
return NULL; return NULL;
return out.val.c; return out.val.c;
} }
/** /**
* Convert from a predicate structure to an UTF-8 string of the form * Convert from a predicate structure to an UTF-8 string of the form
* *
* module:name/arity. * module:name/arity.
@ -1090,7 +1091,7 @@ const char *Yap_TextTermToText(Term t, char *buf, size_t len, encoding_t enc) {
* *
* @return the temporary string * @return the temporary string
*/ */
const char *Yap_PredIndicatorToUTF8String(PredEntry *ap) { const char *Yap_PredIndicatorToUTF8String(PredEntry *ap) {
CACHE_REGS CACHE_REGS
Atom at; Atom at;
arity_t arity = 0; arity_t arity = 0;
@ -1144,18 +1145,18 @@ const char *Yap_PredIndicatorToUTF8String(PredEntry *ap) {
s += strlen(s); s += strlen(s);
snprintf(s, smax - s, "/%" PRIdPTR, arity); snprintf(s, smax - s, "/%" PRIdPTR, arity);
return s0; return s0;
} }
/** /**
* Convert from a text buffer (8-bit) to a term that has the same type as * Convert from a text buffer (8-bit) to a term that has the same type as
* _Tguide_ * _Tguide_
* *
* @param s the buffer * @param s the buffer
* @param tguide the guide * @param tguide the guide
* *
* @return the term * @return the term
*/ */
Term Yap_MkTextTerm(const char *s, encoding_t enc, Term tguide) { Term Yap_MkTextTerm(const char *s, encoding_t enc, Term tguide) {
CACHE_REGS CACHE_REGS
if (IsAtomTerm(tguide)) if (IsAtomTerm(tguide))
return MkAtomTerm(Yap_LookupAtom(s)); return MkAtomTerm(Yap_LookupAtom(s));
@ -1165,4 +1166,4 @@ Term Yap_MkTextTerm(const char *s, encoding_t enc, Term tguide) {
return Yap_CharsToListOfAtoms(s, enc PASS_REGS); return Yap_CharsToListOfAtoms(s, enc PASS_REGS);
} }
return Yap_CharsToListOfCodes(s, enc PASS_REGS); return Yap_CharsToListOfCodes(s, enc PASS_REGS);
} }