Skip to content

Commit 69da74e

Browse files
committed
py/modbuiltins: Use existing utf8_get_char helper in builtin ord func.
1 parent dc948e4 commit 69da74e

File tree

1 file changed

+5
-20
lines changed

1 file changed

+5
-20
lines changed

py/modbuiltins.c

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -348,31 +348,16 @@ STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
348348
if (MP_OBJ_IS_STR(o_in)) {
349349
len = unichar_charlen(str, len);
350350
if (len == 1) {
351-
if (!UTF8_IS_NONASCII(*str)) {
352-
goto return_first_byte;
353-
}
354-
mp_int_t ord = *str++ & 0x7F;
355-
for (mp_int_t mask = 0x40; ord & mask; mask >>= 1) {
356-
ord &= ~mask;
357-
}
358-
while (UTF8_IS_CONT(*str)) {
359-
ord = (ord << 6) | (*str++ & 0x3F);
360-
}
361-
return mp_obj_new_int(ord);
351+
return mp_obj_new_int(utf8_get_char((const byte*)str));
362352
}
363-
} else {
364-
// a bytes object
353+
} else
354+
#endif
355+
{
356+
// a bytes object, or a str without unicode support (don't sign extend the char)
365357
if (len == 1) {
366-
return_first_byte:
367358
return MP_OBJ_NEW_SMALL_INT(((const byte*)str)[0]);
368359
}
369360
}
370-
#else
371-
if (len == 1) {
372-
// don't sign extend when converting to ord
373-
return mp_obj_new_int(((const byte*)str)[0]);
374-
}
375-
#endif
376361

377362
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
378363
mp_raise_TypeError("ord expects a character");

0 commit comments

Comments
 (0)