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 /stmhal | |
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 'stmhal')
-rw-r--r-- | stmhal/input.c | 4 | ||||
-rw-r--r-- | stmhal/modusocket.c | 2 | ||||
-rw-r--r-- | stmhal/pyexec.c | 14 |
3 files changed, 11 insertions, 9 deletions
diff --git a/stmhal/input.c b/stmhal/input.c index b735ca2492..b1c8c1f78f 100644 --- a/stmhal/input.c +++ b/stmhal/input.c @@ -39,9 +39,7 @@ STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) { if (line.len == 0 && ret == CHAR_CTRL_D) { nlr_raise(mp_obj_new_exception(&mp_type_EOFError)); } - mp_obj_t o = mp_obj_new_str(line.buf, line.len, false); - vstr_clear(&line); - return o; + return mp_obj_new_str_from_vstr(&mp_type_str, &line); } MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input); diff --git a/stmhal/modusocket.c b/stmhal/modusocket.c index b52a9c8e88..a2ec08f59e 100644 --- a/stmhal/modusocket.c +++ b/stmhal/modusocket.c @@ -217,7 +217,6 @@ STATIC mp_obj_t socket_recv(mp_obj_t self_in, mp_obj_t len_in) { return mp_const_empty_bytes; } vstr.len = ret; - vstr.buf[vstr.len] = '\0'; return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } STATIC MP_DEFINE_CONST_FUN_OBJ_2(socket_recv_obj, socket_recv); @@ -269,7 +268,6 @@ STATIC mp_obj_t socket_recvfrom(mp_obj_t self_in, mp_obj_t len_in) { tuple[0] = mp_const_empty_bytes; } else { vstr.len = ret; - vstr.buf[vstr.len] = '\0'; tuple[0] = mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr); } tuple[1] = mod_network_format_inet_addr(ip, port); diff --git a/stmhal/pyexec.c b/stmhal/pyexec.c index 1563dc8e4a..930c1c0191 100644 --- a/stmhal/pyexec.c +++ b/stmhal/pyexec.c @@ -248,11 +248,12 @@ int pyexec_friendly_repl_process_char(int c) { return 0; } + vstr_null_terminate(&repl.line); if (!mp_repl_continue_with_input(vstr_str(&repl.line))) { goto exec; } - vstr_add_char(&repl.line, '\n'); + vstr_add_byte(&repl.line, '\n'); repl.cont_line = true; stdout_tx_str("... "); readline_note_newline(); @@ -274,8 +275,9 @@ int pyexec_friendly_repl_process_char(int c) { return 0; } + vstr_null_terminate(&repl.line); if (mp_repl_continue_with_input(vstr_str(&repl.line))) { - vstr_add_char(&repl.line, '\n'); + vstr_add_byte(&repl.line, '\n'); stdout_tx_str("... "); readline_note_newline(); return 0; @@ -362,8 +364,12 @@ friendly_repl_reset: continue; } - while (mp_repl_continue_with_input(vstr_str(&line))) { - vstr_add_char(&line, '\n'); + for (;;) { + vstr_null_terminate(&line); + if (!mp_repl_continue_with_input(vstr_str(&line))) { + break; + } + vstr_add_byte(&line, '\n'); ret = readline(&line, "... "); if (ret == CHAR_CTRL_C) { // cancel everything |