blob: f558e3fcb1aeb8bc162d6aa84f7dec8974eee81d [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
Dave Hylands9f76dcd2015-05-18 13:25:36 -070055// Init the vstr so it allocs exactly enough ram to hold a null-terminated
56// string of the given length, and set the length.
Damien George05005f62015-01-21 22:48:37 +000057void vstr_init_len(vstr_t *vstr, size_t len) {
Dave Hylands9f76dcd2015-05-18 13:25:36 -070058 vstr_init(vstr, len + 1);
Damien George0d3cb672015-01-28 23:43:01 +000059 vstr->len = len;
Damien George05005f62015-01-21 22:48:37 +000060}
61
Damien Georgeb0261342014-09-23 18:10:17 +010062void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf) {
Damien George354d15a2014-02-06 21:11:19 +000063 vstr->alloc = alloc;
64 vstr->len = 0;
65 vstr->buf = buf;
Damien George354d15a2014-02-06 21:11:19 +000066 vstr->had_error = false;
67 vstr->fixed_buf = true;
Damien68f59a92013-10-20 14:39:58 +010068}
69
Damien George7f9d1d62015-04-09 23:56:15 +010070void vstr_init_print(vstr_t *vstr, size_t alloc, mp_print_t *print) {
71 vstr_init(vstr, alloc);
72 print->data = vstr;
73 print->print_strn = (mp_print_strn_t)vstr_add_strn;
74}
75
Damien68f59a92013-10-20 14:39:58 +010076void vstr_clear(vstr_t *vstr) {
Damien George354d15a2014-02-06 21:11:19 +000077 if (!vstr->fixed_buf) {
78 m_del(char, vstr->buf, vstr->alloc);
79 }
Damien68f59a92013-10-20 14:39:58 +010080 vstr->buf = NULL;
81}
82
Damien8b3a7c22013-10-23 20:20:17 +010083vstr_t *vstr_new(void) {
Damien George0d3cb672015-01-28 23:43:01 +000084 vstr_t *vstr = m_new_obj(vstr_t);
Damien68f59a92013-10-20 14:39:58 +010085 if (vstr == NULL) {
86 return NULL;
87 }
Damien George0d3cb672015-01-28 23:43:01 +000088 vstr_init(vstr, 16);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020089 return vstr;
90}
91
Damien Georgeb0261342014-09-23 18:10:17 +010092vstr_t *vstr_new_size(size_t alloc) {
Damien George0d3cb672015-01-28 23:43:01 +000093 vstr_t *vstr = m_new_obj(vstr_t);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020094 if (vstr == NULL) {
95 return NULL;
96 }
97 vstr_init(vstr, alloc);
Damien68f59a92013-10-20 14:39:58 +010098 return vstr;
99}
100
101void vstr_free(vstr_t *vstr) {
102 if (vstr != NULL) {
Damien George354d15a2014-02-06 21:11:19 +0000103 if (!vstr->fixed_buf) {
104 m_del(char, vstr->buf, vstr->alloc);
105 }
Damien732407f2013-12-29 19:33:23 +0000106 m_del_obj(vstr_t, vstr);
Damien68f59a92013-10-20 14:39:58 +0100107 }
108}
109
110void vstr_reset(vstr_t *vstr) {
111 vstr->len = 0;
Damien68f59a92013-10-20 14:39:58 +0100112 vstr->had_error = false;
113}
114
115bool vstr_had_error(vstr_t *vstr) {
116 return vstr->had_error;
117}
118
119char *vstr_str(vstr_t *vstr) {
120 if (vstr->had_error) {
121 return NULL;
122 }
123 return vstr->buf;
124}
125
Damien Georgeb0261342014-09-23 18:10:17 +0100126size_t vstr_len(vstr_t *vstr) {
Damien68f59a92013-10-20 14:39:58 +0100127 if (vstr->had_error) {
128 return 0;
129 }
130 return vstr->len;
131}
132
Damien George0d3cb672015-01-28 23:43:01 +0000133// Extend vstr strictly by requested size, return pointer to newly added chunk.
Damien Georgeb0261342014-09-23 18:10:17 +0100134char *vstr_extend(vstr_t *vstr, size_t size) {
Damien George354d15a2014-02-06 21:11:19 +0000135 if (vstr->fixed_buf) {
136 return NULL;
137 }
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +0200138 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size);
139 if (new_buf == NULL) {
140 vstr->had_error = true;
141 return NULL;
142 }
143 char *p = new_buf + vstr->alloc;
144 vstr->alloc += size;
145 vstr->buf = new_buf;
146 return p;
147}
148
Damien Georgeb0261342014-09-23 18:10:17 +0100149STATIC bool vstr_ensure_extra(vstr_t *vstr, size_t size) {
Damien George0d3cb672015-01-28 23:43:01 +0000150 if (vstr->len + size > vstr->alloc) {
Damien George354d15a2014-02-06 21:11:19 +0000151 if (vstr->fixed_buf) {
152 return false;
153 }
Damien George0d3cb672015-01-28 23:43:01 +0000154 size_t new_alloc = ROUND_ALLOC((vstr->len + size) * 2);
Damien732407f2013-12-29 19:33:23 +0000155 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc);
Damien68f59a92013-10-20 14:39:58 +0100156 if (new_buf == NULL) {
157 vstr->had_error = true;
158 return false;
159 }
160 vstr->alloc = new_alloc;
161 vstr->buf = new_buf;
162 }
163 return true;
164}
165
Damien Georgeb0261342014-09-23 18:10:17 +0100166void vstr_hint_size(vstr_t *vstr, size_t size) {
Damien68f59a92013-10-20 14:39:58 +0100167 // it's not an error if we fail to allocate for the size hint
168 bool er = vstr->had_error;
169 vstr_ensure_extra(vstr, size);
170 vstr->had_error = er;
171}
172
Damien Georgeb0261342014-09-23 18:10:17 +0100173char *vstr_add_len(vstr_t *vstr, size_t len) {
Damien68f59a92013-10-20 14:39:58 +0100174 if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
175 return NULL;
176 }
177 char *buf = vstr->buf + vstr->len;
178 vstr->len += len;
Damien68f59a92013-10-20 14:39:58 +0100179 return buf;
180}
181
Damien George0d3cb672015-01-28 23:43:01 +0000182// Doesn't increase len, just makes sure there is a null byte at the end
Damien George827b0f72015-01-29 13:57:23 +0000183char *vstr_null_terminated_str(vstr_t *vstr) {
Paul Sokolovskya1f22452016-05-09 22:39:57 +0300184 if (vstr->had_error) {
Damien George827b0f72015-01-29 13:57:23 +0000185 return NULL;
Damien George0d3cb672015-01-28 23:43:01 +0000186 }
Paul Sokolovskya1f22452016-05-09 22:39:57 +0300187 // If there's no more room, add single byte
188 if (vstr->alloc == vstr->len) {
189 if (vstr_extend(vstr, 1) == NULL) {
190 return NULL;
191 }
192 }
Damien George0d3cb672015-01-28 23:43:01 +0000193 vstr->buf[vstr->len] = '\0';
Damien George827b0f72015-01-29 13:57:23 +0000194 return vstr->buf;
Damien George0d3cb672015-01-28 23:43:01 +0000195}
196
Damien George280e7202014-03-15 14:33:09 +0000197void vstr_add_byte(vstr_t *vstr, byte b) {
Damien68f59a92013-10-20 14:39:58 +0100198 byte *buf = (byte*)vstr_add_len(vstr, 1);
199 if (buf == NULL) {
200 return;
201 }
Damien George280e7202014-03-15 14:33:09 +0000202 buf[0] = b;
Damien68f59a92013-10-20 14:39:58 +0100203}
204
205void vstr_add_char(vstr_t *vstr, unichar c) {
Paul Sokolovsky165eb692014-06-13 02:42:34 +0300206#if MICROPY_PY_BUILTINS_STR_UNICODE
Chris Angelico2ba22992014-06-04 05:28:12 +1000207 // TODO: Can this be simplified and deduplicated?
208 // Is it worth just calling vstr_add_len(vstr, 4)?
209 if (c < 0x80) {
210 byte *buf = (byte*)vstr_add_len(vstr, 1);
211 if (buf == NULL) {
212 return;
213 }
214 *buf = (byte)c;
215 } else if (c < 0x800) {
216 byte *buf = (byte*)vstr_add_len(vstr, 2);
217 if (buf == NULL) {
218 return;
219 }
220 buf[0] = (c >> 6) | 0xC0;
221 buf[1] = (c & 0x3F) | 0x80;
222 } else if (c < 0x10000) {
223 byte *buf = (byte*)vstr_add_len(vstr, 3);
224 if (buf == NULL) {
225 return;
226 }
227 buf[0] = (c >> 12) | 0xE0;
228 buf[1] = ((c >> 6) & 0x3F) | 0x80;
229 buf[2] = (c & 0x3F) | 0x80;
230 } else {
231 assert(c < 0x110000);
232 byte *buf = (byte*)vstr_add_len(vstr, 4);
233 if (buf == NULL) {
234 return;
235 }
236 buf[0] = (c >> 18) | 0xF0;
237 buf[1] = ((c >> 12) & 0x3F) | 0x80;
238 buf[2] = ((c >> 6) & 0x3F) | 0x80;
239 buf[3] = (c & 0x3F) | 0x80;
Damien68f59a92013-10-20 14:39:58 +0100240 }
Paul Sokolovsky165eb692014-06-13 02:42:34 +0300241#else
Damien George0d3cb672015-01-28 23:43:01 +0000242 vstr_add_byte(vstr, c);
Paul Sokolovsky165eb692014-06-13 02:42:34 +0300243#endif
Damien68f59a92013-10-20 14:39:58 +0100244}
245
246void vstr_add_str(vstr_t *vstr, const char *str) {
247 vstr_add_strn(vstr, str, strlen(str));
248}
249
Damien Georgeb0261342014-09-23 18:10:17 +0100250void vstr_add_strn(vstr_t *vstr, const char *str, size_t len) {
Damien68f59a92013-10-20 14:39:58 +0100251 if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
Damien George354d15a2014-02-06 21:11:19 +0000252 // if buf is fixed, we got here because there isn't enough room left
Damien George0d3cb672015-01-28 23:43:01 +0000253 // so just try to copy as much as we can, with room for a possible null byte
Damien George1c9a4992015-04-18 14:23:13 +0100254 if (vstr->fixed_buf && vstr->len < vstr->alloc) {
255 len = vstr->alloc - vstr->len;
Damien George354d15a2014-02-06 21:11:19 +0000256 goto copy;
257 }
Damien68f59a92013-10-20 14:39:58 +0100258 return;
259 }
Damien George354d15a2014-02-06 21:11:19 +0000260copy:
Damien68f59a92013-10-20 14:39:58 +0100261 memmove(vstr->buf + vstr->len, str, len);
262 vstr->len += len;
Damien68f59a92013-10-20 14:39:58 +0100263}
264
Damien George969a6b32014-12-10 22:07:04 +0000265STATIC char *vstr_ins_blank_bytes(vstr_t *vstr, size_t byte_pos, size_t byte_len) {
Damien George280e7202014-03-15 14:33:09 +0000266 if (vstr->had_error) {
267 return NULL;
268 }
Damien Georgeb0261342014-09-23 18:10:17 +0100269 size_t l = vstr->len;
Damien George280e7202014-03-15 14:33:09 +0000270 if (byte_pos > l) {
271 byte_pos = l;
272 }
273 if (byte_len > 0) {
274 // ensure room for the new bytes
275 if (!vstr_ensure_extra(vstr, byte_len)) {
276 return NULL;
277 }
Damien George0d3cb672015-01-28 23:43:01 +0000278 // copy up the string to make room for the new bytes
279 memmove(vstr->buf + byte_pos + byte_len, vstr->buf + byte_pos, l - byte_pos);
Damien George280e7202014-03-15 14:33:09 +0000280 // increase the length
281 vstr->len += byte_len;
Damien George280e7202014-03-15 14:33:09 +0000282 }
283 return vstr->buf + byte_pos;
284}
285
Damien Georgeb0261342014-09-23 18:10:17 +0100286void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b) {
Damien George280e7202014-03-15 14:33:09 +0000287 char *s = vstr_ins_blank_bytes(vstr, byte_pos, 1);
288 if (s != NULL) {
289 *s = b;
290 }
291}
292
Damien Georgeb0261342014-09-23 18:10:17 +0100293void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr) {
Damien George280e7202014-03-15 14:33:09 +0000294 // TODO UNICODE
Damien Georgeecd58ae2014-03-15 16:54:06 +0000295 char *s = vstr_ins_blank_bytes(vstr, char_pos, 1);
Damien George280e7202014-03-15 14:33:09 +0000296 if (s != NULL) {
297 *s = chr;
298 }
299}
300
Damien Georgeb0261342014-09-23 18:10:17 +0100301void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut) {
Damien George280e7202014-03-15 14:33:09 +0000302 vstr_cut_out_bytes(vstr, 0, bytes_to_cut);
303}
304
Damien Georgeb0261342014-09-23 18:10:17 +0100305void vstr_cut_tail_bytes(vstr_t *vstr, size_t len) {
Damien68f59a92013-10-20 14:39:58 +0100306 if (vstr->had_error) {
307 return;
308 }
309 if (len > vstr->len) {
310 vstr->len = 0;
311 } else {
312 vstr->len -= len;
313 }
314}
315
Damien Georgeb0261342014-09-23 18:10:17 +0100316void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut) {
Damien George280e7202014-03-15 14:33:09 +0000317 if (vstr->had_error || byte_pos >= vstr->len) {
318 return;
319 } else if (byte_pos + bytes_to_cut >= vstr->len) {
320 vstr->len = byte_pos;
Damien George280e7202014-03-15 14:33:09 +0000321 } else {
Damien George0d3cb672015-01-28 23:43:01 +0000322 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 +0000323 vstr->len -= bytes_to_cut;
324 }
325}
326
Damien68f59a92013-10-20 14:39:58 +0100327void vstr_printf(vstr_t *vstr, const char *fmt, ...) {
Damien2f06c572013-11-03 18:20:56 +0000328 va_list ap;
329 va_start(ap, fmt);
330 vstr_vprintf(vstr, fmt, ap);
331 va_end(ap);
332}
333
334void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap) {
Damien George7f9d1d62015-04-09 23:56:15 +0100335 mp_print_t print = {vstr, (mp_print_strn_t)vstr_add_strn};
336 mp_vprintf(&print, fmt, ap);
Damien68f59a92013-10-20 14:39:58 +0100337}