diff options
author | Mike Pall <mike> | 2013-02-27 17:11:31 +0100 |
---|---|---|
committer | Mike Pall <mike> | 2013-02-27 17:29:35 +0100 |
commit | 28cfcf77445e144335961a020e3e08d84cf0091f (patch) | |
tree | 1a769d0ee0fab26a79073a118ba4f9e1557b081a /src/lj_str.c | |
parent | d44337a566bb3de06a6ac4ecf2d2a77767b86029 (diff) | |
download | luajit-28cfcf77445e144335961a020e3e08d84cf0091f.tar.gz luajit-28cfcf77445e144335961a020e3e08d84cf0091f.tar.bz2 luajit-28cfcf77445e144335961a020e3e08d84cf0091f.zip |
String buffer refactoring, part 1.
Move string buffer handling to lj_buf.*.
Use common buffer resizing function.
Diffstat (limited to 'src/lj_str.c')
-rw-r--r-- | src/lj_str.c | 34 |
1 files changed, 6 insertions, 28 deletions
diff --git a/src/lj_str.c b/src/lj_str.c index 6548ee4d..623b362d 100644 --- a/src/lj_str.c +++ b/src/lj_str.c | |||
@@ -1,9 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | ** String handling. | 2 | ** String handling. |
3 | ** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h | 3 | ** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h |
4 | ** | ||
5 | ** Portions taken verbatim or adapted from the Lua interpreter. | ||
6 | ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h | ||
7 | */ | 4 | */ |
8 | 5 | ||
9 | #include <stdio.h> | 6 | #include <stdio.h> |
@@ -14,6 +11,7 @@ | |||
14 | #include "lj_obj.h" | 11 | #include "lj_obj.h" |
15 | #include "lj_gc.h" | 12 | #include "lj_gc.h" |
16 | #include "lj_err.h" | 13 | #include "lj_err.h" |
14 | #include "lj_buf.h" | ||
17 | #include "lj_str.h" | 15 | #include "lj_str.h" |
18 | #include "lj_state.h" | 16 | #include "lj_state.h" |
19 | #include "lj_char.h" | 17 | #include "lj_char.h" |
@@ -222,33 +220,24 @@ GCstr * LJ_FASTCALL lj_str_fromnumber(lua_State *L, cTValue *o) | |||
222 | 220 | ||
223 | static void addstr(lua_State *L, SBuf *sb, const char *str, MSize len) | 221 | static void addstr(lua_State *L, SBuf *sb, const char *str, MSize len) |
224 | { | 222 | { |
225 | char *p; | ||
226 | MSize i; | 223 | MSize i; |
227 | if (sb->n + len > sb->sz) { | 224 | char *p = lj_buf_need(L, sb, sb->n+len) + sb->n; |
228 | MSize sz = sb->sz * 2; | ||
229 | while (sb->n + len > sz) sz = sz * 2; | ||
230 | lj_str_resizebuf(L, sb, sz); | ||
231 | } | ||
232 | p = sb->buf + sb->n; | ||
233 | sb->n += len; | 225 | sb->n += len; |
234 | for (i = 0; i < len; i++) p[i] = str[i]; | 226 | for (i = 0; i < len; i++) p[i] = str[i]; |
235 | } | 227 | } |
236 | 228 | ||
237 | static void addchar(lua_State *L, SBuf *sb, int c) | 229 | static void addchar(lua_State *L, SBuf *sb, int c) |
238 | { | 230 | { |
239 | if (sb->n + 1 > sb->sz) { | 231 | char *p = lj_buf_need(L, sb, sb->n+1); |
240 | MSize sz = sb->sz * 2; | 232 | p[sb->n++] = (char)c; |
241 | lj_str_resizebuf(L, sb, sz); | ||
242 | } | ||
243 | sb->buf[sb->n++] = (char)c; | ||
244 | } | 233 | } |
245 | 234 | ||
246 | /* Push formatted message as a string object to Lua stack. va_list variant. */ | 235 | /* Push formatted message as a string object to Lua stack. va_list variant. */ |
247 | const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp) | 236 | const char *lj_str_pushvf(lua_State *L, const char *fmt, va_list argp) |
248 | { | 237 | { |
249 | SBuf *sb = &G(L)->tmpbuf; | 238 | SBuf *sb = &G(L)->tmpbuf; |
250 | lj_str_needbuf(L, sb, (MSize)strlen(fmt)); | 239 | lj_buf_need(L, sb, (MSize)strlen(fmt)); |
251 | lj_str_resetbuf(sb); | 240 | lj_buf_reset(sb); |
252 | for (;;) { | 241 | for (;;) { |
253 | const char *e = strchr(fmt, '%'); | 242 | const char *e = strchr(fmt, '%'); |
254 | if (e == NULL) break; | 243 | if (e == NULL) break; |
@@ -326,14 +315,3 @@ const char *lj_str_pushf(lua_State *L, const char *fmt, ...) | |||
326 | return msg; | 315 | return msg; |
327 | } | 316 | } |
328 | 317 | ||
329 | /* -- Buffer handling ----------------------------------------------------- */ | ||
330 | |||
331 | char *lj_str_needbuf(lua_State *L, SBuf *sb, MSize sz) | ||
332 | { | ||
333 | if (sz > sb->sz) { | ||
334 | if (sz < LJ_MIN_SBUF) sz = LJ_MIN_SBUF; | ||
335 | lj_str_resizebuf(L, sb, sz); | ||
336 | } | ||
337 | return sb->buf; | ||
338 | } | ||
339 | |||