summaryrefslogtreecommitdiffstats
path: root/Objects/stringlib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-10-30 10:03:53 (GMT)
committerGitHub <noreply@github.com>2019-10-30 10:03:53 (GMT)
commit865c3b257fe38154a4320c7ee6afb416f665b9c2 (patch)
treeab97967500a250cac15ac0d69aeb2c061bf313ba /Objects/stringlib
parent25fc088607c855060ed142296dc1bd0125fad1af (diff)
downloadcpython-865c3b257fe38154a4320c7ee6afb416f665b9c2.zip
cpython-865c3b257fe38154a4320c7ee6afb416f665b9c2.tar.gz
cpython-865c3b257fe38154a4320c7ee6afb416f665b9c2.tar.bz2
bpo-28029: Make "".replace("", s, n) returning s for any n != 0. (GH-16981)
Diffstat (limited to 'Objects/stringlib')
-rw-r--r--Objects/stringlib/transmogrify.h13
1 files changed, 5 insertions, 8 deletions
diff --git a/Objects/stringlib/transmogrify.h b/Objects/stringlib/transmogrify.h
index 9506019..e1165ea 100644
--- a/Objects/stringlib/transmogrify.h
+++ b/Objects/stringlib/transmogrify.h
@@ -680,9 +680,13 @@ stringlib_replace(PyObject *self,
const char *to_s, Py_ssize_t to_len,
Py_ssize_t maxcount)
{
+ if (STRINGLIB_LEN(self) < from_len) {
+ /* nothing to do; return the original bytes */
+ return return_self(self);
+ }
if (maxcount < 0) {
maxcount = PY_SSIZE_T_MAX;
- } else if (maxcount == 0 || STRINGLIB_LEN(self) == 0) {
+ } else if (maxcount == 0) {
/* nothing to do; return the original bytes */
return return_self(self);
}
@@ -699,13 +703,6 @@ stringlib_replace(PyObject *self,
return stringlib_replace_interleave(self, to_s, to_len, maxcount);
}
- /* Except for b"".replace(b"", b"A") == b"A" there is no way beyond this */
- /* point for an empty self bytes to generate a non-empty bytes */
- /* Special case so the remaining code always gets a non-empty bytes */
- if (STRINGLIB_LEN(self) == 0) {
- return return_self(self);
- }
-
if (to_len == 0) {
/* delete all occurrences of 'from' bytes */
if (from_len == 1) {