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 /py/stream.c | |
parent | 0b9ee86133a2a0524691c6cdac209dbfcb3bf116 (diff) |
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 'py/stream.c')
-rw-r--r-- | py/stream.c | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/py/stream.c b/py/stream.c index b7d4a9000..3415b74b9 100644 --- a/py/stream.c +++ b/py/stream.c @@ -160,11 +160,12 @@ STATIC mp_obj_t stream_read(mp_uint_t n_args, const mp_obj_t *args) { } #endif - byte *buf; - mp_obj_t ret_obj = mp_obj_str_builder_start(STREAM_CONTENT_TYPE(o->type->stream_p), sz, &buf); + vstr_t vstr; + vstr_init_len(&vstr, sz); int error; - mp_uint_t out_sz = o->type->stream_p->read(o, buf, sz, &error); + mp_uint_t out_sz = o->type->stream_p->read(o, vstr.buf, sz, &error); if (out_sz == MP_STREAM_ERROR) { + vstr_clear(&vstr); if (is_nonblocking_error(error)) { // https://docs.python.org/3.4/library/io.html#io.RawIOBase.read // "If the object is in non-blocking mode and no bytes are available, @@ -175,7 +176,9 @@ STATIC mp_obj_t stream_read(mp_uint_t n_args, const mp_obj_t *args) { } nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error))); } else { - return mp_obj_str_builder_end_with_len(ret_obj, out_sz); + vstr.len = out_sz; + vstr.buf[vstr.len] = '\0'; + return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(o->type->stream_p), &vstr); } } @@ -252,7 +255,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) { vstr_t vstr; vstr_init(&vstr, DEFAULT_BUFFER_SIZE); char *p = vstr.buf; - mp_uint_t current_read = DEFAULT_BUFFER_SIZE; + mp_uint_t current_read = DEFAULT_BUFFER_SIZE - 1; // save 1 byte for null termination while (true) { int error; mp_uint_t out_sz = o->type->stream_p->read(self_in, p, current_read, &error); @@ -276,8 +279,8 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) { current_read -= out_sz; p += out_sz; } else { - current_read = DEFAULT_BUFFER_SIZE; - p = vstr_extend(&vstr, current_read); + p = vstr_extend(&vstr, DEFAULT_BUFFER_SIZE); + current_read = DEFAULT_BUFFER_SIZE - 1; // save 1 byte for null termination if (p == NULL) { // TODO nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError/*&mp_type_RuntimeError*/, "Out of memory")); @@ -286,7 +289,7 @@ STATIC mp_obj_t stream_readall(mp_obj_t self_in) { } vstr.len = total_size; - vstr.buf[vstr.len] = '\0'; // XXX is there enough space? + vstr.buf[vstr.len] = '\0'; return mp_obj_new_str_from_vstr(STREAM_CONTENT_TYPE(o->type->stream_p), &vstr); } |