Browse Source

Use string buffer for table.concat().

Mike Pall 12 years ago
parent
commit
deb61e0be0
2 changed files with 18 additions and 11 deletions
  1. 2 2
      src/Makefile.dep
  2. 16 9
      src/lib_table.c

+ 2 - 2
src/Makefile.dep

@@ -36,8 +36,8 @@ lib_string.o: lib_string.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
  lj_tab.h lj_meta.h lj_state.h lj_ff.h lj_ffdef.h lj_bcdump.h lj_lex.h \
  lj_char.h lj_lib.h lj_libdef.h
 lib_table.o: lib_table.c lua.h luaconf.h lauxlib.h lualib.h lj_obj.h \
- lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_lib.h \
- lj_libdef.h
+ lj_def.h lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_buf.h lj_str.h \
+ lj_tab.h lj_lib.h lj_libdef.h
 lj_alloc.o: lj_alloc.c lj_def.h lua.h luaconf.h lj_arch.h lj_alloc.h
 lj_api.o: lj_api.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
  lj_err.h lj_errmsg.h lj_debug.h lj_str.h lj_tab.h lj_func.h lj_udata.h \

+ 16 - 9
src/lib_table.c

@@ -16,6 +16,8 @@
 #include "lj_obj.h"
 #include "lj_gc.h"
 #include "lj_err.h"
+#include "lj_buf.h"
+#include "lj_str.h"
 #include "lj_tab.h"
 #include "lj_lib.h"
 
@@ -129,28 +131,33 @@ LJLIB_LUA(table_remove) /*
 
 LJLIB_CF(table_concat)
 {
-  luaL_Buffer b;
   GCtab *t = lj_lib_checktab(L, 1);
   GCstr *sep = lj_lib_optstr(L, 2);
   MSize seplen = sep ? sep->len : 0;
   int32_t i = lj_lib_optint(L, 3, 1);
   int32_t e = L->base+3 < L->top ? lj_lib_checkint(L, 4) :
 				   (int32_t)lj_tab_len(t);
-  luaL_buffinit(L, &b);
   if (i <= e) {
+    char buf[LJ_STR_NUMBERBUF];
+    SBuf *sb = &G(L)->tmpbuf;
+    setsbufL(sb, L);
+    lj_buf_reset(sb);
     for (;;) {
-      cTValue *o;
-      lua_rawgeti(L, 1, i);
-      o = L->top-1;
-      if (!(tvisstr(o) || tvisnumber(o)))
+      cTValue *o = lj_tab_getint(t, i);
+      MSize len;
+      const char *p = lj_str_buftv(buf, o, &len);
+      if (!p)
 	lj_err_callerv(L, LJ_ERR_TABCAT, lj_typename(o), i);
-      luaL_addvalue(&b);
+      lj_buf_putmem(sb, p, len);
       if (i++ == e) break;
       if (seplen)
-	luaL_addlstring(&b, strdata(sep), seplen);
+	lj_buf_putmem(sb, strdata(sep), seplen);
     }
+    setstrV(L, L->top-1, lj_buf_str(L, sb));
+    lj_gc_check(L);
+  } else {
+    setstrV(L, L->top-1, &G(L)->strempty);
   }
-  luaL_pushresult(&b);
   return 1;
 }