diff options
Diffstat (limited to '')
-rw-r--r-- | src/lj_buf.c | 234 |
1 files changed, 234 insertions, 0 deletions
diff --git a/src/lj_buf.c b/src/lj_buf.c new file mode 100644 index 00000000..1f6e97bf --- /dev/null +++ b/src/lj_buf.c | |||
@@ -0,0 +1,234 @@ | |||
1 | /* | ||
2 | ** Buffer handling. | ||
3 | ** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h | ||
4 | */ | ||
5 | |||
6 | #define lj_buf_c | ||
7 | #define LUA_CORE | ||
8 | |||
9 | #include "lj_obj.h" | ||
10 | #include "lj_gc.h" | ||
11 | #include "lj_err.h" | ||
12 | #include "lj_buf.h" | ||
13 | #include "lj_str.h" | ||
14 | #include "lj_tab.h" | ||
15 | #include "lj_strfmt.h" | ||
16 | |||
17 | /* -- Buffer management --------------------------------------------------- */ | ||
18 | |||
19 | static void buf_grow(SBuf *sb, MSize sz) | ||
20 | { | ||
21 | MSize osz = sbufsz(sb), len = sbuflen(sb), nsz = osz; | ||
22 | char *b; | ||
23 | if (nsz < LJ_MIN_SBUF) nsz = LJ_MIN_SBUF; | ||
24 | while (nsz < sz) nsz += nsz; | ||
25 | b = (char *)lj_mem_realloc(sbufL(sb), sbufB(sb), osz, nsz); | ||
26 | setmref(sb->b, b); | ||
27 | setmref(sb->p, b + len); | ||
28 | setmref(sb->e, b + nsz); | ||
29 | } | ||
30 | |||
31 | LJ_NOINLINE char *LJ_FASTCALL lj_buf_need2(SBuf *sb, MSize sz) | ||
32 | { | ||
33 | lua_assert(sz > sbufsz(sb)); | ||
34 | if (LJ_UNLIKELY(sz > LJ_MAX_MEM)) | ||
35 | lj_err_mem(sbufL(sb)); | ||
36 | buf_grow(sb, sz); | ||
37 | return sbufB(sb); | ||
38 | } | ||
39 | |||
40 | LJ_NOINLINE char *LJ_FASTCALL lj_buf_more2(SBuf *sb, MSize sz) | ||
41 | { | ||
42 | MSize len = sbuflen(sb); | ||
43 | lua_assert(sz > sbufleft(sb)); | ||
44 | if (LJ_UNLIKELY(sz > LJ_MAX_MEM || len + sz > LJ_MAX_MEM)) | ||
45 | lj_err_mem(sbufL(sb)); | ||
46 | buf_grow(sb, len + sz); | ||
47 | return sbufP(sb); | ||
48 | } | ||
49 | |||
50 | void LJ_FASTCALL lj_buf_shrink(lua_State *L, SBuf *sb) | ||
51 | { | ||
52 | char *b = sbufB(sb); | ||
53 | MSize osz = (MSize)(sbufE(sb) - b); | ||
54 | if (osz > 2*LJ_MIN_SBUF) { | ||
55 | MSize n = (MSize)(sbufP(sb) - b); | ||
56 | b = lj_mem_realloc(L, b, osz, (osz >> 1)); | ||
57 | setmref(sb->b, b); | ||
58 | setmref(sb->p, b + n); | ||
59 | setmref(sb->e, b + (osz >> 1)); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | char * LJ_FASTCALL lj_buf_tmp(lua_State *L, MSize sz) | ||
64 | { | ||
65 | SBuf *sb = &G(L)->tmpbuf; | ||
66 | setsbufL(sb, L); | ||
67 | return lj_buf_need(sb, sz); | ||
68 | } | ||
69 | |||
70 | /* -- Low-level buffer put operations ------------------------------------- */ | ||
71 | |||
72 | SBuf *lj_buf_putmem(SBuf *sb, const void *q, MSize len) | ||
73 | { | ||
74 | char *p = lj_buf_more(sb, len); | ||
75 | p = lj_buf_wmem(p, q, len); | ||
76 | setsbufP(sb, p); | ||
77 | return sb; | ||
78 | } | ||
79 | |||
80 | #if LJ_HASJIT | ||
81 | SBuf * LJ_FASTCALL lj_buf_putchar(SBuf *sb, int c) | ||
82 | { | ||
83 | char *p = lj_buf_more(sb, 1); | ||
84 | *p++ = (char)c; | ||
85 | setsbufP(sb, p); | ||
86 | return sb; | ||
87 | } | ||
88 | #endif | ||
89 | |||
90 | SBuf * LJ_FASTCALL lj_buf_putstr(SBuf *sb, GCstr *s) | ||
91 | { | ||
92 | MSize len = s->len; | ||
93 | char *p = lj_buf_more(sb, len); | ||
94 | p = lj_buf_wmem(p, strdata(s), len); | ||
95 | setsbufP(sb, p); | ||
96 | return sb; | ||
97 | } | ||
98 | |||
99 | /* -- High-level buffer put operations ------------------------------------ */ | ||
100 | |||
101 | SBuf * LJ_FASTCALL lj_buf_putstr_reverse(SBuf *sb, GCstr *s) | ||
102 | { | ||
103 | MSize len = s->len; | ||
104 | char *p = lj_buf_more(sb, len), *e = p+len; | ||
105 | const char *q = strdata(s)+len-1; | ||
106 | while (p < e) | ||
107 | *p++ = *q--; | ||
108 | setsbufP(sb, p); | ||
109 | return sb; | ||
110 | } | ||
111 | |||
112 | SBuf * LJ_FASTCALL lj_buf_putstr_lower(SBuf *sb, GCstr *s) | ||
113 | { | ||
114 | MSize len = s->len; | ||
115 | char *p = lj_buf_more(sb, len), *e = p+len; | ||
116 | const char *q = strdata(s); | ||
117 | for (; p < e; p++, q++) { | ||
118 | uint32_t c = *(unsigned char *)q; | ||
119 | #if LJ_TARGET_PPC | ||
120 | *p = c + ((c >= 'A' && c <= 'Z') << 5); | ||
121 | #else | ||
122 | if (c >= 'A' && c <= 'Z') c += 0x20; | ||
123 | *p = c; | ||
124 | #endif | ||
125 | } | ||
126 | setsbufP(sb, p); | ||
127 | return sb; | ||
128 | } | ||
129 | |||
130 | SBuf * LJ_FASTCALL lj_buf_putstr_upper(SBuf *sb, GCstr *s) | ||
131 | { | ||
132 | MSize len = s->len; | ||
133 | char *p = lj_buf_more(sb, len), *e = p+len; | ||
134 | const char *q = strdata(s); | ||
135 | for (; p < e; p++, q++) { | ||
136 | uint32_t c = *(unsigned char *)q; | ||
137 | #if LJ_TARGET_PPC | ||
138 | *p = c - ((c >= 'a' && c <= 'z') << 5); | ||
139 | #else | ||
140 | if (c >= 'a' && c <= 'z') c -= 0x20; | ||
141 | *p = c; | ||
142 | #endif | ||
143 | } | ||
144 | setsbufP(sb, p); | ||
145 | return sb; | ||
146 | } | ||
147 | |||
148 | SBuf *lj_buf_putstr_rep(SBuf *sb, GCstr *s, int32_t rep) | ||
149 | { | ||
150 | MSize len = s->len; | ||
151 | if (rep > 0 && len) { | ||
152 | uint64_t tlen = (uint64_t)rep * len; | ||
153 | char *p; | ||
154 | if (LJ_UNLIKELY(tlen > LJ_MAX_STR)) | ||
155 | lj_err_mem(sbufL(sb)); | ||
156 | p = lj_buf_more(sb, (MSize)tlen); | ||
157 | if (len == 1) { /* Optimize a common case. */ | ||
158 | uint32_t c = strdata(s)[0]; | ||
159 | do { *p++ = c; } while (--rep > 0); | ||
160 | } else { | ||
161 | const char *e = strdata(s) + len; | ||
162 | do { | ||
163 | const char *q = strdata(s); | ||
164 | do { *p++ = *q++; } while (q < e); | ||
165 | } while (--rep > 0); | ||
166 | } | ||
167 | setsbufP(sb, p); | ||
168 | } | ||
169 | return sb; | ||
170 | } | ||
171 | |||
172 | SBuf *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_strfmt_wint(lj_buf_more(sb, STRFMT_MAXBUF_INT+seplen), intV(o)); | ||
188 | } else if (tvisnum(o)) { | ||
189 | p = lj_strfmt_wnum(lj_buf_more(sb, STRFMT_MAXBUF_NUM+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 | |||
204 | /* -- Miscellaneous buffer operations ------------------------------------- */ | ||
205 | |||
206 | GCstr * LJ_FASTCALL lj_buf_tostr(SBuf *sb) | ||
207 | { | ||
208 | return lj_str_new(sbufL(sb), sbufB(sb), sbuflen(sb)); | ||
209 | } | ||
210 | |||
211 | /* Concatenate two strings. */ | ||
212 | GCstr *lj_buf_cat2str(lua_State *L, GCstr *s1, GCstr *s2) | ||
213 | { | ||
214 | MSize len1 = s1->len, len2 = s2->len; | ||
215 | char *buf = lj_buf_tmp(L, len1 + len2); | ||
216 | memcpy(buf, strdata(s1), len1); | ||
217 | memcpy(buf+len1, strdata(s2), len2); | ||
218 | return lj_str_new(L, buf, len1 + len2); | ||
219 | } | ||
220 | |||
221 | /* Read ULEB128 from buffer. */ | ||
222 | uint32_t LJ_FASTCALL lj_buf_ruleb128(const char **pp) | ||
223 | { | ||
224 | const uint8_t *p = (const uint8_t *)*pp; | ||
225 | uint32_t v = *p++; | ||
226 | if (LJ_UNLIKELY(v >= 0x80)) { | ||
227 | int sh = 0; | ||
228 | v &= 0x7f; | ||
229 | do { v |= ((*p & 0x7f) << (sh += 7)); } while (*p++ >= 0x80); | ||
230 | } | ||
231 | *pp = (const char *)p; | ||
232 | return v; | ||
233 | } | ||
234 | |||