Skip to content

Commit bcdffe5

Browse files
committed
objstr: *strip(): Fix handling of one-char subject strings.
1 parent 059f95b commit bcdffe5

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

py/objstr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) {
667667
for (machine_uint_t len = orig_str_len; len > 0; len--) {
668668
if (find_subbytes(chars_to_del, chars_to_del_len, &orig_str[i], 1, 1) == NULL) {
669669
if (!first_good_char_pos_set) {
670+
first_good_char_pos_set = true;
670671
first_good_char_pos = i;
671672
if (type == LSTRIP) {
672673
last_good_char_pos = orig_str_len - 1;
@@ -676,14 +677,13 @@ STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) {
676677
last_good_char_pos = i;
677678
break;
678679
}
679-
first_good_char_pos_set = true;
680680
}
681681
last_good_char_pos = i;
682682
}
683683
i += delta;
684684
}
685685

686-
if (first_good_char_pos == 0 && last_good_char_pos == 0) {
686+
if (!first_good_char_pos_set) {
687687
// string is all whitespace, return ''
688688
return MP_OBJ_NEW_QSTR(MP_QSTR_);
689689
}

tests/basics/string_strip.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,14 @@
2020
print('mississippi'.rstrip(b'ipz'))
2121
except TypeError:
2222
print("TypeError")
23+
24+
# single-char subj string used to give a problem
25+
print("a".strip())
26+
print("a".lstrip())
27+
print("a".rstrip())
28+
print(" a".strip())
29+
print(" a".lstrip())
30+
print(" a".rstrip())
31+
print("a ".strip())
32+
print("a ".lstrip())
33+
print("a ".rstrip())

0 commit comments

Comments
 (0)