aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Pall <mike>2013-03-20 16:53:15 +0100
committerMike Pall <mike>2013-03-20 16:54:30 +0100
commitd38d10a3dd1d01941b320d6c0715dc871abba35a (patch)
tree864124de1287743a3eeef093be1ca808a1e92174
parentdeb61e0be027ee9b30299295c0941f7e8fae77b5 (diff)
downloadluajit-d38d10a3dd1d01941b320d6c0715dc871abba35a.tar.gz
luajit-d38d10a3dd1d01941b320d6c0715dc871abba35a.tar.bz2
luajit-d38d10a3dd1d01941b320d6c0715dc871abba35a.zip
Use string buffer for os.date().
Diffstat (limited to '')
-rw-r--r--src/Makefile.dep3
-rw-r--r--src/lib_os.c34
2 files changed, 21 insertions, 16 deletions
diff --git a/src/Makefile.dep b/src/Makefile.dep
index 484553c4..82834811 100644
--- a/src/Makefile.dep
+++ b/src/Makefile.dep
@@ -28,7 +28,8 @@ lib_jit.o: lib_jit.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h \
28lib_math.o: lib_math.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ 28lib_math.o: lib_math.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
29 lj_def.h lj_arch.h lj_lib.h lj_vm.h lj_libdef.h 29 lj_def.h lj_arch.h lj_lib.h lj_vm.h lj_libdef.h
30lib_os.o: lib_os.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ 30lib_os.o: lib_os.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
31 lj_arch.h lj_err.h lj_errmsg.h lj_lib.h lj_libdef.h 31 lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_lib.h \
32 lj_libdef.h
32lib_package.o: lib_package.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ 33lib_package.o: lib_package.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
33 lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_lib.h 34 lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_lib.h
34lib_string.o: lib_string.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ 35lib_string.o: lib_string.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
diff --git a/src/lib_os.c b/src/lib_os.c
index 0a784129..d0291f52 100644
--- a/src/lib_os.c
+++ b/src/lib_os.c
@@ -18,7 +18,9 @@
18#include "lualib.h" 18#include "lualib.h"
19 19
20#include "lj_obj.h" 20#include "lj_obj.h"
21#include "lj_gc.h"
21#include "lj_err.h" 22#include "lj_err.h"
23#include "lj_buf.h"
22#include "lj_lib.h" 24#include "lj_lib.h"
23 25
24#if LJ_TARGET_POSIX 26#if LJ_TARGET_POSIX
@@ -197,23 +199,25 @@ LJLIB_CF(os_date)
197 setfield(L, "wday", stm->tm_wday+1); 199 setfield(L, "wday", stm->tm_wday+1);
198 setfield(L, "yday", stm->tm_yday+1); 200 setfield(L, "yday", stm->tm_yday+1);
199 setboolfield(L, "isdst", stm->tm_isdst); 201 setboolfield(L, "isdst", stm->tm_isdst);
200 } else { 202 } else if (*s) {
201 char cc[3]; 203 SBuf *sb = &G(L)->tmpbuf;
202 luaL_Buffer b; 204 MSize sz = 0;
203 cc[0] = '%'; cc[2] = '\0'; 205 const char *q;
204 luaL_buffinit(L, &b); 206 for (q = s; *q; q++)
205 for (; *s; s++) { 207 sz += (*q == '%') ? 30 : 1; /* Overflow doesn't matter. */
206 if (*s != '%' || *(s + 1) == '\0') { /* No conversion specifier? */ 208 setmref(sb->L, L);
207 luaL_addchar(&b, *s); 209 for (;;) {
208 } else { 210 char *buf = lj_buf_need(sb, sz);
209 size_t reslen; 211 size_t len = strftime(buf, sbufsz(sb), s, stm);
210 char buff[200]; /* Should be big enough for any conversion result. */ 212 if (len) {
211 cc[1] = *(++s); 213 setstrV(L, L->top-1, lj_str_new(L, buf, len));
212 reslen = strftime(buff, sizeof(buff), cc, stm); 214 lj_gc_check(L);
213 luaL_addlstring(&b, buff, reslen); 215 break;
214 } 216 }
217 sz += (sz|1);
215 } 218 }
216 luaL_pushresult(&b); 219 } else {
220 setstrV(L, L->top-1, &G(L)->strempty);
217 } 221 }
218 return 1; 222 return 1;
219} 223}