Commit 9c012af3 authored by Tim Peters's avatar Tim Peters

Heh. I need a break. After this: stropmodule & stringobject were more

out of synch than I realized, and I managed to break replace's "count"
argument when it was 0.  All is well again.  Maybe.
Bugfix candidate.
parent 4cd44ef4
...@@ -1058,8 +1058,10 @@ mymemreplace(const char *str, int len, /* input string */ ...@@ -1058,8 +1058,10 @@ mymemreplace(const char *str, int len, /* input string */
/* find length of output string */ /* find length of output string */
nfound = mymemcnt(str, len, pat, pat_len); nfound = mymemcnt(str, len, pat, pat_len);
if (count > 0) if (count < 0)
nfound = nfound > count ? count : nfound; count = INT_MAX;
else if (nfound > count)
nfound = count;
if (nfound == 0) if (nfound == 0)
goto return_same; goto return_same;
...@@ -1067,7 +1069,7 @@ mymemreplace(const char *str, int len, /* input string */ ...@@ -1067,7 +1069,7 @@ mymemreplace(const char *str, int len, /* input string */
if (new_len == 0) { if (new_len == 0) {
/* Have to allocate something for the caller to free(). */ /* Have to allocate something for the caller to free(). */
out_s = (char *)PyMem_MALLOC(1); out_s = (char *)PyMem_MALLOC(1);
if (out_s = NULL) if (out_s == NULL)
return NULL; return NULL;
out_s[0] = '\0'; out_s[0] = '\0';
} }
...@@ -1078,7 +1080,7 @@ mymemreplace(const char *str, int len, /* input string */ ...@@ -1078,7 +1080,7 @@ mymemreplace(const char *str, int len, /* input string */
return NULL; return NULL;
out_s = new_s; out_s = new_s;
while (len > 0) { for (; count > 0 && len > 0; --count) {
/* find index of next instance of pattern */ /* find index of next instance of pattern */
offset = mymemfind(str, len, pat, pat_len); offset = mymemfind(str, len, pat, pat_len);
if (offset == -1) if (offset == -1)
...@@ -1093,10 +1095,6 @@ mymemreplace(const char *str, int len, /* input string */ ...@@ -1093,10 +1095,6 @@ mymemreplace(const char *str, int len, /* input string */
new_s += offset; new_s += offset;
memcpy(new_s, sub, sub_len); memcpy(new_s, sub, sub_len);
new_s += sub_len; new_s += sub_len;
/* note count==0 is effectively infinity */
if (--count == 0)
break;
} }
/* copy any remaining values into output string */ /* copy any remaining values into output string */
if (len > 0) if (len > 0)
......
...@@ -1557,8 +1557,10 @@ mymemreplace(const char *str, int len, /* input string */ ...@@ -1557,8 +1557,10 @@ mymemreplace(const char *str, int len, /* input string */
/* find length of output string */ /* find length of output string */
nfound = mymemcnt(str, len, pat, pat_len); nfound = mymemcnt(str, len, pat, pat_len);
if (count > 0) if (count < 0)
nfound = nfound > count ? count : nfound; count = INT_MAX;
else if (nfound > count)
nfound = count;
if (nfound == 0) if (nfound == 0)
goto return_same; goto return_same;
...@@ -1566,7 +1568,7 @@ mymemreplace(const char *str, int len, /* input string */ ...@@ -1566,7 +1568,7 @@ mymemreplace(const char *str, int len, /* input string */
if (new_len == 0) { if (new_len == 0) {
/* Have to allocate something for the caller to free(). */ /* Have to allocate something for the caller to free(). */
out_s = (char *)PyMem_MALLOC(1); out_s = (char *)PyMem_MALLOC(1);
if (out_s = NULL) if (out_s == NULL)
return NULL; return NULL;
out_s[0] = '\0'; out_s[0] = '\0';
} }
...@@ -1577,7 +1579,7 @@ mymemreplace(const char *str, int len, /* input string */ ...@@ -1577,7 +1579,7 @@ mymemreplace(const char *str, int len, /* input string */
return NULL; return NULL;
out_s = new_s; out_s = new_s;
while (len > 0) { for (; count > 0 && len > 0; --count) {
/* find index of next instance of pattern */ /* find index of next instance of pattern */
offset = mymemfind(str, len, pat, pat_len); offset = mymemfind(str, len, pat, pat_len);
if (offset == -1) if (offset == -1)
...@@ -1592,10 +1594,6 @@ mymemreplace(const char *str, int len, /* input string */ ...@@ -1592,10 +1594,6 @@ mymemreplace(const char *str, int len, /* input string */
new_s += offset; new_s += offset;
memcpy(new_s, sub, sub_len); memcpy(new_s, sub, sub_len);
new_s += sub_len; new_s += sub_len;
/* note count==0 is effectively infinity */
if (--count == 0)
break;
} }
/* copy any remaining values into output string */ /* copy any remaining values into output string */
if (len > 0) if (len > 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment