8000 ENH: Add replace ufunc for bytes and unicode dtypes by lysnikolaou · Pull Request #25171 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add replace ufunc for bytes and unicode dtypes #25171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 24, 2023
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
5 changes: 5 additions & 0 deletions numpy/_core/code_generators/generate_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,11 @@ def english_upper(s):
docstrings.get('numpy._core.umath.count'),
None,
),
'_replace':
Ufunc(4, 1, None,
docstrings.get('numpy.core.umath._replace'),
None,
),
'startswith':
Ufunc(4, 1, False_,
docstrings.get('numpy._core.umath.startswith'),
Expand Down
7 changes: 7 additions & 0 deletions numpy/_core/code_generators/ufunc_docstrings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4558,6 +4558,13 @@ def add_newdoc(place, name, doc):

""")

add_newdoc('numpy.core.umath', '_replace',
"""
UFunc implementation of ``replace``. This internal function
is called by ``replace`` with ``out`` set, so that the
size of the resulting string buffer is known.
""")

add_newdoc('numpy._core.umath', 'startswith',
"""
Returns a boolean array which is `True` where the string element
Expand Down
17 changes: 14 additions & 3 deletions numpy/_core/defchararray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1261,8 +1261,19 @@ def replace(a, old, new, count=None):
>>> np.char.replace(a, 'is', 'was')
array(['The dwash was fresh', 'Thwas was it'], dtype='<U19')
"""
return _to_bytes_or_str_array(
_vec_string(a, object_, 'replace', [old, new] + _clean_args(count)), a)
max_int64 = numpy.iinfo(numpy.int64).max
count = count if count is not None else max_int64

counts = numpy._core.umath.count(a, old, 0, max_int64)
buffersizes = (
numpy._core.umath.str_len(a)
+ counts * (numpy._core.umath.str_len(new) -
numpy._core.umath.str_len(old))
)
max_buffersize = numpy.max(buffersizes)
out = numpy.empty(a.shape, dtype=f"{a.dtype.char}{max_buffersize}")
numpy._core.umath._replace(a, old, new, count, out=out)
return out


@array_function_dispatch(_count_dispatcher)
Expand Down Expand Up @@ -2506,7 +2517,7 @@ def replace(self, old, new, count=None):
char.replace

"""
return asarray(replace(self, old, new, count))
return replace(self, old, new, count)

def rfind(self, sub, start=0, end=None):
"""
Expand Down
57 changes: 57 additions & 0 deletions numpy/_core/src/umath/string_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,63 @@ struct Buffer {
}
return true;
}

inline Buffer<enc>
rstrip()
{
Buffer<enc> tmp(after, 0);
tmp--;
while (tmp >= *this && (*tmp == '\0' || NumPyOS_ascii_isspace(*tmp))) {
tmp--;
}
tmp++;

after = tmp.buf;
return *this;
}

inline int
strcmp(Buffer<enc> other, bool rstrip)
{
Buffer tmp1 = rstrip ? this->rstrip() : *this;
Buffer tmp2 = rstrip ? other.rstrip() : other;

while (tmp1.buf < tmp1.after && tmp2.buf < tmp2.after) {
if (*tmp1 < *tmp2) {
return -1;
}
if (*tmp1 > *tmp2) {
return 1;
}
tmp1++;
tmp2++;
}
while (tmp1.buf < tmp1.after) {
if (*tmp1 < 0) {
return -1;
}
if (*tmp1 > 0) {
return 1;
}
tmp1++;
}
while (tmp2.buf < tmp2.after) {
if (*tmp2 < 0) {
return -1;
}
if (*tmp2 > 0) {
return 1;
}
tmp2++;
}
return 0;
}

inline int
strcmp(Buffer<enc> other)
{
return strcmp(other, false);
}
};


Expand Down
Loading
0