aboutsummaryrefslogtreecommitdiff
path: root/src/lj_str.c
diff options
context:
space:
mode:
authorMike Pall <mike>2013-03-18 17:08:37 +0100
committerMike Pall <mike>2013-03-18 17:08:37 +0100
commitd1645c88a189259b38b0a733f59cd01767cd1245 (patch)
tree9e5849cbc697ad369f0c0ec0d4b091c0ca54d27b /src/lj_str.c
parent18d7c975d6017f401c193fecb93081739dc5a94d (diff)
downloadluajit-d1645c88a189259b38b0a733f59cd01767cd1245.tar.gz
luajit-d1645c88a189259b38b0a733f59cd01767cd1245.tar.bz2
luajit-d1645c88a189259b38b0a733f59cd01767cd1245.zip
Clean up TValue to buffer conversions.
Diffstat (limited to 'src/lj_str.c')
-rw-r--r--src/lj_str.c38
1 files changed, 28 insertions, 10 deletions
diff --git a/src/lj_str.c b/src/lj_str.c
index 67353154..5482f7c4 100644
--- a/src/lj_str.c
+++ b/src/lj_str.c
@@ -168,14 +168,14 @@ void LJ_FASTCALL lj_str_free(global_State *g, GCstr *s)
168/* -- Type conversions ---------------------------------------------------- */ 168/* -- Type conversions ---------------------------------------------------- */
169 169
170/* Print number to buffer. Canonicalizes non-finite values. */ 170/* Print number to buffer. Canonicalizes non-finite values. */
171size_t LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o) 171MSize LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o)
172{ 172{
173 if (LJ_LIKELY((o->u32.hi << 1) < 0xffe00000)) { /* Finite? */ 173 if (LJ_LIKELY((o->u32.hi << 1) < 0xffe00000)) { /* Finite? */
174 lua_Number n = o->n; 174 lua_Number n = o->n;
175#if __BIONIC__ 175#if __BIONIC__
176 if (tvismzero(o)) { s[0] = '-'; s[1] = '0'; return 2; } 176 if (tvismzero(o)) { s[0] = '-'; s[1] = '0'; return 2; }
177#endif 177#endif
178 return (size_t)lua_number2str(s, n); 178 return (MSize)lua_number2str(s, n);
179 } else if (((o->u32.hi & 0x000fffff) | o->u32.lo) != 0) { 179 } else if (((o->u32.hi & 0x000fffff) | o->u32.lo) != 0) {
180 s[0] = 'n'; s[1] = 'a'; s[2] = 'n'; return 3; 180 s[0] = 'n'; s[1] = 'a'; s[2] = 'n'; return 3;
181 } else if ((o->u32.hi & 0x80000000) == 0) { 181 } else if ((o->u32.hi & 0x80000000) == 0) {
@@ -185,30 +185,48 @@ size_t LJ_FASTCALL lj_str_bufnum(char *s, cTValue *o)
185 } 185 }
186} 186}
187 187
188/* Print integer to buffer. Returns pointer to start. */ 188/* Print integer to buffer. Returns pointer to start (!= buffer start). */
189char * LJ_FASTCALL lj_str_bufint(char *p, int32_t k) 189static char * str_bufint(char *p, int32_t k)
190{ 190{
191 uint32_t u = (uint32_t)(k < 0 ? -k : k); 191 uint32_t u = (uint32_t)(k < 0 ? -k : k);
192 p += 1+10; 192 p += LJ_STR_INTBUF;
193 do { *--p = (char)('0' + u % 10); } while (u /= 10); 193 do { *--p = (char)('0' + u % 10); } while (u /= 10);
194 if (k < 0) *--p = '-'; 194 if (k < 0) *--p = '-';
195 return p; 195 return p;
196} 196}
197 197
198/* Print TValue to buffer (only for numbers) and return pointer to start. */
199const char *lj_str_buftv(char *buf, cTValue *o, MSize *lenp)
200{
201 if (tvisstr(o)) {
202 *lenp = strV(o)->len;
203 return strVdata(o);
204 } else if (tvisint(o)) {
205 char *p = str_bufint(buf, intV(o));
206 *lenp = (MSize)(buf+LJ_STR_INTBUF-p);
207 return p;
208 } else if (tvisnum(o)) {
209 *lenp = lj_str_bufnum(buf, o);
210 return buf;
211 } else {
212 return NULL;
213 }
214}
215
198/* Convert number to string. */ 216/* Convert number to string. */
199GCstr * LJ_FASTCALL lj_str_fromnum(lua_State *L, const lua_Number *np) 217GCstr * LJ_FASTCALL lj_str_fromnum(lua_State *L, const lua_Number *np)
200{ 218{
201 char buf[LJ_STR_NUMBUF]; 219 char buf[LJ_STR_NUMBUF];
202 size_t len = lj_str_bufnum(buf, (TValue *)np); 220 MSize len = lj_str_bufnum(buf, (TValue *)np);
203 return lj_str_new(L, buf, len); 221 return lj_str_new(L, buf, len);
204} 222}
205 223
206/* Convert integer to string. */ 224/* Convert integer to string. */
207GCstr * LJ_FASTCALL lj_str_fromint(lua_State *L, int32_t k) 225GCstr * LJ_FASTCALL lj_str_fromint(lua_State *L, int32_t k)
208{ 226{
209 char s[1+10]; 227 char buf[LJ_STR_INTBUF];
210 char *p = lj_str_bufint(s, k); 228 char *p = str_bufint(buf, k);
211 return lj_str_new(L, p, (size_t)(s+sizeof(s)-p)); 229 return lj_str_new(L, p, (size_t)(buf+sizeof(buf)-p));
212} 230}
213 231
214GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o) 232GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o)
@@ -242,7 +260,7 @@ const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp)
242 break; 260 break;
243 case 'd': { 261 case 'd': {
244 char buf[LJ_STR_INTBUF]; 262 char buf[LJ_STR_INTBUF];
245 char *p = lj_str_bufint(buf, va_arg(argp, int32_t)); 263 char *p = str_bufint(buf, va_arg(argp, int32_t));
246 lj_buf_putmem(sb, p, (MSize)(buf+LJ_STR_INTBUF-p)); 264 lj_buf_putmem(sb, p, (MSize)(buf+LJ_STR_INTBUF-p));
247 break; 265 break;
248 } 266 }