blob: 5096475f10414b93bfe678629b6c0b8caa82e4a9 [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);
Damien George354d15a2014-02-06 21:11:19 +000047 vstr->fixed_buf = false;
48}
49
Dave Hylands9f76dcd2015-05-18 13:25:36 -070050// Init the vstr so it allocs exactly enough ram to hold a null-terminated
51// string of the given length, and set the length.
Damien George05005f62015-01-21 22:48:37 +000052void vstr_init_len(vstr_t *vstr, size_t len) {
Dave Hylands9f76dcd2015-05-18 13:25:36 -070053 vstr_init(vstr, len + 1);
Damien George0d3cb672015-01-28 23:43:01 +000054 vstr->len = len;
Damien George05005f62015-01-21 22:48:37 +000055}
56
Damien Georgeb0261342014-09-23 18:10:17 +010057void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf) {
Damien George354d15a2014-02-06 21:11:19 +000058 vstr->alloc = alloc;
59 vstr->len = 0;
60 vstr->buf = buf;
Damien George354d15a2014-02-06 21:11:19 +000061 vstr->fixed_buf = true;
Damien68f59a92013-10-20 14:39:58 +010062}
63
Damien George7f9d1d62015-04-09 23:56:15 +010064void vstr_init_print(vstr_t *vstr, size_t alloc, mp_print_t *print) {
65 vstr_init(vstr, alloc);
66 print->data = vstr;
67 print->print_strn = (mp_print_strn_t)vstr_add_strn;
68}
69
Damien68f59a92013-10-20 14:39:58 +010070void vstr_clear(vstr_t *vstr) {
Damien George354d15a2014-02-06 21:11:19 +000071 if (!vstr->fixed_buf) {
72 m_del(char, vstr->buf, vstr->alloc);
73 }
Damien68f59a92013-10-20 14:39:58 +010074 vstr->buf = NULL;
75}
76
Damien8b3a7c22013-10-23 20:20:17 +010077vstr_t *vstr_new(void) {
Damien George0d3cb672015-01-28 23:43:01 +000078 vstr_t *vstr = m_new_obj(vstr_t);
Damien68f59a92013-10-20 14:39:58 +010079 if (vstr == NULL) {
80 return NULL;
81 }
Damien George0d3cb672015-01-28 23:43:01 +000082 vstr_init(vstr, 16);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020083 return vstr;
84}
85
Damien Georgeb0261342014-09-23 18:10:17 +010086vstr_t *vstr_new_size(size_t alloc) {
Damien George0d3cb672015-01-28 23:43:01 +000087 vstr_t *vstr = m_new_obj(vstr_t);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020088 if (vstr == NULL) {
89 return NULL;
90 }
91 vstr_init(vstr, alloc);
Damien68f59a92013-10-20 14:39:58 +010092 return vstr;
93}
94
95void vstr_free(vstr_t *vstr) {
96 if (vstr != NULL) {
Damien George354d15a2014-02-06 21:11:19 +000097 if (!vstr->fixed_buf) {
98 m_del(char, vstr->buf, vstr->alloc);
99 }
Damien732407f2013-12-29 19:33:23 +0000100 m_del_obj(vstr_t, vstr);
Damien68f59a92013-10-20 14:39:58 +0100101 }
102}
103
Damien George0d3cb672015-01-28 23:43:01 +0000104// Extend vstr strictly by requested size, return pointer to newly added chunk.
Damien Georgeb0261342014-09-23 18:10:17 +0100105char *vstr_extend(vstr_t *vstr, size_t size) {
Damien George354d15a2014-02-06 21:11:19 +0000106 if (vstr->fixed_buf) {
107 return NULL;
108 }
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +0200109 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +0200110 char *p = new_buf + vstr->alloc;
111 vstr->alloc += size;
112 vstr->buf = new_buf;
113 return p;
114}
115
Damien Georgeb0261342014-09-23 18:10:17 +0100116STATIC bool vstr_ensure_extra(vstr_t *vstr, size_t size) {
Damien George0d3cb672015-01-28 23:43:01 +0000117 if (vstr->len + size > vstr->alloc) {
Damien George354d15a2014-02-06 21:11:19 +0000118 if (vstr->fixed_buf) {
119 return false;
120 }
Paul Sokolovsky6f34e132016-05-10 00:56:51 +0300121 size_t new_alloc = ROUND_ALLOC((vstr->len + size) + 16);
Damien732407f2013-12-29 19:33:23 +0000122 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc);
Damien68f59a92013-10-20 14:39:58 +0100123 vstr->alloc = new_alloc;
124 vstr->buf = new_buf;
125 }
126 return true;
127}
128
Damien Georgeb0261342014-09-23 18:10:17 +0100129void vstr_hint_size(vstr_t *vstr, size_t size) {
Damien68f59a92013-10-20 14:39:58 +0100130 vstr_ensure_extra(vstr, size);
Damien68f59a92013-10-20 14:39:58 +0100131}
132
Damien Georgeb0261342014-09-23 18:10:17 +0100133char *vstr_add_len(vstr_t *vstr, size_t len) {
Damien George5da0d292016-09-19 11:17:02 +1000134 if (!vstr_ensure_extra(vstr, len)) {
Damien68f59a92013-10-20 14:39:58 +0100135 return NULL;
136 }
137 char *buf = vstr->buf + vstr->len;
138 vstr->len += len;
Damien68f59a92013-10-20 14:39:58 +0100139 return buf;
140}
141
Damien George0d3cb672015-01-28 23:43:01 +0000142// Doesn't increase len, just makes sure there is a null byte at the end
Damien George827b0f72015-01-29 13:57:23 +0000143char *vstr_null_terminated_str(vstr_t *vstr) {
Paul Sokolovskya1f22452016-05-09 22:39:57 +0300144 // If there's no more room, add single byte
145 if (vstr->alloc == vstr->len) {
146 if (vstr_extend(vstr, 1) == NULL) {
147 return NULL;
148 }
149 }
Damien George0d3cb672015-01-28 23:43:01 +0000150 vstr->buf[vstr->len] = '\0';
Damien George827b0f72015-01-29 13:57:23 +0000151 return vstr->buf;
Damien George0d3cb672015-01-28 23:43:01 +0000152}
153
Damien George280e7202014-03-15 14:33:09 +0000154void vstr_add_byte(vstr_t *vstr, byte b) {
Damien68f59a92013-10-20 14:39:58 +0100155 byte *buf = (byte*)vstr_add_len(vstr, 1);
156 if (buf == NULL) {
157 return;
158 }
Damien George280e7202014-03-15 14:33:09 +0000159 buf[0] = b;
Damien68f59a92013-10-20 14:39:58 +0100160}
161
162void vstr_add_char(vstr_t *vstr, unichar c) {
Paul Sokolovsky165eb692014-06-13 02:42:34 +0300163#if MICROPY_PY_BUILTINS_STR_UNICODE
Chris Angelico2ba22992014-06-04 05:28:12 +1000164 // TODO: Can this be simplified and deduplicated?
165 // Is it worth just calling vstr_add_len(vstr, 4)?
166 if (c < 0x80) {
167 byte *buf = (byte*)vstr_add_len(vstr, 1);
168 if (buf == NULL) {
169 return;
170 }
171 *buf = (byte)c;
172 } else if (c < 0x800) {
173 byte *buf = (byte*)vstr_add_len(vstr, 2);
174 if (buf == NULL) {
175 return;
176 }
177 buf[0] = (c >> 6) | 0xC0;
178 buf[1] = (c & 0x3F) | 0x80;
179 } else if (c < 0x10000) {
180 byte *buf = (byte*)vstr_add_len(vstr, 3);
181 if (buf == NULL) {
182 return;
183 }
184 buf[0] = (c >> 12) | 0xE0;
185 buf[1] = ((c >> 6) & 0x3F) | 0x80;
186 buf[2] = (c & 0x3F) | 0x80;
187 } else {
188 assert(c < 0x110000);
189 byte *buf = (byte*)vstr_add_len(vstr, 4);
190 if (buf == NULL) {
191 return;
192 }
193 buf[0] = (c >> 18) | 0xF0;
194 buf[1] = ((c >> 12) & 0x3F) | 0x80;
195 buf[2] = ((c >> 6) & 0x3F) | 0x80;
196 buf[3] = (c & 0x3F) | 0x80;
Damien68f59a92013-10-20 14:39:58 +0100197 }
Paul Sokolovsky165eb692014-06-13 02:42:34 +0300198#else
Damien George0d3cb672015-01-28 23:43:01 +0000199 vstr_add_byte(vstr, c);
Paul Sokolovsky165eb692014-06-13 02:42:34 +0300200#endif
Damien68f59a92013-10-20 14:39:58 +0100201}
202
203void vstr_add_str(vstr_t *vstr, const char *str) {
204 vstr_add_strn(vstr, str, strlen(str));
205}
206
Damien Georgeb0261342014-09-23 18:10:17 +0100207void vstr_add_strn(vstr_t *vstr, const char *str, size_t len) {
Damien George5da0d292016-09-19 11:17:02 +1000208 if (!vstr_ensure_extra(vstr, len)) {
Damien George354d15a2014-02-06 21:11:19 +0000209 // if buf is fixed, we got here because there isn't enough room left
Damien George0d3cb672015-01-28 23:43:01 +0000210 // so just try to copy as much as we can, with room for a possible null byte
Damien George1c9a4992015-04-18 14:23:13 +0100211 if (vstr->fixed_buf && vstr->len < vstr->alloc) {
212 len = vstr->alloc - vstr->len;
Damien George354d15a2014-02-06 21:11:19 +0000213 goto copy;
214 }
Damien68f59a92013-10-20 14:39:58 +0100215 return;
216 }
Damien George354d15a2014-02-06 21:11:19 +0000217copy:
Damien68f59a92013-10-20 14:39:58 +0100218 memmove(vstr->buf + vstr->len, str, len);
219 vstr->len += len;
Damien68f59a92013-10-20 14:39:58 +0100220}
221
Damien George969a6b32014-12-10 22:07:04 +0000222STATIC char *vstr_ins_blank_bytes(vstr_t *vstr, size_t byte_pos, size_t byte_len) {
Damien Georgeb0261342014-09-23 18:10:17 +0100223 size_t l = vstr->len;
Damien George280e7202014-03-15 14:33:09 +0000224 if (byte_pos > l) {
225 byte_pos = l;
226 }
227 if (byte_len > 0) {
228 // ensure room for the new bytes
229 if (!vstr_ensure_extra(vstr, byte_len)) {
230 return NULL;
231 }
Damien George0d3cb672015-01-28 23:43:01 +0000232 // copy up the string to make room for the new bytes
233 memmove(vstr->buf + byte_pos + byte_len, vstr->buf + byte_pos, l - byte_pos);
Damien George280e7202014-03-15 14:33:09 +0000234 // increase the length
235 vstr->len += byte_len;
Damien George280e7202014-03-15 14:33:09 +0000236 }
237 return vstr->buf + byte_pos;
238}
239
Damien Georgeb0261342014-09-23 18:10:17 +0100240void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b) {
Damien George280e7202014-03-15 14:33:09 +0000241 char *s = vstr_ins_blank_bytes(vstr, byte_pos, 1);
242 if (s != NULL) {
243 *s = b;
244 }
245}
246
Damien Georgeb0261342014-09-23 18:10:17 +0100247void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr) {
Damien George280e7202014-03-15 14:33:09 +0000248 // TODO UNICODE
Damien Georgeecd58ae2014-03-15 16:54:06 +0000249 char *s = vstr_ins_blank_bytes(vstr, char_pos, 1);
Damien George280e7202014-03-15 14:33:09 +0000250 if (s != NULL) {
251 *s = chr;
252 }
253}
254
Damien Georgeb0261342014-09-23 18:10:17 +0100255void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut) {
Damien George280e7202014-03-15 14:33:09 +0000256 vstr_cut_out_bytes(vstr, 0, bytes_to_cut);
257}
258
Damien Georgeb0261342014-09-23 18:10:17 +0100259void vstr_cut_tail_bytes(vstr_t *vstr, size_t len) {
Damien68f59a92013-10-20 14:39:58 +0100260 if (len > vstr->len) {
261 vstr->len = 0;
262 } else {
263 vstr->len -= len;
264 }
265}
266
Damien Georgeb0261342014-09-23 18:10:17 +0100267void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut) {
Damien George5da0d292016-09-19 11:17:02 +1000268 if (byte_pos >= vstr->len) {
Damien George280e7202014-03-15 14:33:09 +0000269 return;
270 } else if (byte_pos + bytes_to_cut >= vstr->len) {
271 vstr->len = byte_pos;
Damien George280e7202014-03-15 14:33:09 +0000272 } else {
Damien George0d3cb672015-01-28 23:43:01 +0000273 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 +0000274 vstr->len -= bytes_to_cut;
275 }
276}
277
Damien68f59a92013-10-20 14:39:58 +0100278void vstr_printf(vstr_t *vstr, const char *fmt, ...) {
Damien2f06c572013-11-03 18:20:56 +0000279 va_list ap;
280 va_start(ap, fmt);
281 vstr_vprintf(vstr, fmt, ap);
282 va_end(ap);
283}
284
285void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap) {
Damien George7f9d1d62015-04-09 23:56:15 +0100286 mp_print_t print = {vstr, (mp_print_strn_t)vstr_add_strn};
287 mp_vprintf(&print, fmt, ap);
Damien68f59a92013-10-20 14:39:58 +0100288}