2014-05-03 18:27:38 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2021-04-22 20:55:39 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2014 Paul Sokolovsky
|
2014-05-03 18:27:38 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2013-10-20 09:39:58 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2014-02-06 16:11:19 -05:00
|
|
|
#include <assert.h>
|
2015-01-01 15:27:54 -05:00
|
|
|
|
|
|
|
#include "py/mpconfig.h"
|
2017-08-31 18:05:24 -04:00
|
|
|
#include "py/runtime.h"
|
2015-04-09 18:56:15 -04:00
|
|
|
#include "py/mpprint.h"
|
2013-10-20 09:39:58 -04:00
|
|
|
|
|
|
|
// returned value is always at least 1 greater than argument
|
2017-06-30 19:23:29 -04:00
|
|
|
#define ROUND_ALLOC(a) (((a) & ((~0U) - 7)) + 8)
|
2013-10-20 09:39:58 -04:00
|
|
|
|
2015-01-28 18:43:01 -05:00
|
|
|
// Init the vstr so it allocs exactly given number of bytes. Set length to zero.
|
2014-09-23 13:10:17 -04:00
|
|
|
void vstr_init(vstr_t *vstr, size_t alloc) {
|
2015-01-28 18:43:01 -05:00
|
|
|
if (alloc < 1) {
|
|
|
|
alloc = 1;
|
2014-03-31 12:10:59 -04:00
|
|
|
}
|
2014-01-13 16:15:23 -05:00
|
|
|
vstr->alloc = alloc;
|
2013-10-20 09:39:58 -04:00
|
|
|
vstr->len = 0;
|
|
|
|
vstr->buf = m_new(char, vstr->alloc);
|
2014-02-06 16:11:19 -05:00
|
|
|
vstr->fixed_buf = false;
|
|
|
|
}
|
|
|
|
|
2015-05-18 16:25:36 -04:00
|
|
|
// Init the vstr so it allocs exactly enough ram to hold a null-terminated
|
|
|
|
// string of the given length, and set the length.
|
2015-01-21 17:48:37 -05:00
|
|
|
void vstr_init_len(vstr_t *vstr, size_t len) {
|
2021-03-15 09:57:36 -04:00
|
|
|
if (len == SIZE_MAX) {
|
2020-01-08 10:42:44 -05:00
|
|
|
m_malloc_fail(len);
|
2020-01-08 17:36:43 -05:00
|
|
|
}
|
2015-05-18 16:25:36 -04:00
|
|
|
vstr_init(vstr, len + 1);
|
2015-01-28 18:43:01 -05:00
|
|
|
vstr->len = len;
|
2015-01-21 17:48:37 -05:00
|
|
|
}
|
|
|
|
|
2014-09-23 13:10:17 -04:00
|
|
|
void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf) {
|
2014-02-06 16:11:19 -05:00
|
|
|
vstr->alloc = alloc;
|
|
|
|
vstr->len = 0;
|
|
|
|
vstr->buf = buf;
|
|
|
|
vstr->fixed_buf = true;
|
2013-10-20 09:39:58 -04:00
|
|
|
}
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
void vstr_init_print(vstr_t *vstr, size_t alloc, mp_print_t *print) {
|
|
|
|
vstr_init(vstr, alloc);
|
|
|
|
print->data = vstr;
|
|
|
|
print->print_strn = (mp_print_strn_t)vstr_add_strn;
|
|
|
|
}
|
|
|
|
|
2013-10-20 09:39:58 -04:00
|
|
|
void vstr_clear(vstr_t *vstr) {
|
2014-02-06 16:11:19 -05:00
|
|
|
if (!vstr->fixed_buf) {
|
|
|
|
m_del(char, vstr->buf, vstr->alloc);
|
|
|
|
}
|
2013-10-20 09:39:58 -04:00
|
|
|
vstr->buf = NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:46:34 -04:00
|
|
|
vstr_t *vstr_new(size_t alloc) {
|
2015-01-28 18:43:01 -05:00
|
|
|
vstr_t *vstr = m_new_obj(vstr_t);
|
2014-01-13 16:15:23 -05:00
|
|
|
vstr_init(vstr, alloc);
|
2013-10-20 09:39:58 -04:00
|
|
|
return vstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vstr_free(vstr_t *vstr) {
|
|
|
|
if (vstr != NULL) {
|
2014-02-06 16:11:19 -05:00
|
|
|
if (!vstr->fixed_buf) {
|
|
|
|
m_del(char, vstr->buf, vstr->alloc);
|
|
|
|
}
|
2013-12-29 14:33:23 -05:00
|
|
|
m_del_obj(vstr_t, vstr);
|
2013-10-20 09:39:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-28 18:43:01 -05:00
|
|
|
// Extend vstr strictly by requested size, return pointer to newly added chunk.
|
2014-09-23 13:10:17 -04:00
|
|
|
char *vstr_extend(vstr_t *vstr, size_t size) {
|
2014-02-06 16:11:19 -05:00
|
|
|
if (vstr->fixed_buf) {
|
2017-08-31 18:05:24 -04:00
|
|
|
// We can't reallocate, and the caller is expecting the space to
|
|
|
|
// be there, so the only safe option is to raise an exception.
|
|
|
|
mp_raise_msg(&mp_type_RuntimeError, NULL);
|
2014-02-06 16:11:19 -05:00
|
|
|
}
|
2014-01-13 16:15:23 -05:00
|
|
|
char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size);
|
|
|
|
char *p = new_buf + vstr->alloc;
|
|
|
|
vstr->alloc += size;
|
|
|
|
vstr->buf = new_buf;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2017-08-31 18:05:24 -04:00
|
|
|
STATIC void vstr_ensure_extra(vstr_t *vstr, size_t size) {
|
2015-01-28 18:43:01 -05:00
|
|
|
if (vstr->len + size > vstr->alloc) {
|
2014-02-06 16:11:19 -05:00
|
|
|
if (vstr->fixed_buf) {
|
2017-08-31 18:05:24 -04:00
|
|
|
// We can't reallocate, and the caller is expecting the space to
|
|
|
|
// be there, so the only safe option is to raise an exception.
|
|
|
|
mp_raise_msg(&mp_type_RuntimeError, NULL);
|
2014-02-06 16:11:19 -05:00
|
|
|
}
|
2016-05-09 17:56:51 -04:00
|
|
|
size_t new_alloc = ROUND_ALLOC((vstr->len + size) + 16);
|
2013-12-29 14:33:23 -05:00
|
|
|
char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc);
|
2013-10-20 09:39:58 -04:00
|
|
|
vstr->alloc = new_alloc;
|
|
|
|
vstr->buf = new_buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 13:10:17 -04:00
|
|
|
void vstr_hint_size(vstr_t *vstr, size_t size) {
|
2013-10-20 09:39:58 -04:00
|
|
|
vstr_ensure_extra(vstr, size);
|
|
|
|
}
|
|
|
|
|
2014-09-23 13:10:17 -04:00
|
|
|
char *vstr_add_len(vstr_t *vstr, size_t len) {
|
2017-08-31 18:05:24 -04:00
|
|
|
vstr_ensure_extra(vstr, len);
|
2013-10-20 09:39:58 -04:00
|
|
|
char *buf = vstr->buf + vstr->len;
|
|
|
|
vstr->len += len;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2015-01-28 18:43:01 -05:00
|
|
|
// Doesn't increase len, just makes sure there is a null byte at the end
|
2015-01-29 08:57:23 -05:00
|
|
|
char *vstr_null_terminated_str(vstr_t *vstr) {
|
2016-05-09 15:39:57 -04:00
|
|
|
// If there's no more room, add single byte
|
|
|
|
if (vstr->alloc == vstr->len) {
|
2017-08-31 18:05:24 -04:00
|
|
|
vstr_extend(vstr, 1);
|
2016-05-09 15:39:57 -04:00
|
|
|
}
|
2015-01-28 18:43:01 -05:00
|
|
|
vstr->buf[vstr->len] = '\0';
|
2015-01-29 08:57:23 -05:00
|
|
|
return vstr->buf;
|
2015-01-28 18:43:01 -05:00
|
|
|
}
|
|
|
|
|
2014-03-15 10:33:09 -04:00
|
|
|
void vstr_add_byte(vstr_t *vstr, byte b) {
|
2021-03-15 09:57:36 -04:00
|
|
|
byte *buf = (byte *)vstr_add_len(vstr, 1);
|
2014-03-15 10:33:09 -04:00
|
|
|
buf[0] = b;
|
2013-10-20 09:39:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void vstr_add_char(vstr_t *vstr, unichar c) {
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_STR_UNICODE
|
2014-06-03 15:28:12 -04:00
|
|
|
// TODO: Can this be simplified and deduplicated?
|
|
|
|
// Is it worth just calling vstr_add_len(vstr, 4)?
|
|
|
|
if (c < 0x80) {
|
2021-03-15 09:57:36 -04:00
|
|
|
byte *buf = (byte *)vstr_add_len(vstr, 1);
|
2014-06-03 15:28:12 -04:00
|
|
|
*buf = (byte)c;
|
|
|
|
} else if (c < 0x800) {
|
2021-03-15 09:57:36 -04:00
|
|
|
byte *buf = (byte *)vstr_add_len(vstr, 2);
|
2014-06-03 15:28:12 -04:00
|
|
|
buf[0] = (c >> 6) | 0xC0;
|
|
|
|
buf[1] = (c & 0x3F) | 0x80;
|
|
|
|
} else if (c < 0x10000) {
|
2021-03-15 09:57:36 -04:00
|
|
|
byte *buf = (byte *)vstr_add_len(vstr, 3);
|
2014-06-03 15:28:12 -04:00
|
|
|
buf[0] = (c >> 12) | 0xE0;
|
|
|
|
buf[1] = ((c >> 6) & 0x3F) | 0x80;
|
|
|
|
buf[2] = (c & 0x3F) | 0x80;
|
|
|
|
} else {
|
|
|
|
assert(c < 0x110000);
|
2021-03-15 09:57:36 -04:00
|
|
|
byte *buf = (byte *)vstr_add_len(vstr, 4);
|
2014-06-03 15:28:12 -04:00
|
|
|
buf[0] = (c >> 18) | 0xF0;
|
|
|
|
buf[1] = ((c >> 12) & 0x3F) | 0x80;
|
|
|
|
buf[2] = ((c >> 6) & 0x3F) | 0x80;
|
|
|
|
buf[3] = (c & 0x3F) | 0x80;
|
2013-10-20 09:39:58 -04:00
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
#else
|
2015-01-28 18:43:01 -05:00
|
|
|
vstr_add_byte(vstr, c);
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
2013-10-20 09:39:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void vstr_add_str(vstr_t *vstr, const char *str) {
|
|
|
|
vstr_add_strn(vstr, str, strlen(str));
|
|
|
|
}
|
|
|
|
|
2014-09-23 13:10:17 -04:00
|
|
|
void vstr_add_strn(vstr_t *vstr, const char *str, size_t len) {
|
2017-08-31 18:05:24 -04:00
|
|
|
vstr_ensure_extra(vstr, len);
|
2013-10-20 09:39:58 -04:00
|
|
|
memmove(vstr->buf + vstr->len, str, len);
|
|
|
|
vstr->len += len;
|
|
|
|
}
|
|
|
|
|
2014-12-10 17:07:04 -05:00
|
|
|
STATIC char *vstr_ins_blank_bytes(vstr_t *vstr, size_t byte_pos, size_t byte_len) {
|
2014-09-23 13:10:17 -04:00
|
|
|
size_t l = vstr->len;
|
2014-03-15 10:33:09 -04:00
|
|
|
if (byte_pos > l) {
|
|
|
|
byte_pos = l;
|
|
|
|
}
|
|
|
|
if (byte_len > 0) {
|
|
|
|
// ensure room for the new bytes
|
2017-08-31 18:05:24 -04:00
|
|
|
vstr_ensure_extra(vstr, byte_len);
|
2015-01-28 18:43:01 -05:00
|
|
|
// copy up the string to make room for the new bytes
|
|
|
|
memmove(vstr->buf + byte_pos + byte_len, vstr->buf + byte_pos, l - byte_pos);
|
2014-03-15 10:33:09 -04:00
|
|
|
// increase the length
|
|
|
|
vstr->len += byte_len;
|
|
|
|
}
|
|
|
|
return vstr->buf + byte_pos;
|
|
|
|
}
|
|
|
|
|
2014-09-23 13:10:17 -04:00
|
|
|
void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b) {
|
2014-03-15 10:33:09 -04:00
|
|
|
char *s = vstr_ins_blank_bytes(vstr, byte_pos, 1);
|
2017-08-31 18:05:24 -04:00
|
|
|
*s = b;
|
2014-03-15 10:33:09 -04:00
|
|
|
}
|
|
|
|
|
2014-09-23 13:10:17 -04:00
|
|
|
void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr) {
|
2014-03-15 10:33:09 -04:00
|
|
|
// TODO UNICODE
|
2014-03-15 12:54:06 -04:00
|
|
|
char *s = vstr_ins_blank_bytes(vstr, char_pos, 1);
|
2017-08-31 18:05:24 -04:00
|
|
|
*s = chr;
|
2014-03-15 10:33:09 -04:00
|
|
|
}
|
|
|
|
|
2014-09-23 13:10:17 -04:00
|
|
|
void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut) {
|
2014-03-15 10:33:09 -04:00
|
|
|
vstr_cut_out_bytes(vstr, 0, bytes_to_cut);
|
|
|
|
}
|
|
|
|
|
2014-09-23 13:10:17 -04:00
|
|
|
void vstr_cut_tail_bytes(vstr_t *vstr, size_t len) {
|
2013-10-20 09:39:58 -04:00
|
|
|
if (len > vstr->len) {
|
|
|
|
vstr->len = 0;
|
|
|
|
} else {
|
|
|
|
vstr->len -= len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 13:10:17 -04:00
|
|
|
void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut) {
|
py/vstr: Remove vstr.had_error flag and inline basic vstr functions.
The vstr.had_error flag was a relic from the very early days which assumed
that the malloc functions (eg m_new, m_renew) returned NULL if they failed
to allocate. But that's no longer the case: these functions will raise an
exception if they fail.
Since it was impossible for had_error to be set, this patch introduces no
change in behaviour.
An alternative option would be to change the malloc calls to the _maybe
variants, which return NULL instead of raising, but then a lot of code
will need to explicitly check if the vstr had an error and raise if it
did.
The code-size savings for this patch are, in bytes: bare-arm:188,
minimal:456, unix(NDEBUG,x86-64):368, stmhal:228, esp8266:360.
2016-09-18 21:17:02 -04:00
|
|
|
if (byte_pos >= vstr->len) {
|
2014-03-15 10:33:09 -04:00
|
|
|
return;
|
|
|
|
} else if (byte_pos + bytes_to_cut >= vstr->len) {
|
|
|
|
vstr->len = byte_pos;
|
|
|
|
} else {
|
2015-01-28 18:43:01 -05:00
|
|
|
memmove(vstr->buf + byte_pos, vstr->buf + byte_pos + bytes_to_cut, vstr->len - byte_pos - bytes_to_cut);
|
2014-03-15 10:33:09 -04:00
|
|
|
vstr->len -= bytes_to_cut;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-20 09:39:58 -04:00
|
|
|
void vstr_printf(vstr_t *vstr, const char *fmt, ...) {
|
2013-11-03 13:20:56 -05:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vstr_vprintf(vstr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void vstr_vprintf(vstr_t *vstr, const char *fmt, va_list ap) {
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_print_t print = {vstr, (mp_print_strn_t)vstr_add_strn};
|
|
|
|
mp_vprintf(&print, fmt, ap);
|
2013-10-20 09:39:58 -04:00
|
|
|
}
|