Skip to content

Commit 827b0f7

Browse files
committed
py: Change vstr_null_terminate -> vstr_null_terminated_str, returns str.
1 parent 26c0b15 commit 827b0f7

File tree

7 files changed

+17
-29
lines changed

7 files changed

+17
-29
lines changed

lib/mp-readline/readline.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ int readline_process_char(int c) {
111111
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)) {
112112
// a line which is not empty and different from the last one
113113
// so update the history
114-
vstr_null_terminate(rl.line);
115-
char *most_recent_hist = str_dup_maybe(rl.line->buf + rl.orig_line_len);
114+
char *most_recent_hist = str_dup_maybe(vstr_null_terminated_str(rl.line) + rl.orig_line_len);
116115
if (most_recent_hist != NULL) {
117116
for (int i = READLINE_HIST_SIZE - 1; i > 0; i--) {
118117
MP_STATE_PORT(readline_hist)[i] = MP_STATE_PORT(readline_hist)[i - 1];

py/builtinimport.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,13 @@ bool mp_obj_is_package(mp_obj_t module) {
6161
}
6262

6363
STATIC mp_import_stat_t stat_dir_or_file(vstr_t *path) {
64-
vstr_null_terminate(path);
6564
//printf("stat %s\n", vstr_str(path));
66-
mp_import_stat_t stat = mp_import_stat(vstr_str(path));
65+
mp_import_stat_t stat = mp_import_stat(vstr_null_terminated_str(path));
6766
if (stat == MP_IMPORT_STAT_DIR) {
6867
return stat;
6968
}
7069
vstr_add_str(path, ".py");
71-
vstr_null_terminate(path);
72-
stat = mp_import_stat(vstr_str(path));
70+
stat = mp_import_stat(vstr_null_terminated_str(path));
7371
if (stat == MP_IMPORT_STAT_FILE) {
7472
return stat;
7573
}
@@ -136,9 +134,9 @@ STATIC void do_load_from_lexer(mp_obj_t module_obj, mp_lexer_t *lex, const char
136134

137135
STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
138136
// create the lexer
139-
vstr_null_terminate(file);
140-
mp_lexer_t *lex = mp_lexer_new_from_file(vstr_str(file));
141-
do_load_from_lexer(module_obj, lex, vstr_str(file));
137+
char *file_str = vstr_null_terminated_str(file);
138+
mp_lexer_t *lex = mp_lexer_new_from_file(file_str);
139+
do_load_from_lexer(module_obj, lex, file_str);
142140
}
143141

144142
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) {
329327
mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str(vstr_str(&path), vstr_len(&path), false));
330328
vstr_add_char(&path, PATH_SEP_CHAR);
331329
vstr_add_str(&path, "__init__.py");
332-
vstr_null_terminate(&path);
333-
if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) {
330+
if (mp_import_stat(vstr_null_terminated_str(&path)) != MP_IMPORT_STAT_FILE) {
334331
vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
335332
mp_warning("%s is imported as namespace package", vstr_str(&path));
336333
} else {

py/misc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ size_t vstr_len(vstr_t *vstr);
145145
void vstr_hint_size(vstr_t *vstr, size_t size);
146146
char *vstr_extend(vstr_t *vstr, size_t size);
147147
char *vstr_add_len(vstr_t *vstr, size_t len);
148-
void vstr_null_terminate(vstr_t *vstr);
148+
char *vstr_null_terminated_str(vstr_t *vstr);
149149
void vstr_add_byte(vstr_t *vstr, byte v);
150150
void vstr_add_char(vstr_t *vstr, unichar chr);
151151
void vstr_add_str(vstr_t *vstr, const char *str);

py/objstr.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
856856
while (str < top && *str != '}' && *str != '!' && *str != ':') {
857857
vstr_add_char(field_name, *str++);
858858
}
859-
vstr_null_terminate(field_name);
860859
}
861860

862861
// 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
887886
while (str < top && *str != '}') {
888887
vstr_add_char(format_spec, *str++);
889888
}
890-
vstr_null_terminate(format_spec);
891889
}
892890
}
893891
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
911909

912910
if (field_name) {
913911
int index = 0;
914-
const char *field = vstr_str(field_name);
912+
const char *field = vstr_null_terminated_str(field_name);
915913
const char *lookup = NULL;
916914
if (MP_LIKELY(unichar_isdigit(*field))) {
917915
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
999997
// precision ::= integer
1000998
// type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
1001999

1002-
const char *s = vstr_str(format_spec);
1000+
const char *s = vstr_null_terminated_str(format_spec);
10031001
if (isalignment(*s)) {
10041002
align = *s++;
10051003
} else if (*s && isalignment(s[1])) {

py/vstr.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,12 @@ char *vstr_add_len(vstr_t *vstr, size_t len) {
172172
}
173173

174174
// Doesn't increase len, just makes sure there is a null byte at the end
175-
void vstr_null_terminate(vstr_t *vstr) {
175+
char *vstr_null_terminated_str(vstr_t *vstr) {
176176
if (vstr->had_error || !vstr_ensure_extra(vstr, 1)) {
177-
return;
177+
return NULL;
178178
}
179179
vstr->buf[vstr->len] = '\0';
180+
return vstr->buf;
180181
}
181182

182183
void vstr_add_byte(vstr_t *vstr, byte b) {

stmhal/pyexec.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@ int pyexec_friendly_repl_process_char(int c) {
248248
return 0;
249249
}
250250

251-
vstr_null_terminate(&repl.line);
252-
if (!mp_repl_continue_with_input(vstr_str(&repl.line))) {
251+
if (!mp_repl_continue_with_input(vstr_null_terminated_str(&repl.line))) {
253252
goto exec;
254253
}
255254

@@ -275,8 +274,7 @@ int pyexec_friendly_repl_process_char(int c) {
275274
return 0;
276275
}
277276

278-
vstr_null_terminate(&repl.line);
279-
if (mp_repl_continue_with_input(vstr_str(&repl.line))) {
277+
if (mp_repl_continue_with_input(vstr_null_terminated_str(&repl.line))) {
280278
vstr_add_byte(&repl.line, '\n');
281279
stdout_tx_str("... ");
282280
readline_note_newline();
@@ -364,11 +362,7 @@ int pyexec_friendly_repl(void) {
364362
continue;
365363
}
366364

367-
for (;;) {
368-
vstr_null_terminate(&line);
369-
if (!mp_repl_continue_with_input(vstr_str(&line))) {
370-
break;
371-
}
365+
while (mp_repl_continue_with_input(vstr_null_terminated_str(&line))) {
372366
vstr_add_byte(&line, '\n');
373367
ret = readline(&line, "... ");
374368
if (ret == CHAR_CTRL_C) {

teensy/main.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,7 @@ int main(void) {
319319
} else {
320320
vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main));
321321
}
322-
vstr_null_terminate(vstr);
323-
if (!pyexec_file(vstr_str(vstr))) {
322+
if (!pyexec_file(vstr_null_terminated_str(vstr))) {
324323
flash_error(3);
325324
}
326325
vstr_free(vstr);

0 commit comments

Comments
 (0)