diff options
author | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-01-04 03:47:34 +0200 |
---|---|---|
committer | Paul Sokolovsky <pfalcon@users.sourceforge.net> | 2014-01-04 03:55:37 +0200 |
commit | 6ee1e383d6b95d0bb5f2902ec91b8d831e4b5803 (patch) | |
tree | ce8fc6ff3cfae972e1780fbcf0ee6258c36135cd /py/objstr.c | |
parent | c8d1384fc0c7aafa5dee3445ece20f4e43dfa9c1 (diff) | |
download | micropython-6ee1e383d6b95d0bb5f2902ec91b8d831e4b5803.tar.gz micropython-6ee1e383d6b95d0bb5f2902ec91b8d831e4b5803.zip |
str slice: Trim slice indexes to be in range.
Diffstat (limited to 'py/objstr.c')
-rw-r--r-- | py/objstr.c | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/py/objstr.c b/py/objstr.c index 59547e3cd6..54e6f37705 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -41,9 +41,20 @@ mp_obj_t str_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { int len = strlen(lhs_str); if (start < 0) { start = len + start; + if (start < 0) { + start = 0; + } + } else if (start > len) { + start = len; } if (stop <= 0) { stop = len + stop; + // CPython returns empty string in such case + if (stop < 0) { + stop = start; + } + } else if (stop > len) { + stop = len; } return mp_obj_new_str(qstr_from_strn_copy(lhs_str + start, stop - start)); #endif |