summaryrefslogtreecommitdiffstatshomepage
path: root/py/objstrunicode.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/objstrunicode.c')
-rw-r--r--py/objstrunicode.c30
1 files changed, 21 insertions, 9 deletions
diff --git a/py/objstrunicode.c b/py/objstrunicode.c
index c6c775d109..8444a26892 100644
--- a/py/objstrunicode.c
+++ b/py/objstrunicode.c
@@ -116,7 +116,14 @@ STATIC mp_obj_t uni_unary_op(mp_uint_t op, mp_obj_t self_in) {
// be capped to the first/last character of the string, depending on is_slice.
const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, size_t self_len,
mp_obj_t index, bool is_slice) {
- (void)type;
+ // All str functions also handle bytes objects, and they call str_index_to_ptr(),
+ // so it must handle bytes.
+ if (type == &mp_type_bytes) {
+ // Taken from objstr.c:str_index_to_ptr()
+ mp_uint_t index_val = mp_get_index(type, self_len, index, is_slice);
+ return self_data + index_val;
+ }
+
mp_int_t i;
// Copied from mp_get_index; I don't want bounds checking, just give me
// the integer as-is. (I can't bounds-check without scanning the whole
@@ -142,26 +149,29 @@ const byte *str_index_to_ptr(const mp_obj_type_t *type, const byte *self_data, s
}
}
++s;
- } else if (!i) {
- return self_data; // Shortcut - str[0] is its base pointer
} else {
// Positive indexing, correspondingly, counts from the start of the string.
// It's assumed that negative indexing will generally be used with small
// absolute values (eg str[-1], not str[-1000000]), which means it'll be
// more efficient this way.
- for (s = self_data; true; ++s) {
+ s = self_data;
+ while (1) {
+ // First check out-of-bounds
if (s >= top) {
if (is_slice) {
return top;
}
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "string index out of range"));
}
+ // Then check completion
+ if (i-- == 0) {
+ break;
+ }
+ // Then skip UTF-8 char
+ ++s;
while (UTF8_IS_CONT(*s)) {
++s;
}
- if (!i--) {
- return s;
- }
}
}
return s;
@@ -236,11 +246,13 @@ STATIC const mp_rom_map_elem_t struni_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_format), MP_ROM_PTR(&str_format_obj) },
{ MP_ROM_QSTR(MP_QSTR_replace), MP_ROM_PTR(&str_replace_obj) },
{ MP_ROM_QSTR(MP_QSTR_count), MP_ROM_PTR(&str_count_obj) },
+ #if MICROPY_PY_BUILTINS_STR_PARTITION
{ MP_ROM_QSTR(MP_QSTR_partition), MP_ROM_PTR(&str_partition_obj) },
{ MP_ROM_QSTR(MP_QSTR_rpartition), MP_ROM_PTR(&str_rpartition_obj) },
-#if MICROPY_PY_BUILTINS_STR_CENTER
+ #endif
+ #if MICROPY_PY_BUILTINS_STR_CENTER
{ MP_ROM_QSTR(MP_QSTR_center), MP_ROM_PTR(&str_center_obj) },
-#endif
+ #endif
{ MP_ROM_QSTR(MP_QSTR_lower), MP_ROM_PTR(&str_lower_obj) },
{ MP_ROM_QSTR(MP_QSTR_upper), MP_ROM_PTR(&str_upper_obj) },
{ MP_ROM_QSTR(MP_QSTR_isspace), MP_ROM_PTR(&str_isspace_obj) },