Skip to content
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

Added missing replace functions to C++ std::string definition #6037

Merged
merged 2 commits into from Feb 28, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cython/Includes/libcpp/string.pxd
Expand Up @@ -196,6 +196,16 @@ cdef extern from "<string>" namespace "std" nogil:
void insert(iterator p, size_t n, char c) except +
iterator insert(iterator p, char c) except +

string& replace(size_t pos, size_t len, const string& str) except +
string& replace(iterator i1, iterator i2, const string& str) except +
string& replace(size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) except +
string& replace(size_t pos, size_t len, const char* s) except +
string& replace(iterator i1, iterator i2, const char* s) except +
string& replace(size_t pos, size_t len, const char* s, size_t n) except +
string& replace(iterator i1, iterator i2, const char* s, size_t n) except +
string& replace(size_t pos, size_t len, size_t n, char c) except +
string& replace(iterator i1, iterator i2, size_t n, char c) except +

size_t copy(char* s, size_t len, size_t pos) except +
size_t copy(char* s, size_t len) except +

Expand Down
13 changes: 13 additions & 0 deletions tests/run/cpp_stl_string.pyx
Expand Up @@ -196,6 +196,19 @@ def test_substr(char *a):
z = s.substr()
return x.c_str(), y.c_str(), z.c_str()

def test_replace(char *a, char *b, char* fill):
"""
>>> test_replace(b_asdf, b_asdg, b_s) == (b_asdg+b_asdf[2:], b_asdf[:-2]+b_asdg[-2:], (b_s*4)+b_asdg[2:])
True
"""
cdef string s1 = string(a)
cdef string s2 = string(a)
cdef string s3 = string(b)
s1.replace(0, 2, s3)
s2.replace(s2.size()-2, 2, s3, s3.size()-2, 2)
s3.replace(0, 2, 4, fill[0])
return s1.c_str(), s2.c_str(), s3.c_str()

def test_append(char *a, char *b):
"""
>>> test_append(b_asdf, '1234'.encode('ASCII')) == b_asdf + '1234'.encode('ASCII')
Expand Down