blob: 45915e7c42695f8521dabd10cfda57c590bc6834 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Damien68f59a92013-10-20 14:39:58 +010027#include <stdio.h>
28#include <stdarg.h>
29#include <string.h>
Damien George354d15a2014-02-06 21:11:19 +000030#include <assert.h>
Damien George51dfcb42015-01-01 20:27:54 +000031
32#include "py/mpconfig.h"
33#include "py/misc.h"
Damien George7f9d1d62015-04-09 23:56:15 +010034#include "py/mpprint.h"
Damien68f59a92013-10-20 14:39:58 +010035
36// returned value is always at least 1 greater than argument
37#define ROUND_ALLOC(a) (((a) & ((~0) - 7)) + 8)
38
Damien George0d3cb672015-01-28 23:43:01 +000039// Init the vstr so it allocs exactly given number of bytes. Set length to zero.
Damien Georgeb0261342014-09-23 18:10:17 +010040void vstr_init(vstr_t *vstr, size_t alloc) {
Damien George0d3cb672015-01-28 23:43:01 +000041 if (alloc < 1) {
42 alloc = 1;
Damien George8cd72bd2014-03-31 17:10:59 +010043 }
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020044 vstr->alloc = alloc;
Damien68f59a92013-10-20 14:39:58 +010045 vstr->len = 0;
46 vstr->buf = m_new(char, vstr->alloc);
47 if (vstr->buf == NULL) {
Damien68f59a92013-10-20 14:39:58 +010048 vstr->had_error = true;
49 return;
50 }
Damien68f59a92013-10-20 14:39:58 +010051 vstr->had_error = false;
Damien George354d15a2014-02-06 21:11:19 +000052 vstr->fixed_buf = false;
53}
54
Damien George0d3cb672015-01-28 23:43:01 +000055// Init the vstr so it allocs exactly enough ram to hold given length, and set the length.
Damien George05005f62015-01-21 22:48:37 +000056void vstr_init_len(vstr_t *vstr, size_t len) {
Damien George0d3cb672015-01-28 23:43:01 +000057 vstr_init(vstr, len);
58 vstr->len = len;
Damien George05005f62015-01-21 22:48:37 +000059}
60
Damien Georgeb0261342014-09-23 18:10:17 +010061void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf) {
Damien George354d15a2014-02-06 21:11:19 +000062 vstr->alloc = alloc;
63 vstr->len = 0;
64 vstr->buf = buf;
Damien George354d15a2014-02-06 21:11:19 +000065 vstr->had_error = false;
66 vstr->fixed_buf = true;
Damien68f59a92013-10-20 14:39:58 +010067}
68
Damien George7f9d1d62015-04-09 23:56:15 +010069void vstr_init_print(vstr_t *vstr, size_t alloc, mp_print_t *print) {
70 vstr_init(vstr, alloc);
71 print->data = vstr;
72 print->print_strn = (mp_print_strn_t)vstr_add_strn;
73}
74
Damien68f59a92013-10-20 14:39:58 +010075void vstr_clear(vstr_t *vstr) {
Damien George354d15a2014-02-06 21:11:19 +000076 if (!vstr->fixed_buf) {
77 m_del(char, vstr->buf, vstr->alloc);
78 }
Damien68f59a92013-10-20 14:39:58 +010079 vstr->buf = NULL;
80}
81
Damien8b3a7c22013-10-23 20:20:17 +010082vstr_t *vstr_new(void) {
Damien George0d3cb672015-01-28 23:43:01 +000083 vstr_t *vstr = m_new_obj(vstr_t);
Damien68f59a92013-10-20 14:39:58 +010084 if (vstr == NULL) {
85 return NULL;
86 }
Damien George0d3cb672015-01-28 23:43:01 +000087 vstr_init(vstr, 16);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020088 return vstr;
89}
90
Damien Georgeb0261342014-09-23 18:10:17 +010091vstr_t *vstr_new_size(size_t alloc) {
Damien George0d3cb672015-01-28 23:43:01 +000092 vstr_t *vstr = m_new_obj(vstr_t);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020093 if (vstr == NULL) {
94 return NULL;
95 }
96 vstr_init(vstr, alloc);
Damien68f59a92013-10-20 14:39:58 +010097 return vstr;
98}
99
100void vstr_free(vstr_t *vstr) {
101 if (vstr != NULL) {
Damien George354d15a2014-02-06 21:11:19 +0000102 if (!vstr->fixed_buf) {
103 m_del(char, vstr->buf, vstr->alloc);
104 }
Damien732407f2013-12-29 19:33:23 +0000105 m_del_obj(vstr_t, vstr);
Damien68f59a92013-10-20 14:39:58 +0100106 }
107}
108
109void vstr_reset(vstr_t *vstr) {
110 vstr->len = 0;
Damien68f59a92013-10-20 14:39:58 +0100111 vstr->had_error = false;
112}
113
114bool vstr_had_error(vstr_t *vstr) {
115 return vstr->had_error;
116}
117
118char *vstr_str(vstr_t *vstr) {
119 if (vstr->had_error) {
120 return NULL;
121 }
122 return vstr->buf;
123}
124
Damien Georgeb0261342014-09-23 18:10:17 +0100125size_t vstr_len(vstr_t *vstr) {
Damien68f59a92013-10-20 14:39:58 +0100126 if (vstr->had_error) {
127 return 0;
128 }
129 return vstr->len;
130}
131
Damien George0d3cb672015-01-28 23:43:01 +0000132// Extend vstr strictly by requested size, return pointer to newly added chunk.
Damien Georgeb0261342014-09-23 18:10:17 +0100133char *vstr_extend(vstr_t *vstr, size_t size) {
Damien George354d15a2014-02-06 21:11:19 +0000134 if (vstr->fixed_buf) {
135 return NULL;
136 }
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +0200137 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size);
138 if (new_buf == NULL) {
139 vstr->had_error = true;
140 return NULL;
141 }
142 char *p = new_buf + vstr->alloc;
143 vstr->alloc += size;
144 vstr->buf = new_buf;
145 return p;
146}
147
Damien Georgeb0261342014-09-23 18:10:17 +0100148STATIC bool vstr_ensure_extra(vstr_t *vstr, size_t size) {
Damien George0d3cb672015-01-28 23:43:01 +0000149 if (vstr->len + size > vstr->alloc) {
Damien George354d15a2014-02-06 21:11:19 +0000150 if (vstr->fixed_buf) {
151 return false;
152 }
Damien George0d3cb672015-01-28 23:43:01 +0000153 size_t new_alloc = ROUND_ALLOC((vstr->len + size) * 2);
Damien732407f2013-12-29 19:33:23 +0000154 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc);
Damien68f59a92013-10-20 14:39:58 +0100155 if (new_buf == NULL) {
156 vstr->had_error = true;
157 return false;
158 }
159 vstr->alloc = new_alloc;
160 vstr->buf = new_buf;
161 }
162 return true;
163}
164
Damien Georgeb0261342014-09-23 18:10:17 +0100165void vstr_hint_size(vstr_t *vstr, size_t size) {
Damien68f59a92013-10-20 14:39:58 +0100166 // it's not an error if we fail to allocate for the size hint
167 bool er = vstr->had_error;
168 vstr_ensure_extra(vstr, size);
169 vstr->had_error = er;
170}
171
Damien Georgeb0261342014-09-23 18:10:17 +0100172char *vstr_add_len(vstr_t *vstr, size_t len) {
Damien68f59a92013-10-20 14:39:58 +0100173 if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
174 return NULL;
175 }
176 char *buf = vstr->buf + vstr->len;
177 vstr->len += len;
Damien68f59a92013-10-20 14:39:58 +0100178 return buf;
179}
180
Damien George0d3cb672015-01-28 23:43:01 +0000181// Doesn't increase len, just makes sure there is a null byte at the end
Damien George827b0f72015-01-29 13:57:23 +0000182char *vstr_null_terminated_str(vstr_t *vstr) {
Damien George0d3cb672015-01-28 23:43:01 +0000183 if (vstr->had_error || !vstr_ensure_extra(vstr, 1)) {
Damien George827b0f72015-01-29 13:57:23 +0000184 return NULL;
Damien George0d3cb672015-01-28 23:43:01 +0000185 }
186 vstr->buf[vstr->len] = '\0';
Damien George827b0f72015-01-29 13:57:23 +0000187 return vstr->buf;
Damien George0d3cb672015-01-28 23:43:01 +0000188}
189
Damien George280e7202014-03-15 14:33:09 +0000190void vstr_add_byte(vstr_t *vstr, byte b) {
Damien68f59a92013-10-20 14:39:58 +0100191 byte *buf = (byte*)vstr_add_len(vstr, 1);
192 if (buf == NULL) {
193 return;
194 }
Damien George280e7202014-03-15 14:33:09 +0000195 buf[0] = b;
Damien68f59a92013-10-20 14:39:58 +0100196}
197
198void vstr_add_char(vstr_t *vstr, unichar c) {
Paul Sokolovsky165eb692014-06-13 02:42:34 +0300199#if MICROPY_PY_BUILTINS_STR_UNICODE
Chris Angelico2ba22992014-06-04 05:28:12 +1000200 // TODO: Can this be simplified and deduplicated?
201 // Is it worth just calling vstr_add_len(vstr, 4)?
202 if (c < 0x80) {
203 byte *buf = (byte*)vstr_add_len(vstr, 1);
204 if (buf == NULL) {
205 return;
206 }
207 *buf = (byte)c;
208 } else if (c < 0x800) {
209 byte *buf = (byte*)vstr_add_len(vstr, 2);
210 if (buf == NULL) {
211 return;
212 }
213 buf[0] = (c >> 6) | 0xC0;
214 buf[1] = (c & 0x3F) | 0x80;
215 } else if (c < 0x10000) {
216 byte *buf = (byte*)vstr_add_len(vstr, 3);
217 if (buf == NULL) {
218 return;
219 }
220 buf[0] = (c >> 12) | 0xE0;
221 buf[1] = ((c >> 6) & 0x3F) | 0x80;
222 buf[2] = (c & 0x3F) | 0x80;
223 } else {
224 assert(c < 0x110000);
225 byte *buf = (byte*)vstr_add_len(vstr, 4);
226 if (buf == NULL) {
227 return;
228 }
229 buf[0] = (c >> 18) | 0xF0;
230 buf[1] = ((c >> 12) & 0x3F) | 0x80;
231 buf[2] = ((c >> 6) & 0x3F) | 0x80;
232 buf[3] = (c & 0x3F) | 0x80;
Damien68f59a92013-10-20 14:39:58 +0100233 }
Paul Sokolovsky165eb692014-06-13 02:42:34 +0300234#else
Damien George0d3cb672015-01-28 23:43:01 +0000235 vstr_add_byte(vstr, c);
Paul Sokolovsky165eb692014-06-13 02:42:34 +0300236#endif
Damien68f59a92013-10-20 14:39:58 +0100237}
238
239void vstr_add_str(vstr_t *vstr, const char *str) {
240 vstr_add_strn(vstr, str, strlen(str));
241}
242
Damien Georgeb0261342014-09-23 18:10:17 +0100243void vstr_add_strn(vstr_t *vstr, const char *str, size_t len) {
Damien68f59a92013-10-20 14:39:58 +0100244 if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
Damien George354d15a2014-02-06 21:11:19 +0000245 // if buf is fixed, we got here because there isn't enough room left
Damien George0d3cb672015-01-28 23:43:01 +0000246 // so just try to copy as much as we can, with room for a possible null byte
Damien George354d15a2014-02-06 21:11:19 +0000247 if (vstr->fixed_buf && vstr->len + 1 < vstr->alloc) {
248 len = vstr->alloc - vstr->len - 1;
249 goto copy;
250 }
Damien68f59a92013-10-20 14:39:58 +0100251 return;
252 }
Damien George354d15a2014-02-06 21:11:19 +0000253copy:
Damien68f59a92013-10-20 14:39:58 +0100254 memmove(vstr->buf + vstr->len, str, len);
255 vstr->len += len;
Damien68f59a92013-10-20 14:39:58 +0100256}
257
Damien George969a6b32014-12-10 22:07:04 +0000258STATIC char *vstr_ins_blank_bytes(vstr_t *vstr, size_t byte_pos, size_t byte_len) {
Damien George280e7202014-03-15 14:33:09 +0000259 if (vstr->had_error) {
260 return NULL;
261 }
Damien Georgeb0261342014-09-23 18:10:17 +0100262 size_t l = vstr->len;
Damien George280e7202014-03-15 14:33:09 +0000263 if (byte_pos > l) {
264 byte_pos = l;
265 }
266 if (byte_len > 0) {
267 // ensure room for the new bytes
268 if (!vstr_ensure_extra(vstr, byte_len)) {
269 return NULL;
270 }
Damien George0d3cb672015-01-28 23:43:01 +0000271 // copy up the string to make room for the new bytes
272 memmove(vstr->buf + byte_pos + byte_len, vstr->buf + byte_pos, l - byte_pos);
Damien George280e7202014-03-15 14:33:09 +0000273 // increase the length
274 vstr->len += byte_len;
Damien George280e7202014-03-15 14:33:09 +0000275 }
276 return vstr->buf + byte_pos;
277}
278
Damien Georgeb0261342014-09-23 18:10:17 +0100279void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b) {
Damien George280e7202014-03-15 14:33:09 +0000280 char *s = vstr_ins_blank_bytes(vstr, byte_pos, 1);
281 if (s != NULL) {
282 *s = b;
283 }
284}
285
Damien Georgeb0261342014-09-23 18:10:17 +0100286void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr) {
Damien George280e7202014-03-15 14:33:09 +0000287 // TODO UNICODE
Damien Georgeecd58ae2014-03-15 16:54:06 +0000288 char *s = vstr_ins_blank_bytes(vstr, char_pos, 1);
Damien George280e7202014-03-15 14:33:09 +0000289 if (s != NULL) {
290 *s = chr;
291 }
292}
293
Damien Georgeb0261342014-09-23 18:10:17 +0100294void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut) {
Damien George280e7202014-03-15 14:33:09 +0000295 vstr_cut_out_bytes(vstr, 0, bytes_to_cut);
296}
297
Damien Georgeb0261342014-09-23 18:10:17 +0100298void vstr_cut_tail_bytes(vstr_t *vstr, size_t len) {
Damien68f59a92013-10-20 14:39:58 +0100299 if (vstr->had_error) {
300 return;
301 }
302 if (len > vstr->len) {
303 vstr->len = 0;
304 } else {
305 vstr->len -= len;
306 }
307}
308
Damien Georgeb0261342014-09-23 18:10:17 +0100309void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut) {
Damien George280e7202014-03-15 14:33:09 +0000310 if (vstr->had_error || byte_pos >= vstr->len) {
311 return;
312 } else if (byte_pos + bytes_to_cut >= vstr->len) {
313 vstr->len = byte_pos;
Damien George280e7202014-03-15 14:33:09 +0000314 } else {
Damien George0d3cb672015-01-28 23:43:01 +0000315 memmove(vstr->buf + byte_pos, vstr->buf + byte_pos + bytes_to_cut, vstr->len - byte_pos - bytes_to_cut);
Damien George280e7202014-03-15 14:33:09 +0000316 vstr->len -= bytes_to_cut;
317 }
318}
319
Damien68f59a92013-10-20 14:39:58 +0100320void vstr_printf(vstr_t *vstr, const char *fmt, ...) {
Damien2f06c572013-11-03 18:20:56 +0000321 va_list ap;
322 va_start(ap, fmt);
323 vstr_vprintf(vstr, fmt, ap);
324 va_end(ap);
325}
326
327void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap) {
Damien68f59a92013-10-20 14:39:58 +0100328 if (vstr->had_error || !vstr_ensure_extra(vstr, strlen(fmt))) {
329 return;
330 }
331
Damien George7f9d1d62015-04-09 23:56:15 +0100332 mp_print_t print = {vstr, (mp_print_strn_t)vstr_add_strn};
333 mp_vprintf(&print, fmt, ap);
Damien68f59a92013-10-20 14:39:58 +0100334}