2014-05-03 16:27:38 -06:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
*
|
|
|
|
* 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 07:39:58 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2014-02-06 14:11:19 -07:00
|
|
|
#include <assert.h>
|
2015-01-01 13:27:54 -07:00
|
|
|
|
|
|
|
#include "py/mpconfig.h"
|
|
|
|
#include "py/misc.h"
|
2013-10-20 07:39:58 -06:00
|
|
|
|
|
|
|
// returned value is always at least 1 greater than argument
|
|
|
|
#define ROUND_ALLOC(a) (((a) & ((~0) - 7)) + 8)
|
|
|
|
|
2015-01-28 16:43:01 -07:00
|
|
|
// Init the vstr so it allocs exactly given number of bytes. Set length to zero.
|
2014-09-23 11:10:17 -06:00
|
|
|
void vstr_init(vstr_t *vstr, size_t alloc) {
|
2015-01-28 16:43:01 -07:00
|
|
|
if (alloc < 1) {
|
|
|
|
alloc = 1;
|
2014-03-31 10:10:59 -06:00
|
|
|
}
|
2014-01-13 14:15:23 -07:00
|
|
|
vstr->alloc = alloc;
|
2013-10-20 07:39:58 -06:00
|
|
|
vstr->len = 0;
|
|
|
|
vstr->buf = m_new(char, vstr->alloc);
|
|
|
|
if (vstr->buf == NULL) {
|
|
|
|
vstr->had_error = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
vstr->had_error = false;
|
2014-02-06 14:11:19 -07:00
|
|
|
vstr->fixed_buf = false;
|
|
|
|
}
|
|
|
|
|
2015-01-28 16:43:01 -07:00
|
|
|
// Init the vstr so it allocs exactly enough ram to hold given length, and set the length.
|
2015-01-21 15:48:37 -07:00
|
|
|
void vstr_init_len(vstr_t *vstr, size_t len) {
|
2015-01-28 16:43:01 -07:00
|
|
|
vstr_init(vstr, len);
|
|
|
|
vstr->len = len;
|
2015-01-21 15:48:37 -07:00
|
|
|
}
|
|
|
|
|
2014-09-23 11:10:17 -06:00
|
|
|
void vstr_init_fixed_buf(vstr_t *vstr, size_t alloc, char *buf) {
|
2014-02-06 14:11:19 -07:00
|
|
|
vstr->alloc = alloc;
|
|
|
|
vstr->len = 0;
|
|
|
|
vstr->buf = buf;
|
|
|
|
vstr->had_error = false;
|
|
|
|
vstr->fixed_buf = true;
|
2013-10-20 07:39:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void vstr_clear(vstr_t *vstr) {
|
2014-02-06 14:11:19 -07:00
|
|
|
if (!vstr->fixed_buf) {
|
|
|
|
m_del(char, vstr->buf, vstr->alloc);
|
|
|
|
}
|
2013-10-20 07:39:58 -06:00
|
|
|
vstr->buf = NULL;
|
|
|
|
}
|
|
|
|
|
2013-10-23 13:20:17 -06:00
|
|
|
vstr_t *vstr_new(void) {
|
2015-01-28 16:43:01 -07:00
|
|
|
vstr_t *vstr = m_new_obj(vstr_t);
|
2013-10-20 07:39:58 -06:00
|
|
|
if (vstr == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-28 16:43:01 -07:00
|
|
|
vstr_init(vstr, 16);
|
2014-01-13 14:15:23 -07:00
|
|
|
return vstr;
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:10:17 -06:00
|
|
|
vstr_t *vstr_new_size(size_t alloc) {
|
2015-01-28 16:43:01 -07:00
|
|
|
vstr_t *vstr = m_new_obj(vstr_t);
|
2014-01-13 14:15:23 -07:00
|
|
|
if (vstr == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
vstr_init(vstr, alloc);
|
2013-10-20 07:39:58 -06:00
|
|
|
return vstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vstr_free(vstr_t *vstr) {
|
|
|
|
if (vstr != NULL) {
|
2014-02-06 14:11:19 -07:00
|
|
|
if (!vstr->fixed_buf) {
|
|
|
|
m_del(char, vstr->buf, vstr->alloc);
|
|
|
|
}
|
2013-12-29 12:33:23 -07:00
|
|
|
m_del_obj(vstr_t, vstr);
|
2013-10-20 07:39:58 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void vstr_reset(vstr_t *vstr) {
|
|
|
|
vstr->len = 0;
|
|
|
|
vstr->had_error = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool vstr_had_error(vstr_t *vstr) {
|
|
|
|
return vstr->had_error;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *vstr_str(vstr_t *vstr) {
|
|
|
|
if (vstr->had_error) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return vstr->buf;
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:10:17 -06:00
|
|
|
size_t vstr_len(vstr_t *vstr) {
|
2013-10-20 07:39:58 -06:00
|
|
|
if (vstr->had_error) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return vstr->len;
|
|
|
|
}
|
|
|
|
|
2015-01-28 16:43:01 -07:00
|
|
|
// Extend vstr strictly by requested size, return pointer to newly added chunk.
|
2014-09-23 11:10:17 -06:00
|
|
|
char *vstr_extend(vstr_t *vstr, size_t size) {
|
2014-02-06 14:11:19 -07:00
|
|
|
if (vstr->fixed_buf) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-01-13 14:15:23 -07:00
|
|
|
char *new_buf = m_renew(char, vstr->buf, vstr->alloc, vstr->alloc + size);
|
|
|
|
if (new_buf == NULL) {
|
|
|
|
vstr->had_error = true;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
char *p = new_buf + vstr->alloc;
|
|
|
|
vstr->alloc += size;
|
|
|
|
vstr->buf = new_buf;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:10:17 -06:00
|
|
|
STATIC bool vstr_ensure_extra(vstr_t *vstr, size_t size) {
|
2015-01-28 16:43:01 -07:00
|
|
|
if (vstr->len + size > vstr->alloc) {
|
2014-02-06 14:11:19 -07:00
|
|
|
if (vstr->fixed_buf) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-28 16:43:01 -07:00
|
|
|
size_t new_alloc = ROUND_ALLOC((vstr->len + size) * 2);
|
2013-12-29 12:33:23 -07:00
|
|
|
char *new_buf = m_renew(char, vstr->buf, vstr->alloc, new_alloc);
|
2013-10-20 07:39:58 -06:00
|
|
|
if (new_buf == NULL) {
|
|
|
|
vstr->had_error = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
vstr->alloc = new_alloc;
|
|
|
|
vstr->buf = new_buf;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:10:17 -06:00
|
|
|
void vstr_hint_size(vstr_t *vstr, size_t size) {
|
2013-10-20 07:39:58 -06:00
|
|
|
// it's not an error if we fail to allocate for the size hint
|
|
|
|
bool er = vstr->had_error;
|
|
|
|
vstr_ensure_extra(vstr, size);
|
|
|
|
vstr->had_error = er;
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:10:17 -06:00
|
|
|
char *vstr_add_len(vstr_t *vstr, size_t len) {
|
2013-10-20 07:39:58 -06:00
|
|
|
if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
char *buf = vstr->buf + vstr->len;
|
|
|
|
vstr->len += len;
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2015-01-28 16:43:01 -07:00
|
|
|
// Doesn't increase len, just makes sure there is a null byte at the end
|
|
|
|
void vstr_null_terminate(vstr_t *vstr) {
|
|
|
|
if (vstr->had_error || !vstr_ensure_extra(vstr, 1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
vstr->buf[vstr->len] = '\0';
|
|
|
|
}
|
|
|
|
|
2014-03-15 08:33:09 -06:00
|
|
|
void vstr_add_byte(vstr_t *vstr, byte b) {
|
2013-10-20 07:39:58 -06:00
|
|
|
byte *buf = (byte*)vstr_add_len(vstr, 1);
|
|
|
|
if (buf == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-15 08:33:09 -06:00
|
|
|
buf[0] = b;
|
2013-10-20 07:39:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void vstr_add_char(vstr_t *vstr, unichar c) {
|
2014-06-12 17:42:34 -06:00
|
|
|
#if MICROPY_PY_BUILTINS_STR_UNICODE
|
2014-06-03 13:28:12 -06:00
|
|
|
// TODO: Can this be simplified and deduplicated?
|
|
|
|
// Is it worth just calling vstr_add_len(vstr, 4)?
|
|
|
|
if (c < 0x80) {
|
|
|
|
byte *buf = (byte*)vstr_add_len(vstr, 1);
|
|
|
|
if (buf == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*buf = (byte)c;
|
|
|
|
} else if (c < 0x800) {
|
|
|
|
byte *buf = (byte*)vstr_add_len(vstr, 2);
|
|
|
|
if (buf == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buf[0] = (c >> 6) | 0xC0;
|
|
|
|
buf[1] = (c & 0x3F) | 0x80;
|
|
|
|
} else if (c < 0x10000) {
|
|
|
|
byte *buf = (byte*)vstr_add_len(vstr, 3);
|
|
|
|
if (buf == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buf[0] = (c >> 12) | 0xE0;
|
|
|
|
buf[1] = ((c >> 6) & 0x3F) | 0x80;
|
|
|
|
buf[2] = (c & 0x3F) | 0x80;
|
|
|
|
} else {
|
|
|
|
assert(c < 0x110000);
|
|
|
|
byte *buf = (byte*)vstr_add_len(vstr, 4);
|
|
|
|
if (buf == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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 07:39:58 -06:00
|
|
|
}
|
2014-06-12 17:42:34 -06:00
|
|
|
#else
|
2015-01-28 16:43:01 -07:00
|
|
|
vstr_add_byte(vstr, c);
|
2014-06-12 17:42:34 -06:00
|
|
|
#endif
|
2013-10-20 07:39:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
void vstr_add_str(vstr_t *vstr, const char *str) {
|
|
|
|
vstr_add_strn(vstr, str, strlen(str));
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:10:17 -06:00
|
|
|
void vstr_add_strn(vstr_t *vstr, const char *str, size_t len) {
|
2013-10-20 07:39:58 -06:00
|
|
|
if (vstr->had_error || !vstr_ensure_extra(vstr, len)) {
|
2014-02-06 14:11:19 -07:00
|
|
|
// if buf is fixed, we got here because there isn't enough room left
|
2015-01-28 16:43:01 -07:00
|
|
|
// so just try to copy as much as we can, with room for a possible null byte
|
2014-02-06 14:11:19 -07:00
|
|
|
if (vstr->fixed_buf && vstr->len + 1 < vstr->alloc) {
|
|
|
|
len = vstr->alloc - vstr->len - 1;
|
|
|
|
goto copy;
|
|
|
|
}
|
2013-10-20 07:39:58 -06:00
|
|
|
return;
|
|
|
|
}
|
2014-02-06 14:11:19 -07:00
|
|
|
copy:
|
2013-10-20 07:39:58 -06:00
|
|
|
memmove(vstr->buf + vstr->len, str, len);
|
|
|
|
vstr->len += len;
|
|
|
|
}
|
|
|
|
|
2014-12-10 15:07:04 -07:00
|
|
|
STATIC char *vstr_ins_blank_bytes(vstr_t *vstr, size_t byte_pos, size_t byte_len) {
|
2014-03-15 08:33:09 -06:00
|
|
|
if (vstr->had_error) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-09-23 11:10:17 -06:00
|
|
|
size_t l = vstr->len;
|
2014-03-15 08:33:09 -06:00
|
|
|
if (byte_pos > l) {
|
|
|
|
byte_pos = l;
|
|
|
|
}
|
|
|
|
if (byte_len > 0) {
|
|
|
|
// ensure room for the new bytes
|
|
|
|
if (!vstr_ensure_extra(vstr, byte_len)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-01-28 16:43:01 -07: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 08:33:09 -06:00
|
|
|
// increase the length
|
|
|
|
vstr->len += byte_len;
|
|
|
|
}
|
|
|
|
return vstr->buf + byte_pos;
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:10:17 -06:00
|
|
|
void vstr_ins_byte(vstr_t *vstr, size_t byte_pos, byte b) {
|
2014-03-15 08:33:09 -06:00
|
|
|
char *s = vstr_ins_blank_bytes(vstr, byte_pos, 1);
|
|
|
|
if (s != NULL) {
|
|
|
|
*s = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:10:17 -06:00
|
|
|
void vstr_ins_char(vstr_t *vstr, size_t char_pos, unichar chr) {
|
2014-03-15 08:33:09 -06:00
|
|
|
// TODO UNICODE
|
2014-03-15 10:54:06 -06:00
|
|
|
char *s = vstr_ins_blank_bytes(vstr, char_pos, 1);
|
2014-03-15 08:33:09 -06:00
|
|
|
if (s != NULL) {
|
|
|
|
*s = chr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:10:17 -06:00
|
|
|
void vstr_cut_head_bytes(vstr_t *vstr, size_t bytes_to_cut) {
|
2014-03-15 08:33:09 -06:00
|
|
|
vstr_cut_out_bytes(vstr, 0, bytes_to_cut);
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:10:17 -06:00
|
|
|
void vstr_cut_tail_bytes(vstr_t *vstr, size_t len) {
|
2013-10-20 07:39:58 -06:00
|
|
|
if (vstr->had_error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (len > vstr->len) {
|
|
|
|
vstr->len = 0;
|
|
|
|
} else {
|
|
|
|
vstr->len -= len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:10:17 -06:00
|
|
|
void vstr_cut_out_bytes(vstr_t *vstr, size_t byte_pos, size_t bytes_to_cut) {
|
2014-03-15 08:33:09 -06:00
|
|
|
if (vstr->had_error || byte_pos >= vstr->len) {
|
|
|
|
return;
|
|
|
|
} else if (byte_pos + bytes_to_cut >= vstr->len) {
|
|
|
|
vstr->len = byte_pos;
|
|
|
|
} else {
|
2015-01-28 16:43:01 -07:00
|
|
|
memmove(vstr->buf + byte_pos, vstr->buf + byte_pos + bytes_to_cut, vstr->len - byte_pos - bytes_to_cut);
|
2014-03-15 08:33:09 -06:00
|
|
|
vstr->len -= bytes_to_cut;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-20 07:39:58 -06:00
|
|
|
void vstr_printf(vstr_t *vstr, const char *fmt, ...) {
|
2013-11-03 11:20:56 -07: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) {
|
2013-10-20 07:39:58 -06:00
|
|
|
if (vstr->had_error || !vstr_ensure_extra(vstr, strlen(fmt))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// try to print in the allocated space
|
2014-01-03 07:03:48 -07:00
|
|
|
// need to make a copy of the va_list because we may call vsnprintf multiple times
|
2014-09-23 11:10:17 -06:00
|
|
|
size_t size = vstr->alloc - vstr->len;
|
2014-01-03 07:03:48 -07:00
|
|
|
va_list ap2;
|
|
|
|
va_copy(ap2, ap);
|
|
|
|
int n = vsnprintf(vstr->buf + vstr->len, size, fmt, ap2);
|
|
|
|
va_end(ap2);
|
2013-10-20 07:39:58 -06:00
|
|
|
|
|
|
|
// if that worked, return
|
2015-01-16 10:47:07 -07:00
|
|
|
if (n > -1 && (size_t)n < size) {
|
2013-10-20 07:39:58 -06:00
|
|
|
vstr->len += n;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// else try again with more space
|
|
|
|
if (n > -1) { // glibc 2.1
|
|
|
|
// n + 1 is precisely what is needed
|
|
|
|
if (!vstr_ensure_extra(vstr, n + 1)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else { // glibc 2.0
|
|
|
|
// increase to twice the old size
|
|
|
|
if (!vstr_ensure_extra(vstr, size * 2)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|