aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Pall <mike>2013-04-27 15:51:50 +0200
committerMike Pall <mike>2013-04-27 15:51:50 +0200
commit64d2883ab4fdd3090d8a428aafc9a2bb3a1ec724 (patch)
treed44858c5103ec89234d584ee113766468ff3c175
parent723574d08cf9f902bd917e83d8fe25717690b0c6 (diff)
downloadluajit-64d2883ab4fdd3090d8a428aafc9a2bb3a1ec724.tar.gz
luajit-64d2883ab4fdd3090d8a428aafc9a2bb3a1ec724.tar.bz2
luajit-64d2883ab4fdd3090d8a428aafc9a2bb3a1ec724.zip
Refactor table.concat().
-rw-r--r--src/Makefile.dep2
-rw-r--r--src/lib_table.c30
-rw-r--r--src/lj_buf.c33
-rw-r--r--src/lj_buf.h2
4 files changed, 45 insertions, 22 deletions
diff --git a/src/Makefile.dep b/src/Makefile.dep
index 6747e599..074d0908 100644
--- a/src/Makefile.dep
+++ b/src/Makefile.dep
@@ -58,7 +58,7 @@ lj_bcwrite.o: lj_bcwrite.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
58 lj_gc.h lj_buf.h lj_str.h lj_bc.h lj_ctype.h lj_dispatch.h lj_jit.h \ 58 lj_gc.h lj_buf.h lj_str.h lj_bc.h lj_ctype.h lj_dispatch.h lj_jit.h \
59 lj_ir.h lj_bcdump.h lj_lex.h lj_err.h lj_errmsg.h lj_vm.h 59 lj_ir.h lj_bcdump.h lj_lex.h lj_err.h lj_errmsg.h lj_vm.h
60lj_buf.o: lj_buf.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ 60lj_buf.o: lj_buf.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
61 lj_err.h lj_errmsg.h lj_buf.h lj_str.h 61 lj_err.h lj_errmsg.h lj_buf.h lj_str.h lj_tab.h
62lj_carith.o: lj_carith.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 62lj_carith.o: lj_carith.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
63 lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_meta.h lj_ir.h lj_ctype.h \ 63 lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_meta.h lj_ir.h lj_ctype.h \
64 lj_cconv.h lj_cdata.h lj_carith.h lj_strscan.h 64 lj_cconv.h lj_cdata.h lj_carith.h lj_strscan.h
diff --git a/src/lib_table.c b/src/lib_table.c
index 5619f9ad..011b9fdf 100644
--- a/src/lib_table.c
+++ b/src/lib_table.c
@@ -133,31 +133,19 @@ LJLIB_CF(table_concat)
133{ 133{
134 GCtab *t = lj_lib_checktab(L, 1); 134 GCtab *t = lj_lib_checktab(L, 1);
135 GCstr *sep = lj_lib_optstr(L, 2); 135 GCstr *sep = lj_lib_optstr(L, 2);
136 MSize seplen = sep ? sep->len : 0;
137 int32_t i = lj_lib_optint(L, 3, 1); 136 int32_t i = lj_lib_optint(L, 3, 1);
138 int32_t e = L->base+3 < L->top ? lj_lib_checkint(L, 4) : 137 int32_t e = L->base+3 < L->top ? lj_lib_checkint(L, 4) :
139 (int32_t)lj_tab_len(t); 138 (int32_t)lj_tab_len(t);
140 if (i <= e) { 139 SBuf *sb = lj_buf_tmp_(L);
141 char buf[LJ_STR_NUMBERBUF]; 140 SBuf *sbx = lj_buf_puttab(sb, t, sep, i, e);
142 SBuf *sb = &G(L)->tmpbuf; 141 if (LJ_UNLIKELY(!sbx)) { /* Error: bad element type. */
143 setsbufL(sb, L); 142 int32_t idx = (int32_t)(intptr_t)sbufP(sb);
144 lj_buf_reset(sb); 143 cTValue *o = lj_tab_getint(t, idx);
145 for (;;) { 144 lj_err_callerv(L, LJ_ERR_TABCAT,
146 cTValue *o = lj_tab_getint(t, i); 145 lj_obj_itypename[o ? itypemap(o) : ~LJ_TNIL], idx);
147 MSize len;
148 const char *p = lj_str_buftv(buf, o, &len);
149 if (!p)
150 lj_err_callerv(L, LJ_ERR_TABCAT, lj_typename(o), i);
151 lj_buf_putmem(sb, p, len);
152 if (i++ == e) break;
153 if (seplen)
154 lj_buf_putmem(sb, strdata(sep), seplen);
155 }
156 setstrV(L, L->top-1, lj_buf_str(L, sb));
157 lj_gc_check(L);
158 } else {
159 setstrV(L, L->top-1, &G(L)->strempty);
160 } 146 }
147 setstrV(L, L->top-1, lj_buf_str(L, sbx));
148 lj_gc_check(L);
161 return 1; 149 return 1;
162} 150}
163 151
diff --git a/src/lj_buf.c b/src/lj_buf.c
index 55a885a9..c60cc51f 100644
--- a/src/lj_buf.c
+++ b/src/lj_buf.c
@@ -13,6 +13,7 @@
13#include "lj_err.h" 13#include "lj_err.h"
14#include "lj_buf.h" 14#include "lj_buf.h"
15#include "lj_str.h" 15#include "lj_str.h"
16#include "lj_tab.h"
16 17
17LJ_NOINLINE void LJ_FASTCALL lj_buf_grow(SBuf *sb, char *en) 18LJ_NOINLINE void LJ_FASTCALL lj_buf_grow(SBuf *sb, char *en)
18{ 19{
@@ -168,6 +169,38 @@ SBuf *lj_buf_putstr_rep(SBuf *sb, GCstr *s, int32_t rep)
168 return sb; 169 return sb;
169} 170}
170 171
172SBuf *lj_buf_puttab(SBuf *sb, GCtab *t, GCstr *sep, int32_t i, int32_t e)
173{
174 MSize seplen = sep ? sep->len : 0;
175 if (i <= e) {
176 for (;;) {
177 cTValue *o = lj_tab_getint(t, i);
178 char *p;
179 if (!o) {
180 badtype: /* Error: bad element type. */
181 setsbufP(sb, (intptr_t)i); /* Store failing index. */
182 return NULL;
183 } else if (tvisstr(o)) {
184 MSize len = strV(o)->len;
185 p = lj_buf_wmem(lj_buf_more(sb, len + seplen), strVdata(o), len);
186 } else if (tvisint(o)) {
187 p = lj_str_bufint(lj_buf_more(sb, LJ_STR_INTBUF + seplen), intV(o));
188 } else if (tvisnum(o)) {
189 p = lj_str_bufnum(lj_buf_more(sb, LJ_STR_NUMBUF + seplen), o);
190 } else {
191 goto badtype;
192 }
193 if (i++ == e) {
194 setsbufP(sb, p);
195 break;
196 }
197 if (seplen) p = lj_buf_wmem(p, strdata(sep), seplen);
198 setsbufP(sb, p);
199 }
200 }
201 return sb;
202}
203
171GCstr * LJ_FASTCALL lj_buf_tostr(SBuf *sb) 204GCstr * LJ_FASTCALL lj_buf_tostr(SBuf *sb)
172{ 205{
173 return lj_str_new(sbufL(sb), sbufB(sb), sbuflen(sb)); 206 return lj_str_new(sbufL(sb), sbufB(sb), sbuflen(sb));
diff --git a/src/lj_buf.h b/src/lj_buf.h
index 4d764938..6931a693 100644
--- a/src/lj_buf.h
+++ b/src/lj_buf.h
@@ -36,6 +36,8 @@ LJ_FUNCA SBuf * LJ_FASTCALL lj_buf_putstr_reverse(SBuf *sb, GCstr *s);
36LJ_FUNCA SBuf * LJ_FASTCALL lj_buf_putstr_lower(SBuf *sb, GCstr *s); 36LJ_FUNCA SBuf * LJ_FASTCALL lj_buf_putstr_lower(SBuf *sb, GCstr *s);
37LJ_FUNCA SBuf * LJ_FASTCALL lj_buf_putstr_upper(SBuf *sb, GCstr *s); 37LJ_FUNCA SBuf * LJ_FASTCALL lj_buf_putstr_upper(SBuf *sb, GCstr *s);
38LJ_FUNC SBuf *lj_buf_putstr_rep(SBuf *sb, GCstr *s, int32_t rep); 38LJ_FUNC SBuf *lj_buf_putstr_rep(SBuf *sb, GCstr *s, int32_t rep);
39LJ_FUNC SBuf *lj_buf_puttab(SBuf *sb, GCtab *t, GCstr *sep,
40 int32_t i, int32_t e);
39LJ_FUNCA GCstr * LJ_FASTCALL lj_buf_tostr(SBuf *sb); 41LJ_FUNCA GCstr * LJ_FASTCALL lj_buf_tostr(SBuf *sb);
40LJ_FUNC GCstr *lj_buf_cat2str(lua_State *L, GCstr *s1, GCstr *s2); 42LJ_FUNC GCstr *lj_buf_cat2str(lua_State *L, GCstr *s1, GCstr *s2);
41LJ_FUNC uint32_t LJ_FASTCALL lj_buf_ruleb128(const char **pp); 43LJ_FUNC uint32_t LJ_FASTCALL lj_buf_ruleb128(const char **pp);