aboutsummaryrefslogtreecommitdiff
path: root/py/objstrunicode.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-07-25 19:02:51 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2016-07-25 19:28:04 +0300
commited1c194ebf12c1e894d7620ca89c70f87b5d5709 (patch)
treed61028698a7ea2e0b6c5f77112547b3860201c8d /py/objstrunicode.c
parent6af90b29725a85f275380d1973b1454e25e6bdbc (diff)
py/objstrunicode: str_index_to_ptr: Implement positive indexing properly.
Order out-of-bounds check, completion check, and increment in the right way.
Diffstat (limited to 'py/objstrunicode.c')
-rw-r--r--py/objstrunicode.c15
1 files changed, 9 insertions, 6 deletions
diff --git a/py/objstrunicode.c b/py/objstrunicode.c
index c3aa00833..495ef3ae9 100644
--- a/py/objstrunicode.c
+++ b/py/objstrunicode.c
@@ -149,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;