diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-01-21 20:14:18 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2017-01-21 20:14:18 +0300 |
commit | bec7bfb29d76f995dbf223c20f5d04ae1e33cee6 (patch) | |
tree | f44914a3ee66ef924f15f477f0fc87ade4a06c5d /py/objint.c | |
parent | 1b42f5251fb126496584e2fc61fad12bca7f7152 (diff) |
py/objint: from_bytes(): Implement "byteorder" param and arbitrary precision.
If result guaranteedly fits in a small int, it is handled in objint.c.
Otherwise, it is delegated to mp_obj_int_from_bytes_impl(), which should
be implemented by individual objint_*.c, similar to
mp_obj_int_to_bytes_impl().
Diffstat (limited to 'py/objint.c')
-rw-r--r-- | py/objint.c | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/py/objint.c b/py/objint.c index 5842a00a4..a61ed8e27 100644 --- a/py/objint.c +++ b/py/objint.c @@ -374,26 +374,33 @@ mp_obj_t mp_obj_int_binary_op_extra_cases(mp_uint_t op, mp_obj_t lhs_in, mp_obj_ // this is a classmethod STATIC mp_obj_t int_from_bytes(size_t n_args, const mp_obj_t *args) { - // TODO: Support long ints - // TODO: Support byteorder param // TODO: Support signed param (assumes signed=False at the moment) (void)n_args; - if (args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little)) { - mp_not_implemented(""); - } - // get the buffer info mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ); - // convert the bytes to an integer - mp_uint_t value = 0; - for (const byte* buf = (const byte*)bufinfo.buf + bufinfo.len - 1; buf >= (byte*)bufinfo.buf; buf--) { - value = (value << 8) | *buf; - } + #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE + // If result guaranteedly fits in small int, use that + if (!MP_SMALL_INT_FITS(1 << (bufinfo.len * 8 - 1))) { + return mp_obj_int_from_bytes_impl(args[2] != MP_OBJ_NEW_QSTR(MP_QSTR_little), bufinfo.len, bufinfo.buf); + } else + #endif + { + const byte* buf = (const byte*)bufinfo.buf; + int delta = 1; + if (args[2] == MP_OBJ_NEW_QSTR(MP_QSTR_little)) { + buf += bufinfo.len - 1; + delta = -1; + } - return mp_obj_new_int_from_uint(value); + mp_uint_t value = 0; + for (; bufinfo.len--; buf += delta) { + value = (value << 8) | *buf; + } + return mp_obj_new_int_from_uint(value); + } } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(int_from_bytes_fun_obj, 3, 4, int_from_bytes); |