summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-01-03 02:51:16 +0200
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-01-04 02:35:48 +0200
commit31ba60f8364a4009ddc3d45fee90c84b43d88d2c (patch)
tree2968078f3511e1339b2a7b9b98f9e334bc1cd8fc /py
parentded0a1efa5744235fbb4cd07c96598018a3661eb (diff)
downloadmicropython-31ba60f8364a4009ddc3d45fee90c84b43d88d2c.tar.gz
micropython-31ba60f8364a4009ddc3d45fee90c84b43d88d2c.zip
str: Initial implementation of string slicing.
Only step=1 and non-negative indexes are supported so far.
Diffstat (limited to 'py')
-rw-r--r--py/objstr.c16
1 files changed, 15 insertions, 1 deletions
diff --git a/py/objstr.c b/py/objstr.c
index 48abf4951d..46adabcec9 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -29,7 +29,21 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
case RT_BINARY_OP_SUBSCR:
// string access
// XXX a massive hack!
- return mp_obj_new_int(lhs_str[mp_obj_get_int(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
+ return mp_obj_new_int(lhs_str[mp_obj_get_int(rhs_in)]);
+ } else if (MP_OBJ_IS_TYPE(rhs_in, &slice_type)) {
+ int start, stop, step;
+ mp_obj_slice_get(rhs_in, &start, &stop, &step);
+ assert(step == 1);
+ return mp_obj_new_str(qstr_from_strn_copy(lhs_str + start, stop - start));
+ } else {
+ // Throw TypeError here
+ assert(0);
+ }
case RT_BINARY_OP_ADD:
case RT_BINARY_OP_INPLACE_ADD: