diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-01-21 02:22:02 +0200 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-01-21 02:25:59 +0200 |
commit | 7380a837802b4630bdcef6e01cd5be32f92750ca (patch) | |
tree | 8a26b5e0ab0581819d7d57e91992b7347103627d /py/objstr.c | |
parent | 545591a696bdff73f68573c54a05ca70eb58032d (diff) | |
download | micropython-7380a837802b4630bdcef6e01cd5be32f92750ca.tar.gz micropython-7380a837802b4630bdcef6e01cd5be32f92750ca.zip |
str: Implement proper string (instead of byte string) indexing.
Also, support negative indexes.
Diffstat (limited to 'py/objstr.c')
-rw-r--r-- | py/objstr.c | 5 |
1 files changed, 2 insertions, 3 deletions
diff --git a/py/objstr.c b/py/objstr.c index 01de5e3674..4adfef6f80 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -44,9 +44,8 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { // TODO: need predicate to check for int-like type (bools are such for example) // ["no", "yes"][1 == 2] is common idiom if (MP_OBJ_IS_SMALL_INT(rhs_in)) { - // TODO: This implements byte string access for single index so far - // TODO: Handle negative indexes. - return mp_obj_new_int(lhs_str[mp_obj_get_int(rhs_in)]); + uint index = mp_get_index(lhs->base.type, strlen(lhs_str), rhs_in); + return mp_obj_new_str(qstr_from_strn_copy(lhs_str + index, 1)); #if MICROPY_ENABLE_SLICE } else if (MP_OBJ_IS_TYPE(rhs_in, &slice_type)) { machine_int_t start, stop, step; |