[Python-checkins] CVS: python/dist/src/Modules stropmodule.c,2.75.6.1,2.75.6.2
Thomas Wouters
twouters@users.sourceforge.net
Wed, 23 May 2001 07:38:55 -0700
Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv19878/Modules
Modified Files:
Tag: release21-maint
stropmodule.c
Log Message:
Net result of Tim's checkins to stropmodule.c (2.78, 2.79, 2.80, 2.81),
stringobject.c (2.114, 2.115) and test_strop.py (1.11, 1.12). Fixes
'replace' behaviour on systems on which 'malloc(0)' returns NULL (together
with previous checkins) and re-synchs the string-operation code in
stringobject.c and stropmodule.c, with the exception of 'replace', which has
the old semantics in stropmodule but the new semantics in stringobjects.
Index: stropmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/stropmodule.c,v
retrieving revision 2.75.6.1
retrieving revision 2.75.6.2
diff -C2 -r2.75.6.1 -r2.75.6.2
*** stropmodule.c 2001/05/23 13:36:37 2.75.6.1
--- stropmodule.c 2001/05/23 14:38:53 2.75.6.2
***************
*** 983,987 ****
MEM, the function returns -1.
*/
! static int mymemfind(char *mem, int len, char *pat, int pat_len)
{
register int ii;
--- 983,988 ----
MEM, the function returns -1.
*/
! static int
! mymemfind(const char *mem, int len, const char *pat, int pat_len)
{
register int ii;
***************
*** 1007,1011 ****
mem=11111 and pat==11 also return 2.
*/
! static int mymemcnt(char *mem, int len, char *pat, int pat_len)
{
register int offset = 0;
--- 1008,1013 ----
mem=11111 and pat==11 also return 2.
*/
! static int
! mymemcnt(const char *mem, int len, const char *pat, int pat_len)
{
register int offset = 0;
***************
*** 1042,1046 ****
NULL if an error occurred.
*/
! static char *mymemreplace(char *str, int len, char *pat, int pat_len, char *sub, int sub_len, int count, int *out_len)
{
char *out_s;
--- 1044,1053 ----
NULL if an error occurred.
*/
! static char *
! mymemreplace(const char *str, int len, /* input string */
! const char *pat, int pat_len, /* pattern string to find */
! const char *sub, int sub_len, /* substitution string */
! int count, /* number of replacements */
! int *out_len)
{
char *out_s;
***************
*** 1053,1058 ****
/* find length of output string */
nfound = mymemcnt(str, len, pat, pat_len);
! if (count > 0)
! nfound = nfound > count ? count : nfound;
if (nfound == 0)
goto return_same;
--- 1060,1067 ----
/* find length of output string */
nfound = mymemcnt(str, len, pat, pat_len);
! if (count < 0)
! count = INT_MAX;
! else if (nfound > count)
! nfound = count;
if (nfound == 0)
goto return_same;
***************
*** 1060,1064 ****
new_len = len + nfound*(sub_len - pat_len);
if (new_len == 0) {
! out_s = "";
}
else {
--- 1069,1077 ----
new_len = len + nfound*(sub_len - pat_len);
if (new_len == 0) {
! /* Have to allocate something for the caller to free(). */
! out_s = (char *)PyMem_MALLOC(1);
! if (out_s == NULL)
! return NULL;
! out_s[0] = '\0';
}
else {
***************
*** 1069,1073 ****
out_s = new_s;
! while (len > 0) {
/* find index of next instance of pattern */
offset = mymemfind(str, len, pat, pat_len);
--- 1082,1086 ----
out_s = new_s;
! for (; count > 0 && len > 0; --count) {
/* find index of next instance of pattern */
offset = mymemfind(str, len, pat, pat_len);
***************
*** 1084,1091 ****
memcpy(new_s, sub, sub_len);
new_s += sub_len;
-
- /* note count==0 is effectively infinity */
- if (--count == 0)
- break;
}
/* copy any remaining values into output string */
--- 1097,1100 ----
***************
*** 1098,1102 ****
return_same:
*out_len = -1;
! return str;
}
--- 1107,1111 ----
return_same:
*out_len = -1;
! return (char *)str; /* cast away const */
}
***************
*** 1114,1118 ****
char *str, *pat,*sub,*new_s;
int len,pat_len,sub_len,out_len;
! int count = 0;
PyObject *new;
--- 1123,1127 ----
char *str, *pat,*sub,*new_s;
int len,pat_len,sub_len,out_len;
! int count = -1;
PyObject *new;
***************
*** 1125,1128 ****
--- 1134,1143 ----
return NULL;
}
+ /* CAUTION: strop treats a replace count of 0 as infinity, unlke
+ * current (2.1) string.py and string methods. Preserve this for
+ * ... well, hard to say for what <wink>.
+ */
+ if (count == 0)
+ count = -1;
new_s = mymemreplace(str,len,pat,pat_len,sub,sub_len,count,&out_len);
if (new_s == NULL) {