diff options
author | Damien George <damien.p.george@gmail.com> | 2014-04-07 21:50:22 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-04-07 21:50:22 +0100 |
commit | 97543c5285b47c765e19d6ab7c08b36630e23c6a (patch) | |
tree | fd55f309f8eaa4059dd7e4160250c22f115d2b59 /py/objstr.c | |
parent | 98dd6d7712acdefb9179743564ee546339da9519 (diff) | |
parent | 23dc6d0aceba51756f01e96c099678c38b4a88f9 (diff) | |
download | micropython-97543c5285b47c765e19d6ab7c08b36630e23c6a.tar.gz micropython-97543c5285b47c765e19d6ab7c08b36630e23c6a.zip |
Merge pull request #447 from dhylands/str-format-mpz
Add string formatting support for longlong and mpz.
Diffstat (limited to 'py/objstr.c')
-rw-r--r-- | py/objstr.c | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/py/objstr.c b/py/objstr.c index 4395757a46..07134687d1 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -562,13 +562,17 @@ static bool arg_looks_numeric(mp_obj_t arg) { ; } -static machine_int_t arg_as_int(mp_obj_t arg) { +static mp_obj_t arg_as_int(mp_obj_t arg) { #if MICROPY_ENABLE_FLOAT if (MP_OBJ_IS_TYPE(arg, &mp_type_float)) { - return mp_obj_get_float(arg); + + // TODO: Needs a way to construct an mpz integer from a float + + mp_small_int_t num = mp_obj_get_float(arg); + return MP_OBJ_NEW_SMALL_INT(num); } #endif - return mp_obj_get_int(arg); + return arg; } mp_obj_t str_format(uint n_args, const mp_obj_t *args) { @@ -788,7 +792,7 @@ mp_obj_t str_format(uint n_args, const mp_obj_t *args) { if (arg_looks_integer(arg)) { switch (type) { case 'b': - pfenv_print_int(&pfenv_vstr, mp_obj_get_int(arg), 1, 2, 'a', flags, fill, width); + pfenv_print_int(&pfenv_vstr, arg, 1, 2, 'a', flags, fill, width); continue; case 'c': @@ -801,19 +805,23 @@ mp_obj_t str_format(uint n_args, const mp_obj_t *args) { case '\0': // No explicit format type implies 'd' case 'n': // I don't think we support locales in uPy so use 'd' case 'd': - pfenv_print_int(&pfenv_vstr, mp_obj_get_int(arg), 1, 10, 'a', flags, fill, width); + pfenv_print_int(&pfenv_vstr, arg, 1, 10, 'a', flags, fill, width); continue; case 'o': - pfenv_print_int(&pfenv_vstr, mp_obj_get_int(arg), 1, 8, 'a', flags, fill, width); + if (flags & PF_FLAG_SHOW_PREFIX) { + flags |= PF_FLAG_SHOW_OCTAL_LETTER; + } + + pfenv_print_int(&pfenv_vstr, arg, 1, 8, 'a', flags, fill, width); continue; case 'x': - pfenv_print_int(&pfenv_vstr, mp_obj_get_int(arg), 1, 16, 'a', flags, fill, width); + pfenv_print_int(&pfenv_vstr, arg, 1, 16, 'a', flags, fill, width); continue; case 'X': - pfenv_print_int(&pfenv_vstr, mp_obj_get_int(arg), 1, 16, 'A', flags, fill, width); + pfenv_print_int(&pfenv_vstr, arg, 1, 16, 'A', flags, fill, width); continue; case 'e': @@ -1050,7 +1058,7 @@ STATIC mp_obj_t str_modulo_format(mp_obj_t pattern, uint n_args, const mp_obj_t case 'o': if (alt) { - flags |= PF_FLAG_SHOW_PREFIX; + flags |= (PF_FLAG_SHOW_PREFIX | PF_FLAG_SHOW_OCTAL_LETTER); } pfenv_print_int(&pfenv_vstr, arg_as_int(arg), 1, 8, 'a', flags, fill, width); break; |