summaryrefslogtreecommitdiffstatshomepage
path: root/py/modbuiltins.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2017-10-11 11:25:20 +1100
committerDamien George <damien.p.george@gmail.com>2017-10-11 11:25:20 +1100
commit69da74e53830412cf88ff577aa7d3cbf5741eb83 (patch)
tree71b8be0f6e1b48ca3cdf84eca965a9ea2b827698 /py/modbuiltins.c
parentdc948e4d54eb8f7fecbd26efcfb3cd5d02bf8500 (diff)
downloadmicropython-69da74e53830412cf88ff577aa7d3cbf5741eb83.tar.gz
micropython-69da74e53830412cf88ff577aa7d3cbf5741eb83.zip
py/modbuiltins: Use existing utf8_get_char helper in builtin ord func.
Diffstat (limited to 'py/modbuiltins.c')
-rw-r--r--py/modbuiltins.c25
1 files changed, 5 insertions, 20 deletions
diff --git a/py/modbuiltins.c b/py/modbuiltins.c
index 82b08cdc91..65c03d5231 100644
--- a/py/modbuiltins.c
+++ b/py/modbuiltins.c
@@ -348,31 +348,16 @@ STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
if (MP_OBJ_IS_STR(o_in)) {
len = unichar_charlen(str, len);
if (len == 1) {
- if (!UTF8_IS_NONASCII(*str)) {
- goto return_first_byte;
- }
- mp_int_t ord = *str++ & 0x7F;
- for (mp_int_t mask = 0x40; ord & mask; mask >>= 1) {
- ord &= ~mask;
- }
- while (UTF8_IS_CONT(*str)) {
- ord = (ord << 6) | (*str++ & 0x3F);
- }
- return mp_obj_new_int(ord);
+ return mp_obj_new_int(utf8_get_char((const byte*)str));
}
- } else {
- // a bytes object
+ } else
+ #endif
+ {
+ // a bytes object, or a str without unicode support (don't sign extend the char)
if (len == 1) {
- return_first_byte:
return MP_OBJ_NEW_SMALL_INT(((const byte*)str)[0]);
}
}
- #else
- if (len == 1) {
- // don't sign extend when converting to ord
- return mp_obj_new_int(((const byte*)str)[0]);
- }
- #endif
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
mp_raise_TypeError("ord expects a character");