blob: 6a91552b5a6bc9f2c86d36c66962273dc20737a9 [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
Damien George824f5c52016-10-14 16:46:34 +110077vstr_t *vstr_new(size_t alloc) {
Damien George0d3cb672015-01-28 23:43:01 +000078 vstr_t *vstr = m_new_obj(vstr_t);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020079 vstr_init(vstr, alloc);
Damien68f59a92013-10-20 14:39:58 +010080 return vstr;
81}
82
83void vstr_free(vstr_t *vstr) {
84 if (vstr != NULL) {
Damien George354d15a2014-02-06 21:11:19 +000085 if (!vstr->fixed_buf) {
86 m_del(char, vstr->buf, vstr->alloc);
87 }
Damien732407f2013-12-29 19:33:23 +000088 m_del_obj(vstr_t, vstr);
Damien68f59a92013-10-20 14:39:58 +010089 }
90}
91
Damien George0d3cb672015-01-28 23:43:01 +000092// Extend vstr strictly by requested size, return pointer to newly added chunk.
Damien Georgeb0261342014-09-23 18:10:17 +010093char *vstr_extend(vstr_t *vstr, size_t size) {
Damien George354d15a2014-02-06 21:11:19 +000094 if (vstr->fixed_buf) {
95 return NULL;
96 }
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020097 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020098 char *p = new_buf + vstr->alloc;
99 vstr->alloc += size;
100 vstr->buf = new_buf;
101 return p;
102}
103
Damien Georgeb0261342014-09-23 18:10:17 +0100104STATIC bool vstr_ensure_extra(vstr_t *vstr, size_t size) {
Damien George0d3cb672015-01-28 23:43:01 +0000105 if (vstr->len + size > vstr->alloc) {
Damien George354d15a2014-02-06 21:11:19 +0000106 if (vstr->fixed_buf) {
107 return false;
108 }
Paul Sokolovsky6f34e132016-05-10 00:56:51 +0300109 size_t new_alloc = ROUND_ALLOC((vstr->len + size) + 16);
Damien732407f2013-12-29 19:33:23 +0000110 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc);
Damien68f59a92013-10-20 14:39:58 +0100111 vstr->alloc = new_alloc;
112 vstr->buf = new_buf;
113 }
114 return true;
115}
116
Damien Georgeb0261342014-09-23 18:10:17 +0100117void vstr_hint_size(vstr_t *vstr, size_t size) {
Damien68f59a92013-10-20 14:39:58 +0100118 vstr_ensure_extra(vstr, size);
Damien68f59a92013-10-20 14:39:58 +0100119}
120
Damien Georgeb0261342014-09-23 18:10:17 +0100121char *vstr_add_len(vstr_t *vstr, size_t len) {
Damien George5da0d292016-09-19 11:17:02 +1000122 if (!vstr_ensure_extra(vstr, len)) {
Damien68f59a92013-10-20 14:39:58 +0100123 return NULL;
124 }
125 char *buf = vstr->buf + vstr->len;
126 vstr->len += len;
Damien68f59a92013-10-20 14:39:58 +0100127 return buf;
128}
129
Damien George0d3cb672015-01-28 23:43:01 +0000130// Doesn't increase len, just makes sure there is a null byte at the end
Damien George827b0f72015-01-29 13:57:23 +0000131char *vstr_null_terminated_str(vstr_t *vstr) {
Paul Sokolovskya1f22452016-05-09 22:39:57 +0300132 // If there's no more room, add single byte
133 if (vstr->alloc == vstr->len) {
134 if (vstr_extend(vstr, 1) == NULL) {
135 return NULL;
136 }
137 }
Damien George0d3cb672015-01-28 23:43:01 +0000138 vstr->buf[vstr->len] = '\0';
Damien George827b0f72015-01-29 13:57:23 +0000139 return vstr->buf;
Damien George0d3cb672015-01-28 23:43:01 +0000140}
141
Damien George280e7202014-03-15 14:33:09 +0000142void vstr_add_byte(vstr_t *vstr, byte b) {
Damien68f59a92013-10-20 14:39:58 +0100143 byte *buf = (byte*)vstr_add_len(vstr, 1);
144 if (buf == NULL) {
145 return;
146 }
Damien George280e7202014-03-15 14:33:09 +0000147 buf[0] = b;
Damien68f59a92013-10-20 14:39:58 +0100148}
149
150void vstr_add_char(vstr_t *vstr, unichar c) {
Paul Sokolovsky165eb692014-06-13 02:42:34 +0300151#if MICROPY_PY_BUILTINS_STR_UNICODE
Chris Angelico2ba22992014-06-04 05:28:12 +1000152 // TODO: Can this be simplified and deduplicated?
153 // Is it worth just calling vstr_add_len(vstr, 4)?
154 if (c < 0x80) {
155 byte *buf = (byte*)vstr_add_len(vstr, 1);
156 if (buf == NULL) {
157 return;
158 }
159 *buf = (byte)c;
160 } else if (c < 0x800) {
161 byte *buf = (byte*)vstr_add_len(vstr, 2);
162 if (buf == NULL) {
163 return;
164 }
165 buf[0] = (c >> 6) | 0xC0;
166 buf[1] = (c & 0x3F) | 0x80;
167 } else if (c < 0x10000) {
168 byte *buf = (byte*)vstr_add_len(vstr, 3);
169 if (buf == NULL) {
170 return;
171 }
172 buf[0] = (c >> 12) | 0xE0;
173 buf[1] = ((c >> 6) & 0x3F) | 0x80;
174 buf[2] = (c & 0x3F) | 0x80;
175 } else {
176 assert(c < 0x110000);
177 byte *buf = (byte*)vstr_add_len(vstr, 4);
178 if (buf == NULL) {
179 return;
180 }
181 buf[0] = (c >> 18) | 0xF0;
182 buf[1] = ((c >> 12) & 0x3F) | 0x80;
183 buf[2] = ((c >> 6) & 0x3F) | 0x80;
184 buf[3] = (c & 0x3F) | 0x80;
Damien68f59a92013-10-20 14:39:58 +0100185 }
Paul Sokolovsky165eb692014-06-13 02:42:34 +0300186#else
Damien George0d3cb672015-01-28 23:43:01 +0000187 vstr_add_byte(vstr, c);
Paul Sokolovsky165eb692014-06-13 02:42:34 +0300188#endif
Damien68f59a92013-10-20 14:39:58 +0100189}
190
191void vstr_add_str(vstr_t *vstr, const char *str) {
192 vstr_add_strn(vstr, str, strlen(str));
193}
194
Damien Georgeb0261342014-09-23 18:10:17 +0100195void vstr_add_strn(vstr_t *vstr, const char *str, size_t len) {
Damien George5da0d292016-09-19 11:17:02 +1000196 if (!vstr_ensure_extra(vstr, len)) {
Damien George354d15a2014-02-06 21:11:19 +0000197 // if buf is fixed, we got here because there isn't enough room left
Damien George0d3cb672015-01-28 23:43:01 +0000198 // so just try to copy as much as we can, with room for a possible null byte
Damien George1c9a4992015-04-18 14:23:13 +0100199 if (vstr->fixed_buf && vstr->len < vstr->alloc) {
200 len = vstr->alloc - vstr->len;
Damien George354d15a2014-02-06 21:11:19 +0000201 goto copy;
202 }
Damien68f59a92013-10-20 14:39:58 +0100203 return;
204 }
Damien George354d15a2014-02-06 21:11:19 +0000205copy:
Damien68f59a92013-10-20 14:39:58 +0100206 memmove(vstr->buf + vstr->len, str, len);
207 vstr->len += len;
Damien68f59a92013-10-20 14:39:58 +0100208}
209
Damien George969a6b32014-12-10 22:07:04 +0000210STATIC char *vstr_ins_blank_bytes(vstr_t *vstr, size_t byte_pos, size_t byte_len) {
Damien Georgeb0261342014-09-23 18:10:17 +0100211 size_t l = vstr->len;
Damien George280e7202014-03-15 14:33:09 +0000212 if (byte_pos > l) {
213 byte_pos = l;
214 }
215 if (byte_len > 0) {
216 // ensure room for the new bytes
217 if (!vstr_ensure_extra(vstr, byte_len)) {
218 return NULL;
219 }
Damien George0d3cb672015-01-28 23:43:01 +0000220 // copy up the string to make room for the new bytes
221 memmove(vstr->buf + byte_pos + byte_len, vstr->buf + byte_pos, l - byte_pos);
Damien George280e7202014-03-15 14:33:09 +0000222 // increase the length
223 vstr->len += byte_len;
Damien George280e7202014-03-15 14:33:09 +0000224 }
225 return vstr->buf + byte_pos;
226}
227
Damien Georgeb0261342014-09-23 18:10:17 +0100228void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b) {
Damien George280e7202014-03-15 14:33:09 +0000229 char *s = vstr_ins_blank_bytes(vstr, byte_pos, 1);
230 if (s != NULL) {
231 *s = b;
232 }
233}
234
Damien Georgeb0261342014-09-23 18:10:17 +0100235void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr) {
Damien George280e7202014-03-15 14:33:09 +0000236 // TODO UNICODE
Damien Georgeecd58ae2014-03-15 16:54:06 +0000237 char *s = vstr_ins_blank_bytes(vstr, char_pos, 1);
Damien George280e7202014-03-15 14:33:09 +0000238 if (s != NULL) {
239 *s = chr;
240 }
241}
242
Damien Georgeb0261342014-09-23 18:10:17 +0100243void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut) {
Damien George280e7202014-03-15 14:33:09 +0000244 vstr_cut_out_bytes(vstr, 0, bytes_to_cut);
245}
246
Damien Georgeb0261342014-09-23 18:10:17 +0100247void vstr_cut_tail_bytes(vstr_t *vstr, size_t len) {
Damien68f59a92013-10-20 14:39:58 +0100248 if (len > vstr->len) {
249 vstr->len = 0;
250 } else {
251 vstr->len -= len;
252 }
253}
254
Damien Georgeb0261342014-09-23 18:10:17 +0100255void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut) {
Damien George5da0d292016-09-19 11:17:02 +1000256 if (byte_pos >= vstr->len) {
Damien George280e7202014-03-15 14:33:09 +0000257 return;
258 } else if (byte_pos + bytes_to_cut >= vstr->len) {
259 vstr->len = byte_pos;
Damien George280e7202014-03-15 14:33:09 +0000260 } else {
Damien George0d3cb672015-01-28 23:43:01 +0000261 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 +0000262 vstr->len -= bytes_to_cut;
263 }
264}
265
Damien68f59a92013-10-20 14:39:58 +0100266void vstr_printf(vstr_t *vstr, const char *fmt, ...) {
Damien2f06c572013-11-03 18:20:56 +0000267 va_list ap;
268 va_start(ap, fmt);
269 vstr_vprintf(vstr, fmt, ap);
270 va_end(ap);
271}
272
273void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap) {
Damien George7f9d1d62015-04-09 23:56:15 +0100274 mp_print_t print = {vstr, (mp_print_strn_t)vstr_add_strn};
275 mp_vprintf(&print, fmt, ap);
Damien68f59a92013-10-20 14:39:58 +0100276}