8000 Merge pull request #25775 from lysnikolaou/string-ufuncs-index · numpy/numpy@74893ab · GitHub
[go: up one dir, main page]

Skip to content

Commit 74893ab

Browse files
authored
Merge pull request #25775 from lysnikolaou/string-ufuncs-index
2 parents 81dbf29 + 347a901 commit 74893ab

File tree

7 files changed

+516
-396
lines changed

7 files changed

+516
-396
lines changed

numpy/_core/code_generators/generate_umath.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,9 +1195,19 @@ def english_upper(s):
11951195
docstrings.get('numpy._core.umath.count'),
11961196
None,
11971197
),
1198+
'index':
1199+
Ufunc(4, 1, None,
1200+
docstrings.get('numpy._core.umath.index'),
1201+
None,
1202+
),
1203+
'rindex':
1204+
Ufunc(4, 1, None,
1205+
docstrings.get('numpy._core.umath.rindex'),
1206+
None,
1207+
),
11981208
'_replace':
11991209
Ufunc(4, 1, None,
1200-
docstrings.get('numpy.core.umath._replace'),
1210+
docstrings.get('numpy._core.umath._replace'),
12011211
None,
12021212
),
12031213
'startswith':

numpy/_core/code_generators/ufunc_docstrings.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4616,7 +4616,71 @@ def add_newdoc(place, name, doc):
46164616
46174617
""")
46184618

4619-
add_newdoc('numpy.core.umath', '_replace',
4619+
add_newdoc('numpy._core.umath', 'index',
4620+
"""
4621+
Like `find`, but raises :exc:`ValueError` when the substring is not found.
4622+
4623+
Parameters
4624+
----------
4625+
x1 : array_like, with ``StringDType``, ``bytes_`` or ``unicode_`` dtype
4626+
4627+
x2 : array_like, with ``StringDType``, ``bytes_`` or ``unicode_`` dtype
4628+
4629+
x3, x4 : array_like, with any integer dtype
4630+
The range to look in, interpreted as in slice notation.
4631+
$PARAMS
4632+
4633+
Returns
4634+
-------
4635+
out : ndarray
4636+
Output array of ints. Raises :exc:`ValueError` if `x2` is not found.
4637+
$OUT_SCALAR_2
4638+
4639+
See Also
4640+
--------
4641+
find, str.find
4642+
4643+
Examples
4644+
--------
4645+
>>> a = np.array(["Computer Science"])
4646+
>>> np.strings.index(a, "Science")
4647+
array([9])
4648+
4649+
""")
4650+
4651+
add_newdoc('numpy._core.umath', 'rindex',
4652+
"""
4653+
Like `rfind`, but raises :exc:`ValueError` when the substring is not found.
4654+
4655+
Parameters
4656+
----------
4657+
x1 : array_like, with ``StringDType``, ``bytes_`` or ``unicode_`` dtype
4658+
4659+
x2 : array_like, with ``StringDType``, ``bytes_`` or ``unicode_`` dtype
4660+
4661+
x3, x4 : array_like, with any integer dtype
4662+
The range to look in, interpreted as in slice notation.
4663+
$PARAMS
4664+
4665+
Returns
4666+
-------
4667+
out : ndarray
4668+
Output array of ints. Raises :exc:`ValueError` if `x2` is not found.
4669+
$OUT_SCALAR_2
4670+
4671+
See Also
4672+
--------
4673+
rfind, str.rfind
4674+
4675+
Examples
4676+
--------
4677+
>>> a = np.array(["Computer Science"])
4678+
>>> np.strings.rindex(a, "Science")
4679+
array([9])
4680+
4681+
""")
4682+
4683+
add_newdoc('numpy._core.umath', '_replace',
46204684
"""
46214685
UFunc implementation of ``replace``. This internal function
46224686
is called by ``replace`` with ``out`` set, so that the

numpy/_core/src/umath/string_buffer.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "numpy/ndarraytypes.h"
1313
#include "stringdtype/utf8_utils.h"
1414
#include "string_fastsearch.h"
15+
#include "gil_utils.h"
1516

1617
#define CHECK_OVERFLOW(index) if (buf + (index) >= after) return 0
1718
#define MSB(val) ((val) >> 7 & 1)
@@ -695,6 +696,19 @@ string_find(Buffer<enc> buf1, Buffer<enc> buf2, npy_int64 start, npy_int64 end)
695696
return pos;
696697
}
697698

699+
/* string_index returns -2 to signify a raised exception */< 2364 /div>
700+
template <ENCODING enc>
701+
static inline npy_intp
702+
string_index(Buffer<enc> buf1, Buffer<enc> buf2, npy_int64 start, npy_int64 end)
703+
{
704+
npy_intp pos = string_find(buf1, buf2, start, end);
705+
if (pos == -1) {
706+
npy_gil_error(PyExc_ValueError, "substring not found");
707+
return -2;
708+
}
709+
return pos;
710+
}
711+
698712
template <ENCODING enc>
699713
static inline npy_intp
700714
string_rfind(Buffer<enc> buf1, Buffer<enc> buf2, npy_int64 start, npy_int64 end)
@@ -786,6 +800,20 @@ string_rfind(Buffer<enc> buf1, Buffer<enc> buf2, npy_int64 start, npy_int64 end)
786800
}
787801

788802

803+
/* string_rindex returns -2 to signify a raised exception */
804+
template <ENCODING enc>
805+
static inline npy_intp
806+
string_rindex(Buffer<enc> buf1, Buffer<enc> buf2, npy_int64 start, npy_int64 end)
807+
{
808+
npy_intp pos = string_rfind(buf1, buf2, start, end);
809+
if (pos == -1) {
810+
npy_gil_error(PyExc_ValueError, "substring not found");
811+
return -2;
812+
}
813+
return pos;
814+
}
815+
816+
789817
/*
790818
* Count the number of occurences of buf2 in buf1 between
791819
* start (inclusive) and end (exclusive)

0 commit comments

Comments
 (0)
0