[Python-checkins] python/dist/src/Objects stringobject.c,2.183,2.184 unicodeobject.c,2.164,2.165

gvanrossum@users.sourceforge.net gvanrossum@users.sourceforge.net
Fri, 23 Aug 2002 11:21:30 -0700


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

Modified Files:
	stringobject.c unicodeobject.c 
Log Message:
Code by Inyeol Lee, submitted to SF bug 595350, to implement
the string/unicode method .replace() with a zero-lengt first argument.
Inyeol contributed tests for this too.


Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.183
retrieving revision 2.184
diff -C2 -d -r2.183 -r2.184
*** stringobject.c	20 Aug 2002 17:29:29 -0000	2.183
--- stringobject.c	23 Aug 2002 18:21:28 -0000	2.184
***************
*** 2216,2224 ****
  	int nfound, offset, new_len;
  
! 	if (len == 0 || pat_len > len)
  		goto return_same;
  
  	/* find length of output string */
! 	nfound = mymemcnt(str, len, pat, pat_len);
  	if (count < 0)
  		count = INT_MAX;
--- 2216,2224 ----
  	int nfound, offset, new_len;
  
! 	if (len == 0 || (pat_len == 0 && sub_len == 0) || pat_len > len)
  		goto return_same;
  
  	/* find length of output string */
! 	nfound = (pat_len > 0) ? mymemcnt(str, len, pat, pat_len) : len + 1;
  	if (count < 0)
  		count = INT_MAX;
***************
*** 2243,2265 ****
  		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;
--- 2243,2278 ----
  		out_s = new_s;
  
! 		if (pat_len > 0) {
! 			for (; nfound > 0; --nfound) {
! 				/* 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);
! 		}
! 		else {
! 			for (;;++str, --len) {
! 				memcpy(new_s, sub, sub_len);
! 				new_s += sub_len;
! 				if (--nfound <= 0) {
! 					memcpy(new_s, str, len);
! 					break;
! 				}
! 				*new_s++ = *str;
! 			}
  		}
  	}
  	*out_len = new_len;
***************
*** 2318,2325 ****
  		return NULL;
  
- 	if (sub_len <= 0) {
- 		PyErr_SetString(PyExc_ValueError, "empty pattern string");
- 		return NULL;
- 	}
  	new_s = mymemreplace(str,len,sub,sub_len,repl,repl_len,count,&out_len);
  	if (new_s == NULL) {
--- 2331,2334 ----

Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.164
retrieving revision 2.165
diff -C2 -d -r2.164 -r2.165
*** unicodeobject.c	20 Aug 2002 17:29:29 -0000	2.164
--- unicodeobject.c	23 Aug 2002 18:21:28 -0000	2.165
***************
*** 3495,3503 ****
      PyUnicodeObject *u;
  
-     if (str1->length == 0) {
- 	PyErr_SetString(PyExc_ValueError, "empty pattern string");
- 	return NULL;
-     }
- 
      if (maxcount < 0)
  	maxcount = INT_MAX;
--- 3495,3498 ----
***************
*** 3550,3566 ****
                  i = 0;
                  p = u->str;
!                 while (i <= self->length - str1->length)
!                     if (Py_UNICODE_MATCH(self, i, str1)) {
!                         /* replace string segment */
                          Py_UNICODE_COPY(p, str2->str, str2->length);
                          p += str2->length;
!                         i += str1->length;
!                         if (--n <= 0) {
!                             /* copy remaining part */
!                             Py_UNICODE_COPY(p, self->str+i, self->length-i);
                              break;
-                         }
-                     } else
                          *p++ = self->str[i++];
              }
          }
--- 3545,3572 ----
                  i = 0;
                  p = u->str;
!                 if (str1->length > 0) {
!                     while (i <= self->length - str1->length)
!                         if (Py_UNICODE_MATCH(self, i, str1)) {
!                             /* replace string segment */
!                             Py_UNICODE_COPY(p, str2->str, str2->length);
!                             p += str2->length;
!                             i += str1->length;
!                             if (--n <= 0) {
!                                 /* copy remaining part */
!                                 Py_UNICODE_COPY(p, self->str+i, self->length-i);
!                                 break;
!                             }
!                         } else
!                             *p++ = self->str[i++];
!                 } else {
!                     while (n > 0) {
                          Py_UNICODE_COPY(p, str2->str, str2->length);
                          p += str2->length;
!                         if (--n <= 0)
                              break;
                          *p++ = self->str[i++];
+                     }
+                     Py_UNICODE_COPY(p, self->str+i, self->length-i);
+                 }
              }
          }