Big renaming of string buffer/formatting/conversion functions.

This commit is contained in:
Mike Pall
2013-05-13 10:15:07 +02:00
parent 625ffca739
commit 8f90a1279e
40 changed files with 395 additions and 356 deletions

View File

@@ -11,7 +11,6 @@
#include "lj_obj.h"
#include "lj_gc.h"
#include "lj_err.h"
#include "lj_buf.h"
#include "lj_str.h"
#include "lj_char.h"
@@ -198,120 +197,3 @@ void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s)
lj_mem_free(g, s, sizestring(s));
}
/* -- Type conversions ---------------------------------------------------- */
/* Print number to buffer. Canonicalizes non-finite values. */
char * LJ_FASTCALL lj_str_bufnum(char *p, cTValue *o)
{
if (LJ_LIKELY((o->u32.hi << 1) < 0xffe00000)) { /* Finite? */
#if __BIONIC__
if (tvismzero(o)) { *p++ = '-'; *p++ = '0'; return p; }
#endif
return p + lua_number2str(p, o->n);
} else if (((o->u32.hi & 0x000fffff) | o->u32.lo) != 0) {
*p++ = 'n'; *p++ = 'a'; *p++ = 'n';
} else if ((o->u32.hi & 0x80000000) == 0) {
*p++ = 'i'; *p++ = 'n'; *p++ = 'f';
} else {
*p++ = '-'; *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
}
return p;
}
#define STR_BUFINT_R(x, sh, sc) \
{ uint32_t d = (x*(((1<<sh)+sc-1)/sc))>>sh; x -= d*sc; *p++ = (char)('0'+d); }
/* Print integer to buffer. */
char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k)
{
uint32_t u = (uint32_t)k;
if (k < 0) { u = (uint32_t)-k; *p++ = '-'; }
if (u < 10000) {
if (u < 10) goto dig1; if (u < 100) goto dig2; if (u < 1000) goto dig3;
} else {
uint32_t v = u / 10000; u -= v * 10000;
if (v < 10000) {
if (v < 10) goto dig5; if (v < 100) goto dig6; if (v < 1000) goto dig7;
} else {
uint32_t w = v / 10000; v -= w * 10000;
if (w >= 10) STR_BUFINT_R(w, 10, 10)
*p++ = (char)('0'+w);
}
STR_BUFINT_R(v, 23, 1000)
dig7: STR_BUFINT_R(v, 12, 100)
dig6: STR_BUFINT_R(v, 10, 10)
dig5: *p++ = (char)('0'+v);
}
STR_BUFINT_R(u, 23, 1000)
dig3: STR_BUFINT_R(u, 12, 100)
dig2: STR_BUFINT_R(u, 10, 10)
dig1: *p++ = (char)('0'+u);
return p;
}
/* Print pointer to buffer. */
char * LJ_FASTCALL lj_str_bufptr(char *p, const void *v)
{
ptrdiff_t x = (ptrdiff_t)v;
MSize i, n = LJ_STR_PTRBUF;
if (x == 0) {
*p++ = 'N'; *p++ = 'U'; *p++ = 'L'; *p++ = 'L';
return p;
}
#if LJ_64
/* Shorten output for 64 bit pointers. */
n = 2+2*4+((x >> 32) ? 2+2*(lj_fls((uint32_t)(x >> 32))>>3) : 0);
#endif
p[0] = '0';
p[1] = 'x';
for (i = n-1; i >= 2; i--, x >>= 4)
p[i] = "0123456789abcdef"[(x & 15)];
return p+n;
}
/* Print TValue to buffer (only for numbers) and return pointer to start. */
const char *lj_str_buftv(char *buf, cTValue *o, MSize *lenp)
{
if (tvisstr(o)) {
*lenp = strV(o)->len;
return strVdata(o);
} else if (tvisint(o)) {
*lenp = (MSize)(lj_str_bufint(buf, intV(o)) - buf);
return buf;
} else if (tvisnum(o)) {
*lenp = (MSize)(lj_str_bufnum(buf, o) - buf);
return buf;
} else {
return NULL;
}
}
/* Convert number to string. */
GCstr * LJ_FASTCALL lj_str_fromnum(lua_State *L, const lua_Number *np)
{
char buf[LJ_STR_NUMBUF];
MSize len = (MSize)(lj_str_bufnum(buf, (TValue *)np) - buf);
return lj_str_new(L, buf, len);
}
/* Convert integer to string. */
GCstr * LJ_FASTCALL lj_str_fromint(lua_State *L, int32_t k)
{
char buf[LJ_STR_INTBUF];
MSize len = (MSize)(lj_str_bufint(buf, k) - buf);
return lj_str_new(L, buf, len);
}
GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o)
{
return tvisint(o) ? lj_str_fromint(L, intV(o)) : lj_str_fromnum(L, &o->n);
}
/* Convert char value to string. */
GCstr * LJ_FASTCALL lj_str_fromchar(lua_State *L, int c)
{
char buf[1];
buf[0] = c;
return lj_str_new(L, buf, 1);
}