py: Change vstr_null_terminate -> vstr_null_terminated_str, returns str.

stackless
Damien George 2015-01-29 13:57:23 +00:00
parent 26c0b155fa
commit 827b0f747b
7 changed files with 17 additions and 29 deletions

View File

@ -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)) { 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 // a line which is not empty and different from the last one
// so update the history // so update the history
vstr_null_terminate(rl.line); char *most_recent_hist = str_dup_maybe(vstr_null_terminated_str(rl.line) + rl.orig_line_len);
char *most_recent_hist = str_dup_maybe(rl.line->buf + rl.orig_line_len);
if (most_recent_hist != NULL) { if (most_recent_hist != NULL) {
for (int i = READLINE_HIST_SIZE - 1; i > 0; i--) { for (int i = READLINE_HIST_SIZE - 1; i > 0; i--) {
MP_STATE_PORT(readline_hist)[i] = MP_STATE_PORT(readline_hist)[i - 1]; MP_STATE_PORT(readline_hist)[i] = MP_STATE_PORT(readline_hist)[i - 1];

View File

@ -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) { STATIC mp_import_stat_t stat_dir_or_file(vstr_t *path) {
vstr_null_terminate(path);
//printf("stat %s\n", vstr_str(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) { if (stat == MP_IMPORT_STAT_DIR) {
return stat; return stat;
} }
vstr_add_str(path, ".py"); vstr_add_str(path, ".py");
vstr_null_terminate(path); stat = mp_import_stat(vstr_null_terminated_str(path));
stat = mp_import_stat(vstr_str(path));
if (stat == MP_IMPORT_STAT_FILE) { if (stat == MP_IMPORT_STAT_FILE) {
return stat; 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) { STATIC void do_load(mp_obj_t module_obj, vstr_t *file) {
// create the lexer // create the lexer
vstr_null_terminate(file); char *file_str = vstr_null_terminated_str(file);
mp_lexer_t *lex = mp_lexer_new_from_file(vstr_str(file)); mp_lexer_t *lex = mp_lexer_new_from_file(file_str);
do_load_from_lexer(module_obj, lex, vstr_str(file)); 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) { 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)); 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_char(&path, PATH_SEP_CHAR);
vstr_add_str(&path, "__init__.py"); vstr_add_str(&path, "__init__.py");
vstr_null_terminate(&path); if (mp_import_stat(vstr_null_terminated_str(&path)) != MP_IMPORT_STAT_FILE) {
if (mp_import_stat(vstr_str(&path)) != MP_IMPORT_STAT_FILE) {
vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py vstr_cut_tail_bytes(&path, sizeof("/__init__.py") - 1); // cut off /__init__.py
mp_warning("%s is imported as namespace package", vstr_str(&path)); mp_warning("%s is imported as namespace package", vstr_str(&path));
} else { } else {

View File

@ -145,7 +145,7 @@ size_t vstr_len(vstr_t *vstr);
void vstr_hint_size(vstr_t *vstr, size_t size); void vstr_hint_size(vstr_t *vstr, size_t size);
char *vstr_extend(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); 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_byte(vstr_t *vstr, byte v);
void vstr_add_char(vstr_t *vstr, unichar chr); void vstr_add_char(vstr_t *vstr, unichar chr);
void vstr_add_str(vstr_t *vstr, const char *str); void vstr_add_str(vstr_t *vstr, const char *str);

View File

@ -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 != ':') { while (str < top && *str != '}' && *str != '!' && *str != ':') {
vstr_add_char(field_name, *str++); vstr_add_char(field_name, *str++);
} }
vstr_null_terminate(field_name);
} }
// conversion ::= "r" | "s" // 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 != '}') { while (str < top && *str != '}') {
vstr_add_char(format_spec, *str++); vstr_add_char(format_spec, *str++);
} }
vstr_null_terminate(format_spec);
} }
} }
if (str >= top) { 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) { if (field_name) {
int index = 0; int index = 0;
const char *field = vstr_str(field_name); const char *field = vstr_null_terminated_str(field_name);
const char *lookup = NULL; const char *lookup = NULL;
if (MP_LIKELY(unichar_isdigit(*field))) { if (MP_LIKELY(unichar_isdigit(*field))) {
if (arg_i > 0) { 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 // precision ::= integer
// type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%" // 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)) { if (isalignment(*s)) {
align = *s++; align = *s++;
} else if (*s && isalignment(s[1])) { } else if (*s && isalignment(s[1])) {

View File

@ -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 // 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)) { if (vstr->had_error || !vstr_ensure_extra(vstr, 1)) {
return; return NULL;
} }
vstr->buf[vstr->len] = '\0'; vstr->buf[vstr->len] = '\0';
return vstr->buf;
} }
void vstr_add_byte(vstr_t *vstr, byte b) { void vstr_add_byte(vstr_t *vstr, byte b) {

View File

@ -248,8 +248,7 @@ int pyexec_friendly_repl_process_char(int c) {
return 0; return 0;
} }
vstr_null_terminate(&repl.line); if (!mp_repl_continue_with_input(vstr_null_terminated_str(&repl.line))) {
if (!mp_repl_continue_with_input(vstr_str(&repl.line))) {
goto exec; goto exec;
} }
@ -275,8 +274,7 @@ int pyexec_friendly_repl_process_char(int c) {
return 0; return 0;
} }
vstr_null_terminate(&repl.line); if (mp_repl_continue_with_input(vstr_null_terminated_str(&repl.line))) {
if (mp_repl_continue_with_input(vstr_str(&repl.line))) {
vstr_add_byte(&repl.line, '\n'); vstr_add_byte(&repl.line, '\n');
stdout_tx_str("... "); stdout_tx_str("... ");
readline_note_newline(); readline_note_newline();
@ -364,11 +362,7 @@ friendly_repl_reset:
continue; continue;
} }
for (;;) { while (mp_repl_continue_with_input(vstr_null_terminated_str(&line))) {
vstr_null_terminate(&line);
if (!mp_repl_continue_with_input(vstr_str(&line))) {
break;
}
vstr_add_byte(&line, '\n'); vstr_add_byte(&line, '\n');
ret = readline(&line, "... "); ret = readline(&line, "... ");
if (ret == CHAR_CTRL_C) { if (ret == CHAR_CTRL_C) {

View File

@ -319,8 +319,7 @@ soft_reset:
} else { } else {
vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main)); vstr_add_str(vstr, mp_obj_str_get_str(pyb_config_main));
} }
vstr_null_terminate(vstr); if (!pyexec_file(vstr_null_terminated_str(vstr))) {
if (!pyexec_file(vstr_str(vstr))) {
flash_error(3); flash_error(3);
} }
vstr_free(vstr); vstr_free(vstr);