From bcdffe53c641ad5832f06cd544f29e3cdd522829 Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Fri, 30 May 2014 03:07:05 +0300
Subject: [PATCH] objstr: *strip(): Fix handling of one-char subject strings.

---
 py/objstr.c                  |  4 ++--
 tests/basics/string_strip.py | 11 +++++++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/py/objstr.c b/py/objstr.c
index 83fd002d1..42a246429 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -667,6 +667,7 @@ STATIC mp_obj_t str_uni_strip(int type, uint n_args, const mp_obj_t *args) {
     for (machine_uint_t len = orig_str_len; len > 0; len--) {
         if (find_subbytes(chars_to_del, chars_to_del_len, &orig_str[i], 1, 1) == NULL) {
             if (!first_good_char_pos_set) {
+                first_good_char_pos_set = true;
                 first_good_char_pos = i;
                 if (type == LSTRIP) {
                     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) {
                     last_good_char_pos = i;
                     break;
                 }
-                first_good_char_pos_set = true;
             }
             last_good_char_pos = i;
         }
         i += delta;
     }
 
-    if (first_good_char_pos == 0 && last_good_char_pos == 0) {
+    if (!first_good_char_pos_set) {
         // string is all whitespace, return ''
         return MP_OBJ_NEW_QSTR(MP_QSTR_);
     }
diff --git a/tests/basics/string_strip.py b/tests/basics/string_strip.py
index 4684c2a24..70c74b383 100644
--- a/tests/basics/string_strip.py
+++ b/tests/basics/string_strip.py
@@ -20,3 +20,14 @@ try:
     print('mississippi'.rstrip(b'ipz'))
 except TypeError:
     print("TypeError")
+
+# single-char subj string used to give a problem
+print("a".strip())
+print("a".lstrip())
+print("a".rstrip())
+print(" a".strip())
+print(" a".lstrip())
+print(" a".rstrip())
+print("a ".strip())
+print("a ".lstrip())
+print("a ".rstrip())
-- 
GitLab