summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-01-04 04:28:57 -0800
committerDamien George <damien.p.george@gmail.com>2014-01-04 04:28:57 -0800
commit0be78d44e59dd82eccde984a899d25bdf9afc093 (patch)
treefbc160a4fa4a38ada77bcd8d293783cb9c2d39c8 /py
parent597bb2f2496fd2d89a69ad6b00ba212359aa96bf (diff)
parent6ee1e383d6b95d0bb5f2902ec91b8d831e4b5803 (diff)
Merge pull request #64 from pfalcon/str-slice-range-check
str slice: Trim slice indexes to be in range.
Diffstat (limited to 'py')
-rw-r--r--py/objstr.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/py/objstr.c b/py/objstr.c
index 59547e3cd..54e6f3770 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