diff options
author | xyb <xieyanbo@gmail.com> | 2014-01-14 21:39:05 +0800 |
---|---|---|
committer | xyb <xieyanbo@gmail.com> | 2014-01-14 21:39:05 +0800 |
commit | c178ea471ee30f32439741181d30d6a89830aabf (patch) | |
tree | bbd572854f1491523ab70f122695f511c3d9c265 /py/obj.c | |
parent | 729e9cce7bd31d3f107a4d6e9498b0fa27119e22 (diff) | |
download | micropython-c178ea471ee30f32439741181d30d6a89830aabf.tar.gz micropython-c178ea471ee30f32439741181d30d6a89830aabf.zip |
Implemented int(str) in UNIX
Diffstat (limited to 'py/obj.c')
-rw-r--r-- | py/obj.c | 18 |
1 files changed, 18 insertions, 0 deletions
@@ -13,6 +13,7 @@ #include "runtime0.h" #include "runtime.h" #include "map.h" +#include "strtonum.h" mp_obj_t mp_obj_get_type(mp_obj_t o_in) { if (MP_OBJ_IS_SMALL_INT(o_in)) { @@ -136,6 +137,12 @@ bool mp_obj_less(mp_obj_t o1, mp_obj_t o2) { } machine_int_t mp_obj_get_int(mp_obj_t arg) { + return mp_obj_get_int_base(arg, 0); +} + +machine_int_t mp_obj_get_int_base(mp_obj_t arg, mp_obj_t base_arg) { + const char *value; + int base; if (arg == mp_const_false) { return 0; } else if (arg == mp_const_true) { @@ -147,6 +154,17 @@ machine_int_t mp_obj_get_int(mp_obj_t arg) { // TODO work out if this should be floor, ceil or trunc return (machine_int_t)mp_obj_float_get(arg); #endif + } else if (MP_OBJ_IS_TYPE(arg, &str_type)) { + if (base_arg == 0) { + value = qstr_str(mp_obj_str_get(arg)); + return (machine_int_t)strtonum(value, 0); + } else if (MP_OBJ_IS_TYPE(base_arg, &int_type)) { + base = MP_OBJ_SMALL_INT_VALUE(base_arg); + value = qstr_str(mp_obj_str_get(arg)); + return (machine_int_t)strtonum(value, base); + } else { + nlr_jump(mp_obj_new_exception_msg(MP_QSTR_TypeError, "an integer is required")); + } } else { nlr_jump(mp_obj_new_exception_msg_varg(MP_QSTR_TypeError, "can't convert %s to int", mp_obj_get_type_str(arg))); } |