Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* String object implementation */ |
| 2 | |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 3 | #define PY_SSIZE_T_CLEAN |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 7 | #include <ctype.h> |
| 8 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 9 | #ifdef COUNT_ALLOCS |
| 10 | int null_strings, one_strings; |
| 11 | #endif |
| 12 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 13 | static PyStringObject *characters[UCHAR_MAX + 1]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 14 | static PyStringObject *nullstring; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 16 | /* This dictionary holds all interned strings. Note that references to |
| 17 | strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 18 | When the interned string reaches a refcnt of 0 the string deallocation |
| 19 | function will delete the reference from this dictionary. |
| 20 | |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 21 | Another way to look at this is that to say that the actual reference |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 22 | count of a string is: s->ob_refcnt + (s->ob_sstate?2:0) |
| 23 | */ |
| 24 | static PyObject *interned; |
| 25 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 26 | /* |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 27 | For both PyString_FromString() and PyString_FromStringAndSize(), the |
| 28 | parameter `size' denotes number of characters to allocate, not counting any |
Martin v. Löwis | 1f803f7 | 2002-01-16 10:53:24 +0000 | [diff] [blame] | 29 | null terminating character. |
Martin v. Löwis | d132750 | 2001-12-02 18:09:41 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 31 | For PyString_FromString(), the parameter `str' points to a null-terminated |
Martin v. Löwis | 1f803f7 | 2002-01-16 10:53:24 +0000 | [diff] [blame] | 32 | string containing exactly `size' bytes. |
Martin v. Löwis | d132750 | 2001-12-02 18:09:41 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 34 | For PyString_FromStringAndSize(), the parameter the parameter `str' is |
| 35 | either NULL or else points to a string containing at least `size' bytes. |
| 36 | For PyString_FromStringAndSize(), the string in the `str' parameter does |
| 37 | not have to be null-terminated. (Therefore it is safe to construct a |
| 38 | substring by calling `PyString_FromStringAndSize(origstring, substrlen)'.) |
| 39 | If `str' is NULL then PyString_FromStringAndSize() will allocate `size+1' |
| 40 | bytes (setting the last byte to the null terminating character) and you can |
| 41 | fill in the data yourself. If `str' is non-NULL then the resulting |
| 42 | PyString object must be treated as immutable and you must not fill in nor |
| 43 | alter the data yourself, since the strings may be shared. |
Martin v. Löwis | 8f1ea71 | 2001-12-03 08:24:52 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 45 | The PyObject member `op->ob_size', which denotes the number of "extra |
| 46 | items" in a variable-size object, will contain the number of bytes |
| 47 | allocated for string data, not counting the null terminating character. It |
| 48 | is therefore equal to the equal to the `size' parameter (for |
| 49 | PyString_FromStringAndSize()) or the length of the string in the `str' |
| 50 | parameter (for PyString_FromString()). |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 51 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 52 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 53 | PyString_FromStringAndSize(const char *str, Py_ssize_t size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 55 | register PyStringObject *op; |
Michael W. Hudson | faa7648 | 2005-01-31 17:09:25 +0000 | [diff] [blame] | 56 | assert(size >= 0); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 57 | if (size == 0 && (op = nullstring) != NULL) { |
| 58 | #ifdef COUNT_ALLOCS |
| 59 | null_strings++; |
| 60 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 61 | Py_INCREF(op); |
| 62 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 63 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 64 | if (size == 1 && str != NULL && |
| 65 | (op = characters[*str & UCHAR_MAX]) != NULL) |
| 66 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 67 | #ifdef COUNT_ALLOCS |
| 68 | one_strings++; |
| 69 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 70 | Py_INCREF(op); |
| 71 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 72 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 74 | /* Inline PyObject_NewVar */ |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 75 | op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 76 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 77 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 78 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 79 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 80 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 81 | if (str != NULL) |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 82 | Py_MEMCPY(op->ob_sval, str, size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 83 | op->ob_sval[size] = '\0'; |
Tim Peters | 8deda70 | 2002-03-30 10:06:07 +0000 | [diff] [blame] | 84 | /* share short strings */ |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 85 | if (size == 0) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 86 | PyObject *t = (PyObject *)op; |
| 87 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 88 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 89 | nullstring = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 90 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 91 | } else if (size == 1 && str != NULL) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 92 | PyObject *t = (PyObject *)op; |
| 93 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 94 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 95 | characters[*str & UCHAR_MAX] = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 96 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 97 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 98 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 101 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 102 | PyString_FromString(const char *str) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | { |
Tim Peters | 62de65b | 2001-12-06 20:29:32 +0000 | [diff] [blame] | 104 | register size_t size; |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 105 | register PyStringObject *op; |
Tim Peters | 62de65b | 2001-12-06 20:29:32 +0000 | [diff] [blame] | 106 | |
| 107 | assert(str != NULL); |
| 108 | size = strlen(str); |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 109 | if (size > PY_SSIZE_T_MAX) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 110 | PyErr_SetString(PyExc_OverflowError, |
| 111 | "string is too long for a Python string"); |
| 112 | return NULL; |
| 113 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 114 | if (size == 0 && (op = nullstring) != NULL) { |
| 115 | #ifdef COUNT_ALLOCS |
| 116 | null_strings++; |
| 117 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 118 | Py_INCREF(op); |
| 119 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 120 | } |
| 121 | if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
| 122 | #ifdef COUNT_ALLOCS |
| 123 | one_strings++; |
| 124 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 125 | Py_INCREF(op); |
| 126 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 127 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 129 | /* Inline PyObject_NewVar */ |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 130 | op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 131 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 132 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 133 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 134 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 135 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 136 | Py_MEMCPY(op->ob_sval, str, size+1); |
Tim Peters | 8deda70 | 2002-03-30 10:06:07 +0000 | [diff] [blame] | 137 | /* share short strings */ |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 138 | if (size == 0) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 139 | PyObject *t = (PyObject *)op; |
| 140 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 141 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 142 | nullstring = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 143 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 144 | } else if (size == 1) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 145 | PyObject *t = (PyObject *)op; |
| 146 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 147 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 148 | characters[*str & UCHAR_MAX] = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 149 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 150 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 151 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 154 | PyObject * |
| 155 | PyString_FromFormatV(const char *format, va_list vargs) |
| 156 | { |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 157 | va_list count; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 158 | Py_ssize_t n = 0; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 159 | const char* f; |
| 160 | char *s; |
| 161 | PyObject* string; |
| 162 | |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 163 | #ifdef VA_LIST_IS_ARRAY |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 164 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 165 | #else |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 166 | #ifdef __va_copy |
| 167 | __va_copy(count, vargs); |
| 168 | #else |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 169 | count = vargs; |
| 170 | #endif |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 171 | #endif |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 172 | /* step 1: figure out how large a buffer we need */ |
| 173 | for (f = format; *f; f++) { |
| 174 | if (*f == '%') { |
| 175 | const char* p = f; |
| 176 | while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
| 177 | ; |
| 178 | |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 179 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 180 | * they don't affect the amount of space we reserve. |
| 181 | */ |
| 182 | if ((*f == 'l' || *f == 'z') && |
| 183 | (f[1] == 'd' || f[1] == 'u')) |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 184 | ++f; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 185 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 186 | switch (*f) { |
| 187 | case 'c': |
| 188 | (void)va_arg(count, int); |
| 189 | /* fall through... */ |
| 190 | case '%': |
| 191 | n++; |
| 192 | break; |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 193 | case 'd': case 'u': case 'i': case 'x': |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 194 | (void) va_arg(count, int); |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 195 | /* 20 bytes is enough to hold a 64-bit |
| 196 | integer. Decimal takes the most space. |
| 197 | This isn't enough for octal. */ |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 198 | n += 20; |
| 199 | break; |
| 200 | case 's': |
| 201 | s = va_arg(count, char*); |
| 202 | n += strlen(s); |
| 203 | break; |
| 204 | case 'p': |
| 205 | (void) va_arg(count, int); |
| 206 | /* maximum 64-bit pointer representation: |
| 207 | * 0xffffffffffffffff |
| 208 | * so 19 characters is enough. |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 209 | * XXX I count 18 -- what's the extra for? |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 210 | */ |
| 211 | n += 19; |
| 212 | break; |
| 213 | default: |
| 214 | /* if we stumble upon an unknown |
| 215 | formatting code, copy the rest of |
| 216 | the format string to the output |
| 217 | string. (we cannot just skip the |
| 218 | code, since there's no way to know |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 219 | what's in the argument list) */ |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 220 | n += strlen(p); |
| 221 | goto expand; |
| 222 | } |
| 223 | } else |
| 224 | n++; |
| 225 | } |
| 226 | expand: |
| 227 | /* step 2: fill the buffer */ |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 228 | /* Since we've analyzed how much space we need for the worst case, |
| 229 | use sprintf directly instead of the slower PyOS_snprintf. */ |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 230 | string = PyString_FromStringAndSize(NULL, n); |
| 231 | if (!string) |
| 232 | return NULL; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 233 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 234 | s = PyString_AsString(string); |
| 235 | |
| 236 | for (f = format; *f; f++) { |
| 237 | if (*f == '%') { |
| 238 | const char* p = f++; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 239 | Py_ssize_t i; |
| 240 | int longflag = 0; |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 241 | int size_tflag = 0; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 242 | /* parse the width.precision part (we're only |
| 243 | interested in the precision value, if any) */ |
| 244 | n = 0; |
| 245 | while (isdigit(Py_CHARMASK(*f))) |
| 246 | n = (n*10) + *f++ - '0'; |
| 247 | if (*f == '.') { |
| 248 | f++; |
| 249 | n = 0; |
| 250 | while (isdigit(Py_CHARMASK(*f))) |
| 251 | n = (n*10) + *f++ - '0'; |
| 252 | } |
| 253 | while (*f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
| 254 | f++; |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 255 | /* handle the long flag, but only for %ld and %lu. |
| 256 | others can be added when necessary. */ |
| 257 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 258 | longflag = 1; |
| 259 | ++f; |
| 260 | } |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 261 | /* handle the size_t flag. */ |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 262 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 263 | size_tflag = 1; |
| 264 | ++f; |
| 265 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 266 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 267 | switch (*f) { |
| 268 | case 'c': |
| 269 | *s++ = va_arg(vargs, int); |
| 270 | break; |
| 271 | case 'd': |
| 272 | if (longflag) |
| 273 | sprintf(s, "%ld", va_arg(vargs, long)); |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 274 | else if (size_tflag) |
Martin v. Löwis | 822f34a | 2006-05-13 13:34:04 +0000 | [diff] [blame] | 275 | sprintf(s, "%" PY_FORMAT_SIZE_T "d", |
| 276 | va_arg(vargs, Py_ssize_t)); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 277 | else |
| 278 | sprintf(s, "%d", va_arg(vargs, int)); |
| 279 | s += strlen(s); |
| 280 | break; |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 281 | case 'u': |
| 282 | if (longflag) |
| 283 | sprintf(s, "%lu", |
| 284 | va_arg(vargs, unsigned long)); |
| 285 | else if (size_tflag) |
| 286 | sprintf(s, "%" PY_FORMAT_SIZE_T "u", |
| 287 | va_arg(vargs, size_t)); |
| 288 | else |
| 289 | sprintf(s, "%u", |
| 290 | va_arg(vargs, unsigned int)); |
| 291 | s += strlen(s); |
| 292 | break; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 293 | case 'i': |
| 294 | sprintf(s, "%i", va_arg(vargs, int)); |
| 295 | s += strlen(s); |
| 296 | break; |
| 297 | case 'x': |
| 298 | sprintf(s, "%x", va_arg(vargs, int)); |
| 299 | s += strlen(s); |
| 300 | break; |
| 301 | case 's': |
| 302 | p = va_arg(vargs, char*); |
| 303 | i = strlen(p); |
| 304 | if (n > 0 && i > n) |
| 305 | i = n; |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 306 | Py_MEMCPY(s, p, i); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 307 | s += i; |
| 308 | break; |
| 309 | case 'p': |
| 310 | sprintf(s, "%p", va_arg(vargs, void*)); |
Tim Peters | 6af5bbb | 2001-08-25 03:02:28 +0000 | [diff] [blame] | 311 | /* %p is ill-defined: ensure leading 0x. */ |
| 312 | if (s[1] == 'X') |
| 313 | s[1] = 'x'; |
| 314 | else if (s[1] != 'x') { |
| 315 | memmove(s+2, s, strlen(s)+1); |
| 316 | s[0] = '0'; |
| 317 | s[1] = 'x'; |
| 318 | } |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 319 | s += strlen(s); |
| 320 | break; |
| 321 | case '%': |
| 322 | *s++ = '%'; |
| 323 | break; |
| 324 | default: |
| 325 | strcpy(s, p); |
| 326 | s += strlen(s); |
| 327 | goto end; |
| 328 | } |
| 329 | } else |
| 330 | *s++ = *f; |
| 331 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 332 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 333 | end: |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 334 | _PyString_Resize(&string, s - PyString_AS_STRING(string)); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 335 | return string; |
| 336 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 337 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 338 | PyObject * |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 339 | PyString_FromFormat(const char *format, ...) |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 340 | { |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 341 | PyObject* ret; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 342 | va_list vargs; |
| 343 | |
| 344 | #ifdef HAVE_STDARG_PROTOTYPES |
| 345 | va_start(vargs, format); |
| 346 | #else |
| 347 | va_start(vargs); |
| 348 | #endif |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 349 | ret = PyString_FromFormatV(format, vargs); |
| 350 | va_end(vargs); |
| 351 | return ret; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 355 | PyObject *PyString_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 356 | Py_ssize_t size, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 357 | const char *encoding, |
| 358 | const char *errors) |
| 359 | { |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 360 | PyObject *v, *str; |
| 361 | |
| 362 | str = PyString_FromStringAndSize(s, size); |
| 363 | if (str == NULL) |
| 364 | return NULL; |
| 365 | v = PyString_AsDecodedString(str, encoding, errors); |
| 366 | Py_DECREF(str); |
| 367 | return v; |
| 368 | } |
| 369 | |
| 370 | PyObject *PyString_AsDecodedObject(PyObject *str, |
| 371 | const char *encoding, |
| 372 | const char *errors) |
| 373 | { |
| 374 | PyObject *v; |
| 375 | |
| 376 | if (!PyString_Check(str)) { |
| 377 | PyErr_BadArgument(); |
| 378 | goto onError; |
| 379 | } |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 380 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 381 | if (encoding == NULL) { |
| 382 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 383 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 384 | #else |
| 385 | PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
| 386 | goto onError; |
| 387 | #endif |
| 388 | } |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 389 | |
| 390 | /* Decode via the codec registry */ |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 391 | v = PyCodec_Decode(str, encoding, errors); |
| 392 | if (v == NULL) |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 393 | goto onError; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 394 | |
| 395 | return v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 396 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 397 | onError: |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | PyObject *PyString_AsDecodedString(PyObject *str, |
| 402 | const char *encoding, |
| 403 | const char *errors) |
| 404 | { |
| 405 | PyObject *v; |
| 406 | |
| 407 | v = PyString_AsDecodedObject(str, encoding, errors); |
| 408 | if (v == NULL) |
| 409 | goto onError; |
| 410 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 411 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 412 | /* Convert Unicode to a string using the default encoding */ |
| 413 | if (PyUnicode_Check(v)) { |
| 414 | PyObject *temp = v; |
| 415 | v = PyUnicode_AsEncodedString(v, NULL, NULL); |
| 416 | Py_DECREF(temp); |
| 417 | if (v == NULL) |
| 418 | goto onError; |
| 419 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 420 | #endif |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 421 | if (!PyString_Check(v)) { |
| 422 | PyErr_Format(PyExc_TypeError, |
| 423 | "decoder did not return a string object (type=%.400s)", |
| 424 | v->ob_type->tp_name); |
| 425 | Py_DECREF(v); |
| 426 | goto onError; |
| 427 | } |
| 428 | |
| 429 | return v; |
| 430 | |
| 431 | onError: |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 432 | return NULL; |
| 433 | } |
| 434 | |
| 435 | PyObject *PyString_Encode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 436 | Py_ssize_t size, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 437 | const char *encoding, |
| 438 | const char *errors) |
| 439 | { |
| 440 | PyObject *v, *str; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 441 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 442 | str = PyString_FromStringAndSize(s, size); |
| 443 | if (str == NULL) |
| 444 | return NULL; |
| 445 | v = PyString_AsEncodedString(str, encoding, errors); |
| 446 | Py_DECREF(str); |
| 447 | return v; |
| 448 | } |
| 449 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 450 | PyObject *PyString_AsEncodedObject(PyObject *str, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 451 | const char *encoding, |
| 452 | const char *errors) |
| 453 | { |
| 454 | PyObject *v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 455 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 456 | if (!PyString_Check(str)) { |
| 457 | PyErr_BadArgument(); |
| 458 | goto onError; |
| 459 | } |
| 460 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 461 | if (encoding == NULL) { |
| 462 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 463 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 464 | #else |
| 465 | PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
| 466 | goto onError; |
| 467 | #endif |
| 468 | } |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 469 | |
| 470 | /* Encode via the codec registry */ |
| 471 | v = PyCodec_Encode(str, encoding, errors); |
| 472 | if (v == NULL) |
| 473 | goto onError; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 474 | |
| 475 | return v; |
| 476 | |
| 477 | onError: |
| 478 | return NULL; |
| 479 | } |
| 480 | |
| 481 | PyObject *PyString_AsEncodedString(PyObject *str, |
| 482 | const char *encoding, |
| 483 | const char *errors) |
| 484 | { |
| 485 | PyObject *v; |
| 486 | |
Marc-André Lemburg | 8c2133d | 2001-06-12 13:14:10 +0000 | [diff] [blame] | 487 | v = PyString_AsEncodedObject(str, encoding, errors); |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 488 | if (v == NULL) |
| 489 | goto onError; |
| 490 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 491 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 492 | /* Convert Unicode to a string using the default encoding */ |
| 493 | if (PyUnicode_Check(v)) { |
| 494 | PyObject *temp = v; |
| 495 | v = PyUnicode_AsEncodedString(v, NULL, NULL); |
| 496 | Py_DECREF(temp); |
| 497 | if (v == NULL) |
| 498 | goto onError; |
| 499 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 500 | #endif |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 501 | if (!PyString_Check(v)) { |
| 502 | PyErr_Format(PyExc_TypeError, |
| 503 | "encoder did not return a string object (type=%.400s)", |
| 504 | v->ob_type->tp_name); |
| 505 | Py_DECREF(v); |
| 506 | goto onError; |
| 507 | } |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 508 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 509 | return v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 510 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 511 | onError: |
| 512 | return NULL; |
| 513 | } |
| 514 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 515 | static void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 516 | string_dealloc(PyObject *op) |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 517 | { |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 518 | switch (PyString_CHECK_INTERNED(op)) { |
| 519 | case SSTATE_NOT_INTERNED: |
| 520 | break; |
| 521 | |
| 522 | case SSTATE_INTERNED_MORTAL: |
| 523 | /* revive dead object temporarily for DelItem */ |
| 524 | op->ob_refcnt = 3; |
| 525 | if (PyDict_DelItem(interned, op) != 0) |
| 526 | Py_FatalError( |
| 527 | "deletion of interned string failed"); |
| 528 | break; |
| 529 | |
| 530 | case SSTATE_INTERNED_IMMORTAL: |
| 531 | Py_FatalError("Immortal interned string died."); |
| 532 | |
| 533 | default: |
| 534 | Py_FatalError("Inconsistent interned string state."); |
| 535 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 536 | op->ob_type->tp_free(op); |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 539 | /* Unescape a backslash-escaped string. If unicode is non-zero, |
| 540 | the string is a u-literal. If recode_encoding is non-zero, |
| 541 | the string is UTF-8 encoded and should be re-encoded in the |
| 542 | specified encoding. */ |
| 543 | |
| 544 | PyObject *PyString_DecodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 545 | Py_ssize_t len, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 546 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 547 | Py_ssize_t unicode, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 548 | const char *recode_encoding) |
| 549 | { |
| 550 | int c; |
| 551 | char *p, *buf; |
| 552 | const char *end; |
| 553 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 554 | Py_ssize_t newlen = recode_encoding ? 4*len:len; |
Walter Dörwald | 8709a42 | 2002-09-03 13:53:40 +0000 | [diff] [blame] | 555 | v = PyString_FromStringAndSize((char *)NULL, newlen); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 556 | if (v == NULL) |
| 557 | return NULL; |
| 558 | p = buf = PyString_AsString(v); |
| 559 | end = s + len; |
| 560 | while (s < end) { |
| 561 | if (*s != '\\') { |
Martin v. Löwis | 2412853 | 2002-09-09 06:17:05 +0000 | [diff] [blame] | 562 | non_esc: |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 563 | #ifdef Py_USING_UNICODE |
| 564 | if (recode_encoding && (*s & 0x80)) { |
| 565 | PyObject *u, *w; |
| 566 | char *r; |
| 567 | const char* t; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 568 | Py_ssize_t rn; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 569 | t = s; |
| 570 | /* Decode non-ASCII bytes as UTF-8. */ |
| 571 | while (t < end && (*t & 0x80)) t++; |
| 572 | u = PyUnicode_DecodeUTF8(s, t - s, errors); |
| 573 | if(!u) goto failed; |
| 574 | |
| 575 | /* Recode them in target encoding. */ |
| 576 | w = PyUnicode_AsEncodedString( |
| 577 | u, recode_encoding, errors); |
| 578 | Py_DECREF(u); |
| 579 | if (!w) goto failed; |
| 580 | |
| 581 | /* Append bytes to output buffer. */ |
Neal Norwitz | 2aa9a5d | 2006-03-20 01:53:23 +0000 | [diff] [blame] | 582 | assert(PyString_Check(w)); |
| 583 | r = PyString_AS_STRING(w); |
| 584 | rn = PyString_GET_SIZE(w); |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 585 | Py_MEMCPY(p, r, rn); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 586 | p += rn; |
| 587 | Py_DECREF(w); |
| 588 | s = t; |
| 589 | } else { |
| 590 | *p++ = *s++; |
| 591 | } |
| 592 | #else |
| 593 | *p++ = *s++; |
| 594 | #endif |
| 595 | continue; |
| 596 | } |
| 597 | s++; |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 598 | if (s==end) { |
| 599 | PyErr_SetString(PyExc_ValueError, |
| 600 | "Trailing \\ in string"); |
| 601 | goto failed; |
| 602 | } |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 603 | switch (*s++) { |
| 604 | /* XXX This assumes ASCII! */ |
| 605 | case '\n': break; |
| 606 | case '\\': *p++ = '\\'; break; |
| 607 | case '\'': *p++ = '\''; break; |
| 608 | case '\"': *p++ = '\"'; break; |
| 609 | case 'b': *p++ = '\b'; break; |
| 610 | case 'f': *p++ = '\014'; break; /* FF */ |
| 611 | case 't': *p++ = '\t'; break; |
| 612 | case 'n': *p++ = '\n'; break; |
| 613 | case 'r': *p++ = '\r'; break; |
| 614 | case 'v': *p++ = '\013'; break; /* VT */ |
| 615 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 616 | case '0': case '1': case '2': case '3': |
| 617 | case '4': case '5': case '6': case '7': |
| 618 | c = s[-1] - '0'; |
| 619 | if ('0' <= *s && *s <= '7') { |
| 620 | c = (c<<3) + *s++ - '0'; |
| 621 | if ('0' <= *s && *s <= '7') |
| 622 | c = (c<<3) + *s++ - '0'; |
| 623 | } |
| 624 | *p++ = c; |
| 625 | break; |
| 626 | case 'x': |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 627 | if (isxdigit(Py_CHARMASK(s[0])) |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 628 | && isxdigit(Py_CHARMASK(s[1]))) { |
| 629 | unsigned int x = 0; |
| 630 | c = Py_CHARMASK(*s); |
| 631 | s++; |
| 632 | if (isdigit(c)) |
| 633 | x = c - '0'; |
| 634 | else if (islower(c)) |
| 635 | x = 10 + c - 'a'; |
| 636 | else |
| 637 | x = 10 + c - 'A'; |
| 638 | x = x << 4; |
| 639 | c = Py_CHARMASK(*s); |
| 640 | s++; |
| 641 | if (isdigit(c)) |
| 642 | x += c - '0'; |
| 643 | else if (islower(c)) |
| 644 | x += 10 + c - 'a'; |
| 645 | else |
| 646 | x += 10 + c - 'A'; |
| 647 | *p++ = x; |
| 648 | break; |
| 649 | } |
| 650 | if (!errors || strcmp(errors, "strict") == 0) { |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 651 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 652 | "invalid \\x escape"); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 653 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 654 | } |
| 655 | if (strcmp(errors, "replace") == 0) { |
| 656 | *p++ = '?'; |
| 657 | } else if (strcmp(errors, "ignore") == 0) |
| 658 | /* do nothing */; |
| 659 | else { |
| 660 | PyErr_Format(PyExc_ValueError, |
| 661 | "decoding error; " |
| 662 | "unknown error handling code: %.400s", |
| 663 | errors); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 664 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 665 | } |
| 666 | #ifndef Py_USING_UNICODE |
| 667 | case 'u': |
| 668 | case 'U': |
| 669 | case 'N': |
| 670 | if (unicode) { |
Neal Norwitz | b898d9f | 2002-08-16 23:20:39 +0000 | [diff] [blame] | 671 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 672 | "Unicode escapes not legal " |
| 673 | "when Unicode disabled"); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 674 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 675 | } |
| 676 | #endif |
| 677 | default: |
| 678 | *p++ = '\\'; |
Martin v. Löwis | 2412853 | 2002-09-09 06:17:05 +0000 | [diff] [blame] | 679 | s--; |
| 680 | goto non_esc; /* an arbitry number of unescaped |
| 681 | UTF-8 bytes may follow. */ |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 682 | } |
| 683 | } |
Walter Dörwald | 8709a42 | 2002-09-03 13:53:40 +0000 | [diff] [blame] | 684 | if (p-buf < newlen) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 685 | _PyString_Resize(&v, p - buf); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 686 | return v; |
| 687 | failed: |
| 688 | Py_DECREF(v); |
| 689 | return NULL; |
| 690 | } |
| 691 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 692 | /* -------------------------------------------------------------------- */ |
| 693 | /* object api */ |
| 694 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 695 | static Py_ssize_t |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 696 | string_getsize(register PyObject *op) |
| 697 | { |
| 698 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 699 | Py_ssize_t len; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 700 | if (PyString_AsStringAndSize(op, &s, &len)) |
| 701 | return -1; |
| 702 | return len; |
| 703 | } |
| 704 | |
| 705 | static /*const*/ char * |
| 706 | string_getbuffer(register PyObject *op) |
| 707 | { |
| 708 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 709 | Py_ssize_t len; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 710 | if (PyString_AsStringAndSize(op, &s, &len)) |
| 711 | return NULL; |
| 712 | return s; |
| 713 | } |
| 714 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 715 | Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 716 | PyString_Size(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 717 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 718 | if (!PyString_Check(op)) |
| 719 | return string_getsize(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 720 | return ((PyStringObject *)op) -> ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /*const*/ char * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 724 | PyString_AsString(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 725 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 726 | if (!PyString_Check(op)) |
| 727 | return string_getbuffer(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 728 | return ((PyStringObject *)op) -> ob_sval; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 731 | int |
| 732 | PyString_AsStringAndSize(register PyObject *obj, |
| 733 | register char **s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 734 | register Py_ssize_t *len) |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 735 | { |
| 736 | if (s == NULL) { |
| 737 | PyErr_BadInternalCall(); |
| 738 | return -1; |
| 739 | } |
| 740 | |
| 741 | if (!PyString_Check(obj)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 742 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 743 | if (PyUnicode_Check(obj)) { |
| 744 | obj = _PyUnicode_AsDefaultEncodedString(obj, NULL); |
| 745 | if (obj == NULL) |
| 746 | return -1; |
| 747 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 748 | else |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 749 | #endif |
| 750 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 751 | PyErr_Format(PyExc_TypeError, |
| 752 | "expected string or Unicode object, " |
| 753 | "%.200s found", obj->ob_type->tp_name); |
| 754 | return -1; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | *s = PyString_AS_STRING(obj); |
| 759 | if (len != NULL) |
| 760 | *len = PyString_GET_SIZE(obj); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 761 | else if (strlen(*s) != (size_t)PyString_GET_SIZE(obj)) { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 762 | PyErr_SetString(PyExc_TypeError, |
| 763 | "expected string without null bytes"); |
| 764 | return -1; |
| 765 | } |
| 766 | return 0; |
| 767 | } |
| 768 | |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 769 | /* -------------------------------------------------------------------- */ |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 770 | /* Methods */ |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 771 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 772 | #define STRINGLIB_CHAR char |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 773 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 774 | #define STRINGLIB_CMP memcmp |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 775 | #define STRINGLIB_LEN PyString_GET_SIZE |
| 776 | #define STRINGLIB_NEW PyString_FromStringAndSize |
| 777 | #define STRINGLIB_STR PyString_AS_STRING |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 778 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 779 | #define STRINGLIB_EMPTY nullstring |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 780 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 781 | #include "stringlib/fastsearch.h" |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 782 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 783 | #include "stringlib/count.h" |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 784 | #include "stringlib/find.h" |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 785 | #include "stringlib/partition.h" |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 786 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 787 | |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 788 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 789 | string_print(PyStringObject *op, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 790 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 791 | Py_ssize_t i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 792 | char c; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 793 | int quote; |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 794 | |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 795 | /* XXX Ought to check for interrupts when writing long strings */ |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 796 | if (! PyString_CheckExact(op)) { |
| 797 | int ret; |
| 798 | /* A str subclass may have its own __str__ method. */ |
| 799 | op = (PyStringObject *) PyObject_Str((PyObject *)op); |
| 800 | if (op == NULL) |
| 801 | return -1; |
| 802 | ret = string_print(op, fp, flags); |
| 803 | Py_DECREF(op); |
| 804 | return ret; |
| 805 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 806 | if (flags & Py_PRINT_RAW) { |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 807 | char *data = op->ob_sval; |
| 808 | Py_ssize_t size = op->ob_size; |
| 809 | while (size > INT_MAX) { |
| 810 | /* Very long strings cannot be written atomically. |
| 811 | * But don't write exactly INT_MAX bytes at a time |
| 812 | * to avoid memory aligment issues. |
| 813 | */ |
| 814 | const int chunk_size = INT_MAX & ~0x3FFF; |
| 815 | fwrite(data, 1, chunk_size, fp); |
| 816 | data += chunk_size; |
| 817 | size -= chunk_size; |
| 818 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 819 | #ifdef __VMS |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 820 | if (size) fwrite(data, (int)size, 1, fp); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 821 | #else |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 822 | fwrite(data, 1, (int)size, fp); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 823 | #endif |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 824 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 825 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 826 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 827 | /* figure out which quote to use; single is preferred */ |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 828 | quote = '\''; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 829 | if (memchr(op->ob_sval, '\'', op->ob_size) && |
| 830 | !memchr(op->ob_sval, '"', op->ob_size)) |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 831 | quote = '"'; |
| 832 | |
| 833 | fputc(quote, fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 834 | for (i = 0; i < op->ob_size; i++) { |
| 835 | c = op->ob_sval[i]; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 836 | if (c == quote || c == '\\') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 837 | fprintf(fp, "\\%c", c); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 838 | else if (c == '\t') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 839 | fprintf(fp, "\\t"); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 840 | else if (c == '\n') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 841 | fprintf(fp, "\\n"); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 842 | else if (c == '\r') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 843 | fprintf(fp, "\\r"); |
| 844 | else if (c < ' ' || c >= 0x7f) |
| 845 | fprintf(fp, "\\x%02x", c & 0xff); |
Martin v. Löwis | fed2405 | 2002-10-07 13:55:50 +0000 | [diff] [blame] | 846 | else |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 847 | fputc(c, fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 848 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 849 | fputc(quote, fp); |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 850 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 853 | PyObject * |
| 854 | PyString_Repr(PyObject *obj, int smartquotes) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 855 | { |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 856 | register PyStringObject* op = (PyStringObject*) obj; |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 857 | size_t newsize = 2 + 4 * op->ob_size; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 858 | PyObject *v; |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 859 | if (newsize > PY_SSIZE_T_MAX || newsize / 4 != op->ob_size) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 860 | PyErr_SetString(PyExc_OverflowError, |
| 861 | "string is too large to make repr"); |
| 862 | } |
| 863 | v = PyString_FromStringAndSize((char *)NULL, newsize); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 864 | if (v == NULL) { |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 865 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 866 | } |
| 867 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 868 | register Py_ssize_t i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 869 | register char c; |
| 870 | register char *p; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 871 | int quote; |
| 872 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 873 | /* figure out which quote to use; single is preferred */ |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 874 | quote = '\''; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 875 | if (smartquotes && |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 876 | memchr(op->ob_sval, '\'', op->ob_size) && |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 877 | !memchr(op->ob_sval, '"', op->ob_size)) |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 878 | quote = '"'; |
| 879 | |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 880 | p = PyString_AS_STRING(v); |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 881 | *p++ = quote; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 882 | for (i = 0; i < op->ob_size; i++) { |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 883 | /* There's at least enough room for a hex escape |
| 884 | and a closing quote. */ |
| 885 | assert(newsize - (p - PyString_AS_STRING(v)) >= 5); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 886 | c = op->ob_sval[i]; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 887 | if (c == quote || c == '\\') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 888 | *p++ = '\\', *p++ = c; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 889 | else if (c == '\t') |
| 890 | *p++ = '\\', *p++ = 't'; |
| 891 | else if (c == '\n') |
| 892 | *p++ = '\\', *p++ = 'n'; |
| 893 | else if (c == '\r') |
| 894 | *p++ = '\\', *p++ = 'r'; |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 895 | else if (c < ' ' || c >= 0x7f) { |
| 896 | /* For performance, we don't want to call |
| 897 | PyOS_snprintf here (extra layers of |
| 898 | function call). */ |
| 899 | sprintf(p, "\\x%02x", c & 0xff); |
| 900 | p += 4; |
Martin v. Löwis | fed2405 | 2002-10-07 13:55:50 +0000 | [diff] [blame] | 901 | } |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 902 | else |
| 903 | *p++ = c; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 904 | } |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 905 | assert(newsize - (p - PyString_AS_STRING(v)) >= 1); |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 906 | *p++ = quote; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 907 | *p = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 908 | _PyString_Resize( |
Thomas Wouters | 568f1d0 | 2006-04-21 13:54:43 +0000 | [diff] [blame] | 909 | &v, (p - PyString_AS_STRING(v))); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 910 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 911 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 914 | static PyObject * |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 915 | string_repr(PyObject *op) |
| 916 | { |
| 917 | return PyString_Repr(op, 1); |
| 918 | } |
| 919 | |
| 920 | static PyObject * |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 921 | string_str(PyObject *s) |
| 922 | { |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 923 | assert(PyString_Check(s)); |
| 924 | if (PyString_CheckExact(s)) { |
| 925 | Py_INCREF(s); |
| 926 | return s; |
| 927 | } |
| 928 | else { |
| 929 | /* Subtype -- return genuine string with the same value. */ |
| 930 | PyStringObject *t = (PyStringObject *) s; |
| 931 | return PyString_FromStringAndSize(t->ob_sval, t->ob_size); |
| 932 | } |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 935 | static Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 936 | string_length(PyStringObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 937 | { |
| 938 | return a->ob_size; |
| 939 | } |
| 940 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 941 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 942 | string_concat(register PyStringObject *a, register PyObject *bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 943 | { |
Andrew Dalke | 598710c | 2006-05-25 18:18:39 +0000 | [diff] [blame] | 944 | register Py_ssize_t size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 945 | register PyStringObject *op; |
| 946 | if (!PyString_Check(bb)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 947 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 948 | if (PyUnicode_Check(bb)) |
| 949 | return PyUnicode_Concat((PyObject *)a, bb); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 950 | #endif |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 951 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5c66a26 | 2001-10-22 04:12:44 +0000 | [diff] [blame] | 952 | "cannot concatenate 'str' and '%.200s' objects", |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 953 | bb->ob_type->tp_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 954 | return NULL; |
| 955 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 956 | #define b ((PyStringObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 957 | /* Optimize cases with empty left or right operand */ |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 958 | if ((a->ob_size == 0 || b->ob_size == 0) && |
| 959 | PyString_CheckExact(a) && PyString_CheckExact(b)) { |
| 960 | if (a->ob_size == 0) { |
| 961 | Py_INCREF(bb); |
| 962 | return bb; |
| 963 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 964 | Py_INCREF(a); |
| 965 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 966 | } |
| 967 | size = a->ob_size + b->ob_size; |
Andrew Dalke | 598710c | 2006-05-25 18:18:39 +0000 | [diff] [blame] | 968 | if (size < 0) { |
| 969 | PyErr_SetString(PyExc_OverflowError, |
| 970 | "strings are too large to concat"); |
| 971 | return NULL; |
| 972 | } |
| 973 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 974 | /* Inline PyObject_NewVar */ |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 975 | op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 976 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 977 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 978 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 979 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 980 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 981 | Py_MEMCPY(op->ob_sval, a->ob_sval, a->ob_size); |
| 982 | Py_MEMCPY(op->ob_sval + a->ob_size, b->ob_sval, b->ob_size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 983 | op->ob_sval[size] = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 984 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 985 | #undef b |
| 986 | } |
| 987 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 988 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 989 | string_repeat(register PyStringObject *a, register Py_ssize_t n) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 990 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 991 | register Py_ssize_t i; |
| 992 | register Py_ssize_t j; |
| 993 | register Py_ssize_t size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 994 | register PyStringObject *op; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 995 | size_t nbytes; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 996 | if (n < 0) |
| 997 | n = 0; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 998 | /* watch out for overflows: the size can overflow int, |
| 999 | * and the # of bytes needed can overflow size_t |
| 1000 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1001 | size = a->ob_size * n; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 1002 | if (n && size / n != a->ob_size) { |
| 1003 | PyErr_SetString(PyExc_OverflowError, |
| 1004 | "repeated string is too long"); |
| 1005 | return NULL; |
| 1006 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1007 | if (size == a->ob_size && PyString_CheckExact(a)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1008 | Py_INCREF(a); |
| 1009 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1010 | } |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 1011 | nbytes = (size_t)size; |
| 1012 | if (nbytes + sizeof(PyStringObject) <= nbytes) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 1013 | PyErr_SetString(PyExc_OverflowError, |
| 1014 | "repeated string is too long"); |
| 1015 | return NULL; |
| 1016 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1017 | op = (PyStringObject *) |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 1018 | PyObject_MALLOC(sizeof(PyStringObject) + nbytes); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 1019 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1020 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1021 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1022 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 1023 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Raymond Hettinger | 0a2f849 | 2003-01-06 22:42:41 +0000 | [diff] [blame] | 1024 | op->ob_sval[size] = '\0'; |
| 1025 | if (a->ob_size == 1 && n > 0) { |
| 1026 | memset(op->ob_sval, a->ob_sval[0] , n); |
| 1027 | return (PyObject *) op; |
| 1028 | } |
Raymond Hettinger | 698258a | 2003-01-06 10:33:56 +0000 | [diff] [blame] | 1029 | i = 0; |
| 1030 | if (i < size) { |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 1031 | Py_MEMCPY(op->ob_sval, a->ob_sval, a->ob_size); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1032 | i = a->ob_size; |
Raymond Hettinger | 698258a | 2003-01-06 10:33:56 +0000 | [diff] [blame] | 1033 | } |
| 1034 | while (i < size) { |
| 1035 | j = (i <= size-i) ? i : size-i; |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 1036 | Py_MEMCPY(op->ob_sval+i, op->ob_sval, j); |
Raymond Hettinger | 698258a | 2003-01-06 10:33:56 +0000 | [diff] [blame] | 1037 | i += j; |
| 1038 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1039 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ |
| 1043 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1044 | static PyObject * |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1045 | string_slice(register PyStringObject *a, register Py_ssize_t i, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1046 | register Py_ssize_t j) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1047 | /* j -- may be negative! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1048 | { |
| 1049 | if (i < 0) |
| 1050 | i = 0; |
| 1051 | if (j < 0) |
| 1052 | j = 0; /* Avoid signed/unsigned bug in next line */ |
| 1053 | if (j > a->ob_size) |
| 1054 | j = a->ob_size; |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1055 | if (i == 0 && j == a->ob_size && PyString_CheckExact(a)) { |
| 1056 | /* It's the same as a */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1057 | Py_INCREF(a); |
| 1058 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1059 | } |
| 1060 | if (j < i) |
| 1061 | j = i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1062 | return PyString_FromStringAndSize(a->ob_sval + i, j-i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1065 | static int |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1066 | string_contains(PyObject *str_obj, PyObject *sub_obj) |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1067 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1068 | if (!PyString_CheckExact(sub_obj)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1069 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1070 | if (PyUnicode_Check(sub_obj)) |
| 1071 | return PyUnicode_Contains(str_obj, sub_obj); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1072 | #endif |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1073 | if (!PyString_Check(sub_obj)) { |
Georg Brandl | 283a135 | 2006-11-19 08:48:30 +0000 | [diff] [blame] | 1074 | PyErr_Format(PyExc_TypeError, |
| 1075 | "'in <string>' requires string as left operand, " |
| 1076 | "not %.200s", sub_obj->ob_type->tp_name); |
Guido van Rossum | bf935fd | 2002-08-24 06:57:49 +0000 | [diff] [blame] | 1077 | return -1; |
| 1078 | } |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1079 | } |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 1080 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1081 | return stringlib_contains_obj(str_obj, sub_obj); |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1084 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1085 | string_item(PyStringObject *a, register Py_ssize_t i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1086 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1087 | char pchar; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1088 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1089 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1090 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1091 | return NULL; |
| 1092 | } |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1093 | pchar = a->ob_sval[i]; |
| 1094 | v = (PyObject *)characters[pchar & UCHAR_MAX]; |
Tim Peters | 5b4d477 | 2001-05-08 22:33:50 +0000 | [diff] [blame] | 1095 | if (v == NULL) |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1096 | v = PyString_FromStringAndSize(&pchar, 1); |
Tim Peters | b4bbcd7 | 2001-05-09 00:31:40 +0000 | [diff] [blame] | 1097 | else { |
| 1098 | #ifdef COUNT_ALLOCS |
| 1099 | one_strings++; |
| 1100 | #endif |
Tim Peters | cf5ad5d | 2001-05-09 00:24:55 +0000 | [diff] [blame] | 1101 | Py_INCREF(v); |
Tim Peters | b4bbcd7 | 2001-05-09 00:31:40 +0000 | [diff] [blame] | 1102 | } |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 1103 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1106 | static PyObject* |
| 1107 | string_richcompare(PyStringObject *a, PyStringObject *b, int op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1108 | { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1109 | int c; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1110 | Py_ssize_t len_a, len_b; |
| 1111 | Py_ssize_t min_len; |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1112 | PyObject *result; |
| 1113 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 1114 | /* Make sure both arguments are strings. */ |
| 1115 | if (!(PyString_Check(a) && PyString_Check(b))) { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1116 | result = Py_NotImplemented; |
| 1117 | goto out; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1118 | } |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1119 | if (a == b) { |
| 1120 | switch (op) { |
| 1121 | case Py_EQ:case Py_LE:case Py_GE: |
| 1122 | result = Py_True; |
| 1123 | goto out; |
| 1124 | case Py_NE:case Py_LT:case Py_GT: |
| 1125 | result = Py_False; |
| 1126 | goto out; |
| 1127 | } |
| 1128 | } |
| 1129 | if (op == Py_EQ) { |
| 1130 | /* Supporting Py_NE here as well does not save |
| 1131 | much time, since Py_NE is rarely used. */ |
| 1132 | if (a->ob_size == b->ob_size |
| 1133 | && (a->ob_sval[0] == b->ob_sval[0] |
Neal Norwitz | 7218c2d | 2007-02-25 15:53:36 +0000 | [diff] [blame] | 1134 | && memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0)) { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1135 | result = Py_True; |
| 1136 | } else { |
| 1137 | result = Py_False; |
| 1138 | } |
| 1139 | goto out; |
| 1140 | } |
| 1141 | len_a = a->ob_size; len_b = b->ob_size; |
| 1142 | min_len = (len_a < len_b) ? len_a : len_b; |
| 1143 | if (min_len > 0) { |
| 1144 | c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
| 1145 | if (c==0) |
| 1146 | c = memcmp(a->ob_sval, b->ob_sval, min_len); |
Neal Norwitz | 7218c2d | 2007-02-25 15:53:36 +0000 | [diff] [blame] | 1147 | } else |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1148 | c = 0; |
| 1149 | if (c == 0) |
| 1150 | c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
| 1151 | switch (op) { |
| 1152 | case Py_LT: c = c < 0; break; |
| 1153 | case Py_LE: c = c <= 0; break; |
| 1154 | case Py_EQ: assert(0); break; /* unreachable */ |
| 1155 | case Py_NE: c = c != 0; break; |
| 1156 | case Py_GT: c = c > 0; break; |
| 1157 | case Py_GE: c = c >= 0; break; |
| 1158 | default: |
| 1159 | result = Py_NotImplemented; |
| 1160 | goto out; |
| 1161 | } |
| 1162 | result = c ? Py_True : Py_False; |
| 1163 | out: |
| 1164 | Py_INCREF(result); |
| 1165 | return result; |
| 1166 | } |
| 1167 | |
| 1168 | int |
| 1169 | _PyString_Eq(PyObject *o1, PyObject *o2) |
| 1170 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1171 | PyStringObject *a = (PyStringObject*) o1; |
| 1172 | PyStringObject *b = (PyStringObject*) o2; |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1173 | return a->ob_size == b->ob_size |
| 1174 | && *a->ob_sval == *b->ob_sval |
| 1175 | && memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1178 | static long |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1179 | string_hash(PyStringObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1180 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1181 | register Py_ssize_t len; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1182 | register unsigned char *p; |
| 1183 | register long x; |
| 1184 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1185 | if (a->ob_shash != -1) |
| 1186 | return a->ob_shash; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1187 | len = a->ob_size; |
| 1188 | p = (unsigned char *) a->ob_sval; |
| 1189 | x = *p << 7; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1190 | while (--len >= 0) |
Guido van Rossum | eddcb3b | 1996-09-11 20:22:48 +0000 | [diff] [blame] | 1191 | x = (1000003*x) ^ *p++; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1192 | x ^= a->ob_size; |
| 1193 | if (x == -1) |
| 1194 | x = -2; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1195 | a->ob_shash = x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1196 | return x; |
| 1197 | } |
| 1198 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1199 | static PyObject* |
| 1200 | string_subscript(PyStringObject* self, PyObject* item) |
| 1201 | { |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1202 | if (PyIndex_Check(item)) { |
| 1203 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1204 | if (i == -1 && PyErr_Occurred()) |
| 1205 | return NULL; |
| 1206 | if (i < 0) |
| 1207 | i += PyString_GET_SIZE(self); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1208 | return string_item(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1209 | } |
| 1210 | else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1211 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1212 | char* source_buf; |
| 1213 | char* result_buf; |
| 1214 | PyObject* result; |
| 1215 | |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1216 | if (PySlice_GetIndicesEx((PySliceObject*)item, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1217 | PyString_GET_SIZE(self), |
| 1218 | &start, &stop, &step, &slicelength) < 0) { |
| 1219 | return NULL; |
| 1220 | } |
| 1221 | |
| 1222 | if (slicelength <= 0) { |
| 1223 | return PyString_FromStringAndSize("", 0); |
| 1224 | } |
| 1225 | else { |
| 1226 | source_buf = PyString_AsString((PyObject*)self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1227 | result_buf = (char *)PyMem_Malloc(slicelength); |
Neal Norwitz | 95c1e50 | 2005-10-20 04:15:52 +0000 | [diff] [blame] | 1228 | if (result_buf == NULL) |
| 1229 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1230 | |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1231 | for (cur = start, i = 0; i < slicelength; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1232 | cur += step, i++) { |
| 1233 | result_buf[i] = source_buf[cur]; |
| 1234 | } |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1235 | |
| 1236 | result = PyString_FromStringAndSize(result_buf, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1237 | slicelength); |
| 1238 | PyMem_Free(result_buf); |
| 1239 | return result; |
| 1240 | } |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1241 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1242 | else { |
Georg Brandl | 283a135 | 2006-11-19 08:48:30 +0000 | [diff] [blame] | 1243 | PyErr_Format(PyExc_TypeError, |
| 1244 | "string indices must be integers, not %.200s", |
| 1245 | item->ob_type->tp_name); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1246 | return NULL; |
| 1247 | } |
| 1248 | } |
| 1249 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1250 | static Py_ssize_t |
| 1251 | string_buffer_getreadbuf(PyStringObject *self, Py_ssize_t index, const void **ptr) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1252 | { |
| 1253 | if ( index != 0 ) { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1254 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 1255 | "accessing non-existent string segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1256 | return -1; |
| 1257 | } |
| 1258 | *ptr = (void *)self->ob_sval; |
| 1259 | return self->ob_size; |
| 1260 | } |
| 1261 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1262 | static Py_ssize_t |
| 1263 | string_buffer_getwritebuf(PyStringObject *self, Py_ssize_t index, const void **ptr) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1264 | { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1265 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 07d7800 | 1998-10-01 15:59:48 +0000 | [diff] [blame] | 1266 | "Cannot use string as modifiable buffer"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1267 | return -1; |
| 1268 | } |
| 1269 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1270 | static Py_ssize_t |
| 1271 | string_buffer_getsegcount(PyStringObject *self, Py_ssize_t *lenp) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1272 | { |
| 1273 | if ( lenp ) |
| 1274 | *lenp = self->ob_size; |
| 1275 | return 1; |
| 1276 | } |
| 1277 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1278 | static Py_ssize_t |
| 1279 | string_buffer_getcharbuf(PyStringObject *self, Py_ssize_t index, const char **ptr) |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 1280 | { |
| 1281 | if ( index != 0 ) { |
| 1282 | PyErr_SetString(PyExc_SystemError, |
| 1283 | "accessing non-existent string segment"); |
| 1284 | return -1; |
| 1285 | } |
| 1286 | *ptr = self->ob_sval; |
| 1287 | return self->ob_size; |
| 1288 | } |
| 1289 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1290 | static PySequenceMethods string_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1291 | (lenfunc)string_length, /*sq_length*/ |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1292 | (binaryfunc)string_concat, /*sq_concat*/ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1293 | (ssizeargfunc)string_repeat, /*sq_repeat*/ |
| 1294 | (ssizeargfunc)string_item, /*sq_item*/ |
| 1295 | (ssizessizeargfunc)string_slice, /*sq_slice*/ |
Guido van Rossum | f380e66 | 1991-06-04 19:36:32 +0000 | [diff] [blame] | 1296 | 0, /*sq_ass_item*/ |
| 1297 | 0, /*sq_ass_slice*/ |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1298 | (objobjproc)string_contains /*sq_contains*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1299 | }; |
| 1300 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1301 | static PyMappingMethods string_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1302 | (lenfunc)string_length, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1303 | (binaryfunc)string_subscript, |
| 1304 | 0, |
| 1305 | }; |
| 1306 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1307 | static PyBufferProcs string_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1308 | (readbufferproc)string_buffer_getreadbuf, |
| 1309 | (writebufferproc)string_buffer_getwritebuf, |
| 1310 | (segcountproc)string_buffer_getsegcount, |
| 1311 | (charbufferproc)string_buffer_getcharbuf, |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1312 | }; |
| 1313 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1314 | |
| 1315 | |
| 1316 | #define LEFTSTRIP 0 |
| 1317 | #define RIGHTSTRIP 1 |
| 1318 | #define BOTHSTRIP 2 |
| 1319 | |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1320 | /* Arrays indexed by above */ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 1321 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 1322 | |
| 1323 | #define STRIPNAME(i) (stripformat[i]+3) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1324 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1325 | |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1326 | /* Don't call if length < 2 */ |
| 1327 | #define Py_STRING_MATCH(target, offset, pattern, length) \ |
| 1328 | (target[offset] == pattern[0] && \ |
| 1329 | target[offset+length-1] == pattern[length-1] && \ |
| 1330 | !memcmp(target+offset+1, pattern+1, length-2) ) |
| 1331 | |
| 1332 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1333 | /* Overallocate the initial list to reduce the number of reallocs for small |
| 1334 | split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three |
| 1335 | resizes, to sizes 4, 8, then 16. Most observed string splits are for human |
| 1336 | text (roughly 11 words per line) and field delimited data (usually 1-10 |
| 1337 | fields). For large strings the split algorithms are bandwidth limited |
| 1338 | so increasing the preallocation likely will not improve things.*/ |
| 1339 | |
| 1340 | #define MAX_PREALLOC 12 |
| 1341 | |
| 1342 | /* 5 splits gives 6 elements */ |
| 1343 | #define PREALLOC_SIZE(maxsplit) \ |
| 1344 | (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1) |
| 1345 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1346 | #define SPLIT_APPEND(data, left, right) \ |
| 1347 | str = PyString_FromStringAndSize((data) + (left), \ |
| 1348 | (right) - (left)); \ |
| 1349 | if (str == NULL) \ |
| 1350 | goto onError; \ |
| 1351 | if (PyList_Append(list, str)) { \ |
| 1352 | Py_DECREF(str); \ |
| 1353 | goto onError; \ |
| 1354 | } \ |
| 1355 | else \ |
| 1356 | Py_DECREF(str); |
| 1357 | |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1358 | #define SPLIT_ADD(data, left, right) { \ |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1359 | str = PyString_FromStringAndSize((data) + (left), \ |
| 1360 | (right) - (left)); \ |
| 1361 | if (str == NULL) \ |
| 1362 | goto onError; \ |
| 1363 | if (count < MAX_PREALLOC) { \ |
| 1364 | PyList_SET_ITEM(list, count, str); \ |
| 1365 | } else { \ |
| 1366 | if (PyList_Append(list, str)) { \ |
| 1367 | Py_DECREF(str); \ |
| 1368 | goto onError; \ |
| 1369 | } \ |
| 1370 | else \ |
| 1371 | Py_DECREF(str); \ |
| 1372 | } \ |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1373 | count++; } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1374 | |
| 1375 | /* Always force the list to the expected size. */ |
Neal Norwitz | b16e4e7 | 2006-06-01 05:32:49 +0000 | [diff] [blame] | 1376 | #define FIX_PREALLOC_SIZE(list) ((PyListObject *)list)->ob_size = count |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1377 | |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1378 | #define SKIP_SPACE(s, i, len) { while (i<len && isspace(Py_CHARMASK(s[i]))) i++; } |
| 1379 | #define SKIP_NONSPACE(s, i, len) { while (i<len && !isspace(Py_CHARMASK(s[i]))) i++; } |
| 1380 | #define RSKIP_SPACE(s, i) { while (i>=0 && isspace(Py_CHARMASK(s[i]))) i--; } |
| 1381 | #define RSKIP_NONSPACE(s, i) { while (i>=0 && !isspace(Py_CHARMASK(s[i]))) i--; } |
| 1382 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 1383 | Py_LOCAL_INLINE(PyObject *) |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1384 | split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1385 | { |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1386 | Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1387 | PyObject *str; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1388 | PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1389 | |
| 1390 | if (list == NULL) |
| 1391 | return NULL; |
| 1392 | |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1393 | i = j = 0; |
| 1394 | |
| 1395 | while (maxsplit-- > 0) { |
| 1396 | SKIP_SPACE(s, i, len); |
| 1397 | if (i==len) break; |
| 1398 | j = i; i++; |
| 1399 | SKIP_NONSPACE(s, i, len); |
| 1400 | SPLIT_ADD(s, j, i); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1401 | } |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1402 | |
| 1403 | if (i < len) { |
| 1404 | /* Only occurs when maxsplit was reached */ |
| 1405 | /* Skip any remaining whitespace and copy to end of string */ |
| 1406 | SKIP_SPACE(s, i, len); |
| 1407 | if (i != len) |
| 1408 | SPLIT_ADD(s, i, len); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1409 | } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1410 | FIX_PREALLOC_SIZE(list); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1411 | return list; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1412 | onError: |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1413 | Py_DECREF(list); |
| 1414 | return NULL; |
| 1415 | } |
| 1416 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 1417 | Py_LOCAL_INLINE(PyObject *) |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1418 | split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1419 | { |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1420 | register Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1421 | PyObject *str; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1422 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1423 | |
| 1424 | if (list == NULL) |
| 1425 | return NULL; |
| 1426 | |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1427 | i = j = 0; |
| 1428 | while ((j < len) && (maxcount-- > 0)) { |
| 1429 | for(; j<len; j++) { |
| 1430 | /* I found that using memchr makes no difference */ |
| 1431 | if (s[j] == ch) { |
| 1432 | SPLIT_ADD(s, i, j); |
| 1433 | i = j = j + 1; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1434 | break; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1435 | } |
| 1436 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1437 | } |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1438 | if (i <= len) { |
| 1439 | SPLIT_ADD(s, i, len); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1440 | } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1441 | FIX_PREALLOC_SIZE(list); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1442 | return list; |
| 1443 | |
| 1444 | onError: |
| 1445 | Py_DECREF(list); |
| 1446 | return NULL; |
| 1447 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1448 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1449 | PyDoc_STRVAR(split__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1450 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 1451 | \n\ |
| 1452 | Return a list of the words in the string S, using sep as the\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1453 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Raymond Hettinger | bc552ce | 2002-08-05 06:28:21 +0000 | [diff] [blame] | 1454 | splits are done. If sep is not specified or is None, any\n\ |
| 1455 | whitespace string is a separator."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1456 | |
| 1457 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1458 | string_split(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1459 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1460 | Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1461 | Py_ssize_t maxsplit = -1, count=0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1462 | const char *s = PyString_AS_STRING(self), *sub; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1463 | PyObject *list, *str, *subobj = Py_None; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1464 | #ifdef USE_FAST |
| 1465 | Py_ssize_t pos; |
| 1466 | #endif |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1467 | |
Martin v. Löwis | 9c83076 | 2006-04-13 08:37:17 +0000 | [diff] [blame] | 1468 | if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1469 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1470 | if (maxsplit < 0) |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1471 | maxsplit = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1472 | if (subobj == Py_None) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1473 | return split_whitespace(s, len, maxsplit); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1474 | if (PyString_Check(subobj)) { |
| 1475 | sub = PyString_AS_STRING(subobj); |
| 1476 | n = PyString_GET_SIZE(subobj); |
| 1477 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1478 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1479 | else if (PyUnicode_Check(subobj)) |
| 1480 | return PyUnicode_Split((PyObject *)self, subobj, maxsplit); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1481 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1482 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 1483 | return NULL; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1484 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1485 | if (n == 0) { |
| 1486 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 1487 | return NULL; |
| 1488 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1489 | else if (n == 1) |
| 1490 | return split_char(s, len, sub[0], maxsplit); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1491 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1492 | list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1493 | if (list == NULL) |
| 1494 | return NULL; |
| 1495 | |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1496 | #ifdef USE_FAST |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1497 | i = j = 0; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1498 | while (maxsplit-- > 0) { |
| 1499 | pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH); |
| 1500 | if (pos < 0) |
| 1501 | break; |
| 1502 | j = i+pos; |
| 1503 | SPLIT_ADD(s, i, j); |
| 1504 | i = j + n; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1505 | } |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1506 | #else |
| 1507 | i = j = 0; |
| 1508 | while ((j+n <= len) && (maxsplit-- > 0)) { |
| 1509 | for (; j+n <= len; j++) { |
| 1510 | if (Py_STRING_MATCH(s, j, sub, n)) { |
| 1511 | SPLIT_ADD(s, i, j); |
| 1512 | i = j = j + n; |
| 1513 | break; |
| 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | #endif |
| 1518 | SPLIT_ADD(s, i, len); |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1519 | FIX_PREALLOC_SIZE(list); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1520 | return list; |
| 1521 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1522 | onError: |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1523 | Py_DECREF(list); |
| 1524 | return NULL; |
| 1525 | } |
| 1526 | |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1527 | PyDoc_STRVAR(partition__doc__, |
| 1528 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 1529 | \n\ |
| 1530 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 1531 | the separator itself, and the part after it. If the separator is not\n\ |
| 1532 | found, returns S and two empty strings."); |
| 1533 | |
| 1534 | static PyObject * |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 1535 | string_partition(PyStringObject *self, PyObject *sep_obj) |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1536 | { |
Fredrik Lundh | c2032fb | 2006-05-26 17:26:39 +0000 | [diff] [blame] | 1537 | const char *sep; |
| 1538 | Py_ssize_t sep_len; |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1539 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 1540 | if (PyString_Check(sep_obj)) { |
| 1541 | sep = PyString_AS_STRING(sep_obj); |
| 1542 | sep_len = PyString_GET_SIZE(sep_obj); |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1543 | } |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 1544 | #ifdef Py_USING_UNICODE |
| 1545 | else if (PyUnicode_Check(sep_obj)) |
Fredrik Lundh | c2032fb | 2006-05-26 17:26:39 +0000 | [diff] [blame] | 1546 | return PyUnicode_Partition((PyObject *) self, sep_obj); |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1547 | #endif |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 1548 | else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1549 | return NULL; |
| 1550 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 1551 | return stringlib_partition( |
Fredrik Lundh | c2032fb | 2006-05-26 17:26:39 +0000 | [diff] [blame] | 1552 | (PyObject*) self, |
| 1553 | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
| 1554 | sep_obj, sep, sep_len |
| 1555 | ); |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 1558 | PyDoc_STRVAR(rpartition__doc__, |
Raymond Hettinger | a0c95fa | 2006-09-04 15:32:48 +0000 | [diff] [blame] | 1559 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 1560 | \n\ |
| 1561 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 1562 | the part before it, the separator itself, and the part after it. If the\n\ |
Raymond Hettinger | a0c95fa | 2006-09-04 15:32:48 +0000 | [diff] [blame] | 1563 | separator is not found, returns two empty strings and S."); |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 1564 | |
| 1565 | static PyObject * |
| 1566 | string_rpartition(PyStringObject *self, PyObject *sep_obj) |
| 1567 | { |
| 1568 | const char *sep; |
| 1569 | Py_ssize_t sep_len; |
| 1570 | |
| 1571 | if (PyString_Check(sep_obj)) { |
| 1572 | sep = PyString_AS_STRING(sep_obj); |
| 1573 | sep_len = PyString_GET_SIZE(sep_obj); |
| 1574 | } |
| 1575 | #ifdef Py_USING_UNICODE |
| 1576 | else if (PyUnicode_Check(sep_obj)) |
| 1577 | return PyUnicode_Partition((PyObject *) self, sep_obj); |
| 1578 | #endif |
| 1579 | else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
| 1580 | return NULL; |
| 1581 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 1582 | return stringlib_rpartition( |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 1583 | (PyObject*) self, |
| 1584 | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
| 1585 | sep_obj, sep, sep_len |
| 1586 | ); |
| 1587 | } |
| 1588 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 1589 | Py_LOCAL_INLINE(PyObject *) |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1590 | rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1591 | { |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1592 | Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1593 | PyObject *str; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1594 | PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1595 | |
| 1596 | if (list == NULL) |
| 1597 | return NULL; |
| 1598 | |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1599 | i = j = len-1; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 1600 | |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1601 | while (maxsplit-- > 0) { |
| 1602 | RSKIP_SPACE(s, i); |
| 1603 | if (i<0) break; |
| 1604 | j = i; i--; |
| 1605 | RSKIP_NONSPACE(s, i); |
| 1606 | SPLIT_ADD(s, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1607 | } |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1608 | if (i >= 0) { |
| 1609 | /* Only occurs when maxsplit was reached */ |
| 1610 | /* Skip any remaining whitespace and copy to beginning of string */ |
| 1611 | RSKIP_SPACE(s, i); |
| 1612 | if (i >= 0) |
| 1613 | SPLIT_ADD(s, 0, i + 1); |
| 1614 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1615 | } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1616 | FIX_PREALLOC_SIZE(list); |
Fredrik Lundh | 554da41 | 2006-05-25 19:19:05 +0000 | [diff] [blame] | 1617 | if (PyList_Reverse(list) < 0) |
| 1618 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1619 | return list; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1620 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1621 | Py_DECREF(list); |
| 1622 | return NULL; |
| 1623 | } |
| 1624 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 1625 | Py_LOCAL_INLINE(PyObject *) |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1626 | rsplit_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1627 | { |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1628 | register Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1629 | PyObject *str; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1630 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1631 | |
| 1632 | if (list == NULL) |
| 1633 | return NULL; |
| 1634 | |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1635 | i = j = len - 1; |
| 1636 | while ((i >= 0) && (maxcount-- > 0)) { |
| 1637 | for (; i >= 0; i--) { |
| 1638 | if (s[i] == ch) { |
| 1639 | SPLIT_ADD(s, i + 1, j + 1); |
| 1640 | j = i = i - 1; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1641 | break; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1642 | } |
| 1643 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1644 | } |
| 1645 | if (j >= -1) { |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1646 | SPLIT_ADD(s, 0, j + 1); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1647 | } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1648 | FIX_PREALLOC_SIZE(list); |
Fredrik Lundh | 554da41 | 2006-05-25 19:19:05 +0000 | [diff] [blame] | 1649 | if (PyList_Reverse(list) < 0) |
| 1650 | goto onError; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1651 | return list; |
| 1652 | |
| 1653 | onError: |
| 1654 | Py_DECREF(list); |
| 1655 | return NULL; |
| 1656 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1657 | |
| 1658 | PyDoc_STRVAR(rsplit__doc__, |
| 1659 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 1660 | \n\ |
| 1661 | Return a list of the words in the string S, using sep as the\n\ |
| 1662 | delimiter string, starting at the end of the string and working\n\ |
| 1663 | to the front. If maxsplit is given, at most maxsplit splits are\n\ |
| 1664 | done. If sep is not specified or is None, any whitespace string\n\ |
| 1665 | is a separator."); |
| 1666 | |
| 1667 | static PyObject * |
| 1668 | string_rsplit(PyStringObject *self, PyObject *args) |
| 1669 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1670 | Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1671 | Py_ssize_t maxsplit = -1, count=0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1672 | const char *s = PyString_AS_STRING(self), *sub; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1673 | PyObject *list, *str, *subobj = Py_None; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1674 | |
Martin v. Löwis | 9c83076 | 2006-04-13 08:37:17 +0000 | [diff] [blame] | 1675 | if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1676 | return NULL; |
| 1677 | if (maxsplit < 0) |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1678 | maxsplit = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1679 | if (subobj == Py_None) |
| 1680 | return rsplit_whitespace(s, len, maxsplit); |
| 1681 | if (PyString_Check(subobj)) { |
| 1682 | sub = PyString_AS_STRING(subobj); |
| 1683 | n = PyString_GET_SIZE(subobj); |
| 1684 | } |
| 1685 | #ifdef Py_USING_UNICODE |
| 1686 | else if (PyUnicode_Check(subobj)) |
| 1687 | return PyUnicode_RSplit((PyObject *)self, subobj, maxsplit); |
| 1688 | #endif |
| 1689 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 1690 | return NULL; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1691 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1692 | if (n == 0) { |
| 1693 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 1694 | return NULL; |
| 1695 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1696 | else if (n == 1) |
| 1697 | return rsplit_char(s, len, sub[0], maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1698 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1699 | list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1700 | if (list == NULL) |
| 1701 | return NULL; |
| 1702 | |
| 1703 | j = len; |
| 1704 | i = j - n; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1705 | |
| 1706 | while ( (i >= 0) && (maxsplit-- > 0) ) { |
| 1707 | for (; i>=0; i--) { |
| 1708 | if (Py_STRING_MATCH(s, i, sub, n)) { |
| 1709 | SPLIT_ADD(s, i + n, j); |
| 1710 | j = i; |
| 1711 | i -= n; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1712 | break; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1713 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1714 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1715 | } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1716 | SPLIT_ADD(s, 0, j); |
| 1717 | FIX_PREALLOC_SIZE(list); |
| 1718 | if (PyList_Reverse(list) < 0) |
| 1719 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1720 | return list; |
| 1721 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1722 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1723 | Py_DECREF(list); |
| 1724 | return NULL; |
| 1725 | } |
| 1726 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1727 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1728 | PyDoc_STRVAR(join__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1729 | "S.join(sequence) -> string\n\ |
| 1730 | \n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1731 | Return a string which is the concatenation of the strings in the\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1732 | sequence. The separator between elements is S."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1733 | |
| 1734 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1735 | string_join(PyStringObject *self, PyObject *orig) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1736 | { |
| 1737 | char *sep = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1738 | const Py_ssize_t seplen = PyString_GET_SIZE(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1739 | PyObject *res = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1740 | char *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1741 | Py_ssize_t seqlen = 0; |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1742 | size_t sz = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1743 | Py_ssize_t i; |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1744 | PyObject *seq, *item; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1745 | |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1746 | seq = PySequence_Fast(orig, ""); |
| 1747 | if (seq == NULL) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1748 | return NULL; |
| 1749 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1750 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1751 | seqlen = PySequence_Size(seq); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1752 | if (seqlen == 0) { |
| 1753 | Py_DECREF(seq); |
| 1754 | return PyString_FromString(""); |
| 1755 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1756 | if (seqlen == 1) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1757 | item = PySequence_Fast_GET_ITEM(seq, 0); |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1758 | if (PyString_CheckExact(item) || PyUnicode_CheckExact(item)) { |
| 1759 | Py_INCREF(item); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1760 | Py_DECREF(seq); |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1761 | return item; |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1762 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1763 | } |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1764 | |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1765 | /* There are at least two things to join, or else we have a subclass |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1766 | * of the builtin types in the sequence. |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1767 | * Do a pre-pass to figure out the total amount of space we'll |
| 1768 | * need (sz), see whether any argument is absurd, and defer to |
| 1769 | * the Unicode join if appropriate. |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1770 | */ |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1771 | for (i = 0; i < seqlen; i++) { |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1772 | const size_t old_sz = sz; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1773 | item = PySequence_Fast_GET_ITEM(seq, i); |
| 1774 | if (!PyString_Check(item)){ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1775 | #ifdef Py_USING_UNICODE |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1776 | if (PyUnicode_Check(item)) { |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 1777 | /* Defer to Unicode join. |
| 1778 | * CAUTION: There's no gurantee that the |
| 1779 | * original sequence can be iterated over |
| 1780 | * again, so we must pass seq here. |
| 1781 | */ |
| 1782 | PyObject *result; |
| 1783 | result = PyUnicode_Join((PyObject *)self, seq); |
Barry Warsaw | 771d067 | 2000-07-11 04:58:12 +0000 | [diff] [blame] | 1784 | Py_DECREF(seq); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 1785 | return result; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1786 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1787 | #endif |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1788 | PyErr_Format(PyExc_TypeError, |
Neal Norwitz | 0e2cbab | 2006-04-17 05:56:32 +0000 | [diff] [blame] | 1789 | "sequence item %zd: expected string," |
Jeremy Hylton | 88887aa | 2000-07-11 20:55:38 +0000 | [diff] [blame] | 1790 | " %.80s found", |
Neal Norwitz | 0e2cbab | 2006-04-17 05:56:32 +0000 | [diff] [blame] | 1791 | i, item->ob_type->tp_name); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1792 | Py_DECREF(seq); |
| 1793 | return NULL; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1794 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1795 | sz += PyString_GET_SIZE(item); |
| 1796 | if (i != 0) |
| 1797 | sz += seplen; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1798 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1799 | PyErr_SetString(PyExc_OverflowError, |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 1800 | "join() result is too long for a Python string"); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1801 | Py_DECREF(seq); |
| 1802 | return NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1803 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | /* Allocate result space. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1807 | res = PyString_FromStringAndSize((char*)NULL, sz); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1808 | if (res == NULL) { |
| 1809 | Py_DECREF(seq); |
| 1810 | return NULL; |
| 1811 | } |
| 1812 | |
| 1813 | /* Catenate everything. */ |
| 1814 | p = PyString_AS_STRING(res); |
| 1815 | for (i = 0; i < seqlen; ++i) { |
| 1816 | size_t n; |
| 1817 | item = PySequence_Fast_GET_ITEM(seq, i); |
| 1818 | n = PyString_GET_SIZE(item); |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 1819 | Py_MEMCPY(p, PyString_AS_STRING(item), n); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1820 | p += n; |
| 1821 | if (i < seqlen - 1) { |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 1822 | Py_MEMCPY(p, sep, seplen); |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1823 | p += seplen; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1824 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1825 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1826 | |
Jeremy Hylton | 4904829 | 2000-07-11 03:28:17 +0000 | [diff] [blame] | 1827 | Py_DECREF(seq); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1828 | return res; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Tim Peters | 52e155e | 2001-06-16 05:42:57 +0000 | [diff] [blame] | 1831 | PyObject * |
| 1832 | _PyString_Join(PyObject *sep, PyObject *x) |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1833 | { |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1834 | assert(sep != NULL && PyString_Check(sep)); |
| 1835 | assert(x != NULL); |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1836 | return string_join((PyStringObject *)sep, x); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 1839 | Py_LOCAL_INLINE(void) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1840 | string_adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len) |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 1841 | { |
| 1842 | if (*end > len) |
| 1843 | *end = len; |
| 1844 | else if (*end < 0) |
| 1845 | *end += len; |
| 1846 | if (*end < 0) |
| 1847 | *end = 0; |
| 1848 | if (*start < 0) |
| 1849 | *start += len; |
| 1850 | if (*start < 0) |
| 1851 | *start = 0; |
| 1852 | } |
| 1853 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 1854 | Py_LOCAL_INLINE(Py_ssize_t) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1855 | string_find_internal(PyStringObject *self, PyObject *args, int dir) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1856 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1857 | PyObject *subobj; |
Fredrik Lundh | 0b7ef46 | 2006-05-27 15:26:19 +0000 | [diff] [blame] | 1858 | const char *sub; |
| 1859 | Py_ssize_t sub_len; |
| 1860 | Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1861 | |
Fredrik Lundh | 0b7ef46 | 2006-05-27 15:26:19 +0000 | [diff] [blame] | 1862 | if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", &subobj, |
| 1863 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1864 | return -2; |
| 1865 | if (PyString_Check(subobj)) { |
| 1866 | sub = PyString_AS_STRING(subobj); |
Fredrik Lundh | 0b7ef46 | 2006-05-27 15:26:19 +0000 | [diff] [blame] | 1867 | sub_len = PyString_GET_SIZE(subobj); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1868 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1869 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1870 | else if (PyUnicode_Check(subobj)) |
Fredrik Lundh | 0b7ef46 | 2006-05-27 15:26:19 +0000 | [diff] [blame] | 1871 | return PyUnicode_Find( |
| 1872 | (PyObject *)self, subobj, start, end, dir); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1873 | #endif |
Fredrik Lundh | 0b7ef46 | 2006-05-27 15:26:19 +0000 | [diff] [blame] | 1874 | else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len)) |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 1875 | /* XXX - the "expected a character buffer object" is pretty |
| 1876 | confusing for a non-expert. remap to something else ? */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1877 | return -2; |
| 1878 | |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 1879 | if (dir > 0) |
Fredrik Lundh | 0b7ef46 | 2006-05-27 15:26:19 +0000 | [diff] [blame] | 1880 | return stringlib_find_slice( |
| 1881 | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
| 1882 | sub, sub_len, start, end); |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 1883 | else |
Fredrik Lundh | 0b7ef46 | 2006-05-27 15:26:19 +0000 | [diff] [blame] | 1884 | return stringlib_rfind_slice( |
| 1885 | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
| 1886 | sub, sub_len, start, end); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1890 | PyDoc_STRVAR(find__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1891 | "S.find(sub [,start [,end]]) -> int\n\ |
| 1892 | \n\ |
| 1893 | Return the lowest index in S where substring sub is found,\n\ |
| 1894 | such that sub is contained within s[start,end]. Optional\n\ |
| 1895 | arguments start and end are interpreted as in slice notation.\n\ |
| 1896 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1897 | Return -1 on failure."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1898 | |
| 1899 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1900 | string_find(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1901 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1902 | Py_ssize_t result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1903 | if (result == -2) |
| 1904 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1905 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1909 | PyDoc_STRVAR(index__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1910 | "S.index(sub [,start [,end]]) -> int\n\ |
| 1911 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1912 | Like S.find() but raise ValueError when the substring is not found."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1913 | |
| 1914 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1915 | string_index(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1916 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1917 | Py_ssize_t result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1918 | if (result == -2) |
| 1919 | return NULL; |
| 1920 | if (result == -1) { |
| 1921 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 5d5e7c0 | 2003-01-15 05:32:57 +0000 | [diff] [blame] | 1922 | "substring not found"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1923 | return NULL; |
| 1924 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1925 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1929 | PyDoc_STRVAR(rfind__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1930 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 1931 | \n\ |
| 1932 | Return the highest index in S where substring sub is found,\n\ |
| 1933 | such that sub is contained within s[start,end]. Optional\n\ |
| 1934 | arguments start and end are interpreted as in slice notation.\n\ |
| 1935 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1936 | Return -1 on failure."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1937 | |
| 1938 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1939 | string_rfind(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1940 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1941 | Py_ssize_t result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1942 | if (result == -2) |
| 1943 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1944 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1948 | PyDoc_STRVAR(rindex__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1949 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 1950 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1951 | Like S.rfind() but raise ValueError when the substring is not found."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1952 | |
| 1953 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1954 | string_rindex(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1955 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1956 | Py_ssize_t result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1957 | if (result == -2) |
| 1958 | return NULL; |
| 1959 | if (result == -1) { |
| 1960 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 5d5e7c0 | 2003-01-15 05:32:57 +0000 | [diff] [blame] | 1961 | "substring not found"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1962 | return NULL; |
| 1963 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1964 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 1968 | Py_LOCAL_INLINE(PyObject *) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1969 | do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj) |
| 1970 | { |
| 1971 | char *s = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1972 | Py_ssize_t len = PyString_GET_SIZE(self); |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1973 | char *sep = PyString_AS_STRING(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1974 | Py_ssize_t seplen = PyString_GET_SIZE(sepobj); |
| 1975 | Py_ssize_t i, j; |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1976 | |
| 1977 | i = 0; |
| 1978 | if (striptype != RIGHTSTRIP) { |
| 1979 | while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) { |
| 1980 | i++; |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | j = len; |
| 1985 | if (striptype != LEFTSTRIP) { |
| 1986 | do { |
| 1987 | j--; |
| 1988 | } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen)); |
| 1989 | j++; |
| 1990 | } |
| 1991 | |
| 1992 | if (i == 0 && j == len && PyString_CheckExact(self)) { |
| 1993 | Py_INCREF(self); |
| 1994 | return (PyObject*)self; |
| 1995 | } |
| 1996 | else |
| 1997 | return PyString_FromStringAndSize(s+i, j-i); |
| 1998 | } |
| 1999 | |
| 2000 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2001 | Py_LOCAL_INLINE(PyObject *) |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2002 | do_strip(PyStringObject *self, int striptype) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2003 | { |
| 2004 | char *s = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2005 | Py_ssize_t len = PyString_GET_SIZE(self), i, j; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2006 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2007 | i = 0; |
| 2008 | if (striptype != RIGHTSTRIP) { |
| 2009 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
| 2010 | i++; |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | j = len; |
| 2015 | if (striptype != LEFTSTRIP) { |
| 2016 | do { |
| 2017 | j--; |
| 2018 | } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
| 2019 | j++; |
| 2020 | } |
| 2021 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2022 | if (i == 0 && j == len && PyString_CheckExact(self)) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2023 | Py_INCREF(self); |
| 2024 | return (PyObject*)self; |
| 2025 | } |
| 2026 | else |
| 2027 | return PyString_FromStringAndSize(s+i, j-i); |
| 2028 | } |
| 2029 | |
| 2030 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2031 | Py_LOCAL_INLINE(PyObject *) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2032 | do_argstrip(PyStringObject *self, int striptype, PyObject *args) |
| 2033 | { |
| 2034 | PyObject *sep = NULL; |
| 2035 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 2036 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2037 | return NULL; |
| 2038 | |
| 2039 | if (sep != NULL && sep != Py_None) { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 2040 | if (PyString_Check(sep)) |
| 2041 | return do_xstrip(self, striptype, sep); |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2042 | #ifdef Py_USING_UNICODE |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 2043 | else if (PyUnicode_Check(sep)) { |
| 2044 | PyObject *uniself = PyUnicode_FromObject((PyObject *)self); |
| 2045 | PyObject *res; |
| 2046 | if (uniself==NULL) |
| 2047 | return NULL; |
| 2048 | res = _PyUnicode_XStrip((PyUnicodeObject *)uniself, |
| 2049 | striptype, sep); |
| 2050 | Py_DECREF(uniself); |
| 2051 | return res; |
| 2052 | } |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2053 | #endif |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2054 | PyErr_Format(PyExc_TypeError, |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2055 | #ifdef Py_USING_UNICODE |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2056 | "%s arg must be None, str or unicode", |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2057 | #else |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2058 | "%s arg must be None or str", |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2059 | #endif |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2060 | STRIPNAME(striptype)); |
| 2061 | return NULL; |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
| 2064 | return do_strip(self, striptype); |
| 2065 | } |
| 2066 | |
| 2067 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2068 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2069 | "S.strip([chars]) -> string or unicode\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2070 | \n\ |
| 2071 | Return a copy of the string S with leading and trailing\n\ |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2072 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2073 | If chars is given and not None, remove characters in chars instead.\n\ |
| 2074 | If chars is unicode, S will be converted to unicode before stripping"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2075 | |
| 2076 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2077 | string_strip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2078 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2079 | if (PyTuple_GET_SIZE(args) == 0) |
| 2080 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 2081 | else |
| 2082 | return do_argstrip(self, BOTHSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2086 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2087 | "S.lstrip([chars]) -> string or unicode\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2088 | \n\ |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2089 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2090 | If chars is given and not None, remove characters in chars instead.\n\ |
| 2091 | If chars is unicode, S will be converted to unicode before stripping"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2092 | |
| 2093 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2094 | string_lstrip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2095 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2096 | if (PyTuple_GET_SIZE(args) == 0) |
| 2097 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 2098 | else |
| 2099 | return do_argstrip(self, LEFTSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2103 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2104 | "S.rstrip([chars]) -> string or unicode\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2105 | \n\ |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2106 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2107 | If chars is given and not None, remove characters in chars instead.\n\ |
| 2108 | If chars is unicode, S will be converted to unicode before stripping"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2109 | |
| 2110 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2111 | string_rstrip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2112 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2113 | if (PyTuple_GET_SIZE(args) == 0) |
| 2114 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 2115 | else |
| 2116 | return do_argstrip(self, RIGHTSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2120 | PyDoc_STRVAR(lower__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2121 | "S.lower() -> string\n\ |
| 2122 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2123 | Return a copy of the string S converted to lowercase."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2124 | |
Fredrik Lundh | dfe503d | 2006-05-25 16:10:12 +0000 | [diff] [blame] | 2125 | /* _tolower and _toupper are defined by SUSv2, but they're not ISO C */ |
| 2126 | #ifndef _tolower |
| 2127 | #define _tolower tolower |
| 2128 | #endif |
| 2129 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2130 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2131 | string_lower(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2132 | { |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2133 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2134 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2135 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2136 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2137 | newobj = PyString_FromStringAndSize(NULL, n); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2138 | if (!newobj) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2139 | return NULL; |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2140 | |
| 2141 | s = PyString_AS_STRING(newobj); |
| 2142 | |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2143 | Py_MEMCPY(s, PyString_AS_STRING(self), n); |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2144 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2145 | for (i = 0; i < n; i++) { |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2146 | int c = Py_CHARMASK(s[i]); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2147 | if (isupper(c)) |
| 2148 | s[i] = _tolower(c); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2149 | } |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2150 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2151 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2154 | PyDoc_STRVAR(upper__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2155 | "S.upper() -> string\n\ |
| 2156 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2157 | Return a copy of the string S converted to uppercase."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2158 | |
Fredrik Lundh | dfe503d | 2006-05-25 16:10:12 +0000 | [diff] [blame] | 2159 | #ifndef _toupper |
| 2160 | #define _toupper toupper |
| 2161 | #endif |
| 2162 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2163 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2164 | string_upper(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2165 | { |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2166 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2167 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2168 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2169 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2170 | newobj = PyString_FromStringAndSize(NULL, n); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2171 | if (!newobj) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2172 | return NULL; |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2173 | |
| 2174 | s = PyString_AS_STRING(newobj); |
| 2175 | |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2176 | Py_MEMCPY(s, PyString_AS_STRING(self), n); |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2177 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2178 | for (i = 0; i < n; i++) { |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2179 | int c = Py_CHARMASK(s[i]); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2180 | if (islower(c)) |
| 2181 | s[i] = _toupper(c); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2182 | } |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2183 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2184 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2187 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2188 | "S.title() -> string\n\ |
| 2189 | \n\ |
| 2190 | Return a titlecased version of S, i.e. words start with uppercase\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2191 | characters, all remaining cased characters have lowercase."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2192 | |
| 2193 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2194 | string_title(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2195 | { |
| 2196 | char *s = PyString_AS_STRING(self), *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2197 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2198 | int previous_is_cased = 0; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2199 | PyObject *newobj; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2200 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2201 | newobj = PyString_FromStringAndSize(NULL, n); |
| 2202 | if (newobj == NULL) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2203 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2204 | s_new = PyString_AsString(newobj); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2205 | for (i = 0; i < n; i++) { |
| 2206 | int c = Py_CHARMASK(*s++); |
| 2207 | if (islower(c)) { |
| 2208 | if (!previous_is_cased) |
| 2209 | c = toupper(c); |
| 2210 | previous_is_cased = 1; |
| 2211 | } else if (isupper(c)) { |
| 2212 | if (previous_is_cased) |
| 2213 | c = tolower(c); |
| 2214 | previous_is_cased = 1; |
| 2215 | } else |
| 2216 | previous_is_cased = 0; |
| 2217 | *s_new++ = c; |
| 2218 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2219 | return newobj; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2222 | PyDoc_STRVAR(capitalize__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2223 | "S.capitalize() -> string\n\ |
| 2224 | \n\ |
| 2225 | Return a copy of the string S with only its first character\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2226 | capitalized."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2227 | |
| 2228 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2229 | string_capitalize(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2230 | { |
| 2231 | char *s = PyString_AS_STRING(self), *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2232 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2233 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2234 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2235 | newobj = PyString_FromStringAndSize(NULL, n); |
| 2236 | if (newobj == NULL) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2237 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2238 | s_new = PyString_AsString(newobj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2239 | if (0 < n) { |
| 2240 | int c = Py_CHARMASK(*s++); |
| 2241 | if (islower(c)) |
| 2242 | *s_new = toupper(c); |
| 2243 | else |
| 2244 | *s_new = c; |
| 2245 | s_new++; |
| 2246 | } |
| 2247 | for (i = 1; i < n; i++) { |
| 2248 | int c = Py_CHARMASK(*s++); |
| 2249 | if (isupper(c)) |
| 2250 | *s_new = tolower(c); |
| 2251 | else |
| 2252 | *s_new = c; |
| 2253 | s_new++; |
| 2254 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2255 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2259 | PyDoc_STRVAR(count__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2260 | "S.count(sub[, start[, end]]) -> int\n\ |
| 2261 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 2262 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 2263 | string S[start:end]. Optional arguments start and end are interpreted\n\ |
| 2264 | as in slice notation."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2265 | |
| 2266 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2267 | string_count(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2268 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2269 | PyObject *sub_obj; |
| 2270 | const char *str = PyString_AS_STRING(self), *sub; |
| 2271 | Py_ssize_t sub_len; |
| 2272 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2273 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2274 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj, |
| 2275 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2276 | return NULL; |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 2277 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2278 | if (PyString_Check(sub_obj)) { |
| 2279 | sub = PyString_AS_STRING(sub_obj); |
| 2280 | sub_len = PyString_GET_SIZE(sub_obj); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2281 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2282 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2283 | else if (PyUnicode_Check(sub_obj)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2284 | Py_ssize_t count; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2285 | count = PyUnicode_Count((PyObject *)self, sub_obj, start, end); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2286 | if (count == -1) |
| 2287 | return NULL; |
| 2288 | else |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2289 | return PyInt_FromSsize_t(count); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2290 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2291 | #endif |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2292 | else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2293 | return NULL; |
| 2294 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2295 | string_adjust_indices(&start, &end, PyString_GET_SIZE(self)); |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 2296 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2297 | return PyInt_FromSsize_t( |
| 2298 | stringlib_count(str + start, end - start, sub, sub_len) |
| 2299 | ); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2302 | PyDoc_STRVAR(swapcase__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2303 | "S.swapcase() -> string\n\ |
| 2304 | \n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2305 | Return a copy of the string S with uppercase characters\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2306 | converted to lowercase and vice versa."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2307 | |
| 2308 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2309 | string_swapcase(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2310 | { |
| 2311 | char *s = PyString_AS_STRING(self), *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2312 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2313 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2314 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2315 | newobj = PyString_FromStringAndSize(NULL, n); |
| 2316 | if (newobj == NULL) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2317 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2318 | s_new = PyString_AsString(newobj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2319 | for (i = 0; i < n; i++) { |
| 2320 | int c = Py_CHARMASK(*s++); |
| 2321 | if (islower(c)) { |
| 2322 | *s_new = toupper(c); |
| 2323 | } |
| 2324 | else if (isupper(c)) { |
| 2325 | *s_new = tolower(c); |
| 2326 | } |
| 2327 | else |
| 2328 | *s_new = c; |
| 2329 | s_new++; |
| 2330 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2331 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2335 | PyDoc_STRVAR(translate__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2336 | "S.translate(table [,deletechars]) -> string\n\ |
| 2337 | \n\ |
| 2338 | Return a copy of the string S, where all characters occurring\n\ |
| 2339 | in the optional argument deletechars are removed, and the\n\ |
| 2340 | remaining characters have been mapped through the given\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2341 | translation table, which must be a string of length 256."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2342 | |
| 2343 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2344 | string_translate(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2345 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2346 | register char *input, *output; |
Raymond Hettinger | 4db5fe9 | 2007-04-12 04:10:00 +0000 | [diff] [blame] | 2347 | const char *table; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2348 | register Py_ssize_t i, c, changed = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2349 | PyObject *input_obj = (PyObject*)self; |
Raymond Hettinger | 4db5fe9 | 2007-04-12 04:10:00 +0000 | [diff] [blame] | 2350 | const char *output_start, *del_table=NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2351 | Py_ssize_t inlen, tablen, dellen = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2352 | PyObject *result; |
| 2353 | int trans_table[256]; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2354 | PyObject *tableobj, *delobj = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2355 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 2356 | if (!PyArg_UnpackTuple(args, "translate", 1, 2, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2357 | &tableobj, &delobj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2358 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2359 | |
| 2360 | if (PyString_Check(tableobj)) { |
Raymond Hettinger | 4db5fe9 | 2007-04-12 04:10:00 +0000 | [diff] [blame] | 2361 | table = PyString_AS_STRING(tableobj); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2362 | tablen = PyString_GET_SIZE(tableobj); |
| 2363 | } |
Raymond Hettinger | 4db5fe9 | 2007-04-12 04:10:00 +0000 | [diff] [blame] | 2364 | else if (tableobj == Py_None) { |
| 2365 | table = NULL; |
| 2366 | tablen = 256; |
| 2367 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2368 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2369 | else if (PyUnicode_Check(tableobj)) { |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2370 | /* Unicode .translate() does not support the deletechars |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2371 | parameter; instead a mapping to None will cause characters |
| 2372 | to be deleted. */ |
| 2373 | if (delobj != NULL) { |
| 2374 | PyErr_SetString(PyExc_TypeError, |
| 2375 | "deletions are implemented differently for unicode"); |
| 2376 | return NULL; |
| 2377 | } |
| 2378 | return PyUnicode_Translate((PyObject *)self, tableobj, NULL); |
| 2379 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2380 | #endif |
Raymond Hettinger | 4db5fe9 | 2007-04-12 04:10:00 +0000 | [diff] [blame] | 2381 | else if (PyObject_AsCharBuffer(tableobj, &table, &tablen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2382 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2383 | |
Martin v. Löwis | 00b6127 | 2002-12-12 20:03:19 +0000 | [diff] [blame] | 2384 | if (tablen != 256) { |
| 2385 | PyErr_SetString(PyExc_ValueError, |
| 2386 | "translation table must be 256 characters long"); |
| 2387 | return NULL; |
| 2388 | } |
| 2389 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2390 | if (delobj != NULL) { |
| 2391 | if (PyString_Check(delobj)) { |
| 2392 | del_table = PyString_AS_STRING(delobj); |
| 2393 | dellen = PyString_GET_SIZE(delobj); |
| 2394 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2395 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2396 | else if (PyUnicode_Check(delobj)) { |
| 2397 | PyErr_SetString(PyExc_TypeError, |
| 2398 | "deletions are implemented differently for unicode"); |
| 2399 | return NULL; |
| 2400 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2401 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2402 | else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen)) |
| 2403 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2404 | } |
| 2405 | else { |
| 2406 | del_table = NULL; |
| 2407 | dellen = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
Neal Norwitz | 2aa9a5d | 2006-03-20 01:53:23 +0000 | [diff] [blame] | 2410 | inlen = PyString_GET_SIZE(input_obj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2411 | result = PyString_FromStringAndSize((char *)NULL, inlen); |
| 2412 | if (result == NULL) |
| 2413 | return NULL; |
| 2414 | output_start = output = PyString_AsString(result); |
Neal Norwitz | 2aa9a5d | 2006-03-20 01:53:23 +0000 | [diff] [blame] | 2415 | input = PyString_AS_STRING(input_obj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2416 | |
Raymond Hettinger | 4db5fe9 | 2007-04-12 04:10:00 +0000 | [diff] [blame] | 2417 | if (dellen == 0 && table != NULL) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2418 | /* If no deletions are required, use faster code */ |
| 2419 | for (i = inlen; --i >= 0; ) { |
| 2420 | c = Py_CHARMASK(*input++); |
| 2421 | if (Py_CHARMASK((*output++ = table[c])) != c) |
| 2422 | changed = 1; |
| 2423 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2424 | if (changed || !PyString_CheckExact(input_obj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2425 | return result; |
| 2426 | Py_DECREF(result); |
| 2427 | Py_INCREF(input_obj); |
| 2428 | return input_obj; |
| 2429 | } |
| 2430 | |
Raymond Hettinger | 4db5fe9 | 2007-04-12 04:10:00 +0000 | [diff] [blame] | 2431 | if (table == NULL) { |
| 2432 | for (i = 0; i < 256; i++) |
| 2433 | trans_table[i] = Py_CHARMASK(i); |
| 2434 | } else { |
| 2435 | for (i = 0; i < 256; i++) |
| 2436 | trans_table[i] = Py_CHARMASK(table[i]); |
| 2437 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2438 | |
| 2439 | for (i = 0; i < dellen; i++) |
| 2440 | trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
| 2441 | |
| 2442 | for (i = inlen; --i >= 0; ) { |
| 2443 | c = Py_CHARMASK(*input++); |
| 2444 | if (trans_table[c] != -1) |
| 2445 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 2446 | continue; |
| 2447 | changed = 1; |
| 2448 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2449 | if (!changed && PyString_CheckExact(input_obj)) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2450 | Py_DECREF(result); |
| 2451 | Py_INCREF(input_obj); |
| 2452 | return input_obj; |
| 2453 | } |
| 2454 | /* Fix the size of the resulting string */ |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2455 | if (inlen > 0) |
| 2456 | _PyString_Resize(&result, output - output_start); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2457 | return result; |
| 2458 | } |
| 2459 | |
| 2460 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2461 | #define FORWARD 1 |
| 2462 | #define REVERSE -1 |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2463 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2464 | /* find and count characters and substrings */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2465 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2466 | #define findchar(target, target_len, c) \ |
| 2467 | ((char *)memchr((const void *)(target), c, target_len)) |
| 2468 | |
| 2469 | /* String ops must return a string. */ |
| 2470 | /* If the object is subclass of string, create a copy */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2471 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2472 | return_self(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2473 | { |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2474 | if (PyString_CheckExact(self)) { |
| 2475 | Py_INCREF(self); |
| 2476 | return self; |
| 2477 | } |
| 2478 | return (PyStringObject *)PyString_FromStringAndSize( |
| 2479 | PyString_AS_STRING(self), |
| 2480 | PyString_GET_SIZE(self)); |
| 2481 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2482 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2483 | Py_LOCAL_INLINE(Py_ssize_t) |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2484 | countchar(const char *target, int target_len, char c, Py_ssize_t maxcount) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2485 | { |
| 2486 | Py_ssize_t count=0; |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2487 | const char *start=target; |
| 2488 | const char *end=target+target_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2489 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2490 | while ( (start=findchar(start, end-start, c)) != NULL ) { |
| 2491 | count++; |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2492 | if (count >= maxcount) |
| 2493 | break; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2494 | start += 1; |
| 2495 | } |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2496 | return count; |
| 2497 | } |
| 2498 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2499 | Py_LOCAL(Py_ssize_t) |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2500 | findstring(const char *target, Py_ssize_t target_len, |
| 2501 | const char *pattern, Py_ssize_t pattern_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2502 | Py_ssize_t start, |
| 2503 | Py_ssize_t end, |
| 2504 | int direction) |
| 2505 | { |
| 2506 | if (start < 0) { |
| 2507 | start += target_len; |
| 2508 | if (start < 0) |
| 2509 | start = 0; |
| 2510 | } |
| 2511 | if (end > target_len) { |
| 2512 | end = target_len; |
| 2513 | } else if (end < 0) { |
| 2514 | end += target_len; |
| 2515 | if (end < 0) |
| 2516 | end = 0; |
| 2517 | } |
| 2518 | |
| 2519 | /* zero-length substrings always match at the first attempt */ |
| 2520 | if (pattern_len == 0) |
| 2521 | return (direction > 0) ? start : end; |
| 2522 | |
| 2523 | end -= pattern_len; |
| 2524 | |
| 2525 | if (direction < 0) { |
| 2526 | for (; end >= start; end--) |
| 2527 | if (Py_STRING_MATCH(target, end, pattern, pattern_len)) |
| 2528 | return end; |
| 2529 | } else { |
| 2530 | for (; start <= end; start++) |
| 2531 | if (Py_STRING_MATCH(target, start, pattern, pattern_len)) |
| 2532 | return start; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2533 | } |
| 2534 | return -1; |
| 2535 | } |
| 2536 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2537 | Py_LOCAL_INLINE(Py_ssize_t) |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2538 | countstring(const char *target, Py_ssize_t target_len, |
| 2539 | const char *pattern, Py_ssize_t pattern_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2540 | Py_ssize_t start, |
| 2541 | Py_ssize_t end, |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2542 | int direction, Py_ssize_t maxcount) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2543 | { |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2544 | Py_ssize_t count=0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2545 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2546 | if (start < 0) { |
| 2547 | start += target_len; |
| 2548 | if (start < 0) |
| 2549 | start = 0; |
| 2550 | } |
| 2551 | if (end > target_len) { |
| 2552 | end = target_len; |
| 2553 | } else if (end < 0) { |
| 2554 | end += target_len; |
| 2555 | if (end < 0) |
| 2556 | end = 0; |
| 2557 | } |
| 2558 | |
| 2559 | /* zero-length substrings match everywhere */ |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2560 | if (pattern_len == 0 || maxcount == 0) { |
| 2561 | if (target_len+1 < maxcount) |
| 2562 | return target_len+1; |
| 2563 | return maxcount; |
| 2564 | } |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2565 | |
| 2566 | end -= pattern_len; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2567 | if (direction < 0) { |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2568 | for (; (end >= start); end--) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2569 | if (Py_STRING_MATCH(target, end, pattern, pattern_len)) { |
| 2570 | count++; |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2571 | if (--maxcount <= 0) break; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2572 | end -= pattern_len-1; |
| 2573 | } |
| 2574 | } else { |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2575 | for (; (start <= end); start++) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2576 | if (Py_STRING_MATCH(target, start, pattern, pattern_len)) { |
| 2577 | count++; |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2578 | if (--maxcount <= 0) |
| 2579 | break; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2580 | start += pattern_len-1; |
| 2581 | } |
| 2582 | } |
| 2583 | return count; |
| 2584 | } |
| 2585 | |
| 2586 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2587 | /* Algorithms for different cases of string replacement */ |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2588 | |
| 2589 | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2590 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2591 | replace_interleave(PyStringObject *self, |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2592 | const char *to_s, Py_ssize_t to_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2593 | Py_ssize_t maxcount) |
| 2594 | { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2595 | char *self_s, *result_s; |
| 2596 | Py_ssize_t self_len, result_len; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2597 | Py_ssize_t count, i, product; |
| 2598 | PyStringObject *result; |
| 2599 | |
| 2600 | self_len = PyString_GET_SIZE(self); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2601 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2602 | /* 1 at the end plus 1 after every character */ |
| 2603 | count = self_len+1; |
| 2604 | if (maxcount < count) |
| 2605 | count = maxcount; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2606 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2607 | /* Check for overflow */ |
| 2608 | /* result_len = count * to_len + self_len; */ |
| 2609 | product = count * to_len; |
| 2610 | if (product / to_len != count) { |
| 2611 | PyErr_SetString(PyExc_OverflowError, |
| 2612 | "replace string is too long"); |
| 2613 | return NULL; |
| 2614 | } |
| 2615 | result_len = product + self_len; |
| 2616 | if (result_len < 0) { |
| 2617 | PyErr_SetString(PyExc_OverflowError, |
| 2618 | "replace string is too long"); |
| 2619 | return NULL; |
| 2620 | } |
| 2621 | |
| 2622 | if (! (result = (PyStringObject *) |
| 2623 | PyString_FromStringAndSize(NULL, result_len)) ) |
| 2624 | return NULL; |
| 2625 | |
| 2626 | self_s = PyString_AS_STRING(self); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2627 | result_s = PyString_AS_STRING(result); |
| 2628 | |
| 2629 | /* TODO: special case single character, which doesn't need memcpy */ |
| 2630 | |
| 2631 | /* Lay the first one down (guaranteed this will occur) */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2632 | Py_MEMCPY(result_s, to_s, to_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2633 | result_s += to_len; |
| 2634 | count -= 1; |
| 2635 | |
| 2636 | for (i=0; i<count; i++) { |
| 2637 | *result_s++ = *self_s++; |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2638 | Py_MEMCPY(result_s, to_s, to_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2639 | result_s += to_len; |
| 2640 | } |
| 2641 | |
| 2642 | /* Copy the rest of the original string */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2643 | Py_MEMCPY(result_s, self_s, self_len-i); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2644 | |
| 2645 | return result; |
| 2646 | } |
| 2647 | |
| 2648 | /* Special case for deleting a single character */ |
| 2649 | /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2650 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2651 | replace_delete_single_character(PyStringObject *self, |
| 2652 | char from_c, Py_ssize_t maxcount) |
| 2653 | { |
| 2654 | char *self_s, *result_s; |
| 2655 | char *start, *next, *end; |
| 2656 | Py_ssize_t self_len, result_len; |
| 2657 | Py_ssize_t count; |
| 2658 | PyStringObject *result; |
| 2659 | |
| 2660 | self_len = PyString_GET_SIZE(self); |
| 2661 | self_s = PyString_AS_STRING(self); |
| 2662 | |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2663 | count = countchar(self_s, self_len, from_c, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2664 | if (count == 0) { |
| 2665 | return return_self(self); |
| 2666 | } |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2667 | |
| 2668 | result_len = self_len - count; /* from_len == 1 */ |
| 2669 | assert(result_len>=0); |
| 2670 | |
| 2671 | if ( (result = (PyStringObject *) |
| 2672 | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
| 2673 | return NULL; |
| 2674 | result_s = PyString_AS_STRING(result); |
| 2675 | |
| 2676 | start = self_s; |
| 2677 | end = self_s + self_len; |
| 2678 | while (count-- > 0) { |
| 2679 | next = findchar(start, end-start, from_c); |
| 2680 | if (next == NULL) |
| 2681 | break; |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2682 | Py_MEMCPY(result_s, start, next-start); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2683 | result_s += (next-start); |
| 2684 | start = next+1; |
| 2685 | } |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2686 | Py_MEMCPY(result_s, start, end-start); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2687 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2688 | return result; |
| 2689 | } |
| 2690 | |
| 2691 | /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
| 2692 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2693 | Py_LOCAL(PyStringObject *) |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2694 | replace_delete_substring(PyStringObject *self, |
| 2695 | const char *from_s, Py_ssize_t from_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2696 | Py_ssize_t maxcount) { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2697 | char *self_s, *result_s; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2698 | char *start, *next, *end; |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2699 | Py_ssize_t self_len, result_len; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2700 | Py_ssize_t count, offset; |
| 2701 | PyStringObject *result; |
| 2702 | |
| 2703 | self_len = PyString_GET_SIZE(self); |
| 2704 | self_s = PyString_AS_STRING(self); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2705 | |
| 2706 | count = countstring(self_s, self_len, |
| 2707 | from_s, from_len, |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2708 | 0, self_len, 1, |
| 2709 | maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2710 | |
| 2711 | if (count == 0) { |
| 2712 | /* no matches */ |
| 2713 | return return_self(self); |
| 2714 | } |
| 2715 | |
| 2716 | result_len = self_len - (count * from_len); |
| 2717 | assert (result_len>=0); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2718 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2719 | if ( (result = (PyStringObject *) |
| 2720 | PyString_FromStringAndSize(NULL, result_len)) == NULL ) |
| 2721 | return NULL; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2722 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2723 | result_s = PyString_AS_STRING(result); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2724 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2725 | start = self_s; |
| 2726 | end = self_s + self_len; |
| 2727 | while (count-- > 0) { |
| 2728 | offset = findstring(start, end-start, |
| 2729 | from_s, from_len, |
| 2730 | 0, end-start, FORWARD); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2731 | if (offset == -1) |
| 2732 | break; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2733 | next = start + offset; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2734 | |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2735 | Py_MEMCPY(result_s, start, next-start); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2736 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2737 | result_s += (next-start); |
| 2738 | start = next+from_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2739 | } |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2740 | Py_MEMCPY(result_s, start, end-start); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2741 | return result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2742 | } |
| 2743 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2744 | /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2745 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2746 | replace_single_character_in_place(PyStringObject *self, |
| 2747 | char from_c, char to_c, |
| 2748 | Py_ssize_t maxcount) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2749 | { |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2750 | char *self_s, *result_s, *start, *end, *next; |
| 2751 | Py_ssize_t self_len; |
| 2752 | PyStringObject *result; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2753 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2754 | /* The result string will be the same size */ |
| 2755 | self_s = PyString_AS_STRING(self); |
| 2756 | self_len = PyString_GET_SIZE(self); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2757 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2758 | next = findchar(self_s, self_len, from_c); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2759 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2760 | if (next == NULL) { |
| 2761 | /* No matches; return the original string */ |
| 2762 | return return_self(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2763 | } |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2764 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2765 | /* Need to make a new string */ |
Andrew Dalke | 8c90910 | 2006-05-25 17:53:00 +0000 | [diff] [blame] | 2766 | result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2767 | if (result == NULL) |
| 2768 | return NULL; |
| 2769 | result_s = PyString_AS_STRING(result); |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2770 | Py_MEMCPY(result_s, self_s, self_len); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2771 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2772 | /* change everything in-place, starting with this one */ |
| 2773 | start = result_s + (next-self_s); |
| 2774 | *start = to_c; |
| 2775 | start++; |
| 2776 | end = result_s + self_len; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2777 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2778 | while (--maxcount > 0) { |
| 2779 | next = findchar(start, end-start, from_c); |
| 2780 | if (next == NULL) |
| 2781 | break; |
| 2782 | *next = to_c; |
| 2783 | start = next+1; |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 2784 | } |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2785 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2786 | return result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2787 | } |
| 2788 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2789 | /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2790 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2791 | replace_substring_in_place(PyStringObject *self, |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2792 | const char *from_s, Py_ssize_t from_len, |
| 2793 | const char *to_s, Py_ssize_t to_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2794 | Py_ssize_t maxcount) |
| 2795 | { |
| 2796 | char *result_s, *start, *end; |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2797 | char *self_s; |
| 2798 | Py_ssize_t self_len, offset; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2799 | PyStringObject *result; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2800 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2801 | /* The result string will be the same size */ |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2802 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2803 | self_s = PyString_AS_STRING(self); |
| 2804 | self_len = PyString_GET_SIZE(self); |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2805 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2806 | offset = findstring(self_s, self_len, |
| 2807 | from_s, from_len, |
| 2808 | 0, self_len, FORWARD); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2809 | if (offset == -1) { |
| 2810 | /* No matches; return the original string */ |
| 2811 | return return_self(self); |
| 2812 | } |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2813 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2814 | /* Need to make a new string */ |
Andrew Dalke | 8c90910 | 2006-05-25 17:53:00 +0000 | [diff] [blame] | 2815 | result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2816 | if (result == NULL) |
| 2817 | return NULL; |
| 2818 | result_s = PyString_AS_STRING(result); |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2819 | Py_MEMCPY(result_s, self_s, self_len); |
Andrew Dalke | 8c90910 | 2006-05-25 17:53:00 +0000 | [diff] [blame] | 2820 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2821 | /* change everything in-place, starting with this one */ |
| 2822 | start = result_s + offset; |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2823 | Py_MEMCPY(start, to_s, from_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2824 | start += from_len; |
| 2825 | end = result_s + self_len; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2826 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2827 | while ( --maxcount > 0) { |
| 2828 | offset = findstring(start, end-start, |
| 2829 | from_s, from_len, |
| 2830 | 0, end-start, FORWARD); |
| 2831 | if (offset==-1) |
| 2832 | break; |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2833 | Py_MEMCPY(start+offset, to_s, from_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2834 | start += offset+from_len; |
| 2835 | } |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2836 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2837 | return result; |
| 2838 | } |
| 2839 | |
| 2840 | /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2841 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2842 | replace_single_character(PyStringObject *self, |
| 2843 | char from_c, |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2844 | const char *to_s, Py_ssize_t to_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2845 | Py_ssize_t maxcount) |
| 2846 | { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2847 | char *self_s, *result_s; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2848 | char *start, *next, *end; |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2849 | Py_ssize_t self_len, result_len; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2850 | Py_ssize_t count, product; |
| 2851 | PyStringObject *result; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2852 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2853 | self_s = PyString_AS_STRING(self); |
| 2854 | self_len = PyString_GET_SIZE(self); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2855 | |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2856 | count = countchar(self_s, self_len, from_c, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2857 | if (count == 0) { |
| 2858 | /* no matches, return unchanged */ |
| 2859 | return return_self(self); |
| 2860 | } |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2861 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2862 | /* use the difference between current and new, hence the "-1" */ |
| 2863 | /* result_len = self_len + count * (to_len-1) */ |
| 2864 | product = count * (to_len-1); |
| 2865 | if (product / (to_len-1) != count) { |
| 2866 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 2867 | return NULL; |
| 2868 | } |
| 2869 | result_len = self_len + product; |
| 2870 | if (result_len < 0) { |
| 2871 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 2872 | return NULL; |
| 2873 | } |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2874 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2875 | if ( (result = (PyStringObject *) |
| 2876 | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
| 2877 | return NULL; |
| 2878 | result_s = PyString_AS_STRING(result); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2879 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2880 | start = self_s; |
| 2881 | end = self_s + self_len; |
| 2882 | while (count-- > 0) { |
| 2883 | next = findchar(start, end-start, from_c); |
| 2884 | if (next == NULL) |
| 2885 | break; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2886 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2887 | if (next == start) { |
| 2888 | /* replace with the 'to' */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2889 | Py_MEMCPY(result_s, to_s, to_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2890 | result_s += to_len; |
| 2891 | start += 1; |
| 2892 | } else { |
| 2893 | /* copy the unchanged old then the 'to' */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2894 | Py_MEMCPY(result_s, start, next-start); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2895 | result_s += (next-start); |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2896 | Py_MEMCPY(result_s, to_s, to_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2897 | result_s += to_len; |
| 2898 | start = next+1; |
| 2899 | } |
| 2900 | } |
| 2901 | /* Copy the remainder of the remaining string */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2902 | Py_MEMCPY(result_s, start, end-start); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2903 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2904 | return result; |
| 2905 | } |
| 2906 | |
| 2907 | /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2908 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2909 | replace_substring(PyStringObject *self, |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2910 | const char *from_s, Py_ssize_t from_len, |
| 2911 | const char *to_s, Py_ssize_t to_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2912 | Py_ssize_t maxcount) { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2913 | char *self_s, *result_s; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2914 | char *start, *next, *end; |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2915 | Py_ssize_t self_len, result_len; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2916 | Py_ssize_t count, offset, product; |
| 2917 | PyStringObject *result; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2918 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2919 | self_s = PyString_AS_STRING(self); |
| 2920 | self_len = PyString_GET_SIZE(self); |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2921 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2922 | count = countstring(self_s, self_len, |
| 2923 | from_s, from_len, |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2924 | 0, self_len, FORWARD, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2925 | if (count == 0) { |
| 2926 | /* no matches, return unchanged */ |
| 2927 | return return_self(self); |
| 2928 | } |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2929 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2930 | /* Check for overflow */ |
| 2931 | /* result_len = self_len + count * (to_len-from_len) */ |
| 2932 | product = count * (to_len-from_len); |
| 2933 | if (product / (to_len-from_len) != count) { |
| 2934 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 2935 | return NULL; |
| 2936 | } |
| 2937 | result_len = self_len + product; |
| 2938 | if (result_len < 0) { |
| 2939 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 2940 | return NULL; |
| 2941 | } |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2942 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2943 | if ( (result = (PyStringObject *) |
| 2944 | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
| 2945 | return NULL; |
| 2946 | result_s = PyString_AS_STRING(result); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2947 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2948 | start = self_s; |
| 2949 | end = self_s + self_len; |
| 2950 | while (count-- > 0) { |
| 2951 | offset = findstring(start, end-start, |
| 2952 | from_s, from_len, |
| 2953 | 0, end-start, FORWARD); |
| 2954 | if (offset == -1) |
| 2955 | break; |
| 2956 | next = start+offset; |
| 2957 | if (next == start) { |
| 2958 | /* replace with the 'to' */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2959 | Py_MEMCPY(result_s, to_s, to_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2960 | result_s += to_len; |
| 2961 | start += from_len; |
| 2962 | } else { |
| 2963 | /* copy the unchanged old then the 'to' */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2964 | Py_MEMCPY(result_s, start, next-start); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2965 | result_s += (next-start); |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2966 | Py_MEMCPY(result_s, to_s, to_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2967 | result_s += to_len; |
| 2968 | start = next+from_len; |
| 2969 | } |
| 2970 | } |
| 2971 | /* Copy the remainder of the remaining string */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2972 | Py_MEMCPY(result_s, start, end-start); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2973 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2974 | return result; |
| 2975 | } |
| 2976 | |
| 2977 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2978 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2979 | replace(PyStringObject *self, |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2980 | const char *from_s, Py_ssize_t from_len, |
| 2981 | const char *to_s, Py_ssize_t to_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2982 | Py_ssize_t maxcount) |
| 2983 | { |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2984 | if (maxcount < 0) { |
| 2985 | maxcount = PY_SSIZE_T_MAX; |
| 2986 | } else if (maxcount == 0 || PyString_GET_SIZE(self) == 0) { |
| 2987 | /* nothing to do; return the original string */ |
| 2988 | return return_self(self); |
| 2989 | } |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2990 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2991 | if (maxcount == 0 || |
| 2992 | (from_len == 0 && to_len == 0)) { |
| 2993 | /* nothing to do; return the original string */ |
| 2994 | return return_self(self); |
| 2995 | } |
| 2996 | |
| 2997 | /* Handle zero-length special cases */ |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2998 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2999 | if (from_len == 0) { |
| 3000 | /* insert the 'to' string everywhere. */ |
| 3001 | /* >>> "Python".replace("", ".") */ |
| 3002 | /* '.P.y.t.h.o.n.' */ |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3003 | return replace_interleave(self, to_s, to_len, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
| 3007 | /* point for an empty self string to generate a non-empty string */ |
| 3008 | /* Special case so the remaining code always gets a non-empty string */ |
| 3009 | if (PyString_GET_SIZE(self) == 0) { |
| 3010 | return return_self(self); |
| 3011 | } |
| 3012 | |
| 3013 | if (to_len == 0) { |
| 3014 | /* delete all occurances of 'from' string */ |
| 3015 | if (from_len == 1) { |
| 3016 | return replace_delete_single_character( |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3017 | self, from_s[0], maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3018 | } else { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3019 | return replace_delete_substring(self, from_s, from_len, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3020 | } |
| 3021 | } |
| 3022 | |
| 3023 | /* Handle special case where both strings have the same length */ |
| 3024 | |
| 3025 | if (from_len == to_len) { |
| 3026 | if (from_len == 1) { |
| 3027 | return replace_single_character_in_place( |
| 3028 | self, |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3029 | from_s[0], |
| 3030 | to_s[0], |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3031 | maxcount); |
| 3032 | } else { |
| 3033 | return replace_substring_in_place( |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3034 | self, from_s, from_len, to_s, to_len, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3035 | } |
| 3036 | } |
| 3037 | |
| 3038 | /* Otherwise use the more generic algorithms */ |
| 3039 | if (from_len == 1) { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3040 | return replace_single_character(self, from_s[0], |
| 3041 | to_s, to_len, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3042 | } else { |
| 3043 | /* len('from')>=2, len('to')>=1 */ |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3044 | return replace_substring(self, from_s, from_len, to_s, to_len, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3045 | } |
| 3046 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3047 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3048 | PyDoc_STRVAR(replace__doc__, |
Fred Drake | d22bb65 | 2003-10-22 02:56:40 +0000 | [diff] [blame] | 3049 | "S.replace (old, new[, count]) -> string\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3050 | \n\ |
| 3051 | Return a copy of string S with all occurrences of substring\n\ |
Fred Drake | d22bb65 | 2003-10-22 02:56:40 +0000 | [diff] [blame] | 3052 | old replaced by new. If the optional argument count is\n\ |
| 3053 | given, only the first count occurrences are replaced."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3054 | |
| 3055 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3056 | string_replace(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3057 | { |
Thomas Wouters | dc5f808 | 2006-04-19 15:38:01 +0000 | [diff] [blame] | 3058 | Py_ssize_t count = -1; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3059 | PyObject *from, *to; |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3060 | const char *from_s, *to_s; |
| 3061 | Py_ssize_t from_len, to_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3062 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3063 | if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3064 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3065 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3066 | if (PyString_Check(from)) { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3067 | from_s = PyString_AS_STRING(from); |
| 3068 | from_len = PyString_GET_SIZE(from); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3069 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3070 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3071 | if (PyUnicode_Check(from)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3072 | return PyUnicode_Replace((PyObject *)self, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3073 | from, to, count); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3074 | #endif |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3075 | else if (PyObject_AsCharBuffer(from, &from_s, &from_len)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3076 | return NULL; |
| 3077 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3078 | if (PyString_Check(to)) { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3079 | to_s = PyString_AS_STRING(to); |
| 3080 | to_len = PyString_GET_SIZE(to); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3081 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3082 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3083 | else if (PyUnicode_Check(to)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3084 | return PyUnicode_Replace((PyObject *)self, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3085 | from, to, count); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3086 | #endif |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3087 | else if (PyObject_AsCharBuffer(to, &to_s, &to_len)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3088 | return NULL; |
| 3089 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3090 | return (PyObject *)replace((PyStringObject *) self, |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3091 | from_s, from_len, |
| 3092 | to_s, to_len, count); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3095 | /** End DALKE **/ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3096 | |
Neal Norwitz | 8e6675a | 2006-06-11 05:47:14 +0000 | [diff] [blame] | 3097 | /* Matches the end (direction >= 0) or start (direction < 0) of self |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3098 | * against substr, using the start and end arguments. Returns |
| 3099 | * -1 on error, 0 if not found and 1 if found. |
| 3100 | */ |
| 3101 | Py_LOCAL(int) |
| 3102 | _string_tailmatch(PyStringObject *self, PyObject *substr, Py_ssize_t start, |
| 3103 | Py_ssize_t end, int direction) |
| 3104 | { |
| 3105 | Py_ssize_t len = PyString_GET_SIZE(self); |
| 3106 | Py_ssize_t slen; |
| 3107 | const char* sub; |
| 3108 | const char* str; |
| 3109 | |
| 3110 | if (PyString_Check(substr)) { |
| 3111 | sub = PyString_AS_STRING(substr); |
| 3112 | slen = PyString_GET_SIZE(substr); |
| 3113 | } |
| 3114 | #ifdef Py_USING_UNICODE |
| 3115 | else if (PyUnicode_Check(substr)) |
| 3116 | return PyUnicode_Tailmatch((PyObject *)self, |
| 3117 | substr, start, end, direction); |
| 3118 | #endif |
| 3119 | else if (PyObject_AsCharBuffer(substr, &sub, &slen)) |
| 3120 | return -1; |
| 3121 | str = PyString_AS_STRING(self); |
| 3122 | |
| 3123 | string_adjust_indices(&start, &end, len); |
| 3124 | |
| 3125 | if (direction < 0) { |
| 3126 | /* startswith */ |
| 3127 | if (start+slen > len) |
| 3128 | return 0; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3129 | } else { |
| 3130 | /* endswith */ |
| 3131 | if (end-start < slen || start > len) |
| 3132 | return 0; |
| 3133 | |
| 3134 | if (end-slen > start) |
| 3135 | start = end - slen; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3136 | } |
Neal Norwitz | 8e6675a | 2006-06-11 05:47:14 +0000 | [diff] [blame] | 3137 | if (end-start >= slen) |
| 3138 | return ! memcmp(str+start, sub, slen); |
| 3139 | return 0; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3140 | } |
| 3141 | |
| 3142 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3143 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3144 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3145 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 3146 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 3147 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3148 | With optional end, stop comparing S at that position.\n\ |
| 3149 | prefix can also be a tuple of strings to try."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3150 | |
| 3151 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3152 | string_startswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3153 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3154 | Py_ssize_t start = 0; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 3155 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3156 | PyObject *subobj; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3157 | int result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3158 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 3159 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 3160 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3161 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3162 | if (PyTuple_Check(subobj)) { |
| 3163 | Py_ssize_t i; |
| 3164 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 3165 | result = _string_tailmatch(self, |
| 3166 | PyTuple_GET_ITEM(subobj, i), |
| 3167 | start, end, -1); |
| 3168 | if (result == -1) |
| 3169 | return NULL; |
| 3170 | else if (result) { |
| 3171 | Py_RETURN_TRUE; |
| 3172 | } |
| 3173 | } |
| 3174 | Py_RETURN_FALSE; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3175 | } |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3176 | result = _string_tailmatch(self, subobj, start, end, -1); |
| 3177 | if (result == -1) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3178 | return NULL; |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 3179 | else |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3180 | return PyBool_FromLong(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3181 | } |
| 3182 | |
| 3183 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3184 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3185 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3186 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 3187 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 3188 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3189 | With optional end, stop comparing S at that position.\n\ |
| 3190 | suffix can also be a tuple of strings to try."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3191 | |
| 3192 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3193 | string_endswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3194 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3195 | Py_ssize_t start = 0; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 3196 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3197 | PyObject *subobj; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3198 | int result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3199 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 3200 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 3201 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3202 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3203 | if (PyTuple_Check(subobj)) { |
| 3204 | Py_ssize_t i; |
| 3205 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 3206 | result = _string_tailmatch(self, |
| 3207 | PyTuple_GET_ITEM(subobj, i), |
| 3208 | start, end, +1); |
| 3209 | if (result == -1) |
| 3210 | return NULL; |
| 3211 | else if (result) { |
| 3212 | Py_RETURN_TRUE; |
| 3213 | } |
| 3214 | } |
| 3215 | Py_RETURN_FALSE; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3216 | } |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3217 | result = _string_tailmatch(self, subobj, start, end, +1); |
| 3218 | if (result == -1) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3219 | return NULL; |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 3220 | else |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3221 | return PyBool_FromLong(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3222 | } |
| 3223 | |
| 3224 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3225 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3226 | "S.encode([encoding[,errors]]) -> object\n\ |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3227 | \n\ |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3228 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 3229 | to the default encoding. errors may be given to set a different error\n\ |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3230 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3231 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 3232 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 3233 | codecs.register_error that is able to handle UnicodeEncodeErrors."); |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3234 | |
| 3235 | static PyObject * |
| 3236 | string_encode(PyStringObject *self, PyObject *args) |
| 3237 | { |
| 3238 | char *encoding = NULL; |
| 3239 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3240 | PyObject *v; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 3241 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3242 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 3243 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3244 | v = PyString_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3245 | if (v == NULL) |
| 3246 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3247 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 3248 | PyErr_Format(PyExc_TypeError, |
| 3249 | "encoder did not return a string/unicode object " |
| 3250 | "(type=%.400s)", |
| 3251 | v->ob_type->tp_name); |
| 3252 | Py_DECREF(v); |
| 3253 | return NULL; |
| 3254 | } |
| 3255 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3256 | |
| 3257 | onError: |
| 3258 | return NULL; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3262 | PyDoc_STRVAR(decode__doc__, |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3263 | "S.decode([encoding[,errors]]) -> object\n\ |
| 3264 | \n\ |
| 3265 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 3266 | to the default encoding. errors may be given to set a different error\n\ |
| 3267 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3268 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 3269 | as well as any other name registerd with codecs.register_error that is\n\ |
| 3270 | able to handle UnicodeDecodeErrors."); |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3271 | |
| 3272 | static PyObject * |
| 3273 | string_decode(PyStringObject *self, PyObject *args) |
| 3274 | { |
| 3275 | char *encoding = NULL; |
| 3276 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3277 | PyObject *v; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 3278 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3279 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 3280 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3281 | v = PyString_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3282 | if (v == NULL) |
| 3283 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3284 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 3285 | PyErr_Format(PyExc_TypeError, |
| 3286 | "decoder did not return a string/unicode object " |
| 3287 | "(type=%.400s)", |
| 3288 | v->ob_type->tp_name); |
| 3289 | Py_DECREF(v); |
| 3290 | return NULL; |
| 3291 | } |
| 3292 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3293 | |
| 3294 | onError: |
| 3295 | return NULL; |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3299 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3300 | "S.expandtabs([tabsize]) -> string\n\ |
| 3301 | \n\ |
| 3302 | Return a copy of S where all tab characters are expanded using spaces.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3303 | If tabsize is not given, a tab size of 8 characters is assumed."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3304 | |
| 3305 | static PyObject* |
| 3306 | string_expandtabs(PyStringObject *self, PyObject *args) |
| 3307 | { |
| 3308 | const char *e, *p; |
| 3309 | char *q; |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 3310 | Py_ssize_t i, j, old_j; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3311 | PyObject *u; |
| 3312 | int tabsize = 8; |
| 3313 | |
| 3314 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 3315 | return NULL; |
| 3316 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 3317 | /* First pass: determine size of output string */ |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 3318 | i = j = old_j = 0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3319 | e = PyString_AS_STRING(self) + PyString_GET_SIZE(self); |
| 3320 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 3321 | if (*p == '\t') { |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 3322 | if (tabsize > 0) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3323 | j += tabsize - (j % tabsize); |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 3324 | if (old_j > j) { |
Neal Norwitz | 5c9a81a | 2007-06-11 02:16:10 +0000 | [diff] [blame] | 3325 | PyErr_SetString(PyExc_OverflowError, |
| 3326 | "new string is too long"); |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 3327 | return NULL; |
| 3328 | } |
| 3329 | old_j = j; |
| 3330 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3331 | } |
| 3332 | else { |
| 3333 | j++; |
| 3334 | if (*p == '\n' || *p == '\r') { |
| 3335 | i += j; |
Neal Norwitz | 5c9a81a | 2007-06-11 02:16:10 +0000 | [diff] [blame] | 3336 | old_j = j = 0; |
| 3337 | if (i < 0) { |
| 3338 | PyErr_SetString(PyExc_OverflowError, |
| 3339 | "new string is too long"); |
| 3340 | return NULL; |
| 3341 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3342 | } |
| 3343 | } |
| 3344 | |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 3345 | if ((i + j) < 0) { |
| 3346 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 3347 | return NULL; |
| 3348 | } |
| 3349 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3350 | /* Second pass: create output string and fill it */ |
| 3351 | u = PyString_FromStringAndSize(NULL, i + j); |
| 3352 | if (!u) |
| 3353 | return NULL; |
| 3354 | |
| 3355 | j = 0; |
| 3356 | q = PyString_AS_STRING(u); |
| 3357 | |
| 3358 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 3359 | if (*p == '\t') { |
| 3360 | if (tabsize > 0) { |
| 3361 | i = tabsize - (j % tabsize); |
| 3362 | j += i; |
| 3363 | while (i--) |
| 3364 | *q++ = ' '; |
| 3365 | } |
| 3366 | } |
| 3367 | else { |
| 3368 | j++; |
| 3369 | *q++ = *p; |
| 3370 | if (*p == '\n' || *p == '\r') |
| 3371 | j = 0; |
| 3372 | } |
| 3373 | |
| 3374 | return u; |
| 3375 | } |
| 3376 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 3377 | Py_LOCAL_INLINE(PyObject *) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3378 | pad(PyStringObject *self, Py_ssize_t left, Py_ssize_t right, char fill) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3379 | { |
| 3380 | PyObject *u; |
| 3381 | |
| 3382 | if (left < 0) |
| 3383 | left = 0; |
| 3384 | if (right < 0) |
| 3385 | right = 0; |
| 3386 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3387 | if (left == 0 && right == 0 && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3388 | Py_INCREF(self); |
| 3389 | return (PyObject *)self; |
| 3390 | } |
| 3391 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3392 | u = PyString_FromStringAndSize(NULL, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3393 | left + PyString_GET_SIZE(self) + right); |
| 3394 | if (u) { |
| 3395 | if (left) |
| 3396 | memset(PyString_AS_STRING(u), fill, left); |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 3397 | Py_MEMCPY(PyString_AS_STRING(u) + left, |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3398 | PyString_AS_STRING(self), |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3399 | PyString_GET_SIZE(self)); |
| 3400 | if (right) |
| 3401 | memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self), |
| 3402 | fill, right); |
| 3403 | } |
| 3404 | |
| 3405 | return u; |
| 3406 | } |
| 3407 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3408 | PyDoc_STRVAR(ljust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3409 | "S.ljust(width[, fillchar]) -> string\n" |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3410 | "\n" |
| 3411 | "Return S left justified in a string of length width. Padding is\n" |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3412 | "done using the specified fill character (default is a space)."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3413 | |
| 3414 | static PyObject * |
| 3415 | string_ljust(PyStringObject *self, PyObject *args) |
| 3416 | { |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3417 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3418 | char fillchar = ' '; |
| 3419 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3420 | if (!PyArg_ParseTuple(args, "n|c:ljust", &width, &fillchar)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3421 | return NULL; |
| 3422 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3423 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3424 | Py_INCREF(self); |
| 3425 | return (PyObject*) self; |
| 3426 | } |
| 3427 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3428 | return pad(self, 0, width - PyString_GET_SIZE(self), fillchar); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3432 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3433 | "S.rjust(width[, fillchar]) -> string\n" |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3434 | "\n" |
| 3435 | "Return S right justified in a string of length width. Padding is\n" |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3436 | "done using the specified fill character (default is a space)"); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3437 | |
| 3438 | static PyObject * |
| 3439 | string_rjust(PyStringObject *self, PyObject *args) |
| 3440 | { |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3441 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3442 | char fillchar = ' '; |
| 3443 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3444 | if (!PyArg_ParseTuple(args, "n|c:rjust", &width, &fillchar)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3445 | return NULL; |
| 3446 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3447 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3448 | Py_INCREF(self); |
| 3449 | return (PyObject*) self; |
| 3450 | } |
| 3451 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3452 | return pad(self, width - PyString_GET_SIZE(self), 0, fillchar); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3453 | } |
| 3454 | |
| 3455 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3456 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3457 | "S.center(width[, fillchar]) -> string\n" |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3458 | "\n" |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3459 | "Return S centered in a string of length width. Padding is\n" |
| 3460 | "done using the specified fill character (default is a space)"); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3461 | |
| 3462 | static PyObject * |
| 3463 | string_center(PyStringObject *self, PyObject *args) |
| 3464 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3465 | Py_ssize_t marg, left; |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3466 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3467 | char fillchar = ' '; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3468 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3469 | if (!PyArg_ParseTuple(args, "n|c:center", &width, &fillchar)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3470 | return NULL; |
| 3471 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3472 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3473 | Py_INCREF(self); |
| 3474 | return (PyObject*) self; |
| 3475 | } |
| 3476 | |
| 3477 | marg = width - PyString_GET_SIZE(self); |
| 3478 | left = marg / 2 + (marg & width & 1); |
| 3479 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3480 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3483 | PyDoc_STRVAR(zfill__doc__, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3484 | "S.zfill(width) -> string\n" |
| 3485 | "\n" |
| 3486 | "Pad a numeric string S with zeros on the left, to fill a field\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3487 | "of the specified width. The string S is never truncated."); |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3488 | |
| 3489 | static PyObject * |
| 3490 | string_zfill(PyStringObject *self, PyObject *args) |
| 3491 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3492 | Py_ssize_t fill; |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3493 | PyObject *s; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3494 | char *p; |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3495 | Py_ssize_t width; |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3496 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3497 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3498 | return NULL; |
| 3499 | |
| 3500 | if (PyString_GET_SIZE(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 3501 | if (PyString_CheckExact(self)) { |
| 3502 | Py_INCREF(self); |
| 3503 | return (PyObject*) self; |
| 3504 | } |
| 3505 | else |
| 3506 | return PyString_FromStringAndSize( |
| 3507 | PyString_AS_STRING(self), |
| 3508 | PyString_GET_SIZE(self) |
| 3509 | ); |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | fill = width - PyString_GET_SIZE(self); |
| 3513 | |
| 3514 | s = pad(self, fill, 0, '0'); |
| 3515 | |
| 3516 | if (s == NULL) |
| 3517 | return NULL; |
| 3518 | |
| 3519 | p = PyString_AS_STRING(s); |
| 3520 | if (p[fill] == '+' || p[fill] == '-') { |
| 3521 | /* move sign to beginning of string */ |
| 3522 | p[0] = p[fill]; |
| 3523 | p[fill] = '0'; |
| 3524 | } |
| 3525 | |
| 3526 | return (PyObject*) s; |
| 3527 | } |
| 3528 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3529 | PyDoc_STRVAR(isspace__doc__, |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3530 | "S.isspace() -> bool\n\ |
| 3531 | \n\ |
| 3532 | Return True if all characters in S are whitespace\n\ |
| 3533 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3534 | |
| 3535 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3536 | string_isspace(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3537 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3538 | register const unsigned char *p |
| 3539 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3540 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3541 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3542 | /* Shortcut for single character strings */ |
| 3543 | if (PyString_GET_SIZE(self) == 1 && |
| 3544 | isspace(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3545 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3546 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3547 | /* Special case for empty strings */ |
| 3548 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3549 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3550 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3551 | e = p + PyString_GET_SIZE(self); |
| 3552 | for (; p < e; p++) { |
| 3553 | if (!isspace(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3554 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3555 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3556 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3560 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3561 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3562 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3563 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3564 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3565 | |
| 3566 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3567 | string_isalpha(PyStringObject *self) |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3568 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3569 | register const unsigned char *p |
| 3570 | = (unsigned char *) PyString_AS_STRING(self); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3571 | register const unsigned char *e; |
| 3572 | |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3573 | /* Shortcut for single character strings */ |
| 3574 | if (PyString_GET_SIZE(self) == 1 && |
| 3575 | isalpha(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3576 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3577 | |
| 3578 | /* Special case for empty strings */ |
| 3579 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3580 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3581 | |
| 3582 | e = p + PyString_GET_SIZE(self); |
| 3583 | for (; p < e; p++) { |
| 3584 | if (!isalpha(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3585 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3586 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3587 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3588 | } |
| 3589 | |
| 3590 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3591 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3592 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3593 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3594 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3595 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3596 | |
| 3597 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3598 | string_isalnum(PyStringObject *self) |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3599 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3600 | register const unsigned char *p |
| 3601 | = (unsigned char *) PyString_AS_STRING(self); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3602 | register const unsigned char *e; |
| 3603 | |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3604 | /* Shortcut for single character strings */ |
| 3605 | if (PyString_GET_SIZE(self) == 1 && |
| 3606 | isalnum(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3607 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3608 | |
| 3609 | /* Special case for empty strings */ |
| 3610 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3611 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3612 | |
| 3613 | e = p + PyString_GET_SIZE(self); |
| 3614 | for (; p < e; p++) { |
| 3615 | if (!isalnum(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3616 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3617 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3618 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3619 | } |
| 3620 | |
| 3621 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3622 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3623 | "S.isdigit() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3624 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3625 | Return True if all characters in S are digits\n\ |
| 3626 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3627 | |
| 3628 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3629 | string_isdigit(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3630 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3631 | register const unsigned char *p |
| 3632 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3633 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3634 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3635 | /* Shortcut for single character strings */ |
| 3636 | if (PyString_GET_SIZE(self) == 1 && |
| 3637 | isdigit(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3638 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3639 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3640 | /* Special case for empty strings */ |
| 3641 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3642 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3643 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3644 | e = p + PyString_GET_SIZE(self); |
| 3645 | for (; p < e; p++) { |
| 3646 | if (!isdigit(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3647 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3648 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3649 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
| 3652 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3653 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3654 | "S.islower() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3655 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3656 | Return True if all cased characters in S are lowercase and there is\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3657 | at least one cased character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3658 | |
| 3659 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3660 | string_islower(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3661 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3662 | register const unsigned char *p |
| 3663 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3664 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3665 | int cased; |
| 3666 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3667 | /* Shortcut for single character strings */ |
| 3668 | if (PyString_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3669 | return PyBool_FromLong(islower(*p) != 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3670 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3671 | /* Special case for empty strings */ |
| 3672 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3673 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3674 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3675 | e = p + PyString_GET_SIZE(self); |
| 3676 | cased = 0; |
| 3677 | for (; p < e; p++) { |
| 3678 | if (isupper(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3679 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3680 | else if (!cased && islower(*p)) |
| 3681 | cased = 1; |
| 3682 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3683 | return PyBool_FromLong(cased); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3684 | } |
| 3685 | |
| 3686 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3687 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3688 | "S.isupper() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3689 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3690 | Return True if all cased characters in S are uppercase and there is\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3691 | at least one cased character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3692 | |
| 3693 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3694 | string_isupper(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3695 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3696 | register const unsigned char *p |
| 3697 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3698 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3699 | int cased; |
| 3700 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3701 | /* Shortcut for single character strings */ |
| 3702 | if (PyString_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3703 | return PyBool_FromLong(isupper(*p) != 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3704 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3705 | /* Special case for empty strings */ |
| 3706 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3707 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3708 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3709 | e = p + PyString_GET_SIZE(self); |
| 3710 | cased = 0; |
| 3711 | for (; p < e; p++) { |
| 3712 | if (islower(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3713 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3714 | else if (!cased && isupper(*p)) |
| 3715 | cased = 1; |
| 3716 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3717 | return PyBool_FromLong(cased); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
| 3720 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3721 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3722 | "S.istitle() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3723 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3724 | Return True if S is a titlecased string and there is at least one\n\ |
| 3725 | character in S, i.e. uppercase characters may only follow uncased\n\ |
| 3726 | characters and lowercase characters only cased ones. Return False\n\ |
| 3727 | otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3728 | |
| 3729 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3730 | string_istitle(PyStringObject *self, PyObject *uncased) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3731 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3732 | register const unsigned char *p |
| 3733 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3734 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3735 | int cased, previous_is_cased; |
| 3736 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3737 | /* Shortcut for single character strings */ |
| 3738 | if (PyString_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3739 | return PyBool_FromLong(isupper(*p) != 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3740 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3741 | /* Special case for empty strings */ |
| 3742 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3743 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3744 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3745 | e = p + PyString_GET_SIZE(self); |
| 3746 | cased = 0; |
| 3747 | previous_is_cased = 0; |
| 3748 | for (; p < e; p++) { |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3749 | register const unsigned char ch = *p; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3750 | |
| 3751 | if (isupper(ch)) { |
| 3752 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3753 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3754 | previous_is_cased = 1; |
| 3755 | cased = 1; |
| 3756 | } |
| 3757 | else if (islower(ch)) { |
| 3758 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3759 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3760 | previous_is_cased = 1; |
| 3761 | cased = 1; |
| 3762 | } |
| 3763 | else |
| 3764 | previous_is_cased = 0; |
| 3765 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3766 | return PyBool_FromLong(cased); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3767 | } |
| 3768 | |
| 3769 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3770 | PyDoc_STRVAR(splitlines__doc__, |
Fred Drake | 2bae4fa | 2001-10-13 15:57:55 +0000 | [diff] [blame] | 3771 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3772 | \n\ |
| 3773 | Return a list of the lines in S, breaking at line boundaries.\n\ |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3774 | Line breaks are not included in the resulting list unless keepends\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3775 | is given and true."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3776 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3777 | static PyObject* |
| 3778 | string_splitlines(PyStringObject *self, PyObject *args) |
| 3779 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3780 | register Py_ssize_t i; |
| 3781 | register Py_ssize_t j; |
| 3782 | Py_ssize_t len; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3783 | int keepends = 0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3784 | PyObject *list; |
| 3785 | PyObject *str; |
| 3786 | char *data; |
| 3787 | |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3788 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3789 | return NULL; |
| 3790 | |
| 3791 | data = PyString_AS_STRING(self); |
| 3792 | len = PyString_GET_SIZE(self); |
| 3793 | |
Andrew Dalke | 7e0a62e | 2006-05-26 22:49:03 +0000 | [diff] [blame] | 3794 | /* This does not use the preallocated list because splitlines is |
| 3795 | usually run with hundreds of newlines. The overhead of |
| 3796 | switching between PyList_SET_ITEM and append causes about a |
| 3797 | 2-3% slowdown for that common case. A smarter implementation |
| 3798 | could move the if check out, so the SET_ITEMs are done first |
| 3799 | and the appends only done when the prealloc buffer is full. |
| 3800 | That's too much work for little gain.*/ |
| 3801 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3802 | list = PyList_New(0); |
| 3803 | if (!list) |
| 3804 | goto onError; |
| 3805 | |
| 3806 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3807 | Py_ssize_t eol; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3808 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3809 | /* Find a line and append it */ |
| 3810 | while (i < len && data[i] != '\n' && data[i] != '\r') |
| 3811 | i++; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3812 | |
| 3813 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3814 | eol = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3815 | if (i < len) { |
| 3816 | if (data[i] == '\r' && i + 1 < len && |
| 3817 | data[i+1] == '\n') |
| 3818 | i += 2; |
| 3819 | else |
| 3820 | i++; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3821 | if (keepends) |
| 3822 | eol = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3823 | } |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3824 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3825 | j = i; |
| 3826 | } |
| 3827 | if (j < len) { |
| 3828 | SPLIT_APPEND(data, j, len); |
| 3829 | } |
| 3830 | |
| 3831 | return list; |
| 3832 | |
| 3833 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3834 | Py_XDECREF(list); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3835 | return NULL; |
| 3836 | } |
| 3837 | |
| 3838 | #undef SPLIT_APPEND |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 3839 | #undef SPLIT_ADD |
| 3840 | #undef MAX_PREALLOC |
| 3841 | #undef PREALLOC_SIZE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3842 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3843 | static PyObject * |
| 3844 | string_getnewargs(PyStringObject *v) |
| 3845 | { |
| 3846 | return Py_BuildValue("(s#)", v->ob_sval, v->ob_size); |
| 3847 | } |
| 3848 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3849 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3850 | static PyMethodDef |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3851 | string_methods[] = { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3852 | /* Counterparts of the obsolete stropmodule functions; except |
| 3853 | string.maketrans(). */ |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3854 | {"join", (PyCFunction)string_join, METH_O, join__doc__}, |
| 3855 | {"split", (PyCFunction)string_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 3856 | {"rsplit", (PyCFunction)string_rsplit, METH_VARARGS, rsplit__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3857 | {"lower", (PyCFunction)string_lower, METH_NOARGS, lower__doc__}, |
| 3858 | {"upper", (PyCFunction)string_upper, METH_NOARGS, upper__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3859 | {"islower", (PyCFunction)string_islower, METH_NOARGS, islower__doc__}, |
| 3860 | {"isupper", (PyCFunction)string_isupper, METH_NOARGS, isupper__doc__}, |
| 3861 | {"isspace", (PyCFunction)string_isspace, METH_NOARGS, isspace__doc__}, |
| 3862 | {"isdigit", (PyCFunction)string_isdigit, METH_NOARGS, isdigit__doc__}, |
| 3863 | {"istitle", (PyCFunction)string_istitle, METH_NOARGS, istitle__doc__}, |
| 3864 | {"isalpha", (PyCFunction)string_isalpha, METH_NOARGS, isalpha__doc__}, |
| 3865 | {"isalnum", (PyCFunction)string_isalnum, METH_NOARGS, isalnum__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3866 | {"capitalize", (PyCFunction)string_capitalize, METH_NOARGS, |
| 3867 | capitalize__doc__}, |
| 3868 | {"count", (PyCFunction)string_count, METH_VARARGS, count__doc__}, |
| 3869 | {"endswith", (PyCFunction)string_endswith, METH_VARARGS, |
| 3870 | endswith__doc__}, |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 3871 | {"partition", (PyCFunction)string_partition, METH_O, partition__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3872 | {"find", (PyCFunction)string_find, METH_VARARGS, find__doc__}, |
| 3873 | {"index", (PyCFunction)string_index, METH_VARARGS, index__doc__}, |
| 3874 | {"lstrip", (PyCFunction)string_lstrip, METH_VARARGS, lstrip__doc__}, |
| 3875 | {"replace", (PyCFunction)string_replace, METH_VARARGS, replace__doc__}, |
| 3876 | {"rfind", (PyCFunction)string_rfind, METH_VARARGS, rfind__doc__}, |
| 3877 | {"rindex", (PyCFunction)string_rindex, METH_VARARGS, rindex__doc__}, |
| 3878 | {"rstrip", (PyCFunction)string_rstrip, METH_VARARGS, rstrip__doc__}, |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 3879 | {"rpartition", (PyCFunction)string_rpartition, METH_O, |
| 3880 | rpartition__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3881 | {"startswith", (PyCFunction)string_startswith, METH_VARARGS, |
| 3882 | startswith__doc__}, |
| 3883 | {"strip", (PyCFunction)string_strip, METH_VARARGS, strip__doc__}, |
| 3884 | {"swapcase", (PyCFunction)string_swapcase, METH_NOARGS, |
| 3885 | swapcase__doc__}, |
| 3886 | {"translate", (PyCFunction)string_translate, METH_VARARGS, |
| 3887 | translate__doc__}, |
| 3888 | {"title", (PyCFunction)string_title, METH_NOARGS, title__doc__}, |
| 3889 | {"ljust", (PyCFunction)string_ljust, METH_VARARGS, ljust__doc__}, |
| 3890 | {"rjust", (PyCFunction)string_rjust, METH_VARARGS, rjust__doc__}, |
| 3891 | {"center", (PyCFunction)string_center, METH_VARARGS, center__doc__}, |
| 3892 | {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__}, |
| 3893 | {"encode", (PyCFunction)string_encode, METH_VARARGS, encode__doc__}, |
| 3894 | {"decode", (PyCFunction)string_decode, METH_VARARGS, decode__doc__}, |
| 3895 | {"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, |
| 3896 | expandtabs__doc__}, |
| 3897 | {"splitlines", (PyCFunction)string_splitlines, METH_VARARGS, |
| 3898 | splitlines__doc__}, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3899 | {"__getnewargs__", (PyCFunction)string_getnewargs, METH_NOARGS}, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3900 | {NULL, NULL} /* sentinel */ |
| 3901 | }; |
| 3902 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 3903 | static PyObject * |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3904 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 3905 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3906 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3907 | string_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3908 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3909 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 3910 | static char *kwlist[] = {"object", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3911 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3912 | if (type != &PyString_Type) |
| 3913 | return str_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3914 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x)) |
| 3915 | return NULL; |
| 3916 | if (x == NULL) |
| 3917 | return PyString_FromString(""); |
| 3918 | return PyObject_Str(x); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3919 | } |
| 3920 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3921 | static PyObject * |
| 3922 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3923 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3924 | PyObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3925 | Py_ssize_t n; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3926 | |
| 3927 | assert(PyType_IsSubtype(type, &PyString_Type)); |
| 3928 | tmp = string_new(&PyString_Type, args, kwds); |
| 3929 | if (tmp == NULL) |
| 3930 | return NULL; |
Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 3931 | assert(PyString_CheckExact(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3932 | n = PyString_GET_SIZE(tmp); |
| 3933 | pnew = type->tp_alloc(type, n); |
| 3934 | if (pnew != NULL) { |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 3935 | Py_MEMCPY(PyString_AS_STRING(pnew), PyString_AS_STRING(tmp), n+1); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3936 | ((PyStringObject *)pnew)->ob_shash = |
| 3937 | ((PyStringObject *)tmp)->ob_shash; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 3938 | ((PyStringObject *)pnew)->ob_sstate = SSTATE_NOT_INTERNED; |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3939 | } |
Guido van Rossum | 29d55a3 | 2001-08-31 16:11:15 +0000 | [diff] [blame] | 3940 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3941 | return pnew; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3942 | } |
| 3943 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 3944 | static PyObject * |
| 3945 | basestring_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3946 | { |
| 3947 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 32a7e7f | 2002-05-31 19:58:02 +0000 | [diff] [blame] | 3948 | "The basestring type cannot be instantiated"); |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 3949 | return NULL; |
| 3950 | } |
| 3951 | |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 3952 | static PyObject * |
| 3953 | string_mod(PyObject *v, PyObject *w) |
| 3954 | { |
| 3955 | if (!PyString_Check(v)) { |
| 3956 | Py_INCREF(Py_NotImplemented); |
| 3957 | return Py_NotImplemented; |
| 3958 | } |
| 3959 | return PyString_Format(v, w); |
| 3960 | } |
| 3961 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3962 | PyDoc_STRVAR(basestring_doc, |
| 3963 | "Type basestring cannot be instantiated; it is the base for str and unicode."); |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 3964 | |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 3965 | static PyNumberMethods string_as_number = { |
| 3966 | 0, /*nb_add*/ |
| 3967 | 0, /*nb_subtract*/ |
| 3968 | 0, /*nb_multiply*/ |
| 3969 | 0, /*nb_divide*/ |
| 3970 | string_mod, /*nb_remainder*/ |
| 3971 | }; |
| 3972 | |
| 3973 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 3974 | PyTypeObject PyBaseString_Type = { |
| 3975 | PyObject_HEAD_INIT(&PyType_Type) |
| 3976 | 0, |
Neal Norwitz | 32a7e7f | 2002-05-31 19:58:02 +0000 | [diff] [blame] | 3977 | "basestring", |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 3978 | 0, |
| 3979 | 0, |
| 3980 | 0, /* tp_dealloc */ |
| 3981 | 0, /* tp_print */ |
| 3982 | 0, /* tp_getattr */ |
| 3983 | 0, /* tp_setattr */ |
| 3984 | 0, /* tp_compare */ |
| 3985 | 0, /* tp_repr */ |
| 3986 | 0, /* tp_as_number */ |
| 3987 | 0, /* tp_as_sequence */ |
| 3988 | 0, /* tp_as_mapping */ |
| 3989 | 0, /* tp_hash */ |
| 3990 | 0, /* tp_call */ |
| 3991 | 0, /* tp_str */ |
| 3992 | 0, /* tp_getattro */ |
| 3993 | 0, /* tp_setattro */ |
| 3994 | 0, /* tp_as_buffer */ |
| 3995 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3996 | basestring_doc, /* tp_doc */ |
| 3997 | 0, /* tp_traverse */ |
| 3998 | 0, /* tp_clear */ |
| 3999 | 0, /* tp_richcompare */ |
| 4000 | 0, /* tp_weaklistoffset */ |
| 4001 | 0, /* tp_iter */ |
| 4002 | 0, /* tp_iternext */ |
| 4003 | 0, /* tp_methods */ |
| 4004 | 0, /* tp_members */ |
| 4005 | 0, /* tp_getset */ |
| 4006 | &PyBaseObject_Type, /* tp_base */ |
| 4007 | 0, /* tp_dict */ |
| 4008 | 0, /* tp_descr_get */ |
| 4009 | 0, /* tp_descr_set */ |
| 4010 | 0, /* tp_dictoffset */ |
| 4011 | 0, /* tp_init */ |
| 4012 | 0, /* tp_alloc */ |
| 4013 | basestring_new, /* tp_new */ |
| 4014 | 0, /* tp_free */ |
| 4015 | }; |
| 4016 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4017 | PyDoc_STRVAR(string_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4018 | "str(object) -> string\n\ |
| 4019 | \n\ |
| 4020 | Return a nice string representation of the object.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4021 | If the argument is a string, the return value is the same object."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 4022 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4023 | PyTypeObject PyString_Type = { |
| 4024 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4025 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4026 | "str", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4027 | sizeof(PyStringObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4028 | sizeof(char), |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 4029 | string_dealloc, /* tp_dealloc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4030 | (printfunc)string_print, /* tp_print */ |
| 4031 | 0, /* tp_getattr */ |
| 4032 | 0, /* tp_setattr */ |
| 4033 | 0, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 4034 | string_repr, /* tp_repr */ |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 4035 | &string_as_number, /* tp_as_number */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4036 | &string_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 4037 | &string_as_mapping, /* tp_as_mapping */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4038 | (hashfunc)string_hash, /* tp_hash */ |
| 4039 | 0, /* tp_call */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 4040 | string_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4041 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4042 | 0, /* tp_setattro */ |
| 4043 | &string_as_buffer, /* tp_as_buffer */ |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 4044 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 4045 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_STRING_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4046 | string_doc, /* tp_doc */ |
| 4047 | 0, /* tp_traverse */ |
| 4048 | 0, /* tp_clear */ |
| 4049 | (richcmpfunc)string_richcompare, /* tp_richcompare */ |
| 4050 | 0, /* tp_weaklistoffset */ |
| 4051 | 0, /* tp_iter */ |
| 4052 | 0, /* tp_iternext */ |
| 4053 | string_methods, /* tp_methods */ |
| 4054 | 0, /* tp_members */ |
| 4055 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 4056 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4057 | 0, /* tp_dict */ |
| 4058 | 0, /* tp_descr_get */ |
| 4059 | 0, /* tp_descr_set */ |
| 4060 | 0, /* tp_dictoffset */ |
| 4061 | 0, /* tp_init */ |
| 4062 | 0, /* tp_alloc */ |
| 4063 | string_new, /* tp_new */ |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 4064 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4065 | }; |
| 4066 | |
| 4067 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4068 | PyString_Concat(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4069 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4070 | register PyObject *v; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4071 | if (*pv == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4072 | return; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4073 | if (w == NULL || !PyString_Check(*pv)) { |
| 4074 | Py_DECREF(*pv); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4075 | *pv = NULL; |
| 4076 | return; |
| 4077 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4078 | v = string_concat((PyStringObject *) *pv, w); |
| 4079 | Py_DECREF(*pv); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4080 | *pv = v; |
| 4081 | } |
| 4082 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4083 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4084 | PyString_ConcatAndDel(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4085 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4086 | PyString_Concat(pv, w); |
| 4087 | Py_XDECREF(w); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4088 | } |
| 4089 | |
| 4090 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4091 | /* The following function breaks the notion that strings are immutable: |
| 4092 | it changes the size of a string. We get away with this only if there |
| 4093 | is only one module referencing the object. You can also think of it |
| 4094 | as creating a new string object and destroying the old one, only |
| 4095 | more efficiently. In any case, don't use this if the string may |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 4096 | already be known to some other part of the code... |
| 4097 | Note that if there's not enough memory to resize the string, the original |
| 4098 | string object at *pv is deallocated, *pv is set to NULL, an "out of |
| 4099 | memory" exception is set, and -1 is returned. Else (on success) 0 is |
| 4100 | returned, and the value in *pv may or may not be the same as on input. |
| 4101 | As always, an extra byte is allocated for a trailing \0 byte (newsize |
| 4102 | does *not* include that), and a trailing \0 byte is stored. |
| 4103 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4104 | |
| 4105 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4106 | _PyString_Resize(PyObject **pv, Py_ssize_t newsize) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4107 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4108 | register PyObject *v; |
| 4109 | register PyStringObject *sv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 4110 | v = *pv; |
Armin Rigo | 618fbf5 | 2004-08-07 20:58:32 +0000 | [diff] [blame] | 4111 | if (!PyString_Check(v) || v->ob_refcnt != 1 || newsize < 0 || |
| 4112 | PyString_CHECK_INTERNED(v)) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4113 | *pv = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4114 | Py_DECREF(v); |
| 4115 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 4116 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4117 | } |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 4118 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 4119 | _Py_DEC_REFTOTAL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4120 | _Py_ForgetReference(v); |
| 4121 | *pv = (PyObject *) |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 4122 | PyObject_REALLOC((char *)v, sizeof(PyStringObject) + newsize); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4123 | if (*pv == NULL) { |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 4124 | PyObject_Del(v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4125 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 4126 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4127 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4128 | _Py_NewReference(*pv); |
| 4129 | sv = (PyStringObject *) *pv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 4130 | sv->ob_size = newsize; |
| 4131 | sv->ob_sval[newsize] = '\0'; |
Raymond Hettinger | 561fbf1 | 2004-10-26 01:52:37 +0000 | [diff] [blame] | 4132 | sv->ob_shash = -1; /* invalidate cached hash value */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4133 | return 0; |
| 4134 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4135 | |
| 4136 | /* Helpers for formatstring */ |
| 4137 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 4138 | Py_LOCAL_INLINE(PyObject *) |
Thomas Wouters | 977485d | 2006-02-16 15:59:12 +0000 | [diff] [blame] | 4139 | getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4140 | { |
Thomas Wouters | 977485d | 2006-02-16 15:59:12 +0000 | [diff] [blame] | 4141 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4142 | if (argidx < arglen) { |
| 4143 | (*p_argidx)++; |
| 4144 | if (arglen < 0) |
| 4145 | return args; |
| 4146 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4147 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4148 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4149 | PyErr_SetString(PyExc_TypeError, |
| 4150 | "not enough arguments for format string"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4151 | return NULL; |
| 4152 | } |
| 4153 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4154 | /* Format codes |
| 4155 | * F_LJUST '-' |
| 4156 | * F_SIGN '+' |
| 4157 | * F_BLANK ' ' |
| 4158 | * F_ALT '#' |
| 4159 | * F_ZERO '0' |
| 4160 | */ |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4161 | #define F_LJUST (1<<0) |
| 4162 | #define F_SIGN (1<<1) |
| 4163 | #define F_BLANK (1<<2) |
| 4164 | #define F_ALT (1<<3) |
| 4165 | #define F_ZERO (1<<4) |
| 4166 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 4167 | Py_LOCAL_INLINE(int) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4168 | formatfloat(char *buf, size_t buflen, int flags, |
| 4169 | int prec, int type, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4170 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4171 | /* fmt = '%#.' + `prec` + `type` |
| 4172 | worst case length = 3 + 10 (len of INT_MAX) + 1 = 14 (use 20)*/ |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4173 | char fmt[20]; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4174 | double x; |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4175 | x = PyFloat_AsDouble(v); |
| 4176 | if (x == -1.0 && PyErr_Occurred()) { |
Georg Brandl | 283a135 | 2006-11-19 08:48:30 +0000 | [diff] [blame] | 4177 | PyErr_Format(PyExc_TypeError, "float argument required, " |
| 4178 | "not %.200s", v->ob_type->tp_name); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4179 | return -1; |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4180 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4181 | if (prec < 0) |
| 4182 | prec = 6; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4183 | if (type == 'f' && fabs(x)/1e25 >= 1e25) |
| 4184 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4185 | /* Worst case length calc to ensure no buffer overrun: |
| 4186 | |
| 4187 | 'g' formats: |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4188 | fmt = %#.<prec>g |
| 4189 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4190 | for any double rep.) |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4191 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4192 | |
| 4193 | 'f' formats: |
| 4194 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 4195 | len = 1 + 50 + 1 + prec = 52 + prec |
| 4196 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4197 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 4198 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4199 | |
| 4200 | */ |
Georg Brandl | 7c3b50d | 2007-07-12 08:38:00 +0000 | [diff] [blame^] | 4201 | if (((type == 'g' || type == 'G') && |
| 4202 | buflen <= (size_t)10 + (size_t)prec) || |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4203 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4204 | PyErr_SetString(PyExc_OverflowError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4205 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4206 | return -1; |
| 4207 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4208 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 4209 | (flags&F_ALT) ? "#" : "", |
| 4210 | prec, type); |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 4211 | PyOS_ascii_formatd(buf, buflen, fmt, x); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4212 | return (int)strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4213 | } |
| 4214 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4215 | /* _PyString_FormatLong emulates the format codes d, u, o, x and X, and |
| 4216 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 4217 | * Python's regular ints. |
| 4218 | * Return value: a new PyString*, or NULL if error. |
| 4219 | * . *pbuf is set to point into it, |
| 4220 | * *plen set to the # of chars following that. |
| 4221 | * Caller must decref it when done using pbuf. |
| 4222 | * The string starting at *pbuf is of the form |
| 4223 | * "-"? ("0x" | "0X")? digit+ |
| 4224 | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4225 | * set in flags. The case of hex digits will be correct, |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4226 | * There will be at least prec digits, zero-filled on the left if |
| 4227 | * necessary to get that many. |
| 4228 | * val object to be converted |
| 4229 | * flags bitmask of format flags; only F_ALT is looked at |
| 4230 | * prec minimum number of digits; 0-fill on left if needed |
| 4231 | * type a character in [duoxX]; u acts the same as d |
| 4232 | * |
| 4233 | * CAUTION: o, x and X conversions on regular ints can never |
| 4234 | * produce a '-' sign, but can for Python's unbounded ints. |
| 4235 | */ |
| 4236 | PyObject* |
| 4237 | _PyString_FormatLong(PyObject *val, int flags, int prec, int type, |
| 4238 | char **pbuf, int *plen) |
| 4239 | { |
| 4240 | PyObject *result = NULL; |
| 4241 | char *buf; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4242 | Py_ssize_t i; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4243 | int sign; /* 1 if '-', else 0 */ |
| 4244 | int len; /* number of characters */ |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4245 | Py_ssize_t llen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4246 | int numdigits; /* len == numnondigits + numdigits */ |
| 4247 | int numnondigits = 0; |
| 4248 | |
| 4249 | switch (type) { |
| 4250 | case 'd': |
| 4251 | case 'u': |
| 4252 | result = val->ob_type->tp_str(val); |
| 4253 | break; |
| 4254 | case 'o': |
| 4255 | result = val->ob_type->tp_as_number->nb_oct(val); |
| 4256 | break; |
| 4257 | case 'x': |
| 4258 | case 'X': |
| 4259 | numnondigits = 2; |
| 4260 | result = val->ob_type->tp_as_number->nb_hex(val); |
| 4261 | break; |
| 4262 | default: |
| 4263 | assert(!"'type' not in [duoxX]"); |
| 4264 | } |
| 4265 | if (!result) |
| 4266 | return NULL; |
| 4267 | |
Neal Norwitz | 56423e5 | 2006-08-13 18:11:08 +0000 | [diff] [blame] | 4268 | buf = PyString_AsString(result); |
Georg Brandl | 26a07b5 | 2006-08-14 20:25:39 +0000 | [diff] [blame] | 4269 | if (!buf) { |
| 4270 | Py_DECREF(result); |
Neal Norwitz | 56423e5 | 2006-08-13 18:11:08 +0000 | [diff] [blame] | 4271 | return NULL; |
Georg Brandl | 26a07b5 | 2006-08-14 20:25:39 +0000 | [diff] [blame] | 4272 | } |
Neal Norwitz | 56423e5 | 2006-08-13 18:11:08 +0000 | [diff] [blame] | 4273 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4274 | /* To modify the string in-place, there can only be one reference. */ |
| 4275 | if (result->ob_refcnt != 1) { |
| 4276 | PyErr_BadInternalCall(); |
| 4277 | return NULL; |
| 4278 | } |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4279 | llen = PyString_Size(result); |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 4280 | if (llen > INT_MAX) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4281 | PyErr_SetString(PyExc_ValueError, "string too large in _PyString_FormatLong"); |
| 4282 | return NULL; |
| 4283 | } |
| 4284 | len = (int)llen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4285 | if (buf[len-1] == 'L') { |
| 4286 | --len; |
| 4287 | buf[len] = '\0'; |
| 4288 | } |
| 4289 | sign = buf[0] == '-'; |
| 4290 | numnondigits += sign; |
| 4291 | numdigits = len - numnondigits; |
| 4292 | assert(numdigits > 0); |
| 4293 | |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4294 | /* Get rid of base marker unless F_ALT */ |
| 4295 | if ((flags & F_ALT) == 0) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4296 | /* Need to skip 0x, 0X or 0. */ |
| 4297 | int skipped = 0; |
| 4298 | switch (type) { |
| 4299 | case 'o': |
| 4300 | assert(buf[sign] == '0'); |
| 4301 | /* If 0 is only digit, leave it alone. */ |
| 4302 | if (numdigits > 1) { |
| 4303 | skipped = 1; |
| 4304 | --numdigits; |
| 4305 | } |
| 4306 | break; |
| 4307 | case 'x': |
| 4308 | case 'X': |
| 4309 | assert(buf[sign] == '0'); |
| 4310 | assert(buf[sign + 1] == 'x'); |
| 4311 | skipped = 2; |
| 4312 | numnondigits -= 2; |
| 4313 | break; |
| 4314 | } |
| 4315 | if (skipped) { |
| 4316 | buf += skipped; |
| 4317 | len -= skipped; |
| 4318 | if (sign) |
| 4319 | buf[0] = '-'; |
| 4320 | } |
| 4321 | assert(len == numnondigits + numdigits); |
| 4322 | assert(numdigits > 0); |
| 4323 | } |
| 4324 | |
| 4325 | /* Fill with leading zeroes to meet minimum width. */ |
| 4326 | if (prec > numdigits) { |
| 4327 | PyObject *r1 = PyString_FromStringAndSize(NULL, |
| 4328 | numnondigits + prec); |
| 4329 | char *b1; |
| 4330 | if (!r1) { |
| 4331 | Py_DECREF(result); |
| 4332 | return NULL; |
| 4333 | } |
| 4334 | b1 = PyString_AS_STRING(r1); |
| 4335 | for (i = 0; i < numnondigits; ++i) |
| 4336 | *b1++ = *buf++; |
| 4337 | for (i = 0; i < prec - numdigits; i++) |
| 4338 | *b1++ = '0'; |
| 4339 | for (i = 0; i < numdigits; i++) |
| 4340 | *b1++ = *buf++; |
| 4341 | *b1 = '\0'; |
| 4342 | Py_DECREF(result); |
| 4343 | result = r1; |
| 4344 | buf = PyString_AS_STRING(result); |
| 4345 | len = numnondigits + prec; |
| 4346 | } |
| 4347 | |
| 4348 | /* Fix up case for hex conversions. */ |
Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 4349 | if (type == 'X') { |
| 4350 | /* Need to convert all lower case letters to upper case. |
| 4351 | and need to convert 0x to 0X (and -0x to -0X). */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4352 | for (i = 0; i < len; i++) |
Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 4353 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 4354 | buf[i] -= 'a'-'A'; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4355 | } |
| 4356 | *pbuf = buf; |
| 4357 | *plen = len; |
| 4358 | return result; |
| 4359 | } |
| 4360 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 4361 | Py_LOCAL_INLINE(int) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4362 | formatint(char *buf, size_t buflen, int flags, |
| 4363 | int prec, int type, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4364 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4365 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4366 | worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 4367 | + 1 + 1 = 24 */ |
| 4368 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4369 | char *sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4370 | long x; |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4371 | |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4372 | x = PyInt_AsLong(v); |
| 4373 | if (x == -1 && PyErr_Occurred()) { |
Georg Brandl | 283a135 | 2006-11-19 08:48:30 +0000 | [diff] [blame] | 4374 | PyErr_Format(PyExc_TypeError, "int argument required, not %.200s", |
| 4375 | v->ob_type->tp_name); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4376 | return -1; |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4377 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4378 | if (x < 0 && type == 'u') { |
| 4379 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 4380 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4381 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 4382 | sign = "-"; |
| 4383 | else |
| 4384 | sign = ""; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4385 | if (prec < 0) |
| 4386 | prec = 1; |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4387 | |
| 4388 | if ((flags & F_ALT) && |
| 4389 | (type == 'x' || type == 'X')) { |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4390 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4391 | * of issues that cause pain: |
| 4392 | * - when 0 is being converted, the C standard leaves off |
| 4393 | * the '0x' or '0X', which is inconsistent with other |
| 4394 | * %#x/%#X conversions and inconsistent with Python's |
| 4395 | * hex() function |
| 4396 | * - there are platforms that violate the standard and |
| 4397 | * convert 0 with the '0x' or '0X' |
| 4398 | * (Metrowerks, Compaq Tru64) |
| 4399 | * - there are platforms that give '0x' when converting |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4400 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4401 | * standard (OS/2 EMX) |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4402 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4403 | * We can achieve the desired consistency by inserting our |
| 4404 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 4405 | * of %#x/%#X. |
| 4406 | * |
| 4407 | * Note that this is the same approach as used in |
| 4408 | * formatint() in unicodeobject.c |
| 4409 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4410 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 4411 | sign, type, prec, type); |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4412 | } |
| 4413 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4414 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 4415 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4416 | prec, type); |
| 4417 | } |
| 4418 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4419 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 4420 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4421 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4422 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4423 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4424 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4425 | return -1; |
| 4426 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4427 | if (sign[0]) |
| 4428 | PyOS_snprintf(buf, buflen, fmt, -x); |
| 4429 | else |
| 4430 | PyOS_snprintf(buf, buflen, fmt, x); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4431 | return (int)strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4432 | } |
| 4433 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 4434 | Py_LOCAL_INLINE(int) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4435 | formatchar(char *buf, size_t buflen, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4436 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4437 | /* presume that the buffer is at least 2 characters long */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4438 | if (PyString_Check(v)) { |
| 4439 | if (!PyArg_Parse(v, "c;%c requires int or char", &buf[0])) |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4440 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4441 | } |
| 4442 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4443 | if (!PyArg_Parse(v, "b;%c requires int or char", &buf[0])) |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4444 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4445 | } |
| 4446 | buf[1] = '\0'; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4447 | return 1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4448 | } |
| 4449 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4450 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 4451 | |
| 4452 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 4453 | chars are formatted. XXX This is a magic number. Each formatting |
| 4454 | routine does bounds checking to ensure no overflow, but a better |
| 4455 | solution may be to malloc a buffer of appropriate size for each |
| 4456 | format. For now, the current solution is sufficient. |
| 4457 | */ |
| 4458 | #define FORMATBUFLEN (size_t)120 |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4459 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4460 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4461 | PyString_Format(PyObject *format, PyObject *args) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4462 | { |
| 4463 | char *fmt, *res; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4464 | Py_ssize_t arglen, argidx; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4465 | Py_ssize_t reslen, rescnt, fmtcnt; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4466 | int args_owned = 0; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4467 | PyObject *result, *orig_args; |
| 4468 | #ifdef Py_USING_UNICODE |
| 4469 | PyObject *v, *w; |
| 4470 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4471 | PyObject *dict = NULL; |
| 4472 | if (format == NULL || !PyString_Check(format) || args == NULL) { |
| 4473 | PyErr_BadInternalCall(); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4474 | return NULL; |
| 4475 | } |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4476 | orig_args = args; |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4477 | fmt = PyString_AS_STRING(format); |
| 4478 | fmtcnt = PyString_GET_SIZE(format); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 4479 | reslen = rescnt = fmtcnt + 100; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4480 | result = PyString_FromStringAndSize((char *)NULL, reslen); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4481 | if (result == NULL) |
| 4482 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4483 | res = PyString_AsString(result); |
| 4484 | if (PyTuple_Check(args)) { |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4485 | arglen = PyTuple_GET_SIZE(args); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4486 | argidx = 0; |
| 4487 | } |
| 4488 | else { |
| 4489 | arglen = -1; |
| 4490 | argidx = -2; |
| 4491 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 4492 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 4493 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4494 | dict = args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4495 | while (--fmtcnt >= 0) { |
| 4496 | if (*fmt != '%') { |
| 4497 | if (--rescnt < 0) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 4498 | rescnt = fmtcnt + 100; |
| 4499 | reslen += rescnt; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4500 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4501 | return NULL; |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4502 | res = PyString_AS_STRING(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4503 | + reslen - rescnt; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4504 | --rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4505 | } |
| 4506 | *res++ = *fmt++; |
| 4507 | } |
| 4508 | else { |
| 4509 | /* Got a format specifier */ |
| 4510 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4511 | Py_ssize_t width = -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4512 | int prec = -1; |
Guido van Rossum | 6938a29 | 1993-11-11 14:51:57 +0000 | [diff] [blame] | 4513 | int c = '\0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4514 | int fill; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4515 | PyObject *v = NULL; |
| 4516 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4517 | char *pbuf; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4518 | int sign; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4519 | Py_ssize_t len; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4520 | char formatbuf[FORMATBUFLEN]; |
| 4521 | /* For format{float,int,char}() */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4522 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4523 | char *fmt_start = fmt; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4524 | Py_ssize_t argidx_start = argidx; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4525 | #endif |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4526 | |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 4527 | fmt++; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4528 | if (*fmt == '(') { |
| 4529 | char *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4530 | Py_ssize_t keylen; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4531 | PyObject *key; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4532 | int pcount = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4533 | |
| 4534 | if (dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4535 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4536 | "format requires a mapping"); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4537 | goto error; |
| 4538 | } |
| 4539 | ++fmt; |
| 4540 | --fmtcnt; |
| 4541 | keystart = fmt; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4542 | /* Skip over balanced parentheses */ |
| 4543 | while (pcount > 0 && --fmtcnt >= 0) { |
| 4544 | if (*fmt == ')') |
| 4545 | --pcount; |
| 4546 | else if (*fmt == '(') |
| 4547 | ++pcount; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4548 | fmt++; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4549 | } |
| 4550 | keylen = fmt - keystart - 1; |
| 4551 | if (fmtcnt < 0 || pcount > 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4552 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4553 | "incomplete format key"); |
| 4554 | goto error; |
| 4555 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4556 | key = PyString_FromStringAndSize(keystart, |
| 4557 | keylen); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4558 | if (key == NULL) |
| 4559 | goto error; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4560 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4561 | Py_DECREF(args); |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4562 | args_owned = 0; |
| 4563 | } |
| 4564 | args = PyObject_GetItem(dict, key); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4565 | Py_DECREF(key); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4566 | if (args == NULL) { |
| 4567 | goto error; |
| 4568 | } |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4569 | args_owned = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4570 | arglen = -1; |
| 4571 | argidx = -2; |
| 4572 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4573 | while (--fmtcnt >= 0) { |
| 4574 | switch (c = *fmt++) { |
| 4575 | case '-': flags |= F_LJUST; continue; |
| 4576 | case '+': flags |= F_SIGN; continue; |
| 4577 | case ' ': flags |= F_BLANK; continue; |
| 4578 | case '#': flags |= F_ALT; continue; |
| 4579 | case '0': flags |= F_ZERO; continue; |
| 4580 | } |
| 4581 | break; |
| 4582 | } |
| 4583 | if (c == '*') { |
| 4584 | v = getnextarg(args, arglen, &argidx); |
| 4585 | if (v == NULL) |
| 4586 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4587 | if (!PyInt_Check(v)) { |
| 4588 | PyErr_SetString(PyExc_TypeError, |
| 4589 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4590 | goto error; |
| 4591 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4592 | width = PyInt_AsLong(v); |
Guido van Rossum | 98c9eba | 1999-06-07 15:12:32 +0000 | [diff] [blame] | 4593 | if (width < 0) { |
| 4594 | flags |= F_LJUST; |
| 4595 | width = -width; |
| 4596 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4597 | if (--fmtcnt >= 0) |
| 4598 | c = *fmt++; |
| 4599 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4600 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4601 | width = c - '0'; |
| 4602 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4603 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4604 | if (!isdigit(c)) |
| 4605 | break; |
| 4606 | if ((width*10) / 10 != width) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4607 | PyErr_SetString( |
| 4608 | PyExc_ValueError, |
| 4609 | "width too big"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4610 | goto error; |
| 4611 | } |
| 4612 | width = width*10 + (c - '0'); |
| 4613 | } |
| 4614 | } |
| 4615 | if (c == '.') { |
| 4616 | prec = 0; |
| 4617 | if (--fmtcnt >= 0) |
| 4618 | c = *fmt++; |
| 4619 | if (c == '*') { |
| 4620 | v = getnextarg(args, arglen, &argidx); |
| 4621 | if (v == NULL) |
| 4622 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4623 | if (!PyInt_Check(v)) { |
| 4624 | PyErr_SetString( |
| 4625 | PyExc_TypeError, |
| 4626 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4627 | goto error; |
| 4628 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4629 | prec = PyInt_AsLong(v); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4630 | if (prec < 0) |
| 4631 | prec = 0; |
| 4632 | if (--fmtcnt >= 0) |
| 4633 | c = *fmt++; |
| 4634 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4635 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4636 | prec = c - '0'; |
| 4637 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4638 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4639 | if (!isdigit(c)) |
| 4640 | break; |
| 4641 | if ((prec*10) / 10 != prec) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4642 | PyErr_SetString( |
| 4643 | PyExc_ValueError, |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4644 | "prec too big"); |
| 4645 | goto error; |
| 4646 | } |
| 4647 | prec = prec*10 + (c - '0'); |
| 4648 | } |
| 4649 | } |
| 4650 | } /* prec */ |
| 4651 | if (fmtcnt >= 0) { |
| 4652 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4653 | if (--fmtcnt >= 0) |
| 4654 | c = *fmt++; |
| 4655 | } |
| 4656 | } |
| 4657 | if (fmtcnt < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4658 | PyErr_SetString(PyExc_ValueError, |
| 4659 | "incomplete format"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4660 | goto error; |
| 4661 | } |
| 4662 | if (c != '%') { |
| 4663 | v = getnextarg(args, arglen, &argidx); |
| 4664 | if (v == NULL) |
| 4665 | goto error; |
| 4666 | } |
| 4667 | sign = 0; |
| 4668 | fill = ' '; |
| 4669 | switch (c) { |
| 4670 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4671 | pbuf = "%"; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4672 | len = 1; |
| 4673 | break; |
| 4674 | case 's': |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4675 | #ifdef Py_USING_UNICODE |
Neil Schemenauer | ab61923 | 2005-08-31 23:02:05 +0000 | [diff] [blame] | 4676 | if (PyUnicode_Check(v)) { |
| 4677 | fmt = fmt_start; |
| 4678 | argidx = argidx_start; |
| 4679 | goto unicode; |
| 4680 | } |
Georg Brandl | d45014b | 2005-10-01 17:06:00 +0000 | [diff] [blame] | 4681 | #endif |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 4682 | temp = _PyObject_Str(v); |
Georg Brandl | d45014b | 2005-10-01 17:06:00 +0000 | [diff] [blame] | 4683 | #ifdef Py_USING_UNICODE |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 4684 | if (temp != NULL && PyUnicode_Check(temp)) { |
| 4685 | Py_DECREF(temp); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4686 | fmt = fmt_start; |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 4687 | argidx = argidx_start; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4688 | goto unicode; |
| 4689 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4690 | #endif |
Guido van Rossum | b00c07f | 2002-10-09 19:07:53 +0000 | [diff] [blame] | 4691 | /* Fall through */ |
Walter Dörwald | 9ff3f03 | 2003-06-18 14:17:01 +0000 | [diff] [blame] | 4692 | case 'r': |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 4693 | if (c == 'r') |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 4694 | temp = PyObject_Repr(v); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4695 | if (temp == NULL) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4696 | goto error; |
Guido van Rossum | 4a0144c | 1998-06-09 15:08:41 +0000 | [diff] [blame] | 4697 | if (!PyString_Check(temp)) { |
| 4698 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 8052f89 | 2002-10-09 19:14:30 +0000 | [diff] [blame] | 4699 | "%s argument has non-string str()"); |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4700 | Py_DECREF(temp); |
Guido van Rossum | 4a0144c | 1998-06-09 15:08:41 +0000 | [diff] [blame] | 4701 | goto error; |
| 4702 | } |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4703 | pbuf = PyString_AS_STRING(temp); |
| 4704 | len = PyString_GET_SIZE(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4705 | if (prec >= 0 && len > prec) |
| 4706 | len = prec; |
| 4707 | break; |
| 4708 | case 'i': |
| 4709 | case 'd': |
| 4710 | case 'u': |
| 4711 | case 'o': |
| 4712 | case 'x': |
| 4713 | case 'X': |
| 4714 | if (c == 'i') |
| 4715 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 4716 | if (PyLong_Check(v)) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4717 | int ilen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4718 | temp = _PyString_FormatLong(v, flags, |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4719 | prec, c, &pbuf, &ilen); |
| 4720 | len = ilen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4721 | if (!temp) |
| 4722 | goto error; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4723 | sign = 1; |
Guido van Rossum | 4acdc23 | 1997-01-29 06:00:24 +0000 | [diff] [blame] | 4724 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4725 | else { |
| 4726 | pbuf = formatbuf; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4727 | len = formatint(pbuf, |
| 4728 | sizeof(formatbuf), |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4729 | flags, prec, c, v); |
| 4730 | if (len < 0) |
| 4731 | goto error; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4732 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4733 | } |
| 4734 | if (flags & F_ZERO) |
| 4735 | fill = '0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4736 | break; |
| 4737 | case 'e': |
| 4738 | case 'E': |
| 4739 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 4740 | case 'F': |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4741 | case 'g': |
| 4742 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 4743 | if (c == 'F') |
| 4744 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4745 | pbuf = formatbuf; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4746 | len = formatfloat(pbuf, sizeof(formatbuf), |
| 4747 | flags, prec, c, v); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4748 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4749 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4750 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4751 | if (flags & F_ZERO) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4752 | fill = '0'; |
| 4753 | break; |
| 4754 | case 'c': |
Walter Dörwald | 43440a6 | 2003-03-31 18:07:50 +0000 | [diff] [blame] | 4755 | #ifdef Py_USING_UNICODE |
| 4756 | if (PyUnicode_Check(v)) { |
| 4757 | fmt = fmt_start; |
| 4758 | argidx = argidx_start; |
| 4759 | goto unicode; |
| 4760 | } |
| 4761 | #endif |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4762 | pbuf = formatbuf; |
| 4763 | len = formatchar(pbuf, sizeof(formatbuf), v); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4764 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4765 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4766 | break; |
| 4767 | default: |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4768 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 4769 | "unsupported format character '%c' (0x%x) " |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 4770 | "at index %zd", |
Guido van Rossum | efc1188 | 2002-09-12 14:43:41 +0000 | [diff] [blame] | 4771 | c, c, |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 4772 | (Py_ssize_t)(fmt - 1 - |
| 4773 | PyString_AsString(format))); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4774 | goto error; |
| 4775 | } |
| 4776 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4777 | if (*pbuf == '-' || *pbuf == '+') { |
| 4778 | sign = *pbuf++; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4779 | len--; |
| 4780 | } |
| 4781 | else if (flags & F_SIGN) |
| 4782 | sign = '+'; |
| 4783 | else if (flags & F_BLANK) |
| 4784 | sign = ' '; |
| 4785 | else |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4786 | sign = 0; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4787 | } |
| 4788 | if (width < len) |
| 4789 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 4790 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 4791 | reslen -= rescnt; |
| 4792 | rescnt = width + fmtcnt + 100; |
| 4793 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 4794 | if (reslen < 0) { |
| 4795 | Py_DECREF(result); |
Georg Brandl | 10a4b0e | 2007-02-26 13:51:29 +0000 | [diff] [blame] | 4796 | Py_XDECREF(temp); |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 4797 | return PyErr_NoMemory(); |
| 4798 | } |
Georg Brandl | 10a4b0e | 2007-02-26 13:51:29 +0000 | [diff] [blame] | 4799 | if (_PyString_Resize(&result, reslen) < 0) { |
| 4800 | Py_XDECREF(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4801 | return NULL; |
Georg Brandl | 10a4b0e | 2007-02-26 13:51:29 +0000 | [diff] [blame] | 4802 | } |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4803 | res = PyString_AS_STRING(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4804 | + reslen - rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4805 | } |
| 4806 | if (sign) { |
Guido van Rossum | 71e57d0 | 1993-11-11 15:03:51 +0000 | [diff] [blame] | 4807 | if (fill != ' ') |
| 4808 | *res++ = sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4809 | rescnt--; |
| 4810 | if (width > len) |
| 4811 | width--; |
| 4812 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4813 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 4814 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4815 | assert(pbuf[1] == c); |
| 4816 | if (fill != ' ') { |
| 4817 | *res++ = *pbuf++; |
| 4818 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4819 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4820 | rescnt -= 2; |
| 4821 | width -= 2; |
| 4822 | if (width < 0) |
| 4823 | width = 0; |
| 4824 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4825 | } |
| 4826 | if (width > len && !(flags & F_LJUST)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4827 | do { |
| 4828 | --rescnt; |
| 4829 | *res++ = fill; |
| 4830 | } while (--width > len); |
| 4831 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4832 | if (fill == ' ') { |
| 4833 | if (sign) |
| 4834 | *res++ = sign; |
| 4835 | if ((flags & F_ALT) && |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4836 | (c == 'x' || c == 'X')) { |
| 4837 | assert(pbuf[0] == '0'); |
| 4838 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4839 | *res++ = *pbuf++; |
| 4840 | *res++ = *pbuf++; |
| 4841 | } |
| 4842 | } |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 4843 | Py_MEMCPY(res, pbuf, len); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4844 | res += len; |
| 4845 | rescnt -= len; |
| 4846 | while (--width >= len) { |
| 4847 | --rescnt; |
| 4848 | *res++ = ' '; |
| 4849 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4850 | if (dict && (argidx < arglen) && c != '%') { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4851 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 4852 | "not all arguments converted during string formatting"); |
Georg Brandl | 10a4b0e | 2007-02-26 13:51:29 +0000 | [diff] [blame] | 4853 | Py_XDECREF(temp); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4854 | goto error; |
| 4855 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4856 | Py_XDECREF(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4857 | } /* '%' */ |
| 4858 | } /* until end */ |
Guido van Rossum | caeaafc | 1995-02-27 10:13:23 +0000 | [diff] [blame] | 4859 | if (argidx < arglen && !dict) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4860 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 4861 | "not all arguments converted during string formatting"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4862 | goto error; |
| 4863 | } |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4864 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4865 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4866 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4867 | _PyString_Resize(&result, reslen - rescnt); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4868 | return result; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4869 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4870 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4871 | unicode: |
| 4872 | if (args_owned) { |
| 4873 | Py_DECREF(args); |
| 4874 | args_owned = 0; |
| 4875 | } |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 4876 | /* Fiddle args right (remove the first argidx arguments) */ |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4877 | if (PyTuple_Check(orig_args) && argidx > 0) { |
| 4878 | PyObject *v; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4879 | Py_ssize_t n = PyTuple_GET_SIZE(orig_args) - argidx; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4880 | v = PyTuple_New(n); |
| 4881 | if (v == NULL) |
| 4882 | goto error; |
| 4883 | while (--n >= 0) { |
| 4884 | PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx); |
| 4885 | Py_INCREF(w); |
| 4886 | PyTuple_SET_ITEM(v, n, w); |
| 4887 | } |
| 4888 | args = v; |
| 4889 | } else { |
| 4890 | Py_INCREF(orig_args); |
| 4891 | args = orig_args; |
| 4892 | } |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4893 | args_owned = 1; |
| 4894 | /* Take what we have of the result and let the Unicode formatting |
| 4895 | function format the rest of the input. */ |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4896 | rescnt = res - PyString_AS_STRING(result); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4897 | if (_PyString_Resize(&result, rescnt)) |
| 4898 | goto error; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4899 | fmtcnt = PyString_GET_SIZE(format) - \ |
| 4900 | (fmt - PyString_AS_STRING(format)); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4901 | format = PyUnicode_Decode(fmt, fmtcnt, NULL, NULL); |
| 4902 | if (format == NULL) |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4903 | goto error; |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4904 | v = PyUnicode_Format(format, args); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4905 | Py_DECREF(format); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4906 | if (v == NULL) |
| 4907 | goto error; |
| 4908 | /* Paste what we have (result) to what the Unicode formatting |
| 4909 | function returned (v) and return the result (or error) */ |
| 4910 | w = PyUnicode_Concat(result, v); |
| 4911 | Py_DECREF(result); |
| 4912 | Py_DECREF(v); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4913 | Py_DECREF(args); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4914 | return w; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4915 | #endif /* Py_USING_UNICODE */ |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4916 | |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4917 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4918 | Py_DECREF(result); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4919 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4920 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4921 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4922 | return NULL; |
| 4923 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4924 | |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4925 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4926 | PyString_InternInPlace(PyObject **p) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4927 | { |
| 4928 | register PyStringObject *s = (PyStringObject *)(*p); |
| 4929 | PyObject *t; |
| 4930 | if (s == NULL || !PyString_Check(s)) |
| 4931 | Py_FatalError("PyString_InternInPlace: strings only please!"); |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4932 | /* If it's a string subclass, we don't really know what putting |
| 4933 | it in the interned dict might do. */ |
| 4934 | if (!PyString_CheckExact(s)) |
| 4935 | return; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4936 | if (PyString_CHECK_INTERNED(s)) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4937 | return; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4938 | if (interned == NULL) { |
| 4939 | interned = PyDict_New(); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4940 | if (interned == NULL) { |
| 4941 | PyErr_Clear(); /* Don't leave an exception */ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4942 | return; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4943 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4944 | } |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4945 | t = PyDict_GetItem(interned, (PyObject *)s); |
| 4946 | if (t) { |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4947 | Py_INCREF(t); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4948 | Py_DECREF(*p); |
| 4949 | *p = t; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4950 | return; |
| 4951 | } |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4952 | |
Armin Rigo | 79f7ad2 | 2004-08-07 19:27:39 +0000 | [diff] [blame] | 4953 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4954 | PyErr_Clear(); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4955 | return; |
| 4956 | } |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4957 | /* The two references in interned are not counted by refcnt. |
| 4958 | The string deallocator will take care of this */ |
Armin Rigo | 79f7ad2 | 2004-08-07 19:27:39 +0000 | [diff] [blame] | 4959 | s->ob_refcnt -= 2; |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4960 | PyString_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4961 | } |
| 4962 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4963 | void |
| 4964 | PyString_InternImmortal(PyObject **p) |
| 4965 | { |
| 4966 | PyString_InternInPlace(p); |
| 4967 | if (PyString_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 4968 | PyString_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 4969 | Py_INCREF(*p); |
| 4970 | } |
| 4971 | } |
| 4972 | |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4973 | |
| 4974 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4975 | PyString_InternFromString(const char *cp) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4976 | { |
| 4977 | PyObject *s = PyString_FromString(cp); |
| 4978 | if (s == NULL) |
| 4979 | return NULL; |
| 4980 | PyString_InternInPlace(&s); |
| 4981 | return s; |
| 4982 | } |
| 4983 | |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 4984 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4985 | PyString_Fini(void) |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 4986 | { |
| 4987 | int i; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 4988 | for (i = 0; i < UCHAR_MAX + 1; i++) { |
| 4989 | Py_XDECREF(characters[i]); |
| 4990 | characters[i] = NULL; |
| 4991 | } |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 4992 | Py_XDECREF(nullstring); |
| 4993 | nullstring = NULL; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 4994 | } |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 4995 | |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 4996 | void _Py_ReleaseInternedStrings(void) |
| 4997 | { |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4998 | PyObject *keys; |
| 4999 | PyStringObject *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5000 | Py_ssize_t i, n; |
Neal Norwitz | 1c1a1c5 | 2007-02-25 15:52:27 +0000 | [diff] [blame] | 5001 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5002 | |
| 5003 | if (interned == NULL || !PyDict_Check(interned)) |
| 5004 | return; |
| 5005 | keys = PyDict_Keys(interned); |
| 5006 | if (keys == NULL || !PyList_Check(keys)) { |
| 5007 | PyErr_Clear(); |
| 5008 | return; |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 5009 | } |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5010 | |
| 5011 | /* Since _Py_ReleaseInternedStrings() is intended to help a leak |
| 5012 | detector, interned strings are not forcibly deallocated; rather, we |
| 5013 | give them their stolen references back, and then clear and DECREF |
| 5014 | the interned dict. */ |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 5015 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5016 | n = PyList_GET_SIZE(keys); |
Neal Norwitz | 1c1a1c5 | 2007-02-25 15:52:27 +0000 | [diff] [blame] | 5017 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
| 5018 | n); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5019 | for (i = 0; i < n; i++) { |
| 5020 | s = (PyStringObject *) PyList_GET_ITEM(keys, i); |
| 5021 | switch (s->ob_sstate) { |
| 5022 | case SSTATE_NOT_INTERNED: |
| 5023 | /* XXX Shouldn't happen */ |
| 5024 | break; |
| 5025 | case SSTATE_INTERNED_IMMORTAL: |
| 5026 | s->ob_refcnt += 1; |
Neal Norwitz | 1c1a1c5 | 2007-02-25 15:52:27 +0000 | [diff] [blame] | 5027 | immortal_size += s->ob_size; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5028 | break; |
| 5029 | case SSTATE_INTERNED_MORTAL: |
| 5030 | s->ob_refcnt += 2; |
Neal Norwitz | 1c1a1c5 | 2007-02-25 15:52:27 +0000 | [diff] [blame] | 5031 | mortal_size += s->ob_size; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5032 | break; |
| 5033 | default: |
| 5034 | Py_FatalError("Inconsistent interned string state."); |
| 5035 | } |
| 5036 | s->ob_sstate = SSTATE_NOT_INTERNED; |
| 5037 | } |
Neal Norwitz | 1c1a1c5 | 2007-02-25 15:52:27 +0000 | [diff] [blame] | 5038 | fprintf(stderr, "total size of all interned strings: " |
| 5039 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 5040 | "mortal/immortal\n", mortal_size, immortal_size); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5041 | Py_DECREF(keys); |
| 5042 | PyDict_Clear(interned); |
| 5043 | Py_DECREF(interned); |
| 5044 | interned = NULL; |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 5045 | } |