diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-01-03 23:36:56 +0200 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-01-04 02:35:48 +0200 |
commit | decad08ef57aa3cf3960ce65e29b194cb97c6d22 (patch) | |
tree | 90081a761735054d7c1212106550b858b2562810 | |
parent | 59800afae9f74571eea5bb463a28fd9bd8251150 (diff) | |
download | micropython-decad08ef57aa3cf3960ce65e29b194cb97c6d22.tar.gz micropython-decad08ef57aa3cf3960ce65e29b194cb97c6d22.zip |
str: Handle non-positive slice indexes.
-rw-r--r-- | py/objstr.c | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/py/objstr.c b/py/objstr.c index 46adabcec9..54dd087a45 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -39,6 +39,13 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { int start, stop, step; mp_obj_slice_get(rhs_in, &start, &stop, &step); assert(step == 1); + int len = strlen(lhs_str); + if (start < 0) { + start = len + start; + } + if (stop <= 0) { + stop = len + stop; + } return mp_obj_new_str(qstr_from_strn_copy(lhs_str + start, stop - start)); } else { // Throw TypeError here |