diff options
author | Mike Pall <mike> | 2013-04-27 15:51:50 +0200 |
---|---|---|
committer | Mike Pall <mike> | 2013-04-27 15:51:50 +0200 |
commit | 64d2883ab4fdd3090d8a428aafc9a2bb3a1ec724 (patch) | |
tree | d44858c5103ec89234d584ee113766468ff3c175 /src/lib_table.c | |
parent | 723574d08cf9f902bd917e83d8fe25717690b0c6 (diff) | |
download | luajit-64d2883ab4fdd3090d8a428aafc9a2bb3a1ec724.tar.gz luajit-64d2883ab4fdd3090d8a428aafc9a2bb3a1ec724.tar.bz2 luajit-64d2883ab4fdd3090d8a428aafc9a2bb3a1ec724.zip |
Refactor table.concat().
Diffstat (limited to 'src/lib_table.c')
-rw-r--r-- | src/lib_table.c | 30 |
1 files changed, 9 insertions, 21 deletions
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 | ||