str: Implement proper string (instead of byte string) indexing.
Also, support negative indexes.
This commit is contained in:
@@ -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)
|
// TODO: need predicate to check for int-like type (bools are such for example)
|
||||||
// ["no", "yes"][1 == 2] is common idiom
|
// ["no", "yes"][1 == 2] is common idiom
|
||||||
if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
|
if (MP_OBJ_IS_SMALL_INT(rhs_in)) {
|
||||||
// TODO: This implements byte string access for single index so far
|
uint index = mp_get_index(lhs->base.type, strlen(lhs_str), rhs_in);
|
||||||
// TODO: Handle negative indexes.
|
return mp_obj_new_str(qstr_from_strn_copy(lhs_str + index, 1));
|
||||||
return mp_obj_new_int(lhs_str[mp_obj_get_int(rhs_in)]);
|
|
||||||
#if MICROPY_ENABLE_SLICE
|
#if MICROPY_ENABLE_SLICE
|
||||||
} else if (MP_OBJ_IS_TYPE(rhs_in, &slice_type)) {
|
} else if (MP_OBJ_IS_TYPE(rhs_in, &slice_type)) {
|
||||||
machine_int_t start, stop, step;
|
machine_int_t start, stop, step;
|
||||||
|
|||||||
@@ -10,6 +10,17 @@ print('123' + "456")
|
|||||||
|
|
||||||
print('123' * 5)
|
print('123' * 5)
|
||||||
|
|
||||||
|
print('abc'[1])
|
||||||
|
print('abc'[-1])
|
||||||
|
try:
|
||||||
|
'abc'[100]
|
||||||
|
except IndexError:
|
||||||
|
print('caught')
|
||||||
|
try:
|
||||||
|
'abc'[-4]
|
||||||
|
except IndexError:
|
||||||
|
print('caught2')
|
||||||
|
|
||||||
# iter
|
# iter
|
||||||
print(list('str'))
|
print(list('str'))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user