diff options
author | Damien George <damien.p.george@gmail.com> | 2015-04-09 23:56:15 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-04-16 14:30:16 +0000 |
commit | 7f9d1d6ab923096582622b700bedb6a571518eac (patch) | |
tree | f97a0d56ba0279dd4ef2a44f00676193c4d49d8b /py/objstrunicode.c | |
parent | 56beb01724d4f0027babc5d23f016efbde4c4190 (diff) | |
download | micropython-7f9d1d6ab923096582622b700bedb6a571518eac.tar.gz micropython-7f9d1d6ab923096582622b700bedb6a571518eac.zip |
py: Overhaul and simplify printf/pfenv mechanism.
Previous to this patch the printing mechanism was a bit of a tangled
mess. This patch attempts to consolidate printing into one interface.
All (non-debug) printing now uses the mp_print* family of functions,
mainly mp_printf. All these functions take an mp_print_t structure as
their first argument, and this structure defines the printing backend
through the "print_strn" function of said structure.
Printing from the uPy core can reach the platform-defined print code via
two paths: either through mp_sys_stdout_obj (defined pert port) in
conjunction with mp_stream_write; or through the mp_plat_print structure
which uses the MP_PLAT_PRINT_STRN macro to define how string are printed
on the platform. The former is only used when MICROPY_PY_IO is defined.
With this new scheme printing is generally more efficient (less layers
to go through, less arguments to pass), and, given an mp_print_t*
structure, one can call mp_print_str for efficiency instead of
mp_printf("%s", ...). Code size is also reduced by around 200 bytes on
Thumb2 archs.
Diffstat (limited to 'py/objstrunicode.c')
-rw-r--r-- | py/objstrunicode.c | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/py/objstrunicode.c b/py/objstrunicode.c index 406eaad49f..63ca6a8bf8 100644 --- a/py/objstrunicode.c +++ b/py/objstrunicode.c @@ -33,7 +33,6 @@ #include "py/objlist.h" #include "py/runtime0.h" #include "py/runtime.h" -#include "py/pfenv.h" #if MICROPY_PY_BUILTINS_STR_UNICODE @@ -42,7 +41,7 @@ STATIC mp_obj_t mp_obj_new_str_iterator(mp_obj_t str); /******************************************************************************/ /* str */ -STATIC void uni_print_quoted(void (*print)(void *env, const char *fmt, ...), void *env, const byte *str_data, uint str_len) { +STATIC void uni_print_quoted(const mp_print_t *print, const byte *str_data, uint str_len) { // this escapes characters, but it will be very slow to print (calling print many times) bool has_single_quote = false; bool has_double_quote = false; @@ -57,47 +56,47 @@ STATIC void uni_print_quoted(void (*print)(void *env, const char *fmt, ...), voi if (has_single_quote && !has_double_quote) { quote_char = '"'; } - print(env, "%c", quote_char); + mp_printf(print, "%c", quote_char); const byte *s = str_data, *top = str_data + str_len; while (s < top) { unichar ch; ch = utf8_get_char(s); s = utf8_next_char(s); if (ch == quote_char) { - print(env, "\\%c", quote_char); + mp_printf(print, "\\%c", quote_char); } else if (ch == '\\') { - print(env, "\\\\"); + mp_print_str(print, "\\\\"); } else if (32 <= ch && ch <= 126) { - print(env, "%c", ch); + mp_printf(print, "%c", ch); } else if (ch == '\n') { - print(env, "\\n"); + mp_print_str(print, "\\n"); } else if (ch == '\r') { - print(env, "\\r"); + mp_print_str(print, "\\r"); } else if (ch == '\t') { - print(env, "\\t"); + mp_print_str(print, "\\t"); } else if (ch < 0x100) { - print(env, "\\x%02x", ch); + mp_printf(print, "\\x%02x", ch); } else if (ch < 0x10000) { - print(env, "\\u%04x", ch); + mp_printf(print, "\\u%04x", ch); } else { - print(env, "\\U%08x", ch); + mp_printf(print, "\\U%08x", ch); } } - print(env, "%c", quote_char); + mp_printf(print, "%c", quote_char); } -STATIC void uni_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind) { +STATIC void uni_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { GET_STR_DATA_LEN(self_in, str_data, str_len); #if MICROPY_PY_UJSON if (kind == PRINT_JSON) { - mp_str_print_json(print, env, str_data, str_len); + mp_str_print_json(print, str_data, str_len); return; } #endif if (kind == PRINT_STR) { - print(env, "%.*s", str_len, str_data); + mp_printf(print, "%.*s", str_len, str_data); } else { - uni_print_quoted(print, env, str_data, str_len); + uni_print_quoted(print, str_data, str_len); } } |