improve BigNum handling.

This commit is contained in:
Vítor Santos Costa 2011-06-21 15:11:07 +01:00
parent 42ebf1db4b
commit ded6b2435c
4 changed files with 54 additions and 43 deletions

View File

@ -284,6 +284,7 @@ Yap_MkBlobStringTerm(const char *s, size_t len)
MP_INT *dst = (MP_INT *)(H+2); MP_INT *dst = (MP_INT *)(H+2);
blob_string_t *sp; blob_string_t *sp;
size_t siz; size_t siz;
char *dest;
sz = strlen(s); sz = strlen(s);
if (len > 0 && sz > len) sz = len; if (len > 0 && sz > len) sz = len;
@ -292,17 +293,16 @@ Yap_MkBlobStringTerm(const char *s, size_t len)
} }
H[0] = (CELL)FunctorBigInt; H[0] = (CELL)FunctorBigInt;
H[1] = BLOB_STRING; H[1] = BLOB_STRING;
siz = ALIGN_YAPTYPE((len+1+sizeof(blob_string_t)),CELL);
siz = (sizeof(size_t)+len+sizeof(CELL))/sizeof(CELL);
dst->_mp_size = 0L; dst->_mp_size = 0L;
dst->_mp_alloc = siz; dst->_mp_alloc = siz/sizeof(mp_limb_t);
sp = (blob_string_t *)(dst+1); sp = (blob_string_t *)(dst+1);
H = (CELL *)sp;
sp->len = sz; sp->len = sz;
strncpy((char *)(sp+1), s, sz+1); dest = (char *)(sp+1);
H += siz; strncpy(dest, s, sz);
H[0] = EndSpecials; dest[sz] = '\0';
H++; H += (siz + 2*sizeof(CELL)+sizeof(MP_INT)+sizeof(Functor))/sizeof(CELL);
H[-1] = EndSpecials;
return AbsAppl(ret); return AbsAppl(ret);
} }
@ -313,56 +313,49 @@ Yap_MkBlobWideStringTerm(const wchar_t *s, size_t len)
CELL *ret = H; CELL *ret = H;
size_t sz; size_t sz;
MP_INT *dst = (MP_INT *)(H+2); MP_INT *dst = (MP_INT *)(H+2);
blob_string_t *sp; blob_string_t *sp = (blob_string_t *)(dst+1);
size_t siz, i = 0; size_t siz, i = 0;
H[0] = (CELL)FunctorBigInt;
dst->_mp_size = 0L;
sz = wcslen(s); sz = wcslen(s);
if (len > 0 && sz > len) sz = len; if (len > 0 && sz > len) {
sz = len;
}
if ((len/sizeof(CELL)) > (ASP-ret)-1024) {
return TermNil;
}
while (i < sz) { while (i < sz) {
if (s[i++] >= 255) break; if (s[i++] >= 255) break;
} }
if (i == sz) { if (i == sz) {
/* we have a standard ascii string */
char *target; char *target;
size_t i = 0; size_t i = 0;
if (len/sizeof(CELL) > (ASP-ret)-1024) {
return TermNil;
}
H[0] = (CELL)FunctorBigInt;
H[1] = BLOB_STRING; H[1] = BLOB_STRING;
siz = ALIGN_YAPTYPE((sz+1+sizeof(blob_string_t)),CELL);
siz = (sizeof(size_t)+len+sizeof(CELL))/sizeof(CELL); dst->_mp_alloc = siz/sizeof(mp_limb_t);
dst->_mp_size = 0L;
dst->_mp_alloc = siz;
sp = (blob_string_t *)(dst+1);
H = (CELL *)sp;
sp->len = sz; sp->len = sz;
target = (char *)(sp+1); target = (char *)(sp+1);
while (i < sz+1) { for (i = 0 ; i < sz; i++) {
target[i] = s[i]; target[i] = s[i];
i++;
} }
H += siz; target[sz] = '\0';
H[0] = EndSpecials; H += (siz+2*sizeof(CELL)+sizeof(MP_INT)+sizeof(Functor))/sizeof(CELL);
H++; } else {
return AbsAppl(ret); wchar_t * target;
}
if (len/sizeof(CELL) > (ASP-ret)-1024) {
return TermNil;
}
H[0] = (CELL)FunctorBigInt;
H[1] = BLOB_WIDE_STRING;
siz = (sizeof(size_t)+(len+2)*sizeof(wchar_t))/sizeof(CELL); H[1] = BLOB_WIDE_STRING;
dst->_mp_size = 0L; siz = ALIGN_YAPTYPE((sz+1)*sizeof(wchar_t)+sizeof(blob_string_t),CELL);
dst->_mp_alloc = siz; dst->_mp_alloc = siz/sizeof(mp_limb_t);
sp = (blob_string_t *)(dst+1);
H = (CELL *)sp;
sp->len = sz; sp->len = sz;
wcsncpy((wchar_t *)(sp+1), s, sz+1); target = (wchar_t *)(sp+1);
H += siz; wcsncpy(target, s, sz);
H[0] = EndSpecials; target[sz] = '\0';
H++; H += (siz + 2*sizeof(CELL)+sizeof(MP_INT)+sizeof(Functor))/sizeof(CELL);
}
H[-1] = EndSpecials;
return AbsAppl(ret); return AbsAppl(ret);
} }

