Mercurial > cpython-lru_cache-weakref
comparison Objects/stringobject.c @ 17953:a429da7db706 2.1
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.
| author | Thomas Wouters <thomas@python.org> |
|---|---|
| date | Wed, 23 May 2001 14:38:53 +0000 |
| parents | 5d894a5fce38 |
| children | ba6721ae4b80 |
comparison
equal
deleted
inserted
replaced
| 17952:9dfd22e8e9b7 | 17953:a429da7db706 |
|---|---|
| 1529 static char * | 1529 static char * |
| 1530 mymemreplace(const char *str, int len, /* input string */ | 1530 mymemreplace(const char *str, int len, /* input string */ |
| 1531 const char *pat, int pat_len, /* pattern string to find */ | 1531 const char *pat, int pat_len, /* pattern string to find */ |
| 1532 const char *sub, int sub_len, /* substitution string */ | 1532 const char *sub, int sub_len, /* substitution string */ |
| 1533 int count, /* number of replacements */ | 1533 int count, /* number of replacements */ |
| 1534 int *out_len) | 1534 int *out_len) |
| 1535 { | 1535 { |
| 1536 char *out_s; | 1536 char *out_s; |
| 1537 char *new_s; | 1537 char *new_s; |
| 1538 int nfound, offset, new_len; | 1538 int nfound, offset, new_len; |
| 1539 | 1539 |
| 1546 count = INT_MAX; | 1546 count = INT_MAX; |
| 1547 else if (nfound > count) | 1547 else if (nfound > count) |
| 1548 nfound = count; | 1548 nfound = count; |
| 1549 if (nfound == 0) | 1549 if (nfound == 0) |
| 1550 goto return_same; | 1550 goto return_same; |
| 1551 | |
| 1551 new_len = len + nfound*(sub_len - pat_len); | 1552 new_len = len + nfound*(sub_len - pat_len); |
| 1552 | 1553 if (new_len == 0) { |
| 1553 new_s = (char *)PyMem_MALLOC(new_len); | 1554 /* Have to allocate something for the caller to free(). */ |
| 1554 if (new_s == NULL) return NULL; | 1555 out_s = (char *)PyMem_MALLOC(1); |
| 1555 | 1556 if (out_s == NULL) |
| 1557 return NULL; | |
| 1558 out_s[0] = '\0'; | |
| 1559 } | |
| 1560 else { | |
| 1561 assert(new_len > 0); | |
| 1562 new_s = (char *)PyMem_MALLOC(new_len); | |
| 1563 if (new_s == NULL) | |
| 1564 return NULL; | |
| 1565 out_s = new_s; | |
| 1566 | |
| 1567 for (; count > 0 && len > 0; --count) { | |
| 1568 /* find index of next instance of pattern */ | |
| 1569 offset = mymemfind(str, len, pat, pat_len); | |
| 1570 if (offset == -1) | |
| 1571 break; | |
| 1572 | |
| 1573 /* copy non matching part of input string */ | |
| 1574 memcpy(new_s, str, offset); | |
| 1575 str += offset + pat_len; | |
| 1576 len -= offset + pat_len; | |
| 1577 | |
| 1578 /* copy substitute into the output string */ | |
| 1579 new_s += offset; | |
| 1580 memcpy(new_s, sub, sub_len); | |
| 1581 new_s += sub_len; | |
| 1582 } | |
| 1583 /* copy any remaining values into output string */ | |
| 1584 if (len > 0) | |
| 1585 memcpy(new_s, str, len); | |
| 1586 } | |
| 1556 *out_len = new_len; | 1587 *out_len = new_len; |
| 1557 out_s = new_s; | |
| 1558 | |
| 1559 while (len > 0) { | |
| 1560 /* find index of next instance of pattern */ | |
| 1561 offset = mymemfind(str, len, pat, pat_len); | |
| 1562 /* if not found, break out of loop */ | |
| 1563 if (offset == -1) break; | |
| 1564 | |
| 1565 /* copy non matching part of input string */ | |
| 1566 memcpy(new_s, str, offset); /* copy part of str before pat */ | |
| 1567 str += offset + pat_len; /* move str past pattern */ | |
| 1568 len -= offset + pat_len; /* reduce length of str remaining */ | |
| 1569 | |
| 1570 /* copy substitute into the output string */ | |
| 1571 new_s += offset; /* move new_s to dest for sub string */ | |
| 1572 memcpy(new_s, sub, sub_len); /* copy substring into new_s */ | |
| 1573 new_s += sub_len; /* offset new_s past sub string */ | |
| 1574 | |
| 1575 /* break when we've done count replacements */ | |
| 1576 if (--count == 0) break; | |
| 1577 } | |
| 1578 /* copy any remaining values into output string */ | |
| 1579 if (len > 0) | |
| 1580 memcpy(new_s, str, len); | |
| 1581 return out_s; | 1588 return out_s; |
| 1582 | 1589 |
| 1583 return_same: | 1590 return_same: |
| 1584 *out_len = -1; | 1591 *out_len = -1; |
| 1585 return (char*)str; /* have to cast away constness here */ | 1592 return (char *)str; /* cast away const */ |
| 1586 } | 1593 } |
| 1587 | 1594 |
| 1588 | 1595 |
| 1589 static char replace__doc__[] = | 1596 static char replace__doc__[] = |
| 1590 "S.replace (old, new[, maxsplit]) -> string\n\ | 1597 "S.replace (old, new[, maxsplit]) -> string\n\ |
