merge text to term routines
This commit is contained in:
86
C/text.c
86
C/text.c
@@ -51,9 +51,7 @@ typedef struct TextBuffer_manager {
|
||||
int lvl;
|
||||
} text_buffer_t;
|
||||
|
||||
int push_text_stack(USES_REGS1) {
|
||||
return LOCAL_TextBuffer->lvl++;
|
||||
}
|
||||
int push_text_stack(USES_REGS1) { return LOCAL_TextBuffer->lvl++; }
|
||||
|
||||
int pop_text_stack(int i) {
|
||||
int lvl = LOCAL_TextBuffer->lvl;
|
||||
@@ -81,9 +79,9 @@ void *Malloc(size_t sz USES_REGS) {
|
||||
sz = ALIGN_BY_TYPE(sz + sizeof(struct mblock), CELL);
|
||||
struct mblock *o = malloc(sz);
|
||||
o->prev = LOCAL_TextBuffer->last[lvl];
|
||||
if (o->prev) {
|
||||
o->prev->next = o;
|
||||
}
|
||||
if (o->prev) {
|
||||
o->prev->next = o;
|
||||
}
|
||||
if (LOCAL_TextBuffer->first[lvl]) {
|
||||
LOCAL_TextBuffer->last[lvl] = o;
|
||||
} else {
|
||||
@@ -206,7 +204,7 @@ static Int SkipListCodes(unsigned char **bufp, Term *l, Term **tailp,
|
||||
(*atoms)++;
|
||||
if (*atoms < length) {
|
||||
*tailp = l;
|
||||
return -REPRESENTATION_ERROR_CHARACTER_CODE;
|
||||
return -REPRESENTATION_ERROR_CHARACTER_CODE;
|
||||
} else {
|
||||
AtomEntry *ae = RepAtom(AtomOfTerm(hd));
|
||||
if ((ae->StrOfAE)[1] != '\0') {
|
||||
@@ -386,35 +384,32 @@ unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp) {
|
||||
// this is a term, extract to a buffer, and representation is wide
|
||||
// Yap_DebugPlWriteln(inp->val.t);
|
||||
Atom at = AtomOfTerm(inp->val.t);
|
||||
if (lengp)
|
||||
*lengp = strlen_utf8(at->UStrOfAE);
|
||||
if (lengp)
|
||||
*lengp = strlen_utf8(at->UStrOfAE);
|
||||
return at->UStrOfAE;
|
||||
}
|
||||
if (IsStringTerm(inp->val.t) && inp->type & YAP_STRING_STRING) {
|
||||
// this is a term, extract to a buffer, and representation is wide
|
||||
// Yap_DebugPlWriteln(inp->val.t);
|
||||
if (lengp)
|
||||
*lengp = strlen_utf8(UStringOfTerm(inp->val.t));
|
||||
return (unsigned char *)UStringOfTerm(inp->val.t);
|
||||
if (lengp)
|
||||
*lengp = strlen_utf8(UStringOfTerm(inp->val.t));
|
||||
return (unsigned char *)UStringOfTerm(inp->val.t);
|
||||
}
|
||||
if (((inp->type & (YAP_STRING_CODES | YAP_STRING_ATOMS)) ==
|
||||
(YAP_STRING_CODES | YAP_STRING_ATOMS)) &&
|
||||
IsPairOrNilTerm(inp->val.t)) {
|
||||
// Yap_DebugPlWriteln(inp->val.t);
|
||||
return
|
||||
Yap_ListToBuffer(s0, inp->val.t, inp, &wide, lengp PASS_REGS);
|
||||
return Yap_ListToBuffer(s0, inp->val.t, inp, &wide, lengp 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(s0, inp->val.t, inp, &wide,
|
||||
lengp PASS_REGS);
|
||||
return Yap_ListOfCodesToBuffer(s0, inp->val.t, inp, &wide, lengp 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(s0, inp->val.t, inp, &wide,
|
||||
lengp PASS_REGS);
|
||||
return Yap_ListOfAtomsToBuffer(s0, inp->val.t, inp, &wide, lengp PASS_REGS);
|
||||
// this is a term, extract to a buffer, and representation is wide
|
||||
}
|
||||
if (inp->type & YAP_STRING_INT && IsIntegerTerm(inp->val.t)) {
|
||||
@@ -434,17 +429,21 @@ unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp) {
|
||||
}
|
||||
if (inp->type & YAP_STRING_FLOAT && IsFloatTerm(inp->val.t)) {
|
||||
char *s;
|
||||
size_t sz = 1024;
|
||||
size_t sz = 1024;
|
||||
// Yap_DebugPlWriteln(inp->val.t);
|
||||
if (s0)
|
||||
{ s = (char *)s0; sz = strlen(s);}
|
||||
else
|
||||
if (s0) {
|
||||
s = (char *)s0;
|
||||
sz = strlen(s);
|
||||
} else
|
||||
s = Malloc(sz);
|
||||
if (!s)
|
||||
AUX_ERROR(inp->val.t, MaxTmp(PASS_REGS1), s, char);
|
||||
if (!s)
|
||||
AUX_ERROR(inp->val.t, MaxTmp(PASS_REGS1), s, char);
|
||||
while (!Yap_FormatFloat(FloatOfTerm(inp->val.t), &s, sz - 1)) {
|
||||
if (s0) { s = Malloc(sz=1024); s0 = NULL; }
|
||||
else s = Realloc(s, sz+1024);
|
||||
if (s0) {
|
||||
s = Malloc(sz = 1024);
|
||||
s0 = NULL;
|
||||
} else
|
||||
s = Realloc(s, sz + 1024);
|
||||
}
|
||||
*lengp = strlen(s);
|
||||
return inp->val.uc = (unsigned char *)s;
|
||||
@@ -634,11 +633,11 @@ static Term write_codes(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;
|
||||
int32_t ch;
|
||||
if ( leng == 0) {
|
||||
return Yap_LookupAtom("");
|
||||
}
|
||||
if ( strlen_utf8(s0) <= leng) {
|
||||
return Yap_LookupAtom(s0);
|
||||
if (leng == 0) {
|
||||
return Yap_LookupAtom("");
|
||||
}
|
||||
if (strlen_utf8(s0) <= leng) {
|
||||
return Yap_LookupAtom(s0);
|
||||
} else {
|
||||
size_t n = get_utf8(s, 1, &ch);
|
||||
unsigned char *buf = Malloc(n + 1);
|
||||
@@ -720,18 +719,18 @@ static size_t write_length(const unsigned char *s0, seq_tv_t *out,
|
||||
return leng;
|
||||
}
|
||||
|
||||
static Term write_number(unsigned char *s, seq_tv_t *out, int size, bool error_on USES_REGS) {
|
||||
static Term write_number(unsigned char *s, seq_tv_t *out, int size,
|
||||
bool error_on USES_REGS) {
|
||||
Term t;
|
||||
int i = push_text_stack();
|
||||
t = Yap_StringToNumberTerm((char *)s, &out->enc, error_on);
|
||||
pop_text_stack(i);
|
||||
return t;
|
||||
pop_text_stack(i);
|
||||
return t;
|
||||
}
|
||||
|
||||
static Term string_to_term(void *s, seq_tv_t *out, size_t leng USES_REGS) {
|
||||
Term o;
|
||||
o = out->val.t =
|
||||
Yap_StringToTerm(s, strlen(s) + 1, &out->enc, GLOBAL_MaxPriority, 0L);
|
||||
o = out->val.t = Yap_BufferToTerm(s, strlen(s) + 1, TermNil);
|
||||
return o;
|
||||
}
|
||||
|
||||
@@ -746,7 +745,8 @@ bool write_Text(unsigned char *inp, seq_tv_t *out, size_t leng USES_REGS) {
|
||||
return out->val.t != 0;
|
||||
}
|
||||
if (out->type & (YAP_STRING_INT | YAP_STRING_FLOAT | YAP_STRING_BIG)) {
|
||||
if ((out->val.t = write_number(inp, out, leng,!(out->type & YAP_STRING_ATOM) PASS_REGS)) != 0L) {
|
||||
if ((out->val.t = write_number(
|
||||
inp, out, leng, !(out->type & YAP_STRING_ATOM)PASS_REGS)) != 0L) {
|
||||
// Yap_DebugPlWriteln(out->val.t);
|
||||
|
||||
return true;
|
||||
@@ -800,7 +800,7 @@ bool write_Text(unsigned char *inp, seq_tv_t *out, size_t leng USES_REGS) {
|
||||
out->val.t = write_number(inp, out, leng, true PASS_REGS);
|
||||
// Yap_DebugPlWriteln(out->val.t);
|
||||
return out->val.t != 0;
|
||||
default: { return true ; }
|
||||
default: { return true; }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -917,9 +917,9 @@ static unsigned char *concat(int n, void *sv[] USES_REGS) {
|
||||
buf = Malloc(room + 1);
|
||||
buf0 = buf;
|
||||
for (i = 0; i < n; i++) {
|
||||
#if _WIN32 || defined( __ANDROID__ )
|
||||
#if _WIN32 || defined(__ANDROID__)
|
||||
strcpy(buf, sv[i]);
|
||||
buf = (char*)buf + strlen(buf);
|
||||
buf = (char *)buf + strlen(buf);
|
||||
#else
|
||||
buf = stpcpy(buf, sv[i]);
|
||||
#endif
|
||||
@@ -1021,14 +1021,14 @@ bool Yap_Splice_Text(int n, size_t cuts[], seq_tv_t *inp,
|
||||
next = 0;
|
||||
else
|
||||
next = cuts[i - 1];
|
||||
if (i>0 && cuts[i] == 0)
|
||||
if (i > 0 && cuts[i] == 0)
|
||||
break;
|
||||
void *bufi = slice(next, cuts[i], buf PASS_REGS);
|
||||
if (!write_Text(bufi, outv + i, cuts[i] - next PASS_REGS)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1085,7 +1085,7 @@ const char *Yap_TextTermToText(Term t, char *buf, size_t len, encoding_t enc) {
|
||||
const char *Yap_PredIndicatorToUTF8String(PredEntry *ap) {
|
||||
CACHE_REGS
|
||||
Atom at;
|
||||
arity_t arity = 0;
|
||||
arity_t arity = 0;
|
||||
Functor f;
|
||||
char *s, *smax, *s0;
|
||||
s = s0 = malloc(1024);
|
||||
|
Reference in New Issue
Block a user