View File

@ -813,6 +813,7 @@ close_comment( USES_REGS1 ) {
LOCAL_CommentsBuff[LOCAL_CommentsBuffPos] = '\0'; LOCAL_CommentsBuff[LOCAL_CommentsBuffPos] = '\0';
*LOCAL_CommentsNextChar = Yap_MkBlobWideStringTerm(LOCAL_CommentsBuff, LOCAL_CommentsBuffPos); *LOCAL_CommentsNextChar = Yap_MkBlobWideStringTerm(LOCAL_CommentsBuff, LOCAL_CommentsBuffPos);
free(LOCAL_CommentsBuff); free(LOCAL_CommentsBuff);
LOCAL_CommentsBuff = NULL;
LOCAL_CommentsBuffLim = 0; LOCAL_CommentsBuffLim = 0;
} }
@ -1372,5 +1373,8 @@ Yap_clean_tokenizer(TokEntry *tokstart, VarEntry *vartable, VarEntry *anonvartab
} }
LOCAL_Comments = TermNil; LOCAL_Comments = TermNil;
LOCAL_CommentsNextChar = LOCAL_CommentsTail = NULL; LOCAL_CommentsNextChar = LOCAL_CommentsTail = NULL;
free(LOCAL_CommentsBuff);
LOCAL_CommentsBuff = NULL;
LOCAL_CommentsBuffLim = 0;
} }

View File

@ -210,6 +210,20 @@ writebig(Term t, int p, int depth, int rinfixarg, struct write_globs *wglb, stru
return; return;
} }
#endif #endif
if (pt[0] == BLOB_STRING) {
wrputc('"',wglb->writewch);
wrputs(Yap_BlobStringOfTerm(t),wglb->writewch);
wrputc('"',wglb->writewch);
return;
} else if (pt[0] == BLOB_STRING) {
wchar_t *s = Yap_BlobWideStringOfTerm(t);
wrputc('"', wglb->writewch);
while (*s) {
wrputc(*s++, wglb->writewch);
}
wrputc('"',wglb->writewch);
return;
}
wrputs("0",wglb->writewch); wrputs("0",wglb->writewch);
} }

View File

@ -397,7 +397,7 @@ IsLargeIntTerm (Term t)
#endif #endif
typedef struct string_struct { typedef struct string_struct {
size_t len; UInt len;
} blob_string_t; } blob_string_t;
Term STD_PROTO (Yap_MkBlobStringTerm, (const char *, size_t len)); Term STD_PROTO (Yap_MkBlobStringTerm, (const char *, size_t len));