py: Change vstr_null_terminate -> vstr_null_terminated_str, returns str.
diff --git a/lib/mp-readline/readline.c b/lib/mp-readline/readline.c
index 0303ff3..2bbbc53 100644
--- a/lib/mp-readline/readline.c
+++ b/lib/mp-readline/readline.c
@@ -111,8 +111,7 @@
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 1717c50..3fd517e 100644
--- a/py/builtinimport.c
+++ b/py/builtinimport.c
@@ -61,15 +61,13 @@
}
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(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_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 202fad8..1d40e34 100644
--- a/py/misc.h
+++ b/py/misc.h
@@ -145,7 +145,7 @@
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 1540e62..43de047 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -856,7 +856,6 @@
while (str < top && *str != '}' && *str != '!' && *str != ':') {
vstr_add_char(field_name, *str++);
}
- vstr_null_terminate(field_name);
}
// conversion ::= "r" | "s"
@@ -887,7 +886,6 @@
while (str < top && *str != '}') {
vstr_add_char(format_spec, *str++);
}
- vstr_null_terminate(format_spec);
}
}
if (str >= top) {
@@ -911,7 +909,7 @@
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 @@
// 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 ccd54d5..953e8ed 100644
--- a/py/vstr.c
+++ b/py/vstr.c
@@ -172,11 +172,12 @@
}
// 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 930c1c0..eaad6b5 100644
--- a/stmhal/pyexec.c
+++ b/stmhal/pyexec.c
@@ -248,8 +248,7 @@
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 @@
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 @@
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 34a804c..66f3d93 100644
--- a/teensy/main.c
+++ b/teensy/main.c
@@ -319,8 +319,7 @@
} 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);