diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-07-25 14:43:04 +0300 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2016-07-25 14:45:08 +0300 |
commit | 6af90b29725a85f275380d1973b1454e25e6bdbc (patch) | |
tree | 90a2693cf3e4ab73af68c9f8963521a5ef7f50c7 | |
parent | 16f324641f33201061fb4e79677bc760e45fb3ac (diff) | |
download | micropython-6af90b29725a85f275380d1973b1454e25e6bdbc.tar.gz micropython-6af90b29725a85f275380d1973b1454e25e6bdbc.zip |
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.
-rw-r--r-- | py/objstrunicode.c | 9 | ||||
-rw-r--r-- | tests/basics/bytes_find.py | 3 |
2 files changed, 11 insertions, 1 deletions
diff --git a/py/objstrunicode.c b/py/objstrunicode.c index c6c775d109..c3aa008332 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 diff --git a/tests/basics/bytes_find.py b/tests/basics/bytes_find.py index 434669a901..75ef9796cd 100644 --- a/tests/basics/bytes_find.py +++ b/tests/basics/bytes_find.py @@ -21,3 +21,6 @@ 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', 5)) + +# Non-ascii values (make sure not treated as unicode-like) +print(b"\x80abc".find(b"a", 1)) |