diff options
author | Damien George <damien.p.george@gmail.com> | 2015-01-28 23:43:01 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-01-28 23:43:01 +0000 |
commit | 0d3cb6726ddc1bab9fdd11a0aaa259fb436da4b2 (patch) | |
tree | 7285c3f452efdfce8c0ecb302bbd0e2efcca0c15 /py/objstr.c | |
parent | 57aebe171459fd599f8d430c1ea1660ed307360c (diff) | |
download | micropython-0d3cb6726ddc1bab9fdd11a0aaa259fb436da4b2.tar.gz micropython-0d3cb6726ddc1bab9fdd11a0aaa259fb436da4b2.zip |
py: Change vstr so that it doesn't null terminate buffer by default.
This cleans up vstr so that it's a pure "variable buffer", and the user
can decide whether they need to add a terminating null byte. In most
places where vstr is used, the vstr did not need to be null terminated
and so this patch saves code size, a tiny bit of RAM, and makes vstr
usage more efficient. When null termination is needed it must be
done explicitly using vstr_null_terminate.
Diffstat (limited to 'py/objstr.c')
-rw-r--r-- | py/objstr.c | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/py/objstr.c b/py/objstr.c index 3b2ce8f358..1540e62263 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -217,7 +217,7 @@ STATIC mp_obj_t bytes_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k vstr_init(&vstr, 16); } else { mp_int_t len = MP_OBJ_SMALL_INT_VALUE(len_in); - vstr_init(&vstr, len + 1); + vstr_init(&vstr, len); } mp_obj_t iterable = mp_getiter(args[0]); @@ -856,7 +856,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa while (str < top && *str != '}' && *str != '!' && *str != ':') { vstr_add_char(field_name, *str++); } - vstr_add_char(field_name, '\0'); + vstr_null_terminate(field_name); } // conversion ::= "r" | "s" @@ -887,7 +887,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa while (str < top && *str != '}') { vstr_add_char(format_spec, *str++); } - vstr_add_char(format_spec, '\0'); + vstr_null_terminate(format_spec); } } if (str >= top) { @@ -1890,6 +1890,7 @@ mp_obj_t mp_obj_new_str_from_vstr(const mp_obj_type_t *type, vstr_t *vstr) { o->len = vstr->len; o->hash = qstr_compute_hash((byte*)vstr->buf, vstr->len); o->data = (byte*)m_renew(char, vstr->buf, vstr->alloc, vstr->len + 1); + ((byte*)o->data)[o->len] = '\0'; // add null byte vstr->buf = NULL; vstr->alloc = 0; return o; |