Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions numpy/_core/src/umath/stringdtype_ufuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2264,10 +2264,13 @@ slice_strided_loop(PyArrayMethod_Context *context, char *const data[],

if (step == 1) {
// step == 1 is the easy case, we can just use memcpy
npy_intp outsize = ((size_t)stop < num_codepoints
? codepoint_offsets[stop]
: (unsigned char *)is.buf + is.size) -
codepoint_offsets[start];
unsigned char *start_bounded = ((size_t)start < num_codepoints
? codepoint_offsets[start]
: (unsigned char *)is.buf + is.size);
unsigned char *stop_bounded = ((size_t)stop < num_codepoints
? codepoint_offsets[stop]
: (unsigned char *)is.buf + is.size);
npy_intp outsize = stop_bounded - start_bounded;

if (load_new_string(ops, &os, outsize, oallocator, "slice") < 0) {
goto fail;
Expand All @@ -2276,7 +2279,7 @@ slice_strided_loop(PyArrayMethod_Context *context, char *const data[],
/* explicitly discard const; initializing new buffer */
char *buf = (char *)os.buf;

memcpy(buf, codepoint_offsets[start], outsize);
memcpy(buf, start_bounded, outsize);
}
else {
// handle step != 1
Expand Down
10 changes: 8 additions & 2 deletions numpy/_core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,7 @@ def translate(a, table, deletechars=None):
)

@set_module("numpy.strings")
def slice(a, start=None, stop=None, step=None, /):
def slice(a, start=None, stop=np._NoValue, step=None, /):
"""
Slice the strings in `a` by slices specified by `start`, `stop`, `step`.
Like in the regular Python `slice` object, if only `start` is
Expand Down Expand Up @@ -1776,6 +1776,9 @@ def slice(a, start=None, stop=None, step=None, /):
>>> np.strings.slice(a, 2)
array(['he', 'wo'], dtype='<U5')

>>> np.strings.slice(a, 2, None)
array(['llo', 'rld'], dtype='<U5')

>>> np.strings.slice(a, 1, 5, 2)
array(['el', 'ol'], dtype='<U5')

Expand All @@ -1791,6 +1794,9 @@ def slice(a, start=None, stop=None, step=None, /):
>>> np.strings.slice(b, -2)
array(['hello wor', 'γεια σου κόσ', '你好', '👋'], dtype=StringDType())

>>> np.strings.slice(b, -2, None)
array(['ld', 'με', '世界', ' 🌍'], dtype=StringDType())

>>> np.strings.slice(b, [3, -10, 2, -3], [-1, -2, -1, 3])
array(['lo worl', ' σου κόσ', '世', '👋 🌍'], dtype=StringDType())

Expand All @@ -1801,7 +1807,7 @@ def slice(a, start=None, stop=None, step=None, /):
"""
# Just like in the construction of a regular slice object, if only start
# is specified then start will become stop, see logic in slice_new.
if stop is None:
if stop is np._NoValue:
stop = start
start = None

Expand Down
21 changes: 21 additions & 0 deletions numpy/_core/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,17 +975,38 @@ def test_rpartition(self, buf, sep, res1, res2, res3, dt):

@pytest.mark.parametrize("args", [
(None,),
(None, None),
(None, None, -1),
(0,),
(0, None),
(0, None, -1),
(1,),
(1, None),
(1, None, -1),
(3,),
(3, None),
(5,),
(5, None),
(5, 5),
(5, 5, -1),
(6,), # test index past the end
(6, None),
(6, None, -1),
(6, 7), # test stop index past the end
(-1,),
(-1, None),
(-1, None, -1),
(-3,),
(-3, None),
([3, 4],),
([3, 4], None),
([2, 4],),
([-3, 5],),
([-3, 5], None),
([-3, 5], None, -1),
([0, -5],),
([0, -5], None),
([0, -5], None, -1),
(1, 4),
(-3, 5),
(None, -1),
Expand Down
Loading