From 827b0f747b2aa4870ef8de90cb0c3a4151e5ce71 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 29 Jan 2015 13:57:23 +0000 Subject: [PATCH] py: Change vstr_null_terminate -> vstr_null_terminated_str, returns str. --- lib/mp-readline/readline.c | 3 +-- py/builtinimport.c | 15 ++++++--------- py/misc.h | 2 +- py/objstr.c | 6 ++---- py/vstr.c | 5 +++-- stmhal/pyexec.c | 12 +++--------- teensy/main.c | 3 +-- 7 files changed, 17 insertions(+), 29 deletions(-) diff --git a/lib/mp-readline/readline.c b/lib/mp-readline/readline.c index 0303ff36d..2bbbc5385 100644 --- a/lib/mp-readline/readline.c +++ b/lib/mp-readline/readline.c @@ -111,8 +111,7 @@ int readline_process_char(int c) { if (rl.line->len > rl.orig_line_len && (MP_STATE_PORT(readline_hist)[0] == NULL || strcmp(MP_STATE_PORT(readline_hist)[0], rl.line->buf + rl.orig_line_len) != 0)) { // a line which is not empty and different from the last one // so update the history - vstr_null_terminate(rl.line); - char *most_recent_hist = str_dup_maybe(rl.line->buf + rl.orig_line_len); + char *most_recent_hist = str_dup_maybe(vstr_null_terminated_str(rl.line) + rl.orig_line_len); if (most_recent_hist != NULL) { for (int i = READLINE_HIST_SIZE - 1; i > 0; i--) { MP_STATE_PORT(readline_hist)[i] = MP_STATE_PORT(readline_hist)[i - 1]; diff --git a/py/builtinimport.c b/py/builtinimport.c index 1717c500d..3fd517ead 100644 --- a/py/builtinimport.c +++ b/py/builtinimport.c @@ -61,15 +61,13 @@ bool mp_obj_is_package(mp_obj_t module) { } STATIC mp_import_stat_t stat_dir_or_file(vstr_t *path) { - vstr_null_terminate(path); //printf("stat %s\n", vstr_str(path)); - mp_import_stat_t stat = mp_import_stat(vstr_str(path)); + mp_import_stat_t stat = mp_import_stat(vstr_null_terminated_str(path)); if (stat == MP_IMPORT_STAT_DIR) { return stat; } vstr_add_str(path, ".py"); - vstr_null_terminate(path); - stat = mp_import_stat(vstr_str(path)); + stat = mp_import_stat(vstr_null_terminated_str(path)); if (stat == MP_IMPORT_STAT_FILE) { return stat; } @@ -136,9 +134,9 @@ STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex, const char STATIC void do_load(mp_obj_t module_obj, vstr_t *file) { // create the lexer - vstr_null_terminate(file); - mp_lexer_t *lex = mp_lexer_new_from_file(vstr_str(file)); - do_load_from_lexer(module_obj, lex, vstr_str(file)); + char *file_str = vstr_null_terminated_str(file); + mp_lexer_t *lex = mp_lexer_new_from_file(file_str); + do_load_from_lexer(module_obj, lex, file_str); } mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) { @@ -329,8 +327,7 @@ mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) { mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str(vstr_str(&path), vstr_len(&path), false)); vstr_add_char(&path, PATH_SEP_CHAR); vstr_add_str(&path, "__init__.py"); - vstr_null_terminate(&path); - if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) { + if (mp_import_stat(vstr_null_terminated_str(&path)) != MP_IMPORT_STAT_FILE) { vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py mp_warning("%s is imported as namespace package", vstr_str(&path)); } else { diff --git a/py/misc.h b/py/misc.h index 202fad8d7..1d40e3492 100644 --- a/py/misc.h +++ b/py/misc.h @@ -145,7 +145,7 @@ size_t vstr_len(vstr_t *vstr); void vstr_hint_size(vstr_t *vstr, size_t size); char *vstr_extend(vstr_t *vstr, size_t size); char *vstr_add_len(vstr_t *vstr, size_t len); -void vstr_null_terminate(vstr_t *vstr); +char *vstr_null_terminated_str(vstr_t *vstr); void vstr_add_byte(vstr_t *vstr, byte v); void vstr_add_char(vstr_t *vstr, unichar chr); void vstr_add_str(vstr_t *vstr, const char *str); diff --git a/py/objstr.c b/py/objstr.c index 1540e6226..43de047a7 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -856,7 +856,6 @@ 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_null_terminate(field_name); } // conversion ::= "r" | "s" @@ -887,7 +886,6 @@ 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_null_terminate(format_spec); } } if (str >= top) { @@ -911,7 +909,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa if (field_name) { int index = 0; - const char *field = vstr_str(field_name); + const char *field = vstr_null_terminated_str(field_name); const char *lookup = NULL; if (MP_LIKELY(unichar_isdigit(*field))) { if (arg_i > 0) { @@ -999,7 +997,7 @@ mp_obj_t mp_obj_str_format(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kwa // precision ::= integer // type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%" - const char *s = vstr_str(format_spec); + const char *s = vstr_null_terminated_str(format_spec); if (isalignment(*s)) { align = *s++; } else if (*s && isalignment(s[1])) { diff --git a/py/vstr.c b/py/vstr.c index ccd54d589..953e8edb5 100644 --- a/py/vstr.c +++ b/py/vstr.c @@ -172,11 +172,12 @@ char *vstr_add_len(vstr_t *vstr, size_t len) { } // Doesn't increase len, just makes sure there is a null byte at the end -void vstr_null_terminate(vstr_t *vstr) { +char *vstr_null_terminated_str(vstr_t *vstr) { if (vstr->had_error || !vstr_ensure_extra(vstr, 1)) { - return; + return NULL; } vstr->buf[vstr->len] = '\0'; + return vstr->buf; } void vstr_add_byte(vstr_t *vstr, byte b) { diff --git a/stmhal/pyexec.c b/stmhal/pyexec.c index 930c1c019..eaad6b5d7 100644 --- a/stmhal/pyexec.c +++ b/stmhal/pyexec.c @@ -248,8 +248,7 @@ 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))) { + if (!mp_repl_continue_with_input(vstr_null_terminated_str(&repl.line))) { goto exec; } @@ -275,8 +274,7 @@ 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))) { + if (mp_repl_continue_with_input(vstr_null_terminated_str(&repl.line))) { vstr_add_byte(&repl.line, '\n'); stdout_tx_str("... "); readline_note_newline(); @@ -364,11 +362,7 @@ friendly_repl_reset: continue; } - for (;;) { - vstr_null_terminate(&line); - if (!mp_repl_continue_with_input(vstr_str(&line))) { - break; - } + while (mp_repl_continue_with_input(vstr_null_terminated_str(&line))) { vstr_add_byte(&line, '\n'); ret = readline(&line, "... "); if (ret == CHAR_CTRL_C) { diff --git a/teensy/main.c b/teensy/main.c index 34a804c60..66f3d935f 100644 --- a/teensy/main.c +++ b/teensy/main.c @@ -319,8 +319,7 @@ soft_reset: } else { vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main)); } - vstr_null_terminate(vstr); - if (!pyexec_file(vstr_str(vstr))) { + if (!pyexec_file(vstr_null_terminated_str(vstr))) { flash_error(3); } vstr_free(vstr);