blob: 869b27805737ac7184e9fe39491371629e51dc34 [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien George04b91472014-05-03 23:27:38 +01003 *
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"
Damien Georgeede8a022017-09-01 08:05:24 +100033#include "py/runtime.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
Tom Collins145796f2017-06-30 16:23:29 -070037#define ROUND_ALLOC(a) (((a) & ((~0U) - 7)) + 8)
Damien68f59a92013-10-20 14:39:58 +010038
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) {
Damien Georgeede8a022017-09-01 08:05:24 +100095 // We can't reallocate, and the caller is expecting the space to
96 // be there, so the only safe option is to raise an exception.
97 mp_raise_msg(&mp_type_RuntimeError, NULL);
Damien George354d15a2014-02-06 21:11:19 +000098 }
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +020099 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size);
Paul Sokolovsky5d2499c2014-01-13 23:15:23 +0200100 char *p = new_buf + vstr->alloc;
101 vstr->alloc += size;
102 vstr->buf = new_buf;
103 return p;
104}
105
Damien Georgeede8a022017-09-01 08:05:24 +1000106STATIC void vstr_ensure_extra(vstr_t *vstr, size_t size) {
Damien George0d3cb672015-01-28 23:43:01 +0000107 if (vstr->len + size > vstr->alloc) {
Damien George354d15a2014-02-06 21:11:19 +0000108 if (vstr->fixed_buf) {
Damien Georgeede8a022017-09-01 08:05:24 +1000109 // We can't reallocate, and the caller is expecting the space to
110 // be there, so the only safe option is to raise an exception.
111 mp_raise_msg(&mp_type_RuntimeError, NULL);
Damien George354d15a2014-02-06 21:11:19 +0000112 }
Paul Sokolovsky6f34e132016-05-10 00:56:51 +0300113 size_t new_alloc = ROUND_ALLOC((vstr->len + size) + 16);
Damien732407f2013-12-29 19:33:23 +0000114 char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc);
Damien68f59a92013-10-20 14:39:58 +0100115 vstr->alloc = new_alloc;
116 vstr->buf = new_buf;
117 }
Damien68f59a92013-10-20 14:39:58 +0100118}
119
Damien Georgeb0261342014-09-23 18:10:17 +0100120void vstr_hint_size(vstr_t *vstr, size_t size) {
Damien68f59a92013-10-20 14:39:58 +0100121 vstr_ensure_extra(vstr, size);
Damien68f59a92013-10-20 14:39:58 +0100122}
123
Damien Georgeb0261342014-09-23 18:10:17 +0100124char *vstr_add_len(vstr_t *vstr, size_t len) {
Damien Georgeede8a022017-09-01 08:05:24 +1000125 vstr_ensure_extra(vstr, len);
Damien68f59a92013-10-20 14:39:58 +0100126 char *buf = vstr->buf + vstr->len;
127 vstr->len += len;
Damien68f59a92013-10-20 14:39:58 +0100128 return buf;
129}
130
Damien George0d3cb672015-01-28 23:43:01 +0000131// Doesn't increase len, just makes sure there is a null byte at the end
Damien George827b0f72015-01-29 13:57:23 +0000132char *vstr_null_terminated_str(vstr_t *vstr) {
Paul Sokolovskya1f22452016-05-09 22:39:57 +0300133 // If there's no more room, add single byte
134 if (vstr->alloc == vstr->len) {
Damien Georgeede8a022017-09-01 08:05:24 +1000135 vstr_extend(vstr, 1);
Paul Sokolovskya1f22452016-05-09 22:39:57 +0300136 }
Damien George0d3cb672015-01-28 23:43:01 +0000137 vstr->buf[vstr->len] = '\0';
Damien George827b0f72015-01-29 13:57:23 +0000138 return vstr->buf;
Damien George0d3cb672015-01-28 23:43:01 +0000139}
140
Damien George280e7202014-03-15 14:33:09 +0000141void vstr_add_byte(vstr_t *vstr, byte b) {
Damien68f59a92013-10-20 14:39:58 +0100142 byte *buf = (byte*)vstr_add_len(vstr, 1);
Damien George280e7202014-03-15 14:33:09 +0000143 buf[0] = b;
Damien68f59a92013-10-20 14:39:58 +0100144}
145
146void vstr_add_char(vstr_t *vstr, unichar c) {
Paul Sokolovsky165eb692014-06-13 02:42:34 +0300147#if MICROPY_PY_BUILTINS_STR_UNICODE
Chris Angelico2ba22992014-06-04 05:28:12 +1000148 // TODO: Can this be simplified and deduplicated?
149 // Is it worth just calling vstr_add_len(vstr, 4)?
150 if (c < 0x80) {
151 byte *buf = (byte*)vstr_add_len(vstr, 1);
Chris Angelico2ba22992014-06-04 05:28:12 +1000152 *buf = (byte)c;
153 } else if (c < 0x800) {
154 byte *buf = (byte*)vstr_add_len(vstr, 2);
Chris Angelico2ba22992014-06-04 05:28:12 +1000155 buf[0] = (c >> 6) | 0xC0;
156 buf[1] = (c & 0x3F) | 0x80;
157 } else if (c < 0x10000) {
158 byte *buf = (byte*)vstr_add_len(vstr, 3);
Chris Angelico2ba22992014-06-04 05:28:12 +1000159 buf[0] = (c >> 12) | 0xE0;
160 buf[1] = ((c >> 6) & 0x3F) | 0x80;
161 buf[2] = (c & 0x3F) | 0x80;
162 } else {
163 assert(c < 0x110000);
164 byte *buf = (byte*)vstr_add_len(vstr, 4);
Chris Angelico2ba22992014-06-04 05:28:12 +1000165 buf[0] = (c >> 18) | 0xF0;
166 buf[1] = ((c >> 12) & 0x3F) | 0x80;
167 buf[2] = ((c >> 6) & 0x3F) | 0x80;
168 buf[3] = (c & 0x3F) | 0x80;
Damien68f59a92013-10-20 14:39:58 +0100169 }
Paul Sokolovsky165eb692014-06-13 02:42:34 +0300170#else
Damien George0d3cb672015-01-28 23:43:01 +0000171 vstr_add_byte(vstr, c);
Paul Sokolovsky165eb692014-06-13 02:42:34 +0300172#endif
Damien68f59a92013-10-20 14:39:58 +0100173}
174
175void vstr_add_str(vstr_t *vstr, const char *str) {
176 vstr_add_strn(vstr, str, strlen(str));
177}
178
Damien Georgeb0261342014-09-23 18:10:17 +0100179void vstr_add_strn(vstr_t *vstr, const char *str, size_t len) {
Damien Georgeede8a022017-09-01 08:05:24 +1000180 vstr_ensure_extra(vstr, len);
Damien68f59a92013-10-20 14:39:58 +0100181 memmove(vstr->buf + vstr->len, str, len);
182 vstr->len += len;
Damien68f59a92013-10-20 14:39:58 +0100183}
184
Damien George969a6b32014-12-10 22:07:04 +0000185STATIC char *vstr_ins_blank_bytes(vstr_t *vstr, size_t byte_pos, size_t byte_len) {
Damien Georgeb0261342014-09-23 18:10:17 +0100186 size_t l = vstr->len;
Damien George280e7202014-03-15 14:33:09 +0000187 if (byte_pos > l) {
188 byte_pos = l;
189 }
190 if (byte_len > 0) {
191 // ensure room for the new bytes
Damien Georgeede8a022017-09-01 08:05:24 +1000192 vstr_ensure_extra(vstr, byte_len);
Damien George0d3cb672015-01-28 23:43:01 +0000193 // copy up the string to make room for the new bytes
194 memmove(vstr->buf + byte_pos + byte_len, vstr->buf + byte_pos, l - byte_pos);
Damien George280e7202014-03-15 14:33:09 +0000195 // increase the length
196 vstr->len += byte_len;
Damien George280e7202014-03-15 14:33:09 +0000197 }
198 return vstr->buf + byte_pos;
199}
200
Damien Georgeb0261342014-09-23 18:10:17 +0100201void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b) {
Damien George280e7202014-03-15 14:33:09 +0000202 char *s = vstr_ins_blank_bytes(vstr, byte_pos, 1);
Damien Georgeede8a022017-09-01 08:05:24 +1000203 *s = b;
Damien George280e7202014-03-15 14:33:09 +0000204}
205
Damien Georgeb0261342014-09-23 18:10:17 +0100206void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr) {
Damien George280e7202014-03-15 14:33:09 +0000207 // TODO UNICODE
Damien Georgeecd58ae2014-03-15 16:54:06 +0000208 char *s = vstr_ins_blank_bytes(vstr, char_pos, 1);
Damien Georgeede8a022017-09-01 08:05:24 +1000209 *s = chr;
Damien George280e7202014-03-15 14:33:09 +0000210}
211
Damien Georgeb0261342014-09-23 18:10:17 +0100212void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut) {
Damien George280e7202014-03-15 14:33:09 +0000213 vstr_cut_out_bytes(vstr, 0, bytes_to_cut);
214}
215
Damien Georgeb0261342014-09-23 18:10:17 +0100216void vstr_cut_tail_bytes(vstr_t *vstr, size_t len) {
Damien68f59a92013-10-20 14:39:58 +0100217 if (len > vstr->len) {
218 vstr->len = 0;
219 } else {
220 vstr->len -= len;
221 }
222}
223
Damien Georgeb0261342014-09-23 18:10:17 +0100224void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut) {
Damien George5da0d292016-09-19 11:17:02 +1000225 if (byte_pos >= vstr->len) {
Damien George280e7202014-03-15 14:33:09 +0000226 return;
227 } else if (byte_pos + bytes_to_cut >= vstr->len) {
228 vstr->len = byte_pos;
Damien George280e7202014-03-15 14:33:09 +0000229 } else {
Damien George0d3cb672015-01-28 23:43:01 +0000230 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 +0000231 vstr->len -= bytes_to_cut;
232 }
233}
234
Damien68f59a92013-10-20 14:39:58 +0100235void vstr_printf(vstr_t *vstr, const char *fmt, ...) {
Damien2f06c572013-11-03 18:20:56 +0000236 va_list ap;
237 va_start(ap, fmt);
238 vstr_vprintf(vstr, fmt, ap);
239 va_end(ap);
240}
241
242void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap) {
Damien George7f9d1d62015-04-09 23:56:15 +0100243 mp_print_t print = {vstr, (mp_print_strn_t)vstr_add_strn};
244 mp_vprintf(&print, fmt, ap);
Damien68f59a92013-10-20 14:39:58 +0100245}