Use string buffer for os.date().

This commit is contained in:
Mike Pall 2013-03-20 16:53:15 +01:00
parent deb61e0be0
commit d38d10a3dd
2 changed files with 21 additions and 16 deletions

View File

@ -28,7 +28,8 @@ lib_jit.o: lib_jit.c lua.h luaconf.h lauxlib.h lualib.h lj_arch.h \
lib_math.o: lib_math.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ lib_math.o: lib_math.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
lj_def.h lj_arch.h lj_lib.h lj_vm.h lj_libdef.h lj_def.h lj_arch.h lj_lib.h lj_vm.h lj_libdef.h
lib_os.o: lib_os.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \ lib_os.o: lib_os.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h lj_def.h \
lj_arch.h lj_err.h lj_errmsg.h lj_lib.h lj_libdef.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_lib.h \
lj_libdef.h
lib_package.o: lib_package.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ lib_package.o: lib_package.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_lib.h lj_def.h lj_arch.h lj_err.h lj_errmsg.h lj_lib.h
lib_string.o: lib_string.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \ lib_string.o: lib_string.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \

View File

@ -18,7 +18,9 @@
#include "lualib.h" #include "lualib.h"
#include "lj_obj.h" #include "lj_obj.h"
#include "lj_gc.h"
#include "lj_err.h" #include "lj_err.h"
#include "lj_buf.h"
#include "lj_lib.h" #include "lj_lib.h"
#if LJ_TARGET_POSIX #if LJ_TARGET_POSIX
@ -197,23 +199,25 @@ LJLIB_CF(os_date)
setfield(L, "wday", stm->tm_wday+1); setfield(L, "wday", stm->tm_wday+1);
setfield(L, "yday", stm->tm_yday+1); setfield(L, "yday", stm->tm_yday+1);
setboolfield(L, "isdst", stm->tm_isdst); setboolfield(L, "isdst", stm->tm_isdst);
} else { } else if (*s) {
char cc[3]; SBuf *sb = &G(L)->tmpbuf;
luaL_Buffer b; MSize sz = 0;
cc[0] = '%'; cc[2] = '\0'; const char *q;
luaL_buffinit(L, &b); for (q = s; *q; q++)
for (; *s; s++) { sz += (*q == '%') ? 30 : 1; /* Overflow doesn't matter. */
if (*s != '%' || *(s + 1) == '\0') { /* No conversion specifier? */ setmref(sb->L, L);
luaL_addchar(&b, *s); for (;;) {
} else { char *buf = lj_buf_need(sb, sz);
size_t reslen; size_t len = strftime(buf, sbufsz(sb), s, stm);
char buff[200]; /* Should be big enough for any conversion result. */ if (len) {
cc[1] = *(++s); setstrV(L, L->top-1, lj_str_new(L, buf, len));
reslen = strftime(buff, sizeof(buff), cc, stm); lj_gc_check(L);
luaL_addlstring(&b, buff, reslen); break;
} }
sz += (sz|1);
} }
luaL_pushresult(&b); } else {
setstrV(L, L->top-1, &G(L)->strempty);
} }
return 1; return 1;
} }