diff options
author | Damien George <damien.p.george@gmail.com> | 2015-01-21 22:48:37 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-01-21 23:18:02 +0000 |
commit | 05005f679e00241e15a87751d89327f2c4630cb6 (patch) | |
tree | 50319ab1dee5af8016e95e3c80a3b28346cdca21 /extmod | |
parent | 0b9ee86133a2a0524691c6cdac209dbfcb3bf116 (diff) | |
download | micropython-05005f679e00241e15a87751d89327f2c4630cb6.tar.gz micropython-05005f679e00241e15a87751d89327f2c4630cb6.zip |
py: Remove mp_obj_str_builder and use vstr instead.
With this patch str/bytes construction is streamlined. Always use a
vstr to build a str/bytes object. If the size is known beforehand then
use vstr_init_len to allocate only required memory. Otherwise use
vstr_init and the vstr will grow as needed. Then use
mp_obj_new_str_from_vstr to create a str/bytes object using the vstr
memory.
Saves code ROM: 68 bytes on stmhal, 108 bytes on bare-arm, and 336 bytes
on unix x64.
Diffstat (limited to 'extmod')
-rw-r--r-- | extmod/modubinascii.c | 7 | ||||
-rw-r--r-- | extmod/moduhashlib.c | 8 |
2 files changed, 8 insertions, 7 deletions
diff --git a/extmod/modubinascii.c b/extmod/modubinascii.c index de8b5d9aeb..e258818af4 100644 --- a/extmod/modubinascii.c +++ b/extmod/modubinascii.c @@ -39,8 +39,9 @@ STATIC mp_obj_t mod_binascii_hexlify(mp_uint_t n_args, const mp_obj_t *args) { mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ); - byte *in = bufinfo.buf, *out; - mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, bufinfo.len * 2, &out); + vstr_t vstr; + vstr_init_len(&vstr, bufinfo.len * 2); + byte *in = bufinfo.buf, *out = (byte*)vstr.buf; for (mp_uint_t i = bufinfo.len; i--;) { byte d = (*in >> 4); if (d > 9) { @@ -53,7 +54,7 @@ STATIC mp_obj_t mod_binascii_hexlify(mp_uint_t n_args, const mp_obj_t *args) { } *out++ = d + '0'; } - return mp_obj_str_builder_end(o); + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_hexlify_obj, 1, 2, mod_binascii_hexlify); diff --git a/extmod/moduhashlib.c b/extmod/moduhashlib.c index 40a0f06094..e9100fa1b6 100644 --- a/extmod/moduhashlib.c +++ b/extmod/moduhashlib.c @@ -63,10 +63,10 @@ MP_DEFINE_CONST_FUN_OBJ_2(hash_update_obj, hash_update); STATIC mp_obj_t hash_digest(mp_obj_t self_in) { mp_obj_hash_t *self = self_in; - byte *hash; - mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, SHA256_BLOCK_SIZE, &hash); - sha256_final((SHA256_CTX*)self->state, hash); - return mp_obj_str_builder_end(o); + vstr_t vstr; + vstr_init_len(&vstr, SHA256_BLOCK_SIZE); + sha256_final((SHA256_CTX*)self->state, (byte*)vstr.buf); + return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } MP_DEFINE_CONST_FUN_OBJ_1(hash_digest_obj, hash_digest); |