[Python-checkins] CVS: python/dist/src/Objects stringobject.c,2.102.2.3,2.102.2.4

Thomas Wouters twouters@users.sourceforge.net
Wed, 23 May 2001 07:38:55 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv19878/Objects

Modified Files:
      Tag: release21-maint
	stringobject.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: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.102.2.3
retrieving revision 2.102.2.4
diff -C2 -r2.102.2.3 -r2.102.2.4
*** stringobject.c	2001/05/23 13:14:24	2.102.2.3
--- stringobject.c	2001/05/23 14:38:53	2.102.2.4
***************
*** 1532,1536 ****
               const char *sub, int sub_len,	/* substitution string */
               int count,				/* number of replacements */
!              int *out_len)
  {
  	char *out_s;
--- 1532,1536 ----
               const char *sub, int sub_len,	/* substitution string */
               int count,				/* number of replacements */
! 	     int *out_len)
  {
  	char *out_s;
***************
*** 1549,1587 ****
  	if (nfound == 0)
  		goto return_same;
- 	new_len = len + nfound*(sub_len - pat_len);
- 
- 	new_s = (char *)PyMem_MALLOC(new_len);
- 	if (new_s == NULL) return NULL;
- 
- 	*out_len = new_len;
- 	out_s = new_s;
  
! 	while (len > 0) {
! 		/* find index of next instance of pattern */
! 		offset = mymemfind(str, len, pat, pat_len);
! 		/* if not found,  break out of loop */
! 		if (offset == -1) break;
! 
! 		/* copy non matching part of input string */
! 		memcpy(new_s, str, offset); /* copy part of str before pat */
! 		str += offset + pat_len; /* move str past pattern */
! 		len -= offset + pat_len; /* reduce length of str remaining */
! 
! 		/* copy substitute into the output string */
! 		new_s += offset; /* move new_s to dest for sub string */
! 		memcpy(new_s, sub, sub_len); /* copy substring into new_s */
! 		new_s += sub_len; /* offset new_s past sub string */
! 
! 		/* break when we've done count replacements */
! 		if (--count == 0) break;
  	}
! 	/* copy any remaining values into output string */
! 	if (len > 0)
! 		memcpy(new_s, str, len);
  	return out_s;
  
    return_same:
  	*out_len = -1;
! 	return (char*)str;	/* have to cast away constness here */
  }
  
--- 1549,1594 ----
  	if (nfound == 0)
  		goto return_same;
  
! 	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 {
! 		assert(new_len > 0);
! 		new_s = (char *)PyMem_MALLOC(new_len);
! 		if (new_s == NULL)
! 			return NULL;
! 		out_s = new_s;
! 
! 		for (; count > 0 && len > 0; --count) {
! 			/* find index of next instance of pattern */
! 			offset = mymemfind(str, len, pat, pat_len);
! 			if (offset == -1)
! 				break;
! 
! 			/* copy non matching part of input string */
! 			memcpy(new_s, str, offset);
! 			str += offset + pat_len;
! 			len -= offset + pat_len;
! 
! 			/* copy substitute into the output string */
! 			new_s += offset;
! 			memcpy(new_s, sub, sub_len);
! 			new_s += sub_len;
! 		}
! 		/* copy any remaining values into output string */
! 		if (len > 0)
! 			memcpy(new_s, str, len);
! 	}
! 	*out_len = new_len;
  	return out_s;
  
    return_same:
  	*out_len = -1;
! 	return (char *)str; /* cast away const */
  }