py/objstrunicode: str_index_to_ptr: Should handle bytes too.

There's single str_index_to_ptr() function, called for both bytes and
unicode objects, so should handle each properly.
This commit is contained in:
Paul Sokolovsky 2016-07-25 14:43:04 +03:00
parent 16f324641f
commit 6af90b2972
2 changed files with 11 additions and 1 deletions

View File

@ -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. // 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, 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) { 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; mp_int_t i;
// Copied from mp_get_index; I don't want bounds checking, just give me // 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 // the integer as-is. (I can't bounds-check without scanning the whole

View File

@ -21,3 +21,6 @@ print(b"0000".find(b'-1', 3))
print(b"0000".find(b'1', 3)) print(b"0000".find(b'1', 3))
print(b"0000".find(b'1', 4)) print(b"0000".find(b'1', 4))
print(b"0000".find(b'1', 5)) print(b"0000".find(b'1', 5))
# Non-ascii values (make sure not treated as unicode-like)
print(b"\x80abc".find(b"a", 1))