Repository: numba/numba

Issue:
Unicode lstrip/rstrip/strip do not handle all whitespace characters
<!--

Thanks for opening an issue! To help the Numba team handle your information
efficiently, please first ensure that there is no other issue present that
already describes the issue you have
(search at https://github.com/numba/numba/issues?&q=is%3Aissue).

For more general "how do I do X?" type questions, please speak to us in real
time on https://gitter.im/numba/numba or post to the Numba mailing list
https://groups.google.com/a/continuum.io/forum/#!forum/numba-users.

-->

## Reporting a bug

<!--

Before submitting a bug report please ensure that you can check off these boxes:

-->

- [x] I am using the latest ~released~ master version of Numba (most recent is visible in
 the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [x] I have included below a minimal working reproducer (if you are unsure how
 to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).

<!--

Please include details of the bug here, including, if applicable, what you
expected to happen!

-->
Unicode `lstrip`, `rstrip`, `strip` functions only handle space as whitespace. Other whitespace characters like `\t` are ignored:
```python
In [5]: numba.njit(lambda: '\ta'.lstrip())()                                                                                                                                                                
Out[5]: '\ta'

In [6]: '\ta'.lstrip()                                                                                                                                                                                      
Out[6]: 'a'
```
`string` library reports whitespace characters as below, but we need to look into CPython implementation.
```
In [1]: import string                                                                                                                                                                                       
In [2]: string.whitespace                                                                                                                                                                                   
Out[2]: ' \t\n\r\x0b\x0c'
```
`.strip()` not stripping `\t`.
<!--

Thanks for opening an issue! To help the Numba team handle your information
efficiently, please first ensure that there is no other issue present that
already describes the issue you have
(search at https://github.com/numba/numba/issues?&q=is%3Aissue).

For more general "how do I do X?" type questions, please speak to us in real
time on https://gitter.im/numba/numba or post to the Numba mailing list
https://groups.google.com/a/continuum.io/forum/#!forum/numba-users.

-->

## Reporting a bug

<!--

Before submitting a bug report please ensure that you can check off these boxes:

-->

- [x] I am using the latest released version of Numba (most recent is visible in
 the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [x] I have included below a minimal working reproducer (if you are unsure how
 to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).

```
>>> from numba import njit
>>> s = '  \t bbcd\t '
>>> s.strip()
'bbcd'
>>> @njit
... def foo(s):
...     return s.strip()
...
>>> foo(s)
'\t bbcd\t'
>>>

```

Discussion:

Patch:
diff --git a/numba/cpython/unicode.py b/numba/cpython/unicode.py
index 5ef88103c..1978e603a 100644
--- a/numba/cpython/unicode.py
+++ b/numba/cpython/unicode.py
@@ -1388,24 +1388,35 @@ def unicode_zfill(string, width):
 # ------------------------------------------------------------------------------
 @register_jitable
 def unicode_strip_left_bound(string, chars):
-    chars = ' ' if chars is None else chars
     str_len = len(string)
 
-    for i in range(str_len):
-        if string[i] not in chars:
-            return i
+    if chars is not None:
+        for i in range(str_len):
+            if string[i] not in chars:
+                return i
+    else:
+        for i in range(str_len):
+            if not _PyUnicode_IsSpace(string[i]):
+                return i
+
     return str_len
 
 
 @register_jitable
 def unicode_strip_right_bound(string, chars):
-    chars = ' ' if chars is None else chars
     str_len = len(string)
 
-    for i in range(str_len - 1, -1, -1):
-        if string[i] not in chars:
-            i += 1
-            break
+    if chars is not None:
+        for i in range(str_len - 1, -1, -1):
+            if string[i] not in chars:
+                i += 1
+                break
+    else:
+        for i in range(str_len - 1, -1, -1):
+            if not _PyUnicode_IsSpace(string[i]):
+                i += 1
+                break
+
     return i
 
 


Test patch:
diff --git a/numba/tests/test_unicode.py b/numba/tests/test_unicode.py
index 286e99693..c62cc79b8 100644
--- a/numba/tests/test_unicode.py
+++ b/numba/tests/test_unicode.py
@@ -1701,7 +1701,15 @@ class TestUnicode(BaseTest):
             ('  tú quién te crees?   ', None),
             ('大处 着眼，小处着手。大大大处', '大处'),
             (' 大处大处  ', ''),
-            (' 大处大处  ', None)
+            ('\t\nabcd\t', '\ta'),
+            (' 大处大处  ', None),
+            ('\t abcd \t', None),
+            ('\n abcd \n', None),
+            ('\r abcd \r', None),
+            ('\x0b abcd \x0b', None),
+            ('\x0c abcd \x0c', None),
+            ('\u2029abcd\u205F', None),
+            ('\u0085abcd\u2009', None)
         ]
 
         # form with no parameter