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 | |
| 26 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 27 | /* |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 28 | For both PyString_FromString() and PyString_FromStringAndSize(), the |
| 29 | 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] | 30 | null terminating character. |
Martin v. Löwis | d132750 | 2001-12-02 18:09:41 +0000 | [diff] [blame] | 31 | |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 32 | 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] | 33 | string containing exactly `size' bytes. |
Martin v. Löwis | d132750 | 2001-12-02 18:09:41 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 35 | For PyString_FromStringAndSize(), the parameter the parameter `str' is |
| 36 | either NULL or else points to a string containing at least `size' bytes. |
| 37 | For PyString_FromStringAndSize(), the string in the `str' parameter does |
| 38 | not have to be null-terminated. (Therefore it is safe to construct a |
| 39 | substring by calling `PyString_FromStringAndSize(origstring, substrlen)'.) |
| 40 | If `str' is NULL then PyString_FromStringAndSize() will allocate `size+1' |
| 41 | bytes (setting the last byte to the null terminating character) and you can |
| 42 | fill in the data yourself. If `str' is non-NULL then the resulting |
| 43 | PyString object must be treated as immutable and you must not fill in nor |
| 44 | alter the data yourself, since the strings may be shared. |
Martin v. Löwis | 8f1ea71 | 2001-12-03 08:24:52 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 46 | The PyObject member `op->ob_size', which denotes the number of "extra |
| 47 | items" in a variable-size object, will contain the number of bytes |
| 48 | allocated for string data, not counting the null terminating character. It |
| 49 | is therefore equal to the equal to the `size' parameter (for |
| 50 | PyString_FromStringAndSize()) or the length of the string in the `str' |
| 51 | parameter (for PyString_FromString()). |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 52 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 53 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 54 | PyString_FromStringAndSize(const char *str, Py_ssize_t size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 55 | { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 56 | register PyStringObject *op; |
Michael W. Hudson | faa7648 | 2005-01-31 17:09:25 +0000 | [diff] [blame] | 57 | assert(size >= 0); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 58 | if (size == 0 && (op = nullstring) != NULL) { |
| 59 | #ifdef COUNT_ALLOCS |
| 60 | null_strings++; |
| 61 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | Py_INCREF(op); |
| 63 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 64 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 65 | if (size == 1 && str != NULL && |
| 66 | (op = characters[*str & UCHAR_MAX]) != NULL) |
| 67 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 68 | #ifdef COUNT_ALLOCS |
| 69 | one_strings++; |
| 70 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 71 | Py_INCREF(op); |
| 72 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 73 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 75 | /* Inline PyObject_NewVar */ |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 76 | op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 77 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 78 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 79 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 80 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 81 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 82 | if (str != NULL) |
| 83 | memcpy(op->ob_sval, str, size); |
| 84 | op->ob_sval[size] = '\0'; |
Tim Peters | 8deda70 | 2002-03-30 10:06:07 +0000 | [diff] [blame] | 85 | /* share short strings */ |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 86 | if (size == 0) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 87 | PyObject *t = (PyObject *)op; |
| 88 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 89 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 90 | nullstring = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 91 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 92 | } else if (size == 1 && str != NULL) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 93 | PyObject *t = (PyObject *)op; |
| 94 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 95 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 96 | characters[*str & UCHAR_MAX] = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 97 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 98 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 99 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 102 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 103 | PyString_FromString(const char *str) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 104 | { |
Tim Peters | 62de65b | 2001-12-06 20:29:32 +0000 | [diff] [blame] | 105 | register size_t size; |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 106 | register PyStringObject *op; |
Tim Peters | 62de65b | 2001-12-06 20:29:32 +0000 | [diff] [blame] | 107 | |
| 108 | assert(str != NULL); |
| 109 | size = strlen(str); |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 110 | if (size > PY_SSIZE_T_MAX) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 111 | PyErr_SetString(PyExc_OverflowError, |
| 112 | "string is too long for a Python string"); |
| 113 | return NULL; |
| 114 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 115 | if (size == 0 && (op = nullstring) != NULL) { |
| 116 | #ifdef COUNT_ALLOCS |
| 117 | null_strings++; |
| 118 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 119 | Py_INCREF(op); |
| 120 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 121 | } |
| 122 | if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
| 123 | #ifdef COUNT_ALLOCS |
| 124 | one_strings++; |
| 125 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 126 | Py_INCREF(op); |
| 127 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 128 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 130 | /* Inline PyObject_NewVar */ |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 131 | op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 132 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 133 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 134 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 135 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 136 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Guido van Rossum | 169192e | 2001-12-10 15:45:54 +0000 | [diff] [blame] | 137 | memcpy(op->ob_sval, str, size+1); |
Tim Peters | 8deda70 | 2002-03-30 10:06:07 +0000 | [diff] [blame] | 138 | /* share short strings */ |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 139 | if (size == 0) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 140 | PyObject *t = (PyObject *)op; |
| 141 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 142 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 143 | nullstring = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 144 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 145 | } else if (size == 1) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 146 | PyObject *t = (PyObject *)op; |
| 147 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 148 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 149 | characters[*str & UCHAR_MAX] = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 150 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 151 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 152 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 155 | PyObject * |
| 156 | PyString_FromFormatV(const char *format, va_list vargs) |
| 157 | { |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 158 | va_list count; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 159 | Py_ssize_t n = 0; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 160 | const char* f; |
| 161 | char *s; |
| 162 | PyObject* string; |
| 163 | |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 164 | #ifdef VA_LIST_IS_ARRAY |
| 165 | memcpy(count, vargs, sizeof(va_list)); |
| 166 | #else |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 167 | #ifdef __va_copy |
| 168 | __va_copy(count, vargs); |
| 169 | #else |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 170 | count = vargs; |
| 171 | #endif |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 172 | #endif |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 173 | /* step 1: figure out how large a buffer we need */ |
| 174 | for (f = format; *f; f++) { |
| 175 | if (*f == '%') { |
| 176 | const char* p = f; |
| 177 | while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
| 178 | ; |
| 179 | |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 180 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 181 | * they don't affect the amount of space we reserve. |
| 182 | */ |
| 183 | if ((*f == 'l' || *f == 'z') && |
| 184 | (f[1] == 'd' || f[1] == 'u')) |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 185 | ++f; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 186 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 187 | switch (*f) { |
| 188 | case 'c': |
| 189 | (void)va_arg(count, int); |
| 190 | /* fall through... */ |
| 191 | case '%': |
| 192 | n++; |
| 193 | break; |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 194 | case 'd': case 'u': case 'i': case 'x': |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 195 | (void) va_arg(count, int); |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 196 | /* 20 bytes is enough to hold a 64-bit |
| 197 | integer. Decimal takes the most space. |
| 198 | This isn't enough for octal. */ |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 199 | n += 20; |
| 200 | break; |
| 201 | case 's': |
| 202 | s = va_arg(count, char*); |
| 203 | n += strlen(s); |
| 204 | break; |
| 205 | case 'p': |
| 206 | (void) va_arg(count, int); |
| 207 | /* maximum 64-bit pointer representation: |
| 208 | * 0xffffffffffffffff |
| 209 | * so 19 characters is enough. |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 210 | * XXX I count 18 -- what's the extra for? |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 211 | */ |
| 212 | n += 19; |
| 213 | break; |
| 214 | default: |
| 215 | /* if we stumble upon an unknown |
| 216 | formatting code, copy the rest of |
| 217 | the format string to the output |
| 218 | string. (we cannot just skip the |
| 219 | code, since there's no way to know |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 220 | what's in the argument list) */ |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 221 | n += strlen(p); |
| 222 | goto expand; |
| 223 | } |
| 224 | } else |
| 225 | n++; |
| 226 | } |
| 227 | expand: |
| 228 | /* step 2: fill the buffer */ |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 229 | /* Since we've analyzed how much space we need for the worst case, |
| 230 | use sprintf directly instead of the slower PyOS_snprintf. */ |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 231 | string = PyString_FromStringAndSize(NULL, n); |
| 232 | if (!string) |
| 233 | return NULL; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 234 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 235 | s = PyString_AsString(string); |
| 236 | |
| 237 | for (f = format; *f; f++) { |
| 238 | if (*f == '%') { |
| 239 | const char* p = f++; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 240 | Py_ssize_t i; |
| 241 | int longflag = 0; |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 242 | int size_tflag = 0; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 243 | /* parse the width.precision part (we're only |
| 244 | interested in the precision value, if any) */ |
| 245 | n = 0; |
| 246 | while (isdigit(Py_CHARMASK(*f))) |
| 247 | n = (n*10) + *f++ - '0'; |
| 248 | if (*f == '.') { |
| 249 | f++; |
| 250 | n = 0; |
| 251 | while (isdigit(Py_CHARMASK(*f))) |
| 252 | n = (n*10) + *f++ - '0'; |
| 253 | } |
| 254 | while (*f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
| 255 | f++; |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 256 | /* handle the long flag, but only for %ld and %lu. |
| 257 | others can be added when necessary. */ |
| 258 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 259 | longflag = 1; |
| 260 | ++f; |
| 261 | } |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 262 | /* handle the size_t flag. */ |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 263 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 264 | size_tflag = 1; |
| 265 | ++f; |
| 266 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 267 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 268 | switch (*f) { |
| 269 | case 'c': |
| 270 | *s++ = va_arg(vargs, int); |
| 271 | break; |
| 272 | case 'd': |
| 273 | if (longflag) |
| 274 | sprintf(s, "%ld", va_arg(vargs, long)); |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 275 | else if (size_tflag) |
Martin v. Löwis | 822f34a | 2006-05-13 13:34:04 +0000 | [diff] [blame] | 276 | sprintf(s, "%" PY_FORMAT_SIZE_T "d", |
| 277 | va_arg(vargs, Py_ssize_t)); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 278 | else |
| 279 | sprintf(s, "%d", va_arg(vargs, int)); |
| 280 | s += strlen(s); |
| 281 | break; |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 282 | case 'u': |
| 283 | if (longflag) |
| 284 | sprintf(s, "%lu", |
| 285 | va_arg(vargs, unsigned long)); |
| 286 | else if (size_tflag) |
| 287 | sprintf(s, "%" PY_FORMAT_SIZE_T "u", |
| 288 | va_arg(vargs, size_t)); |
| 289 | else |
| 290 | sprintf(s, "%u", |
| 291 | va_arg(vargs, unsigned int)); |
| 292 | s += strlen(s); |
| 293 | break; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 294 | case 'i': |
| 295 | sprintf(s, "%i", va_arg(vargs, int)); |
| 296 | s += strlen(s); |
| 297 | break; |
| 298 | case 'x': |
| 299 | sprintf(s, "%x", va_arg(vargs, int)); |
| 300 | s += strlen(s); |
| 301 | break; |
| 302 | case 's': |
| 303 | p = va_arg(vargs, char*); |
| 304 | i = strlen(p); |
| 305 | if (n > 0 && i > n) |
| 306 | i = n; |
| 307 | memcpy(s, p, i); |
| 308 | s += i; |
| 309 | break; |
| 310 | case 'p': |
| 311 | sprintf(s, "%p", va_arg(vargs, void*)); |
Tim Peters | 6af5bbb | 2001-08-25 03:02:28 +0000 | [diff] [blame] | 312 | /* %p is ill-defined: ensure leading 0x. */ |
| 313 | if (s[1] == 'X') |
| 314 | s[1] = 'x'; |
| 315 | else if (s[1] != 'x') { |
| 316 | memmove(s+2, s, strlen(s)+1); |
| 317 | s[0] = '0'; |
| 318 | s[1] = 'x'; |
| 319 | } |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 320 | s += strlen(s); |
| 321 | break; |
| 322 | case '%': |
| 323 | *s++ = '%'; |
| 324 | break; |
| 325 | default: |
| 326 | strcpy(s, p); |
| 327 | s += strlen(s); |
| 328 | goto end; |
| 329 | } |
| 330 | } else |
| 331 | *s++ = *f; |
| 332 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 333 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 334 | end: |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 335 | _PyString_Resize(&string, s - PyString_AS_STRING(string)); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 336 | return string; |
| 337 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 338 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 339 | PyObject * |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 340 | PyString_FromFormat(const char *format, ...) |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 341 | { |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 342 | PyObject* ret; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 343 | va_list vargs; |
| 344 | |
| 345 | #ifdef HAVE_STDARG_PROTOTYPES |
| 346 | va_start(vargs, format); |
| 347 | #else |
| 348 | va_start(vargs); |
| 349 | #endif |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 350 | ret = PyString_FromFormatV(format, vargs); |
| 351 | va_end(vargs); |
| 352 | return ret; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 356 | PyObject *PyString_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 357 | Py_ssize_t size, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 358 | const char *encoding, |
| 359 | const char *errors) |
| 360 | { |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 361 | PyObject *v, *str; |
| 362 | |
| 363 | str = PyString_FromStringAndSize(s, size); |
| 364 | if (str == NULL) |
| 365 | return NULL; |
| 366 | v = PyString_AsDecodedString(str, encoding, errors); |
| 367 | Py_DECREF(str); |
| 368 | return v; |
| 369 | } |
| 370 | |
| 371 | PyObject *PyString_AsDecodedObject(PyObject *str, |
| 372 | const char *encoding, |
| 373 | const char *errors) |
| 374 | { |
| 375 | PyObject *v; |
| 376 | |
| 377 | if (!PyString_Check(str)) { |
| 378 | PyErr_BadArgument(); |
| 379 | goto onError; |
| 380 | } |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 381 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 382 | if (encoding == NULL) { |
| 383 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 384 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 385 | #else |
| 386 | PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
| 387 | goto onError; |
| 388 | #endif |
| 389 | } |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 390 | |
| 391 | /* Decode via the codec registry */ |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 392 | v = PyCodec_Decode(str, encoding, errors); |
| 393 | if (v == NULL) |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 394 | goto onError; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 395 | |
| 396 | return v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 397 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 398 | onError: |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 399 | return NULL; |
| 400 | } |
| 401 | |
| 402 | PyObject *PyString_AsDecodedString(PyObject *str, |
| 403 | const char *encoding, |
| 404 | const char *errors) |
| 405 | { |
| 406 | PyObject *v; |
| 407 | |
| 408 | v = PyString_AsDecodedObject(str, encoding, errors); |
| 409 | if (v == NULL) |
| 410 | goto onError; |
| 411 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 412 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 413 | /* Convert Unicode to a string using the default encoding */ |
| 414 | if (PyUnicode_Check(v)) { |
| 415 | PyObject *temp = v; |
| 416 | v = PyUnicode_AsEncodedString(v, NULL, NULL); |
| 417 | Py_DECREF(temp); |
| 418 | if (v == NULL) |
| 419 | goto onError; |
| 420 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 421 | #endif |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 422 | if (!PyString_Check(v)) { |
| 423 | PyErr_Format(PyExc_TypeError, |
| 424 | "decoder did not return a string object (type=%.400s)", |
| 425 | v->ob_type->tp_name); |
| 426 | Py_DECREF(v); |
| 427 | goto onError; |
| 428 | } |
| 429 | |
| 430 | return v; |
| 431 | |
| 432 | onError: |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 433 | return NULL; |
| 434 | } |
| 435 | |
| 436 | PyObject *PyString_Encode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 437 | Py_ssize_t size, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 438 | const char *encoding, |
| 439 | const char *errors) |
| 440 | { |
| 441 | PyObject *v, *str; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 442 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 443 | str = PyString_FromStringAndSize(s, size); |
| 444 | if (str == NULL) |
| 445 | return NULL; |
| 446 | v = PyString_AsEncodedString(str, encoding, errors); |
| 447 | Py_DECREF(str); |
| 448 | return v; |
| 449 | } |
| 450 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 451 | PyObject *PyString_AsEncodedObject(PyObject *str, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 452 | const char *encoding, |
| 453 | const char *errors) |
| 454 | { |
| 455 | PyObject *v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 456 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 457 | if (!PyString_Check(str)) { |
| 458 | PyErr_BadArgument(); |
| 459 | goto onError; |
| 460 | } |
| 461 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 462 | if (encoding == NULL) { |
| 463 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 464 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 465 | #else |
| 466 | PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
| 467 | goto onError; |
| 468 | #endif |
| 469 | } |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 470 | |
| 471 | /* Encode via the codec registry */ |
| 472 | v = PyCodec_Encode(str, encoding, errors); |
| 473 | if (v == NULL) |
| 474 | goto onError; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 475 | |
| 476 | return v; |
| 477 | |
| 478 | onError: |
| 479 | return NULL; |
| 480 | } |
| 481 | |
| 482 | PyObject *PyString_AsEncodedString(PyObject *str, |
| 483 | const char *encoding, |
| 484 | const char *errors) |
| 485 | { |
| 486 | PyObject *v; |
| 487 | |
Marc-André Lemburg | 8c2133d | 2001-06-12 13:14:10 +0000 | [diff] [blame] | 488 | v = PyString_AsEncodedObject(str, encoding, errors); |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 489 | if (v == NULL) |
| 490 | goto onError; |
| 491 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 492 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 493 | /* Convert Unicode to a string using the default encoding */ |
| 494 | if (PyUnicode_Check(v)) { |
| 495 | PyObject *temp = v; |
| 496 | v = PyUnicode_AsEncodedString(v, NULL, NULL); |
| 497 | Py_DECREF(temp); |
| 498 | if (v == NULL) |
| 499 | goto onError; |
| 500 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 501 | #endif |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 502 | if (!PyString_Check(v)) { |
| 503 | PyErr_Format(PyExc_TypeError, |
| 504 | "encoder did not return a string object (type=%.400s)", |
| 505 | v->ob_type->tp_name); |
| 506 | Py_DECREF(v); |
| 507 | goto onError; |
| 508 | } |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 509 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 510 | return v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 511 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 512 | onError: |
| 513 | return NULL; |
| 514 | } |
| 515 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 516 | static void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 517 | string_dealloc(PyObject *op) |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 518 | { |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 519 | switch (PyString_CHECK_INTERNED(op)) { |
| 520 | case SSTATE_NOT_INTERNED: |
| 521 | break; |
| 522 | |
| 523 | case SSTATE_INTERNED_MORTAL: |
| 524 | /* revive dead object temporarily for DelItem */ |
| 525 | op->ob_refcnt = 3; |
| 526 | if (PyDict_DelItem(interned, op) != 0) |
| 527 | Py_FatalError( |
| 528 | "deletion of interned string failed"); |
| 529 | break; |
| 530 | |
| 531 | case SSTATE_INTERNED_IMMORTAL: |
| 532 | Py_FatalError("Immortal interned string died."); |
| 533 | |
| 534 | default: |
| 535 | Py_FatalError("Inconsistent interned string state."); |
| 536 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 537 | op->ob_type->tp_free(op); |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 540 | /* Unescape a backslash-escaped string. If unicode is non-zero, |
| 541 | the string is a u-literal. If recode_encoding is non-zero, |
| 542 | the string is UTF-8 encoded and should be re-encoded in the |
| 543 | specified encoding. */ |
| 544 | |
| 545 | PyObject *PyString_DecodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 546 | Py_ssize_t len, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 547 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 548 | Py_ssize_t unicode, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 549 | const char *recode_encoding) |
| 550 | { |
| 551 | int c; |
| 552 | char *p, *buf; |
| 553 | const char *end; |
| 554 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 555 | Py_ssize_t newlen = recode_encoding ? 4*len:len; |
Walter Dörwald | 8709a42 | 2002-09-03 13:53:40 +0000 | [diff] [blame] | 556 | v = PyString_FromStringAndSize((char *)NULL, newlen); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 557 | if (v == NULL) |
| 558 | return NULL; |
| 559 | p = buf = PyString_AsString(v); |
| 560 | end = s + len; |
| 561 | while (s < end) { |
| 562 | if (*s != '\\') { |
Martin v. Löwis | 2412853 | 2002-09-09 06:17:05 +0000 | [diff] [blame] | 563 | non_esc: |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 564 | #ifdef Py_USING_UNICODE |
| 565 | if (recode_encoding && (*s & 0x80)) { |
| 566 | PyObject *u, *w; |
| 567 | char *r; |
| 568 | const char* t; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 569 | Py_ssize_t rn; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 570 | t = s; |
| 571 | /* Decode non-ASCII bytes as UTF-8. */ |
| 572 | while (t < end && (*t & 0x80)) t++; |
| 573 | u = PyUnicode_DecodeUTF8(s, t - s, errors); |
| 574 | if(!u) goto failed; |
| 575 | |
| 576 | /* Recode them in target encoding. */ |
| 577 | w = PyUnicode_AsEncodedString( |
| 578 | u, recode_encoding, errors); |
| 579 | Py_DECREF(u); |
| 580 | if (!w) goto failed; |
| 581 | |
| 582 | /* Append bytes to output buffer. */ |
Neal Norwitz | 2aa9a5d | 2006-03-20 01:53:23 +0000 | [diff] [blame] | 583 | assert(PyString_Check(w)); |
| 584 | r = PyString_AS_STRING(w); |
| 585 | rn = PyString_GET_SIZE(w); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 586 | memcpy(p, r, rn); |
| 587 | p += rn; |
| 588 | Py_DECREF(w); |
| 589 | s = t; |
| 590 | } else { |
| 591 | *p++ = *s++; |
| 592 | } |
| 593 | #else |
| 594 | *p++ = *s++; |
| 595 | #endif |
| 596 | continue; |
| 597 | } |
| 598 | s++; |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 599 | if (s==end) { |
| 600 | PyErr_SetString(PyExc_ValueError, |
| 601 | "Trailing \\ in string"); |
| 602 | goto failed; |
| 603 | } |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 604 | switch (*s++) { |
| 605 | /* XXX This assumes ASCII! */ |
| 606 | case '\n': break; |
| 607 | case '\\': *p++ = '\\'; break; |
| 608 | case '\'': *p++ = '\''; break; |
| 609 | case '\"': *p++ = '\"'; break; |
| 610 | case 'b': *p++ = '\b'; break; |
| 611 | case 'f': *p++ = '\014'; break; /* FF */ |
| 612 | case 't': *p++ = '\t'; break; |
| 613 | case 'n': *p++ = '\n'; break; |
| 614 | case 'r': *p++ = '\r'; break; |
| 615 | case 'v': *p++ = '\013'; break; /* VT */ |
| 616 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 617 | case '0': case '1': case '2': case '3': |
| 618 | case '4': case '5': case '6': case '7': |
| 619 | c = s[-1] - '0'; |
| 620 | if ('0' <= *s && *s <= '7') { |
| 621 | c = (c<<3) + *s++ - '0'; |
| 622 | if ('0' <= *s && *s <= '7') |
| 623 | c = (c<<3) + *s++ - '0'; |
| 624 | } |
| 625 | *p++ = c; |
| 626 | break; |
| 627 | case 'x': |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 628 | if (isxdigit(Py_CHARMASK(s[0])) |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 629 | && isxdigit(Py_CHARMASK(s[1]))) { |
| 630 | unsigned int x = 0; |
| 631 | c = Py_CHARMASK(*s); |
| 632 | s++; |
| 633 | if (isdigit(c)) |
| 634 | x = c - '0'; |
| 635 | else if (islower(c)) |
| 636 | x = 10 + c - 'a'; |
| 637 | else |
| 638 | x = 10 + c - 'A'; |
| 639 | x = x << 4; |
| 640 | c = Py_CHARMASK(*s); |
| 641 | s++; |
| 642 | if (isdigit(c)) |
| 643 | x += c - '0'; |
| 644 | else if (islower(c)) |
| 645 | x += 10 + c - 'a'; |
| 646 | else |
| 647 | x += 10 + c - 'A'; |
| 648 | *p++ = x; |
| 649 | break; |
| 650 | } |
| 651 | if (!errors || strcmp(errors, "strict") == 0) { |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 652 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 653 | "invalid \\x escape"); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 654 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 655 | } |
| 656 | if (strcmp(errors, "replace") == 0) { |
| 657 | *p++ = '?'; |
| 658 | } else if (strcmp(errors, "ignore") == 0) |
| 659 | /* do nothing */; |
| 660 | else { |
| 661 | PyErr_Format(PyExc_ValueError, |
| 662 | "decoding error; " |
| 663 | "unknown error handling code: %.400s", |
| 664 | errors); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 665 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 666 | } |
| 667 | #ifndef Py_USING_UNICODE |
| 668 | case 'u': |
| 669 | case 'U': |
| 670 | case 'N': |
| 671 | if (unicode) { |
Neal Norwitz | b898d9f | 2002-08-16 23:20:39 +0000 | [diff] [blame] | 672 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 673 | "Unicode escapes not legal " |
| 674 | "when Unicode disabled"); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 675 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 676 | } |
| 677 | #endif |
| 678 | default: |
| 679 | *p++ = '\\'; |
Martin v. Löwis | 2412853 | 2002-09-09 06:17:05 +0000 | [diff] [blame] | 680 | s--; |
| 681 | goto non_esc; /* an arbitry number of unescaped |
| 682 | UTF-8 bytes may follow. */ |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 683 | } |
| 684 | } |
Walter Dörwald | 8709a42 | 2002-09-03 13:53:40 +0000 | [diff] [blame] | 685 | if (p-buf < newlen) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 686 | _PyString_Resize(&v, p - buf); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 687 | return v; |
| 688 | failed: |
| 689 | Py_DECREF(v); |
| 690 | return NULL; |
| 691 | } |
| 692 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 693 | /* -------------------------------------------------------------------- */ |
| 694 | /* object api */ |
| 695 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 696 | static Py_ssize_t |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 697 | string_getsize(register PyObject *op) |
| 698 | { |
| 699 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 700 | Py_ssize_t len; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 701 | if (PyString_AsStringAndSize(op, &s, &len)) |
| 702 | return -1; |
| 703 | return len; |
| 704 | } |
| 705 | |
| 706 | static /*const*/ char * |
| 707 | string_getbuffer(register PyObject *op) |
| 708 | { |
| 709 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 710 | Py_ssize_t len; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 711 | if (PyString_AsStringAndSize(op, &s, &len)) |
| 712 | return NULL; |
| 713 | return s; |
| 714 | } |
| 715 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 716 | Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 717 | PyString_Size(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 718 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 719 | if (!PyString_Check(op)) |
| 720 | return string_getsize(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 721 | return ((PyStringObject *)op) -> ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /*const*/ char * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 725 | PyString_AsString(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 726 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 727 | if (!PyString_Check(op)) |
| 728 | return string_getbuffer(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 729 | return ((PyStringObject *)op) -> ob_sval; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 732 | int |
| 733 | PyString_AsStringAndSize(register PyObject *obj, |
| 734 | register char **s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 735 | register Py_ssize_t *len) |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 736 | { |
| 737 | if (s == NULL) { |
| 738 | PyErr_BadInternalCall(); |
| 739 | return -1; |
| 740 | } |
| 741 | |
| 742 | if (!PyString_Check(obj)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 743 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 744 | if (PyUnicode_Check(obj)) { |
| 745 | obj = _PyUnicode_AsDefaultEncodedString(obj, NULL); |
| 746 | if (obj == NULL) |
| 747 | return -1; |
| 748 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 749 | else |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 750 | #endif |
| 751 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 752 | PyErr_Format(PyExc_TypeError, |
| 753 | "expected string or Unicode object, " |
| 754 | "%.200s found", obj->ob_type->tp_name); |
| 755 | return -1; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | *s = PyString_AS_STRING(obj); |
| 760 | if (len != NULL) |
| 761 | *len = PyString_GET_SIZE(obj); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 762 | else if (strlen(*s) != (size_t)PyString_GET_SIZE(obj)) { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 763 | PyErr_SetString(PyExc_TypeError, |
| 764 | "expected string without null bytes"); |
| 765 | return -1; |
| 766 | } |
| 767 | return 0; |
| 768 | } |
| 769 | |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 770 | /* -------------------------------------------------------------------- */ |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 771 | /* Methods */ |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 772 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 773 | #define STRINGLIB_CHAR char |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 774 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 775 | #define STRINGLIB_CMP memcmp |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 776 | #define STRINGLIB_LEN PyString_GET_SIZE |
| 777 | #define STRINGLIB_NEW PyString_FromStringAndSize |
| 778 | #define STRINGLIB_STR PyString_AS_STRING |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 779 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 780 | #define STRINGLIB_EMPTY nullstring |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 781 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 782 | #include "stringlib/fastsearch.h" |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 783 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 784 | #include "stringlib/count.h" |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 785 | #include "stringlib/find.h" |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 786 | #include "stringlib/partition.h" |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 787 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 788 | |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 789 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 790 | string_print(PyStringObject *op, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 791 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 792 | Py_ssize_t i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 793 | char c; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 794 | int quote; |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 795 | |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 796 | /* XXX Ought to check for interrupts when writing long strings */ |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 797 | if (! PyString_CheckExact(op)) { |
| 798 | int ret; |
| 799 | /* A str subclass may have its own __str__ method. */ |
| 800 | op = (PyStringObject *) PyObject_Str((PyObject *)op); |
| 801 | if (op == NULL) |
| 802 | return -1; |
| 803 | ret = string_print(op, fp, flags); |
| 804 | Py_DECREF(op); |
| 805 | return ret; |
| 806 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 807 | if (flags & Py_PRINT_RAW) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 808 | #ifdef __VMS |
| 809 | if (op->ob_size) fwrite(op->ob_sval, (int) op->ob_size, 1, fp); |
| 810 | #else |
| 811 | fwrite(op->ob_sval, 1, (int) op->ob_size, fp); |
| 812 | #endif |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 813 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 814 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 815 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 816 | /* figure out which quote to use; single is preferred */ |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 817 | quote = '\''; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 818 | if (memchr(op->ob_sval, '\'', op->ob_size) && |
| 819 | !memchr(op->ob_sval, '"', op->ob_size)) |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 820 | quote = '"'; |
| 821 | |
| 822 | fputc(quote, fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 823 | for (i = 0; i < op->ob_size; i++) { |
| 824 | c = op->ob_sval[i]; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 825 | if (c == quote || c == '\\') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 826 | fprintf(fp, "\\%c", c); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 827 | else if (c == '\t') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 828 | fprintf(fp, "\\t"); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 829 | else if (c == '\n') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 830 | fprintf(fp, "\\n"); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 831 | else if (c == '\r') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 832 | fprintf(fp, "\\r"); |
| 833 | else if (c < ' ' || c >= 0x7f) |
| 834 | fprintf(fp, "\\x%02x", c & 0xff); |
Martin v. Löwis | fed2405 | 2002-10-07 13:55:50 +0000 | [diff] [blame] | 835 | else |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 836 | fputc(c, fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 837 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 838 | fputc(quote, fp); |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 839 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 842 | PyObject * |
| 843 | PyString_Repr(PyObject *obj, int smartquotes) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 844 | { |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 845 | register PyStringObject* op = (PyStringObject*) obj; |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 846 | size_t newsize = 2 + 4 * op->ob_size; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 847 | PyObject *v; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 848 | if (newsize > PY_SSIZE_T_MAX) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 849 | PyErr_SetString(PyExc_OverflowError, |
| 850 | "string is too large to make repr"); |
| 851 | } |
| 852 | v = PyString_FromStringAndSize((char *)NULL, newsize); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 853 | if (v == NULL) { |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 854 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 855 | } |
| 856 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 857 | register Py_ssize_t i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 858 | register char c; |
| 859 | register char *p; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 860 | int quote; |
| 861 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 862 | /* figure out which quote to use; single is preferred */ |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 863 | quote = '\''; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 864 | if (smartquotes && |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 865 | memchr(op->ob_sval, '\'', op->ob_size) && |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 866 | !memchr(op->ob_sval, '"', op->ob_size)) |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 867 | quote = '"'; |
| 868 | |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 869 | p = PyString_AS_STRING(v); |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 870 | *p++ = quote; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 871 | for (i = 0; i < op->ob_size; i++) { |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 872 | /* There's at least enough room for a hex escape |
| 873 | and a closing quote. */ |
| 874 | assert(newsize - (p - PyString_AS_STRING(v)) >= 5); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 875 | c = op->ob_sval[i]; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 876 | if (c == quote || c == '\\') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 877 | *p++ = '\\', *p++ = c; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 878 | else if (c == '\t') |
| 879 | *p++ = '\\', *p++ = 't'; |
| 880 | else if (c == '\n') |
| 881 | *p++ = '\\', *p++ = 'n'; |
| 882 | else if (c == '\r') |
| 883 | *p++ = '\\', *p++ = 'r'; |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 884 | else if (c < ' ' || c >= 0x7f) { |
| 885 | /* For performance, we don't want to call |
| 886 | PyOS_snprintf here (extra layers of |
| 887 | function call). */ |
| 888 | sprintf(p, "\\x%02x", c & 0xff); |
| 889 | p += 4; |
Martin v. Löwis | fed2405 | 2002-10-07 13:55:50 +0000 | [diff] [blame] | 890 | } |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 891 | else |
| 892 | *p++ = c; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 893 | } |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 894 | assert(newsize - (p - PyString_AS_STRING(v)) >= 1); |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 895 | *p++ = quote; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 896 | *p = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 897 | _PyString_Resize( |
Thomas Wouters | 568f1d0 | 2006-04-21 13:54:43 +0000 | [diff] [blame] | 898 | &v, (p - PyString_AS_STRING(v))); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 899 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 900 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 903 | static PyObject * |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 904 | string_repr(PyObject *op) |
| 905 | { |
| 906 | return PyString_Repr(op, 1); |
| 907 | } |
| 908 | |
| 909 | static PyObject * |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 910 | string_str(PyObject *s) |
| 911 | { |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 912 | assert(PyString_Check(s)); |
| 913 | if (PyString_CheckExact(s)) { |
| 914 | Py_INCREF(s); |
| 915 | return s; |
| 916 | } |
| 917 | else { |
| 918 | /* Subtype -- return genuine string with the same value. */ |
| 919 | PyStringObject *t = (PyStringObject *) s; |
| 920 | return PyString_FromStringAndSize(t->ob_sval, t->ob_size); |
| 921 | } |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 924 | static Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 925 | string_length(PyStringObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 926 | { |
| 927 | return a->ob_size; |
| 928 | } |
| 929 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 930 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 931 | string_concat(register PyStringObject *a, register PyObject *bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 932 | { |
Andrew Dalke | 598710c | 2006-05-25 18:18:39 +0000 | [diff] [blame] | 933 | register Py_ssize_t size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 934 | register PyStringObject *op; |
| 935 | if (!PyString_Check(bb)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 936 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 937 | if (PyUnicode_Check(bb)) |
| 938 | return PyUnicode_Concat((PyObject *)a, bb); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 939 | #endif |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 940 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5c66a26 | 2001-10-22 04:12:44 +0000 | [diff] [blame] | 941 | "cannot concatenate 'str' and '%.200s' objects", |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 942 | bb->ob_type->tp_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 943 | return NULL; |
| 944 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 945 | #define b ((PyStringObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 946 | /* Optimize cases with empty left or right operand */ |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 947 | if ((a->ob_size == 0 || b->ob_size == 0) && |
| 948 | PyString_CheckExact(a) && PyString_CheckExact(b)) { |
| 949 | if (a->ob_size == 0) { |
| 950 | Py_INCREF(bb); |
| 951 | return bb; |
| 952 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 953 | Py_INCREF(a); |
| 954 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 955 | } |
| 956 | size = a->ob_size + b->ob_size; |
Andrew Dalke | 598710c | 2006-05-25 18:18:39 +0000 | [diff] [blame] | 957 | if (size < 0) { |
| 958 | PyErr_SetString(PyExc_OverflowError, |
| 959 | "strings are too large to concat"); |
| 960 | return NULL; |
| 961 | } |
| 962 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 963 | /* Inline PyObject_NewVar */ |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 964 | op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 965 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 966 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 967 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 968 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 969 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 970 | memcpy(op->ob_sval, a->ob_sval, a->ob_size); |
| 971 | 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] | 972 | op->ob_sval[size] = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 973 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 974 | #undef b |
| 975 | } |
| 976 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 977 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 978 | string_repeat(register PyStringObject *a, register Py_ssize_t n) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 979 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 980 | register Py_ssize_t i; |
| 981 | register Py_ssize_t j; |
| 982 | register Py_ssize_t size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 983 | register PyStringObject *op; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 984 | size_t nbytes; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 985 | if (n < 0) |
| 986 | n = 0; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 987 | /* watch out for overflows: the size can overflow int, |
| 988 | * and the # of bytes needed can overflow size_t |
| 989 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 990 | size = a->ob_size * n; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 991 | if (n && size / n != a->ob_size) { |
| 992 | PyErr_SetString(PyExc_OverflowError, |
| 993 | "repeated string is too long"); |
| 994 | return NULL; |
| 995 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 996 | if (size == a->ob_size && PyString_CheckExact(a)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 997 | Py_INCREF(a); |
| 998 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 999 | } |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 1000 | nbytes = (size_t)size; |
| 1001 | if (nbytes + sizeof(PyStringObject) <= nbytes) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 1002 | PyErr_SetString(PyExc_OverflowError, |
| 1003 | "repeated string is too long"); |
| 1004 | return NULL; |
| 1005 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1006 | op = (PyStringObject *) |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 1007 | PyObject_MALLOC(sizeof(PyStringObject) + nbytes); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 1008 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1009 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1010 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1011 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 1012 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Raymond Hettinger | 0a2f849 | 2003-01-06 22:42:41 +0000 | [diff] [blame] | 1013 | op->ob_sval[size] = '\0'; |
| 1014 | if (a->ob_size == 1 && n > 0) { |
| 1015 | memset(op->ob_sval, a->ob_sval[0] , n); |
| 1016 | return (PyObject *) op; |
| 1017 | } |
Raymond Hettinger | 698258a | 2003-01-06 10:33:56 +0000 | [diff] [blame] | 1018 | i = 0; |
| 1019 | if (i < size) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1020 | memcpy(op->ob_sval, a->ob_sval, a->ob_size); |
| 1021 | i = a->ob_size; |
Raymond Hettinger | 698258a | 2003-01-06 10:33:56 +0000 | [diff] [blame] | 1022 | } |
| 1023 | while (i < size) { |
| 1024 | j = (i <= size-i) ? i : size-i; |
| 1025 | memcpy(op->ob_sval+i, op->ob_sval, j); |
| 1026 | i += j; |
| 1027 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1028 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ |
| 1032 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1033 | static PyObject * |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1034 | string_slice(register PyStringObject *a, register Py_ssize_t i, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1035 | register Py_ssize_t j) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1036 | /* j -- may be negative! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1037 | { |
| 1038 | if (i < 0) |
| 1039 | i = 0; |
| 1040 | if (j < 0) |
| 1041 | j = 0; /* Avoid signed/unsigned bug in next line */ |
| 1042 | if (j > a->ob_size) |
| 1043 | j = a->ob_size; |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1044 | if (i == 0 && j == a->ob_size && PyString_CheckExact(a)) { |
| 1045 | /* It's the same as a */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1046 | Py_INCREF(a); |
| 1047 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1048 | } |
| 1049 | if (j < i) |
| 1050 | j = i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1051 | return PyString_FromStringAndSize(a->ob_sval + i, j-i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1054 | static int |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1055 | string_contains(PyObject *str_obj, PyObject *sub_obj) |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1056 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1057 | if (!PyString_CheckExact(sub_obj)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1058 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1059 | if (PyUnicode_Check(sub_obj)) |
| 1060 | return PyUnicode_Contains(str_obj, sub_obj); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1061 | #endif |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1062 | if (!PyString_Check(sub_obj)) { |
Guido van Rossum | bf935fd | 2002-08-24 06:57:49 +0000 | [diff] [blame] | 1063 | PyErr_SetString(PyExc_TypeError, |
| 1064 | "'in <string>' requires string as left operand"); |
| 1065 | return -1; |
| 1066 | } |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1067 | } |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 1068 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1069 | return stringlib_contains_obj(str_obj, sub_obj); |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1072 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1073 | string_item(PyStringObject *a, register Py_ssize_t i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1074 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1075 | char pchar; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1076 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1077 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1078 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1079 | return NULL; |
| 1080 | } |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1081 | pchar = a->ob_sval[i]; |
| 1082 | v = (PyObject *)characters[pchar & UCHAR_MAX]; |
Tim Peters | 5b4d477 | 2001-05-08 22:33:50 +0000 | [diff] [blame] | 1083 | if (v == NULL) |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1084 | v = PyString_FromStringAndSize(&pchar, 1); |
Tim Peters | b4bbcd7 | 2001-05-09 00:31:40 +0000 | [diff] [blame] | 1085 | else { |
| 1086 | #ifdef COUNT_ALLOCS |
| 1087 | one_strings++; |
| 1088 | #endif |
Tim Peters | cf5ad5d | 2001-05-09 00:24:55 +0000 | [diff] [blame] | 1089 | Py_INCREF(v); |
Tim Peters | b4bbcd7 | 2001-05-09 00:31:40 +0000 | [diff] [blame] | 1090 | } |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 1091 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1094 | static PyObject* |
| 1095 | string_richcompare(PyStringObject *a, PyStringObject *b, int op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1096 | { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1097 | int c; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1098 | Py_ssize_t len_a, len_b; |
| 1099 | Py_ssize_t min_len; |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1100 | PyObject *result; |
| 1101 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 1102 | /* Make sure both arguments are strings. */ |
| 1103 | if (!(PyString_Check(a) && PyString_Check(b))) { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1104 | result = Py_NotImplemented; |
| 1105 | goto out; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1106 | } |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1107 | if (a == b) { |
| 1108 | switch (op) { |
| 1109 | case Py_EQ:case Py_LE:case Py_GE: |
| 1110 | result = Py_True; |
| 1111 | goto out; |
| 1112 | case Py_NE:case Py_LT:case Py_GT: |
| 1113 | result = Py_False; |
| 1114 | goto out; |
| 1115 | } |
| 1116 | } |
| 1117 | if (op == Py_EQ) { |
| 1118 | /* Supporting Py_NE here as well does not save |
| 1119 | much time, since Py_NE is rarely used. */ |
| 1120 | if (a->ob_size == b->ob_size |
| 1121 | && (a->ob_sval[0] == b->ob_sval[0] |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 1122 | && memcmp(a->ob_sval, b->ob_sval, |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1123 | a->ob_size) == 0)) { |
| 1124 | result = Py_True; |
| 1125 | } else { |
| 1126 | result = Py_False; |
| 1127 | } |
| 1128 | goto out; |
| 1129 | } |
| 1130 | len_a = a->ob_size; len_b = b->ob_size; |
| 1131 | min_len = (len_a < len_b) ? len_a : len_b; |
| 1132 | if (min_len > 0) { |
| 1133 | c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
| 1134 | if (c==0) |
| 1135 | c = memcmp(a->ob_sval, b->ob_sval, min_len); |
| 1136 | }else |
| 1137 | c = 0; |
| 1138 | if (c == 0) |
| 1139 | c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
| 1140 | switch (op) { |
| 1141 | case Py_LT: c = c < 0; break; |
| 1142 | case Py_LE: c = c <= 0; break; |
| 1143 | case Py_EQ: assert(0); break; /* unreachable */ |
| 1144 | case Py_NE: c = c != 0; break; |
| 1145 | case Py_GT: c = c > 0; break; |
| 1146 | case Py_GE: c = c >= 0; break; |
| 1147 | default: |
| 1148 | result = Py_NotImplemented; |
| 1149 | goto out; |
| 1150 | } |
| 1151 | result = c ? Py_True : Py_False; |
| 1152 | out: |
| 1153 | Py_INCREF(result); |
| 1154 | return result; |
| 1155 | } |
| 1156 | |
| 1157 | int |
| 1158 | _PyString_Eq(PyObject *o1, PyObject *o2) |
| 1159 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1160 | PyStringObject *a = (PyStringObject*) o1; |
| 1161 | PyStringObject *b = (PyStringObject*) o2; |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1162 | return a->ob_size == b->ob_size |
| 1163 | && *a->ob_sval == *b->ob_sval |
| 1164 | && memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1167 | static long |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1168 | string_hash(PyStringObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1169 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1170 | register Py_ssize_t len; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1171 | register unsigned char *p; |
| 1172 | register long x; |
| 1173 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1174 | if (a->ob_shash != -1) |
| 1175 | return a->ob_shash; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1176 | len = a->ob_size; |
| 1177 | p = (unsigned char *) a->ob_sval; |
| 1178 | x = *p << 7; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1179 | while (--len >= 0) |
Guido van Rossum | eddcb3b | 1996-09-11 20:22:48 +0000 | [diff] [blame] | 1180 | x = (1000003*x) ^ *p++; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1181 | x ^= a->ob_size; |
| 1182 | if (x == -1) |
| 1183 | x = -2; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1184 | a->ob_shash = x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1185 | return x; |
| 1186 | } |
| 1187 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1188 | #define HASINDEX(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_HAVE_INDEX) |
| 1189 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1190 | static PyObject* |
| 1191 | string_subscript(PyStringObject* self, PyObject* item) |
| 1192 | { |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1193 | PyNumberMethods *nb = item->ob_type->tp_as_number; |
| 1194 | if (nb != NULL && HASINDEX(item) && nb->nb_index != NULL) { |
| 1195 | Py_ssize_t i = nb->nb_index(item); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1196 | if (i == -1 && PyErr_Occurred()) |
| 1197 | return NULL; |
| 1198 | if (i < 0) |
| 1199 | i += PyString_GET_SIZE(self); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1200 | return string_item(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1201 | } |
| 1202 | else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1203 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1204 | char* source_buf; |
| 1205 | char* result_buf; |
| 1206 | PyObject* result; |
| 1207 | |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1208 | if (PySlice_GetIndicesEx((PySliceObject*)item, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1209 | PyString_GET_SIZE(self), |
| 1210 | &start, &stop, &step, &slicelength) < 0) { |
| 1211 | return NULL; |
| 1212 | } |
| 1213 | |
| 1214 | if (slicelength <= 0) { |
| 1215 | return PyString_FromStringAndSize("", 0); |
| 1216 | } |
| 1217 | else { |
| 1218 | source_buf = PyString_AsString((PyObject*)self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1219 | result_buf = (char *)PyMem_Malloc(slicelength); |
Neal Norwitz | 95c1e50 | 2005-10-20 04:15:52 +0000 | [diff] [blame] | 1220 | if (result_buf == NULL) |
| 1221 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1222 | |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1223 | for (cur = start, i = 0; i < slicelength; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1224 | cur += step, i++) { |
| 1225 | result_buf[i] = source_buf[cur]; |
| 1226 | } |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1227 | |
| 1228 | result = PyString_FromStringAndSize(result_buf, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1229 | slicelength); |
| 1230 | PyMem_Free(result_buf); |
| 1231 | return result; |
| 1232 | } |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1233 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1234 | else { |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1235 | PyErr_SetString(PyExc_TypeError, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1236 | "string indices must be integers"); |
| 1237 | return NULL; |
| 1238 | } |
| 1239 | } |
| 1240 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1241 | static Py_ssize_t |
| 1242 | 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] | 1243 | { |
| 1244 | if ( index != 0 ) { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1245 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 1246 | "accessing non-existent string segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1247 | return -1; |
| 1248 | } |
| 1249 | *ptr = (void *)self->ob_sval; |
| 1250 | return self->ob_size; |
| 1251 | } |
| 1252 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1253 | static Py_ssize_t |
| 1254 | 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] | 1255 | { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1256 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 07d7800 | 1998-10-01 15:59:48 +0000 | [diff] [blame] | 1257 | "Cannot use string as modifiable buffer"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1258 | return -1; |
| 1259 | } |
| 1260 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1261 | static Py_ssize_t |
| 1262 | string_buffer_getsegcount(PyStringObject *self, Py_ssize_t *lenp) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1263 | { |
| 1264 | if ( lenp ) |
| 1265 | *lenp = self->ob_size; |
| 1266 | return 1; |
| 1267 | } |
| 1268 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1269 | static Py_ssize_t |
| 1270 | 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] | 1271 | { |
| 1272 | if ( index != 0 ) { |
| 1273 | PyErr_SetString(PyExc_SystemError, |
| 1274 | "accessing non-existent string segment"); |
| 1275 | return -1; |
| 1276 | } |
| 1277 | *ptr = self->ob_sval; |
| 1278 | return self->ob_size; |
| 1279 | } |
| 1280 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1281 | static PySequenceMethods string_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1282 | (lenfunc)string_length, /*sq_length*/ |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1283 | (binaryfunc)string_concat, /*sq_concat*/ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1284 | (ssizeargfunc)string_repeat, /*sq_repeat*/ |
| 1285 | (ssizeargfunc)string_item, /*sq_item*/ |
| 1286 | (ssizessizeargfunc)string_slice, /*sq_slice*/ |
Guido van Rossum | f380e66 | 1991-06-04 19:36:32 +0000 | [diff] [blame] | 1287 | 0, /*sq_ass_item*/ |
| 1288 | 0, /*sq_ass_slice*/ |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1289 | (objobjproc)string_contains /*sq_contains*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1290 | }; |
| 1291 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1292 | static PyMappingMethods string_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1293 | (lenfunc)string_length, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1294 | (binaryfunc)string_subscript, |
| 1295 | 0, |
| 1296 | }; |
| 1297 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1298 | static PyBufferProcs string_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1299 | (readbufferproc)string_buffer_getreadbuf, |
| 1300 | (writebufferproc)string_buffer_getwritebuf, |
| 1301 | (segcountproc)string_buffer_getsegcount, |
| 1302 | (charbufferproc)string_buffer_getcharbuf, |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1303 | }; |
| 1304 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1305 | |
| 1306 | |
| 1307 | #define LEFTSTRIP 0 |
| 1308 | #define RIGHTSTRIP 1 |
| 1309 | #define BOTHSTRIP 2 |
| 1310 | |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1311 | /* Arrays indexed by above */ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 1312 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 1313 | |
| 1314 | #define STRIPNAME(i) (stripformat[i]+3) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1315 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1316 | |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1317 | /* Don't call if length < 2 */ |
| 1318 | #define Py_STRING_MATCH(target, offset, pattern, length) \ |
| 1319 | (target[offset] == pattern[0] && \ |
| 1320 | target[offset+length-1] == pattern[length-1] && \ |
| 1321 | !memcmp(target+offset+1, pattern+1, length-2) ) |
| 1322 | |
| 1323 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1324 | /* Overallocate the initial list to reduce the number of reallocs for small |
| 1325 | split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three |
| 1326 | resizes, to sizes 4, 8, then 16. Most observed string splits are for human |
| 1327 | text (roughly 11 words per line) and field delimited data (usually 1-10 |
| 1328 | fields). For large strings the split algorithms are bandwidth limited |
| 1329 | so increasing the preallocation likely will not improve things.*/ |
| 1330 | |
| 1331 | #define MAX_PREALLOC 12 |
| 1332 | |
| 1333 | /* 5 splits gives 6 elements */ |
| 1334 | #define PREALLOC_SIZE(maxsplit) \ |
| 1335 | (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1) |
| 1336 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1337 | #define SPLIT_APPEND(data, left, right) \ |
| 1338 | str = PyString_FromStringAndSize((data) + (left), \ |
| 1339 | (right) - (left)); \ |
| 1340 | if (str == NULL) \ |
| 1341 | goto onError; \ |
| 1342 | if (PyList_Append(list, str)) { \ |
| 1343 | Py_DECREF(str); \ |
| 1344 | goto onError; \ |
| 1345 | } \ |
| 1346 | else \ |
| 1347 | Py_DECREF(str); |
| 1348 | |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1349 | #define SPLIT_ADD(data, left, right) { \ |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1350 | str = PyString_FromStringAndSize((data) + (left), \ |
| 1351 | (right) - (left)); \ |
| 1352 | if (str == NULL) \ |
| 1353 | goto onError; \ |
| 1354 | if (count < MAX_PREALLOC) { \ |
| 1355 | PyList_SET_ITEM(list, count, str); \ |
| 1356 | } else { \ |
| 1357 | if (PyList_Append(list, str)) { \ |
| 1358 | Py_DECREF(str); \ |
| 1359 | goto onError; \ |
| 1360 | } \ |
| 1361 | else \ |
| 1362 | Py_DECREF(str); \ |
| 1363 | } \ |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1364 | count++; } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1365 | |
| 1366 | /* Always force the list to the expected size. */ |
| 1367 | #define FIX_PREALLOC_SIZE(list) ((PyListObject *)list)->ob_size = count; |
| 1368 | |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1369 | #define SKIP_SPACE(s, i, len) { while (i<len && isspace(Py_CHARMASK(s[i]))) i++; } |
| 1370 | #define SKIP_NONSPACE(s, i, len) { while (i<len && !isspace(Py_CHARMASK(s[i]))) i++; } |
| 1371 | #define RSKIP_SPACE(s, i) { while (i>=0 && isspace(Py_CHARMASK(s[i]))) i--; } |
| 1372 | #define RSKIP_NONSPACE(s, i) { while (i>=0 && !isspace(Py_CHARMASK(s[i]))) i--; } |
| 1373 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 1374 | Py_LOCAL(PyObject *) |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1375 | 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] | 1376 | { |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1377 | Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1378 | PyObject *str; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1379 | PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1380 | |
| 1381 | if (list == NULL) |
| 1382 | return NULL; |
| 1383 | |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1384 | i = j = 0; |
| 1385 | |
| 1386 | while (maxsplit-- > 0) { |
| 1387 | SKIP_SPACE(s, i, len); |
| 1388 | if (i==len) break; |
| 1389 | j = i; i++; |
| 1390 | SKIP_NONSPACE(s, i, len); |
| 1391 | SPLIT_ADD(s, j, i); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1392 | } |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1393 | |
| 1394 | if (i < len) { |
| 1395 | /* Only occurs when maxsplit was reached */ |
| 1396 | /* Skip any remaining whitespace and copy to end of string */ |
| 1397 | SKIP_SPACE(s, i, len); |
| 1398 | if (i != len) |
| 1399 | SPLIT_ADD(s, i, len); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1400 | } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1401 | FIX_PREALLOC_SIZE(list); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1402 | return list; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1403 | onError: |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1404 | Py_DECREF(list); |
| 1405 | return NULL; |
| 1406 | } |
| 1407 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 1408 | Py_LOCAL(PyObject *) |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1409 | 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] | 1410 | { |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1411 | register Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1412 | PyObject *str; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1413 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1414 | |
| 1415 | if (list == NULL) |
| 1416 | return NULL; |
| 1417 | |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1418 | i = j = 0; |
| 1419 | while ((j < len) && (maxcount-- > 0)) { |
| 1420 | for(; j<len; j++) { |
| 1421 | /* I found that using memchr makes no difference */ |
| 1422 | if (s[j] == ch) { |
| 1423 | SPLIT_ADD(s, i, j); |
| 1424 | i = j = j + 1; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1425 | break; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1426 | } |
| 1427 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1428 | } |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1429 | if (i <= len) { |
| 1430 | SPLIT_ADD(s, i, len); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1431 | } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1432 | FIX_PREALLOC_SIZE(list); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1433 | return list; |
| 1434 | |
| 1435 | onError: |
| 1436 | Py_DECREF(list); |
| 1437 | return NULL; |
| 1438 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1439 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1440 | PyDoc_STRVAR(split__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1441 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 1442 | \n\ |
| 1443 | 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] | 1444 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Raymond Hettinger | bc552ce | 2002-08-05 06:28:21 +0000 | [diff] [blame] | 1445 | splits are done. If sep is not specified or is None, any\n\ |
| 1446 | whitespace string is a separator."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1447 | |
| 1448 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1449 | string_split(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1450 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1451 | Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1452 | Py_ssize_t maxsplit = -1, count=0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1453 | const char *s = PyString_AS_STRING(self), *sub; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1454 | PyObject *list, *str, *subobj = Py_None; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1455 | #ifdef USE_FAST |
| 1456 | Py_ssize_t pos; |
| 1457 | #endif |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1458 | |
Martin v. Löwis | 9c83076 | 2006-04-13 08:37:17 +0000 | [diff] [blame] | 1459 | if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1460 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1461 | if (maxsplit < 0) |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1462 | maxsplit = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1463 | if (subobj == Py_None) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1464 | return split_whitespace(s, len, maxsplit); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1465 | if (PyString_Check(subobj)) { |
| 1466 | sub = PyString_AS_STRING(subobj); |
| 1467 | n = PyString_GET_SIZE(subobj); |
| 1468 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1469 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1470 | else if (PyUnicode_Check(subobj)) |
| 1471 | return PyUnicode_Split((PyObject *)self, subobj, maxsplit); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1472 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1473 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 1474 | return NULL; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1475 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1476 | if (n == 0) { |
| 1477 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 1478 | return NULL; |
| 1479 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1480 | else if (n == 1) |
| 1481 | return split_char(s, len, sub[0], maxsplit); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1482 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1483 | list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1484 | if (list == NULL) |
| 1485 | return NULL; |
| 1486 | |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1487 | #ifdef USE_FAST |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1488 | i = j = 0; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1489 | while (maxsplit-- > 0) { |
| 1490 | pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH); |
| 1491 | if (pos < 0) |
| 1492 | break; |
| 1493 | j = i+pos; |
| 1494 | SPLIT_ADD(s, i, j); |
| 1495 | i = j + n; |
| 1496 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1497 | } |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1498 | #else |
| 1499 | i = j = 0; |
| 1500 | while ((j+n <= len) && (maxsplit-- > 0)) { |
| 1501 | for (; j+n <= len; j++) { |
| 1502 | if (Py_STRING_MATCH(s, j, sub, n)) { |
| 1503 | SPLIT_ADD(s, i, j); |
| 1504 | i = j = j + n; |
| 1505 | break; |
| 1506 | } |
| 1507 | } |
| 1508 | } |
| 1509 | #endif |
| 1510 | SPLIT_ADD(s, i, len); |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1511 | FIX_PREALLOC_SIZE(list); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1512 | return list; |
| 1513 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1514 | onError: |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1515 | Py_DECREF(list); |
| 1516 | return NULL; |
| 1517 | } |
| 1518 | |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1519 | PyDoc_STRVAR(partition__doc__, |
| 1520 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 1521 | \n\ |
| 1522 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 1523 | the separator itself, and the part after it. If the separator is not\n\ |
| 1524 | found, returns S and two empty strings."); |
| 1525 | |
| 1526 | static PyObject * |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 1527 | string_partition(PyStringObject *self, PyObject *sep_obj) |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1528 | { |
Fredrik Lundh | c2032fb | 2006-05-26 17:26:39 +0000 | [diff] [blame] | 1529 | const char *sep; |
| 1530 | Py_ssize_t sep_len; |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1531 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 1532 | if (PyString_Check(sep_obj)) { |
| 1533 | sep = PyString_AS_STRING(sep_obj); |
| 1534 | sep_len = PyString_GET_SIZE(sep_obj); |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1535 | } |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 1536 | #ifdef Py_USING_UNICODE |
| 1537 | else if (PyUnicode_Check(sep_obj)) |
Fredrik Lundh | c2032fb | 2006-05-26 17:26:39 +0000 | [diff] [blame] | 1538 | return PyUnicode_Partition((PyObject *) self, sep_obj); |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1539 | #endif |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 1540 | else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1541 | return NULL; |
| 1542 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 1543 | return stringlib_partition( |
Fredrik Lundh | c2032fb | 2006-05-26 17:26:39 +0000 | [diff] [blame] | 1544 | (PyObject*) self, |
| 1545 | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
| 1546 | sep_obj, sep, sep_len |
| 1547 | ); |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 1550 | PyDoc_STRVAR(rpartition__doc__, |
| 1551 | "S.rpartition(sep) -> (head, sep, tail)\n\ |
| 1552 | \n\ |
| 1553 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 1554 | the part before it, the separator itself, and the part after it. If the\n\ |
| 1555 | separator is not found, returns S and two empty strings."); |
| 1556 | |
| 1557 | static PyObject * |
| 1558 | string_rpartition(PyStringObject *self, PyObject *sep_obj) |
| 1559 | { |
| 1560 | const char *sep; |
| 1561 | Py_ssize_t sep_len; |
| 1562 | |
| 1563 | if (PyString_Check(sep_obj)) { |
| 1564 | sep = PyString_AS_STRING(sep_obj); |
| 1565 | sep_len = PyString_GET_SIZE(sep_obj); |
| 1566 | } |
| 1567 | #ifdef Py_USING_UNICODE |
| 1568 | else if (PyUnicode_Check(sep_obj)) |
| 1569 | return PyUnicode_Partition((PyObject *) self, sep_obj); |
| 1570 | #endif |
| 1571 | else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
| 1572 | return NULL; |
| 1573 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 1574 | return stringlib_rpartition( |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 1575 | (PyObject*) self, |
| 1576 | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
| 1577 | sep_obj, sep, sep_len |
| 1578 | ); |
| 1579 | } |
| 1580 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 1581 | Py_LOCAL(PyObject *) |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1582 | 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] | 1583 | { |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1584 | Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1585 | PyObject *str; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1586 | PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1587 | |
| 1588 | if (list == NULL) |
| 1589 | return NULL; |
| 1590 | |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1591 | i = j = len-1; |
| 1592 | |
| 1593 | while (maxsplit-- > 0) { |
| 1594 | RSKIP_SPACE(s, i); |
| 1595 | if (i<0) break; |
| 1596 | j = i; i--; |
| 1597 | RSKIP_NONSPACE(s, i); |
| 1598 | SPLIT_ADD(s, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1599 | } |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1600 | if (i >= 0) { |
| 1601 | /* Only occurs when maxsplit was reached */ |
| 1602 | /* Skip any remaining whitespace and copy to beginning of string */ |
| 1603 | RSKIP_SPACE(s, i); |
| 1604 | if (i >= 0) |
| 1605 | SPLIT_ADD(s, 0, i + 1); |
| 1606 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1607 | } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1608 | FIX_PREALLOC_SIZE(list); |
Fredrik Lundh | 554da41 | 2006-05-25 19:19:05 +0000 | [diff] [blame] | 1609 | if (PyList_Reverse(list) < 0) |
| 1610 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1611 | return list; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1612 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1613 | Py_DECREF(list); |
| 1614 | return NULL; |
| 1615 | } |
| 1616 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 1617 | Py_LOCAL(PyObject *) |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1618 | 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] | 1619 | { |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1620 | register Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1621 | PyObject *str; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1622 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1623 | |
| 1624 | if (list == NULL) |
| 1625 | return NULL; |
| 1626 | |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1627 | i = j = len - 1; |
| 1628 | while ((i >= 0) && (maxcount-- > 0)) { |
| 1629 | for (; i >= 0; i--) { |
| 1630 | if (s[i] == ch) { |
| 1631 | SPLIT_ADD(s, i + 1, j + 1); |
| 1632 | j = i = i - 1; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1633 | break; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1634 | } |
| 1635 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1636 | } |
| 1637 | if (j >= -1) { |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1638 | SPLIT_ADD(s, 0, j + 1); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1639 | } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1640 | FIX_PREALLOC_SIZE(list); |
Fredrik Lundh | 554da41 | 2006-05-25 19:19:05 +0000 | [diff] [blame] | 1641 | if (PyList_Reverse(list) < 0) |
| 1642 | goto onError; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1643 | return list; |
| 1644 | |
| 1645 | onError: |
| 1646 | Py_DECREF(list); |
| 1647 | return NULL; |
| 1648 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1649 | |
| 1650 | PyDoc_STRVAR(rsplit__doc__, |
| 1651 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 1652 | \n\ |
| 1653 | Return a list of the words in the string S, using sep as the\n\ |
| 1654 | delimiter string, starting at the end of the string and working\n\ |
| 1655 | to the front. If maxsplit is given, at most maxsplit splits are\n\ |
| 1656 | done. If sep is not specified or is None, any whitespace string\n\ |
| 1657 | is a separator."); |
| 1658 | |
| 1659 | static PyObject * |
| 1660 | string_rsplit(PyStringObject *self, PyObject *args) |
| 1661 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1662 | Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1663 | Py_ssize_t maxsplit = -1, count=0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1664 | const char *s = PyString_AS_STRING(self), *sub; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1665 | PyObject *list, *str, *subobj = Py_None; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1666 | |
Martin v. Löwis | 9c83076 | 2006-04-13 08:37:17 +0000 | [diff] [blame] | 1667 | if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1668 | return NULL; |
| 1669 | if (maxsplit < 0) |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1670 | maxsplit = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1671 | if (subobj == Py_None) |
| 1672 | return rsplit_whitespace(s, len, maxsplit); |
| 1673 | if (PyString_Check(subobj)) { |
| 1674 | sub = PyString_AS_STRING(subobj); |
| 1675 | n = PyString_GET_SIZE(subobj); |
| 1676 | } |
| 1677 | #ifdef Py_USING_UNICODE |
| 1678 | else if (PyUnicode_Check(subobj)) |
| 1679 | return PyUnicode_RSplit((PyObject *)self, subobj, maxsplit); |
| 1680 | #endif |
| 1681 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 1682 | return NULL; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1683 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1684 | if (n == 0) { |
| 1685 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 1686 | return NULL; |
| 1687 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1688 | else if (n == 1) |
| 1689 | return rsplit_char(s, len, sub[0], maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1690 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1691 | list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1692 | if (list == NULL) |
| 1693 | return NULL; |
| 1694 | |
| 1695 | j = len; |
| 1696 | i = j - n; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1697 | |
| 1698 | while ( (i >= 0) && (maxsplit-- > 0) ) { |
| 1699 | for (; i>=0; i--) { |
| 1700 | if (Py_STRING_MATCH(s, i, sub, n)) { |
| 1701 | SPLIT_ADD(s, i + n, j); |
| 1702 | j = i; |
| 1703 | i -= n; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1704 | break; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1705 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1706 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1707 | } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1708 | SPLIT_ADD(s, 0, j); |
| 1709 | FIX_PREALLOC_SIZE(list); |
| 1710 | if (PyList_Reverse(list) < 0) |
| 1711 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1712 | return list; |
| 1713 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1714 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1715 | Py_DECREF(list); |
| 1716 | return NULL; |
| 1717 | } |
| 1718 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1719 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1720 | PyDoc_STRVAR(join__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1721 | "S.join(sequence) -> string\n\ |
| 1722 | \n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1723 | 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] | 1724 | sequence. The separator between elements is S."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1725 | |
| 1726 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1727 | string_join(PyStringObject *self, PyObject *orig) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1728 | { |
| 1729 | char *sep = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1730 | const Py_ssize_t seplen = PyString_GET_SIZE(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1731 | PyObject *res = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1732 | char *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1733 | Py_ssize_t seqlen = 0; |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1734 | size_t sz = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1735 | Py_ssize_t i; |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1736 | PyObject *seq, *item; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1737 | |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1738 | seq = PySequence_Fast(orig, ""); |
| 1739 | if (seq == NULL) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1740 | return NULL; |
| 1741 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1742 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1743 | seqlen = PySequence_Size(seq); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1744 | if (seqlen == 0) { |
| 1745 | Py_DECREF(seq); |
| 1746 | return PyString_FromString(""); |
| 1747 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1748 | if (seqlen == 1) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1749 | item = PySequence_Fast_GET_ITEM(seq, 0); |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1750 | if (PyString_CheckExact(item) || PyUnicode_CheckExact(item)) { |
| 1751 | Py_INCREF(item); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1752 | Py_DECREF(seq); |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1753 | return item; |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1754 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1755 | } |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1756 | |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1757 | /* 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] | 1758 | * of the builtin types in the sequence. |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1759 | * Do a pre-pass to figure out the total amount of space we'll |
| 1760 | * need (sz), see whether any argument is absurd, and defer to |
| 1761 | * the Unicode join if appropriate. |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1762 | */ |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1763 | for (i = 0; i < seqlen; i++) { |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1764 | const size_t old_sz = sz; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1765 | item = PySequence_Fast_GET_ITEM(seq, i); |
| 1766 | if (!PyString_Check(item)){ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1767 | #ifdef Py_USING_UNICODE |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1768 | if (PyUnicode_Check(item)) { |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 1769 | /* Defer to Unicode join. |
| 1770 | * CAUTION: There's no gurantee that the |
| 1771 | * original sequence can be iterated over |
| 1772 | * again, so we must pass seq here. |
| 1773 | */ |
| 1774 | PyObject *result; |
| 1775 | result = PyUnicode_Join((PyObject *)self, seq); |
Barry Warsaw | 771d067 | 2000-07-11 04:58:12 +0000 | [diff] [blame] | 1776 | Py_DECREF(seq); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 1777 | return result; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1778 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1779 | #endif |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1780 | PyErr_Format(PyExc_TypeError, |
Neal Norwitz | 0e2cbab | 2006-04-17 05:56:32 +0000 | [diff] [blame] | 1781 | "sequence item %zd: expected string," |
Jeremy Hylton | 88887aa | 2000-07-11 20:55:38 +0000 | [diff] [blame] | 1782 | " %.80s found", |
Neal Norwitz | 0e2cbab | 2006-04-17 05:56:32 +0000 | [diff] [blame] | 1783 | i, item->ob_type->tp_name); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1784 | Py_DECREF(seq); |
| 1785 | return NULL; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1786 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1787 | sz += PyString_GET_SIZE(item); |
| 1788 | if (i != 0) |
| 1789 | sz += seplen; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1790 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1791 | PyErr_SetString(PyExc_OverflowError, |
| 1792 | "join() is too long for a Python string"); |
| 1793 | Py_DECREF(seq); |
| 1794 | return NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1795 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | /* Allocate result space. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1799 | res = PyString_FromStringAndSize((char*)NULL, sz); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1800 | if (res == NULL) { |
| 1801 | Py_DECREF(seq); |
| 1802 | return NULL; |
| 1803 | } |
| 1804 | |
| 1805 | /* Catenate everything. */ |
| 1806 | p = PyString_AS_STRING(res); |
| 1807 | for (i = 0; i < seqlen; ++i) { |
| 1808 | size_t n; |
| 1809 | item = PySequence_Fast_GET_ITEM(seq, i); |
| 1810 | n = PyString_GET_SIZE(item); |
| 1811 | memcpy(p, PyString_AS_STRING(item), n); |
| 1812 | p += n; |
| 1813 | if (i < seqlen - 1) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1814 | memcpy(p, sep, seplen); |
| 1815 | p += seplen; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1816 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1817 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1818 | |
Jeremy Hylton | 4904829 | 2000-07-11 03:28:17 +0000 | [diff] [blame] | 1819 | Py_DECREF(seq); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1820 | return res; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
Tim Peters | 52e155e | 2001-06-16 05:42:57 +0000 | [diff] [blame] | 1823 | PyObject * |
| 1824 | _PyString_Join(PyObject *sep, PyObject *x) |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1825 | { |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1826 | assert(sep != NULL && PyString_Check(sep)); |
| 1827 | assert(x != NULL); |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1828 | return string_join((PyStringObject *)sep, x); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 1831 | Py_LOCAL(void) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1832 | 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] | 1833 | { |
| 1834 | if (*end > len) |
| 1835 | *end = len; |
| 1836 | else if (*end < 0) |
| 1837 | *end += len; |
| 1838 | if (*end < 0) |
| 1839 | *end = 0; |
| 1840 | if (*start < 0) |
| 1841 | *start += len; |
| 1842 | if (*start < 0) |
| 1843 | *start = 0; |
| 1844 | } |
| 1845 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 1846 | Py_LOCAL(Py_ssize_t) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1847 | string_find_internal(PyStringObject *self, PyObject *args, int dir) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1848 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1849 | const char *s = PyString_AS_STRING(self), *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1850 | Py_ssize_t len = PyString_GET_SIZE(self); |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1851 | Py_ssize_t n, i = 0, last = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1852 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1853 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1854 | /* XXX ssize_t i */ |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1855 | if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 1856 | &subobj, _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1857 | return -2; |
| 1858 | if (PyString_Check(subobj)) { |
| 1859 | sub = PyString_AS_STRING(subobj); |
| 1860 | n = PyString_GET_SIZE(subobj); |
| 1861 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1862 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1863 | else if (PyUnicode_Check(subobj)) |
Guido van Rossum | 76afbd9 | 2002-08-20 17:29:29 +0000 | [diff] [blame] | 1864 | return PyUnicode_Find((PyObject *)self, subobj, i, last, dir); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1865 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1866 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1867 | return -2; |
| 1868 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 1869 | string_adjust_indices(&i, &last, len); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1870 | |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 1871 | if (dir > 0) |
| 1872 | return stringlib_find(s+i, last-i, sub, n, i); |
| 1873 | else |
| 1874 | return stringlib_rfind(s+i, last-i, sub, n, i); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1878 | PyDoc_STRVAR(find__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1879 | "S.find(sub [,start [,end]]) -> int\n\ |
| 1880 | \n\ |
| 1881 | Return the lowest index in S where substring sub is found,\n\ |
| 1882 | such that sub is contained within s[start,end]. Optional\n\ |
| 1883 | arguments start and end are interpreted as in slice notation.\n\ |
| 1884 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1885 | Return -1 on failure."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1886 | |
| 1887 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1888 | string_find(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1889 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1890 | Py_ssize_t result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1891 | if (result == -2) |
| 1892 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1893 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1897 | PyDoc_STRVAR(index__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1898 | "S.index(sub [,start [,end]]) -> int\n\ |
| 1899 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1900 | Like S.find() but raise ValueError when the substring is not found."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1901 | |
| 1902 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1903 | string_index(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1904 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1905 | Py_ssize_t result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1906 | if (result == -2) |
| 1907 | return NULL; |
| 1908 | if (result == -1) { |
| 1909 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 5d5e7c0 | 2003-01-15 05:32:57 +0000 | [diff] [blame] | 1910 | "substring not found"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1911 | return NULL; |
| 1912 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1913 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1914 | } |
| 1915 | |
| 1916 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1917 | PyDoc_STRVAR(rfind__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1918 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 1919 | \n\ |
| 1920 | Return the highest index in S where substring sub is found,\n\ |
| 1921 | such that sub is contained within s[start,end]. Optional\n\ |
| 1922 | arguments start and end are interpreted as in slice notation.\n\ |
| 1923 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1924 | Return -1 on failure."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1925 | |
| 1926 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1927 | string_rfind(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1928 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1929 | Py_ssize_t result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1930 | if (result == -2) |
| 1931 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1932 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1936 | PyDoc_STRVAR(rindex__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1937 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 1938 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1939 | Like S.rfind() but raise ValueError when the substring is not found."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1940 | |
| 1941 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1942 | string_rindex(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1943 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1944 | Py_ssize_t result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1945 | if (result == -2) |
| 1946 | return NULL; |
| 1947 | if (result == -1) { |
| 1948 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 5d5e7c0 | 2003-01-15 05:32:57 +0000 | [diff] [blame] | 1949 | "substring not found"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1950 | return NULL; |
| 1951 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1952 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 1956 | Py_LOCAL(PyObject *) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1957 | do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj) |
| 1958 | { |
| 1959 | char *s = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1960 | Py_ssize_t len = PyString_GET_SIZE(self); |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1961 | char *sep = PyString_AS_STRING(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1962 | Py_ssize_t seplen = PyString_GET_SIZE(sepobj); |
| 1963 | Py_ssize_t i, j; |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1964 | |
| 1965 | i = 0; |
| 1966 | if (striptype != RIGHTSTRIP) { |
| 1967 | while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) { |
| 1968 | i++; |
| 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | j = len; |
| 1973 | if (striptype != LEFTSTRIP) { |
| 1974 | do { |
| 1975 | j--; |
| 1976 | } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen)); |
| 1977 | j++; |
| 1978 | } |
| 1979 | |
| 1980 | if (i == 0 && j == len && PyString_CheckExact(self)) { |
| 1981 | Py_INCREF(self); |
| 1982 | return (PyObject*)self; |
| 1983 | } |
| 1984 | else |
| 1985 | return PyString_FromStringAndSize(s+i, j-i); |
| 1986 | } |
| 1987 | |
| 1988 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 1989 | Py_LOCAL(PyObject *) |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1990 | do_strip(PyStringObject *self, int striptype) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1991 | { |
| 1992 | char *s = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1993 | Py_ssize_t len = PyString_GET_SIZE(self), i, j; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1994 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1995 | i = 0; |
| 1996 | if (striptype != RIGHTSTRIP) { |
| 1997 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
| 1998 | i++; |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | j = len; |
| 2003 | if (striptype != LEFTSTRIP) { |
| 2004 | do { |
| 2005 | j--; |
| 2006 | } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
| 2007 | j++; |
| 2008 | } |
| 2009 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2010 | if (i == 0 && j == len && PyString_CheckExact(self)) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2011 | Py_INCREF(self); |
| 2012 | return (PyObject*)self; |
| 2013 | } |
| 2014 | else |
| 2015 | return PyString_FromStringAndSize(s+i, j-i); |
| 2016 | } |
| 2017 | |
| 2018 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2019 | Py_LOCAL(PyObject *) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2020 | do_argstrip(PyStringObject *self, int striptype, PyObject *args) |
| 2021 | { |
| 2022 | PyObject *sep = NULL; |
| 2023 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 2024 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2025 | return NULL; |
| 2026 | |
| 2027 | if (sep != NULL && sep != Py_None) { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 2028 | if (PyString_Check(sep)) |
| 2029 | return do_xstrip(self, striptype, sep); |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2030 | #ifdef Py_USING_UNICODE |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 2031 | else if (PyUnicode_Check(sep)) { |
| 2032 | PyObject *uniself = PyUnicode_FromObject((PyObject *)self); |
| 2033 | PyObject *res; |
| 2034 | if (uniself==NULL) |
| 2035 | return NULL; |
| 2036 | res = _PyUnicode_XStrip((PyUnicodeObject *)uniself, |
| 2037 | striptype, sep); |
| 2038 | Py_DECREF(uniself); |
| 2039 | return res; |
| 2040 | } |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2041 | #endif |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2042 | PyErr_Format(PyExc_TypeError, |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2043 | #ifdef Py_USING_UNICODE |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2044 | "%s arg must be None, str or unicode", |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2045 | #else |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2046 | "%s arg must be None or str", |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2047 | #endif |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2048 | STRIPNAME(striptype)); |
| 2049 | return NULL; |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | return do_strip(self, striptype); |
| 2053 | } |
| 2054 | |
| 2055 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2056 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2057 | "S.strip([chars]) -> string or unicode\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2058 | \n\ |
| 2059 | 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] | 2060 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2061 | If chars is given and not None, remove characters in chars instead.\n\ |
| 2062 | If chars is unicode, S will be converted to unicode before stripping"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2063 | |
| 2064 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2065 | string_strip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2066 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2067 | if (PyTuple_GET_SIZE(args) == 0) |
| 2068 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 2069 | else |
| 2070 | return do_argstrip(self, BOTHSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2074 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2075 | "S.lstrip([chars]) -> string or unicode\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2076 | \n\ |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2077 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2078 | If chars is given and not None, remove characters in chars instead.\n\ |
| 2079 | If chars is unicode, S will be converted to unicode before stripping"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2080 | |
| 2081 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2082 | string_lstrip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2083 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2084 | if (PyTuple_GET_SIZE(args) == 0) |
| 2085 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 2086 | else |
| 2087 | return do_argstrip(self, LEFTSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2091 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2092 | "S.rstrip([chars]) -> string or unicode\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2093 | \n\ |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2094 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2095 | If chars is given and not None, remove characters in chars instead.\n\ |
| 2096 | If chars is unicode, S will be converted to unicode before stripping"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2097 | |
| 2098 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2099 | string_rstrip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2100 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2101 | if (PyTuple_GET_SIZE(args) == 0) |
| 2102 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 2103 | else |
| 2104 | return do_argstrip(self, RIGHTSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
| 2107 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2108 | PyDoc_STRVAR(lower__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2109 | "S.lower() -> string\n\ |
| 2110 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2111 | Return a copy of the string S converted to lowercase."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2112 | |
Fredrik Lundh | dfe503d | 2006-05-25 16:10:12 +0000 | [diff] [blame] | 2113 | /* _tolower and _toupper are defined by SUSv2, but they're not ISO C */ |
| 2114 | #ifndef _tolower |
| 2115 | #define _tolower tolower |
| 2116 | #endif |
| 2117 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2118 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2119 | string_lower(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2120 | { |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2121 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2122 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2123 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2124 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2125 | newobj = PyString_FromStringAndSize(NULL, n); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2126 | if (!newobj) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2127 | return NULL; |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2128 | |
| 2129 | s = PyString_AS_STRING(newobj); |
| 2130 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2131 | memcpy(s, PyString_AS_STRING(self), n); |
| 2132 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2133 | for (i = 0; i < n; i++) { |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2134 | int c = Py_CHARMASK(s[i]); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2135 | if (isupper(c)) |
| 2136 | s[i] = _tolower(c); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2137 | } |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2138 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2139 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2142 | PyDoc_STRVAR(upper__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2143 | "S.upper() -> string\n\ |
| 2144 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2145 | Return a copy of the string S converted to uppercase."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2146 | |
Fredrik Lundh | dfe503d | 2006-05-25 16:10:12 +0000 | [diff] [blame] | 2147 | #ifndef _toupper |
| 2148 | #define _toupper toupper |
| 2149 | #endif |
| 2150 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2151 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2152 | string_upper(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2153 | { |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2154 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2155 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2156 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2157 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2158 | newobj = PyString_FromStringAndSize(NULL, n); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2159 | if (!newobj) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2160 | return NULL; |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2161 | |
| 2162 | s = PyString_AS_STRING(newobj); |
| 2163 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2164 | memcpy(s, PyString_AS_STRING(self), n); |
| 2165 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2166 | for (i = 0; i < n; i++) { |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2167 | int c = Py_CHARMASK(s[i]); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2168 | if (islower(c)) |
| 2169 | s[i] = _toupper(c); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2170 | } |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2171 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2172 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2175 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2176 | "S.title() -> string\n\ |
| 2177 | \n\ |
| 2178 | 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] | 2179 | characters, all remaining cased characters have lowercase."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2180 | |
| 2181 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2182 | string_title(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2183 | { |
| 2184 | char *s = PyString_AS_STRING(self), *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2185 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2186 | int previous_is_cased = 0; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2187 | PyObject *newobj; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2188 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2189 | newobj = PyString_FromStringAndSize(NULL, n); |
| 2190 | if (newobj == NULL) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2191 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2192 | s_new = PyString_AsString(newobj); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2193 | for (i = 0; i < n; i++) { |
| 2194 | int c = Py_CHARMASK(*s++); |
| 2195 | if (islower(c)) { |
| 2196 | if (!previous_is_cased) |
| 2197 | c = toupper(c); |
| 2198 | previous_is_cased = 1; |
| 2199 | } else if (isupper(c)) { |
| 2200 | if (previous_is_cased) |
| 2201 | c = tolower(c); |
| 2202 | previous_is_cased = 1; |
| 2203 | } else |
| 2204 | previous_is_cased = 0; |
| 2205 | *s_new++ = c; |
| 2206 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2207 | return newobj; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2210 | PyDoc_STRVAR(capitalize__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2211 | "S.capitalize() -> string\n\ |
| 2212 | \n\ |
| 2213 | 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] | 2214 | capitalized."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2215 | |
| 2216 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2217 | string_capitalize(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2218 | { |
| 2219 | char *s = PyString_AS_STRING(self), *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2220 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2221 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2222 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2223 | newobj = PyString_FromStringAndSize(NULL, n); |
| 2224 | if (newobj == NULL) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2225 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2226 | s_new = PyString_AsString(newobj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2227 | if (0 < n) { |
| 2228 | int c = Py_CHARMASK(*s++); |
| 2229 | if (islower(c)) |
| 2230 | *s_new = toupper(c); |
| 2231 | else |
| 2232 | *s_new = c; |
| 2233 | s_new++; |
| 2234 | } |
| 2235 | for (i = 1; i < n; i++) { |
| 2236 | int c = Py_CHARMASK(*s++); |
| 2237 | if (isupper(c)) |
| 2238 | *s_new = tolower(c); |
| 2239 | else |
| 2240 | *s_new = c; |
| 2241 | s_new++; |
| 2242 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2243 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2247 | PyDoc_STRVAR(count__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2248 | "S.count(sub[, start[, end]]) -> int\n\ |
| 2249 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 2250 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 2251 | string S[start:end]. Optional arguments start and end are interpreted\n\ |
| 2252 | as in slice notation."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2253 | |
| 2254 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2255 | string_count(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2256 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2257 | PyObject *sub_obj; |
| 2258 | const char *str = PyString_AS_STRING(self), *sub; |
| 2259 | Py_ssize_t sub_len; |
| 2260 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2261 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2262 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj, |
| 2263 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2264 | return NULL; |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 2265 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2266 | if (PyString_Check(sub_obj)) { |
| 2267 | sub = PyString_AS_STRING(sub_obj); |
| 2268 | sub_len = PyString_GET_SIZE(sub_obj); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2269 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2270 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2271 | else if (PyUnicode_Check(sub_obj)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2272 | Py_ssize_t count; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2273 | count = PyUnicode_Count((PyObject *)self, sub_obj, start, end); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2274 | if (count == -1) |
| 2275 | return NULL; |
| 2276 | else |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2277 | return PyInt_FromSsize_t(count); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2278 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2279 | #endif |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2280 | else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2281 | return NULL; |
| 2282 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2283 | string_adjust_indices(&start, &end, PyString_GET_SIZE(self)); |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 2284 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2285 | return PyInt_FromSsize_t( |
| 2286 | stringlib_count(str + start, end - start, sub, sub_len) |
| 2287 | ); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2290 | PyDoc_STRVAR(swapcase__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2291 | "S.swapcase() -> string\n\ |
| 2292 | \n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2293 | 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] | 2294 | converted to lowercase and vice versa."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2295 | |
| 2296 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2297 | string_swapcase(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2298 | { |
| 2299 | char *s = PyString_AS_STRING(self), *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2300 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2301 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2302 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2303 | newobj = PyString_FromStringAndSize(NULL, n); |
| 2304 | if (newobj == NULL) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2305 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2306 | s_new = PyString_AsString(newobj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2307 | for (i = 0; i < n; i++) { |
| 2308 | int c = Py_CHARMASK(*s++); |
| 2309 | if (islower(c)) { |
| 2310 | *s_new = toupper(c); |
| 2311 | } |
| 2312 | else if (isupper(c)) { |
| 2313 | *s_new = tolower(c); |
| 2314 | } |
| 2315 | else |
| 2316 | *s_new = c; |
| 2317 | s_new++; |
| 2318 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2319 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2323 | PyDoc_STRVAR(translate__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2324 | "S.translate(table [,deletechars]) -> string\n\ |
| 2325 | \n\ |
| 2326 | Return a copy of the string S, where all characters occurring\n\ |
| 2327 | in the optional argument deletechars are removed, and the\n\ |
| 2328 | remaining characters have been mapped through the given\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2329 | translation table, which must be a string of length 256."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2330 | |
| 2331 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2332 | string_translate(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2333 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2334 | register char *input, *output; |
| 2335 | register const char *table; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2336 | register Py_ssize_t i, c, changed = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2337 | PyObject *input_obj = (PyObject*)self; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2338 | const char *table1, *output_start, *del_table=NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2339 | Py_ssize_t inlen, tablen, dellen = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2340 | PyObject *result; |
| 2341 | int trans_table[256]; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2342 | PyObject *tableobj, *delobj = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2343 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 2344 | if (!PyArg_UnpackTuple(args, "translate", 1, 2, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2345 | &tableobj, &delobj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2346 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2347 | |
| 2348 | if (PyString_Check(tableobj)) { |
| 2349 | table1 = PyString_AS_STRING(tableobj); |
| 2350 | tablen = PyString_GET_SIZE(tableobj); |
| 2351 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2352 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2353 | else if (PyUnicode_Check(tableobj)) { |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2354 | /* Unicode .translate() does not support the deletechars |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2355 | parameter; instead a mapping to None will cause characters |
| 2356 | to be deleted. */ |
| 2357 | if (delobj != NULL) { |
| 2358 | PyErr_SetString(PyExc_TypeError, |
| 2359 | "deletions are implemented differently for unicode"); |
| 2360 | return NULL; |
| 2361 | } |
| 2362 | return PyUnicode_Translate((PyObject *)self, tableobj, NULL); |
| 2363 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2364 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2365 | else if (PyObject_AsCharBuffer(tableobj, &table1, &tablen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2366 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2367 | |
Martin v. Löwis | 00b6127 | 2002-12-12 20:03:19 +0000 | [diff] [blame] | 2368 | if (tablen != 256) { |
| 2369 | PyErr_SetString(PyExc_ValueError, |
| 2370 | "translation table must be 256 characters long"); |
| 2371 | return NULL; |
| 2372 | } |
| 2373 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2374 | if (delobj != NULL) { |
| 2375 | if (PyString_Check(delobj)) { |
| 2376 | del_table = PyString_AS_STRING(delobj); |
| 2377 | dellen = PyString_GET_SIZE(delobj); |
| 2378 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2379 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2380 | else if (PyUnicode_Check(delobj)) { |
| 2381 | PyErr_SetString(PyExc_TypeError, |
| 2382 | "deletions are implemented differently for unicode"); |
| 2383 | return NULL; |
| 2384 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2385 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2386 | else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen)) |
| 2387 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2388 | } |
| 2389 | else { |
| 2390 | del_table = NULL; |
| 2391 | dellen = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | table = table1; |
Neal Norwitz | 2aa9a5d | 2006-03-20 01:53:23 +0000 | [diff] [blame] | 2395 | inlen = PyString_GET_SIZE(input_obj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2396 | result = PyString_FromStringAndSize((char *)NULL, inlen); |
| 2397 | if (result == NULL) |
| 2398 | return NULL; |
| 2399 | output_start = output = PyString_AsString(result); |
Neal Norwitz | 2aa9a5d | 2006-03-20 01:53:23 +0000 | [diff] [blame] | 2400 | input = PyString_AS_STRING(input_obj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2401 | |
| 2402 | if (dellen == 0) { |
| 2403 | /* If no deletions are required, use faster code */ |
| 2404 | for (i = inlen; --i >= 0; ) { |
| 2405 | c = Py_CHARMASK(*input++); |
| 2406 | if (Py_CHARMASK((*output++ = table[c])) != c) |
| 2407 | changed = 1; |
| 2408 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2409 | if (changed || !PyString_CheckExact(input_obj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2410 | return result; |
| 2411 | Py_DECREF(result); |
| 2412 | Py_INCREF(input_obj); |
| 2413 | return input_obj; |
| 2414 | } |
| 2415 | |
| 2416 | for (i = 0; i < 256; i++) |
| 2417 | trans_table[i] = Py_CHARMASK(table[i]); |
| 2418 | |
| 2419 | for (i = 0; i < dellen; i++) |
| 2420 | trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
| 2421 | |
| 2422 | for (i = inlen; --i >= 0; ) { |
| 2423 | c = Py_CHARMASK(*input++); |
| 2424 | if (trans_table[c] != -1) |
| 2425 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 2426 | continue; |
| 2427 | changed = 1; |
| 2428 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2429 | if (!changed && PyString_CheckExact(input_obj)) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2430 | Py_DECREF(result); |
| 2431 | Py_INCREF(input_obj); |
| 2432 | return input_obj; |
| 2433 | } |
| 2434 | /* Fix the size of the resulting string */ |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2435 | if (inlen > 0) |
| 2436 | _PyString_Resize(&result, output - output_start); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2437 | return result; |
| 2438 | } |
| 2439 | |
| 2440 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2441 | #define FORWARD 1 |
| 2442 | #define REVERSE -1 |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2443 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2444 | /* find and count characters and substrings */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2445 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2446 | #define findchar(target, target_len, c) \ |
| 2447 | ((char *)memchr((const void *)(target), c, target_len)) |
| 2448 | |
| 2449 | /* String ops must return a string. */ |
| 2450 | /* If the object is subclass of string, create a copy */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2451 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2452 | return_self(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2453 | { |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2454 | if (PyString_CheckExact(self)) { |
| 2455 | Py_INCREF(self); |
| 2456 | return self; |
| 2457 | } |
| 2458 | return (PyStringObject *)PyString_FromStringAndSize( |
| 2459 | PyString_AS_STRING(self), |
| 2460 | PyString_GET_SIZE(self)); |
| 2461 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2462 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2463 | Py_LOCAL(Py_ssize_t) |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2464 | countchar(char *target, int target_len, char c, Py_ssize_t maxcount) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2465 | { |
| 2466 | Py_ssize_t count=0; |
| 2467 | char *start=target; |
| 2468 | char *end=target+target_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2469 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2470 | while ( (start=findchar(start, end-start, c)) != NULL ) { |
| 2471 | count++; |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2472 | if (count >= maxcount) |
| 2473 | break; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2474 | start += 1; |
| 2475 | } |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2476 | return count; |
| 2477 | } |
| 2478 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2479 | Py_LOCAL(Py_ssize_t) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2480 | findstring(char *target, Py_ssize_t target_len, |
| 2481 | char *pattern, Py_ssize_t pattern_len, |
| 2482 | Py_ssize_t start, |
| 2483 | Py_ssize_t end, |
| 2484 | int direction) |
| 2485 | { |
| 2486 | if (start < 0) { |
| 2487 | start += target_len; |
| 2488 | if (start < 0) |
| 2489 | start = 0; |
| 2490 | } |
| 2491 | if (end > target_len) { |
| 2492 | end = target_len; |
| 2493 | } else if (end < 0) { |
| 2494 | end += target_len; |
| 2495 | if (end < 0) |
| 2496 | end = 0; |
| 2497 | } |
| 2498 | |
| 2499 | /* zero-length substrings always match at the first attempt */ |
| 2500 | if (pattern_len == 0) |
| 2501 | return (direction > 0) ? start : end; |
| 2502 | |
| 2503 | end -= pattern_len; |
| 2504 | |
| 2505 | if (direction < 0) { |
| 2506 | for (; end >= start; end--) |
| 2507 | if (Py_STRING_MATCH(target, end, pattern, pattern_len)) |
| 2508 | return end; |
| 2509 | } else { |
| 2510 | for (; start <= end; start++) |
| 2511 | if (Py_STRING_MATCH(target, start, pattern, pattern_len)) |
| 2512 | return start; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2513 | } |
| 2514 | return -1; |
| 2515 | } |
| 2516 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2517 | Py_LOCAL(Py_ssize_t) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2518 | countstring(char *target, Py_ssize_t target_len, |
| 2519 | char *pattern, Py_ssize_t pattern_len, |
| 2520 | Py_ssize_t start, |
| 2521 | Py_ssize_t end, |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2522 | int direction, Py_ssize_t maxcount) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2523 | { |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2524 | Py_ssize_t count=0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2525 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2526 | if (start < 0) { |
| 2527 | start += target_len; |
| 2528 | if (start < 0) |
| 2529 | start = 0; |
| 2530 | } |
| 2531 | if (end > target_len) { |
| 2532 | end = target_len; |
| 2533 | } else if (end < 0) { |
| 2534 | end += target_len; |
| 2535 | if (end < 0) |
| 2536 | end = 0; |
| 2537 | } |
| 2538 | |
| 2539 | /* zero-length substrings match everywhere */ |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2540 | if (pattern_len == 0 || maxcount == 0) { |
| 2541 | if (target_len+1 < maxcount) |
| 2542 | return target_len+1; |
| 2543 | return maxcount; |
| 2544 | } |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2545 | |
| 2546 | end -= pattern_len; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2547 | if (direction < 0) { |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2548 | for (; (end >= start); end--) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2549 | if (Py_STRING_MATCH(target, end, pattern, pattern_len)) { |
| 2550 | count++; |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2551 | if (--maxcount <= 0) break; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2552 | end -= pattern_len-1; |
| 2553 | } |
| 2554 | } else { |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2555 | for (; (start <= end); start++) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2556 | if (Py_STRING_MATCH(target, start, pattern, pattern_len)) { |
| 2557 | count++; |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2558 | if (--maxcount <= 0) |
| 2559 | break; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2560 | start += pattern_len-1; |
| 2561 | } |
| 2562 | } |
| 2563 | return count; |
| 2564 | } |
| 2565 | |
| 2566 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2567 | /* Algorithms for different cases of string replacement */ |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2568 | |
| 2569 | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2570 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2571 | replace_interleave(PyStringObject *self, |
| 2572 | PyStringObject *to, |
| 2573 | Py_ssize_t maxcount) |
| 2574 | { |
| 2575 | char *self_s, *to_s, *result_s; |
| 2576 | Py_ssize_t self_len, to_len, result_len; |
| 2577 | Py_ssize_t count, i, product; |
| 2578 | PyStringObject *result; |
| 2579 | |
| 2580 | self_len = PyString_GET_SIZE(self); |
| 2581 | to_len = PyString_GET_SIZE(to); |
| 2582 | |
| 2583 | /* 1 at the end plus 1 after every character */ |
| 2584 | count = self_len+1; |
| 2585 | if (maxcount < count) |
| 2586 | count = maxcount; |
| 2587 | |
| 2588 | /* Check for overflow */ |
| 2589 | /* result_len = count * to_len + self_len; */ |
| 2590 | product = count * to_len; |
| 2591 | if (product / to_len != count) { |
| 2592 | PyErr_SetString(PyExc_OverflowError, |
| 2593 | "replace string is too long"); |
| 2594 | return NULL; |
| 2595 | } |
| 2596 | result_len = product + self_len; |
| 2597 | if (result_len < 0) { |
| 2598 | PyErr_SetString(PyExc_OverflowError, |
| 2599 | "replace string is too long"); |
| 2600 | return NULL; |
| 2601 | } |
| 2602 | |
| 2603 | if (! (result = (PyStringObject *) |
| 2604 | PyString_FromStringAndSize(NULL, result_len)) ) |
| 2605 | return NULL; |
| 2606 | |
| 2607 | self_s = PyString_AS_STRING(self); |
| 2608 | to_s = PyString_AS_STRING(to); |
| 2609 | to_len = PyString_GET_SIZE(to); |
| 2610 | result_s = PyString_AS_STRING(result); |
| 2611 | |
| 2612 | /* TODO: special case single character, which doesn't need memcpy */ |
| 2613 | |
| 2614 | /* Lay the first one down (guaranteed this will occur) */ |
| 2615 | memcpy(result_s, to_s, to_len); |
| 2616 | result_s += to_len; |
| 2617 | count -= 1; |
| 2618 | |
| 2619 | for (i=0; i<count; i++) { |
| 2620 | *result_s++ = *self_s++; |
| 2621 | memcpy(result_s, to_s, to_len); |
| 2622 | result_s += to_len; |
| 2623 | } |
| 2624 | |
| 2625 | /* Copy the rest of the original string */ |
| 2626 | memcpy(result_s, self_s, self_len-i); |
| 2627 | |
| 2628 | return result; |
| 2629 | } |
| 2630 | |
| 2631 | /* Special case for deleting a single character */ |
| 2632 | /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2633 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2634 | replace_delete_single_character(PyStringObject *self, |
| 2635 | char from_c, Py_ssize_t maxcount) |
| 2636 | { |
| 2637 | char *self_s, *result_s; |
| 2638 | char *start, *next, *end; |
| 2639 | Py_ssize_t self_len, result_len; |
| 2640 | Py_ssize_t count; |
| 2641 | PyStringObject *result; |
| 2642 | |
| 2643 | self_len = PyString_GET_SIZE(self); |
| 2644 | self_s = PyString_AS_STRING(self); |
| 2645 | |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2646 | count = countchar(self_s, self_len, from_c, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2647 | if (count == 0) { |
| 2648 | return return_self(self); |
| 2649 | } |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2650 | |
| 2651 | result_len = self_len - count; /* from_len == 1 */ |
| 2652 | assert(result_len>=0); |
| 2653 | |
| 2654 | if ( (result = (PyStringObject *) |
| 2655 | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
| 2656 | return NULL; |
| 2657 | result_s = PyString_AS_STRING(result); |
| 2658 | |
| 2659 | start = self_s; |
| 2660 | end = self_s + self_len; |
| 2661 | while (count-- > 0) { |
| 2662 | next = findchar(start, end-start, from_c); |
| 2663 | if (next == NULL) |
| 2664 | break; |
| 2665 | memcpy(result_s, start, next-start); |
| 2666 | result_s += (next-start); |
| 2667 | start = next+1; |
| 2668 | } |
| 2669 | memcpy(result_s, start, end-start); |
| 2670 | |
| 2671 | return result; |
| 2672 | } |
| 2673 | |
| 2674 | /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
| 2675 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2676 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2677 | replace_delete_substring(PyStringObject *self, PyStringObject *from, |
| 2678 | Py_ssize_t maxcount) { |
| 2679 | char *self_s, *from_s, *result_s; |
| 2680 | char *start, *next, *end; |
| 2681 | Py_ssize_t self_len, from_len, result_len; |
| 2682 | Py_ssize_t count, offset; |
| 2683 | PyStringObject *result; |
| 2684 | |
| 2685 | self_len = PyString_GET_SIZE(self); |
| 2686 | self_s = PyString_AS_STRING(self); |
| 2687 | from_len = PyString_GET_SIZE(from); |
| 2688 | from_s = PyString_AS_STRING(from); |
| 2689 | |
| 2690 | count = countstring(self_s, self_len, |
| 2691 | from_s, from_len, |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2692 | 0, self_len, 1, |
| 2693 | maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2694 | |
| 2695 | if (count == 0) { |
| 2696 | /* no matches */ |
| 2697 | return return_self(self); |
| 2698 | } |
| 2699 | |
| 2700 | result_len = self_len - (count * from_len); |
| 2701 | assert (result_len>=0); |
| 2702 | |
| 2703 | if ( (result = (PyStringObject *) |
| 2704 | PyString_FromStringAndSize(NULL, result_len)) == NULL ) |
| 2705 | return NULL; |
| 2706 | |
| 2707 | result_s = PyString_AS_STRING(result); |
| 2708 | |
| 2709 | start = self_s; |
| 2710 | end = self_s + self_len; |
| 2711 | while (count-- > 0) { |
| 2712 | offset = findstring(start, end-start, |
| 2713 | from_s, from_len, |
| 2714 | 0, end-start, FORWARD); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2715 | if (offset == -1) |
| 2716 | break; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2717 | next = start + offset; |
| 2718 | |
| 2719 | memcpy(result_s, start, next-start); |
| 2720 | |
| 2721 | result_s += (next-start); |
| 2722 | start = next+from_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2723 | } |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2724 | memcpy(result_s, start, end-start); |
| 2725 | return result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2728 | /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2729 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2730 | replace_single_character_in_place(PyStringObject *self, |
| 2731 | char from_c, char to_c, |
| 2732 | Py_ssize_t maxcount) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2733 | { |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2734 | char *self_s, *result_s, *start, *end, *next; |
| 2735 | Py_ssize_t self_len; |
| 2736 | PyStringObject *result; |
| 2737 | |
| 2738 | /* The result string will be the same size */ |
| 2739 | self_s = PyString_AS_STRING(self); |
| 2740 | self_len = PyString_GET_SIZE(self); |
| 2741 | |
| 2742 | next = findchar(self_s, self_len, from_c); |
| 2743 | |
| 2744 | if (next == NULL) { |
| 2745 | /* No matches; return the original string */ |
| 2746 | return return_self(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2747 | } |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2748 | |
| 2749 | /* Need to make a new string */ |
Andrew Dalke | 8c90910 | 2006-05-25 17:53:00 +0000 | [diff] [blame] | 2750 | result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2751 | if (result == NULL) |
| 2752 | return NULL; |
| 2753 | result_s = PyString_AS_STRING(result); |
Andrew Dalke | 8c90910 | 2006-05-25 17:53:00 +0000 | [diff] [blame] | 2754 | memcpy(result_s, self_s, self_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2755 | |
| 2756 | /* change everything in-place, starting with this one */ |
| 2757 | start = result_s + (next-self_s); |
| 2758 | *start = to_c; |
| 2759 | start++; |
| 2760 | end = result_s + self_len; |
| 2761 | |
| 2762 | while (--maxcount > 0) { |
| 2763 | next = findchar(start, end-start, from_c); |
| 2764 | if (next == NULL) |
| 2765 | break; |
| 2766 | *next = to_c; |
| 2767 | start = next+1; |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 2768 | } |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2769 | |
| 2770 | return result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2773 | /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2774 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2775 | replace_substring_in_place(PyStringObject *self, |
| 2776 | PyStringObject *from, |
| 2777 | PyStringObject *to, |
| 2778 | Py_ssize_t maxcount) |
| 2779 | { |
| 2780 | char *result_s, *start, *end; |
| 2781 | char *self_s, *from_s, *to_s; |
| 2782 | Py_ssize_t self_len, from_len, offset; |
| 2783 | PyStringObject *result; |
| 2784 | |
| 2785 | /* The result string will be the same size */ |
| 2786 | |
| 2787 | self_s = PyString_AS_STRING(self); |
| 2788 | self_len = PyString_GET_SIZE(self); |
| 2789 | |
| 2790 | from_s = PyString_AS_STRING(from); |
| 2791 | from_len = PyString_GET_SIZE(from); |
| 2792 | to_s = PyString_AS_STRING(to); |
| 2793 | |
| 2794 | offset = findstring(self_s, self_len, |
| 2795 | from_s, from_len, |
| 2796 | 0, self_len, FORWARD); |
| 2797 | |
| 2798 | if (offset == -1) { |
| 2799 | /* No matches; return the original string */ |
| 2800 | return return_self(self); |
| 2801 | } |
| 2802 | |
| 2803 | /* Need to make a new string */ |
Andrew Dalke | 8c90910 | 2006-05-25 17:53:00 +0000 | [diff] [blame] | 2804 | result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2805 | if (result == NULL) |
| 2806 | return NULL; |
| 2807 | result_s = PyString_AS_STRING(result); |
Andrew Dalke | 8c90910 | 2006-05-25 17:53:00 +0000 | [diff] [blame] | 2808 | memcpy(result_s, self_s, self_len); |
| 2809 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2810 | |
| 2811 | /* change everything in-place, starting with this one */ |
| 2812 | start = result_s + offset; |
| 2813 | memcpy(start, to_s, from_len); |
| 2814 | start += from_len; |
| 2815 | end = result_s + self_len; |
| 2816 | |
| 2817 | while ( --maxcount > 0) { |
| 2818 | offset = findstring(start, end-start, |
| 2819 | from_s, from_len, |
| 2820 | 0, end-start, FORWARD); |
| 2821 | if (offset==-1) |
| 2822 | break; |
| 2823 | memcpy(start+offset, to_s, from_len); |
| 2824 | start += offset+from_len; |
| 2825 | } |
| 2826 | |
| 2827 | return result; |
| 2828 | } |
| 2829 | |
| 2830 | /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2831 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2832 | replace_single_character(PyStringObject *self, |
| 2833 | char from_c, |
| 2834 | PyStringObject *to, |
| 2835 | Py_ssize_t maxcount) |
| 2836 | { |
| 2837 | char *self_s, *to_s, *result_s; |
| 2838 | char *start, *next, *end; |
| 2839 | Py_ssize_t self_len, to_len, result_len; |
| 2840 | Py_ssize_t count, product; |
| 2841 | PyStringObject *result; |
| 2842 | |
| 2843 | self_s = PyString_AS_STRING(self); |
| 2844 | self_len = PyString_GET_SIZE(self); |
| 2845 | |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2846 | count = countchar(self_s, self_len, from_c, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2847 | |
| 2848 | if (count == 0) { |
| 2849 | /* no matches, return unchanged */ |
| 2850 | return return_self(self); |
| 2851 | } |
| 2852 | |
| 2853 | to_s = PyString_AS_STRING(to); |
| 2854 | to_len = PyString_GET_SIZE(to); |
| 2855 | |
| 2856 | /* use the difference between current and new, hence the "-1" */ |
| 2857 | /* result_len = self_len + count * (to_len-1) */ |
| 2858 | product = count * (to_len-1); |
| 2859 | if (product / (to_len-1) != count) { |
| 2860 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 2861 | return NULL; |
| 2862 | } |
| 2863 | result_len = self_len + product; |
| 2864 | if (result_len < 0) { |
| 2865 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 2866 | return NULL; |
| 2867 | } |
| 2868 | |
| 2869 | if ( (result = (PyStringObject *) |
| 2870 | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
| 2871 | return NULL; |
| 2872 | result_s = PyString_AS_STRING(result); |
| 2873 | |
| 2874 | start = self_s; |
| 2875 | end = self_s + self_len; |
| 2876 | while (count-- > 0) { |
| 2877 | next = findchar(start, end-start, from_c); |
| 2878 | if (next == NULL) |
| 2879 | break; |
| 2880 | |
| 2881 | if (next == start) { |
| 2882 | /* replace with the 'to' */ |
| 2883 | memcpy(result_s, to_s, to_len); |
| 2884 | result_s += to_len; |
| 2885 | start += 1; |
| 2886 | } else { |
| 2887 | /* copy the unchanged old then the 'to' */ |
| 2888 | memcpy(result_s, start, next-start); |
| 2889 | result_s += (next-start); |
| 2890 | memcpy(result_s, to_s, to_len); |
| 2891 | result_s += to_len; |
| 2892 | start = next+1; |
| 2893 | } |
| 2894 | } |
| 2895 | /* Copy the remainder of the remaining string */ |
| 2896 | memcpy(result_s, start, end-start); |
| 2897 | |
| 2898 | return result; |
| 2899 | } |
| 2900 | |
| 2901 | /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2902 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2903 | replace_substring(PyStringObject *self, |
| 2904 | PyStringObject *from, |
| 2905 | PyStringObject *to, |
| 2906 | Py_ssize_t maxcount) { |
| 2907 | char *self_s, *from_s, *to_s, *result_s; |
| 2908 | char *start, *next, *end; |
| 2909 | Py_ssize_t self_len, from_len, to_len, result_len; |
| 2910 | Py_ssize_t count, offset, product; |
| 2911 | PyStringObject *result; |
| 2912 | |
| 2913 | self_s = PyString_AS_STRING(self); |
| 2914 | self_len = PyString_GET_SIZE(self); |
| 2915 | from_s = PyString_AS_STRING(from); |
| 2916 | from_len = PyString_GET_SIZE(from); |
| 2917 | |
| 2918 | count = countstring(self_s, self_len, |
| 2919 | from_s, from_len, |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2920 | 0, self_len, FORWARD, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2921 | if (count == 0) { |
| 2922 | /* no matches, return unchanged */ |
| 2923 | return return_self(self); |
| 2924 | } |
| 2925 | |
| 2926 | to_s = PyString_AS_STRING(to); |
| 2927 | to_len = PyString_GET_SIZE(to); |
| 2928 | |
| 2929 | /* Check for overflow */ |
| 2930 | /* result_len = self_len + count * (to_len-from_len) */ |
| 2931 | product = count * (to_len-from_len); |
| 2932 | if (product / (to_len-from_len) != count) { |
| 2933 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 2934 | return NULL; |
| 2935 | } |
| 2936 | result_len = self_len + product; |
| 2937 | if (result_len < 0) { |
| 2938 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 2939 | return NULL; |
| 2940 | } |
| 2941 | |
| 2942 | if ( (result = (PyStringObject *) |
| 2943 | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
| 2944 | return NULL; |
| 2945 | result_s = PyString_AS_STRING(result); |
| 2946 | |
| 2947 | start = self_s; |
| 2948 | end = self_s + self_len; |
| 2949 | while (count-- > 0) { |
| 2950 | offset = findstring(start, end-start, |
| 2951 | from_s, from_len, |
| 2952 | 0, end-start, FORWARD); |
| 2953 | if (offset == -1) |
| 2954 | break; |
| 2955 | next = start+offset; |
| 2956 | if (next == start) { |
| 2957 | /* replace with the 'to' */ |
| 2958 | memcpy(result_s, to_s, to_len); |
| 2959 | result_s += to_len; |
| 2960 | start += from_len; |
| 2961 | } else { |
| 2962 | /* copy the unchanged old then the 'to' */ |
| 2963 | memcpy(result_s, start, next-start); |
| 2964 | result_s += (next-start); |
| 2965 | memcpy(result_s, to_s, to_len); |
| 2966 | result_s += to_len; |
| 2967 | start = next+from_len; |
| 2968 | } |
| 2969 | } |
| 2970 | /* Copy the remainder of the remaining string */ |
| 2971 | memcpy(result_s, start, end-start); |
| 2972 | |
| 2973 | return result; |
| 2974 | } |
| 2975 | |
| 2976 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2977 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2978 | replace(PyStringObject *self, |
| 2979 | PyStringObject *from, |
| 2980 | PyStringObject *to, |
| 2981 | Py_ssize_t maxcount) |
| 2982 | { |
| 2983 | Py_ssize_t from_len, to_len; |
| 2984 | |
| 2985 | if (maxcount < 0) { |
| 2986 | maxcount = PY_SSIZE_T_MAX; |
| 2987 | } else if (maxcount == 0 || PyString_GET_SIZE(self) == 0) { |
| 2988 | /* nothing to do; return the original string */ |
| 2989 | return return_self(self); |
| 2990 | } |
| 2991 | |
| 2992 | from_len = PyString_GET_SIZE(from); |
| 2993 | to_len = PyString_GET_SIZE(to); |
| 2994 | |
| 2995 | if (maxcount == 0 || |
| 2996 | (from_len == 0 && to_len == 0)) { |
| 2997 | /* nothing to do; return the original string */ |
| 2998 | return return_self(self); |
| 2999 | } |
| 3000 | |
| 3001 | /* Handle zero-length special cases */ |
| 3002 | |
| 3003 | if (from_len == 0) { |
| 3004 | /* insert the 'to' string everywhere. */ |
| 3005 | /* >>> "Python".replace("", ".") */ |
| 3006 | /* '.P.y.t.h.o.n.' */ |
| 3007 | return replace_interleave(self, to, maxcount); |
| 3008 | } |
| 3009 | |
| 3010 | /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
| 3011 | /* point for an empty self string to generate a non-empty string */ |
| 3012 | /* Special case so the remaining code always gets a non-empty string */ |
| 3013 | if (PyString_GET_SIZE(self) == 0) { |
| 3014 | return return_self(self); |
| 3015 | } |
| 3016 | |
| 3017 | if (to_len == 0) { |
| 3018 | /* delete all occurances of 'from' string */ |
| 3019 | if (from_len == 1) { |
| 3020 | return replace_delete_single_character( |
| 3021 | self, PyString_AS_STRING(from)[0], maxcount); |
| 3022 | } else { |
| 3023 | return replace_delete_substring(self, from, maxcount); |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | /* Handle special case where both strings have the same length */ |
| 3028 | |
| 3029 | if (from_len == to_len) { |
| 3030 | if (from_len == 1) { |
| 3031 | return replace_single_character_in_place( |
| 3032 | self, |
| 3033 | PyString_AS_STRING(from)[0], |
| 3034 | PyString_AS_STRING(to)[0], |
| 3035 | maxcount); |
| 3036 | } else { |
| 3037 | return replace_substring_in_place( |
| 3038 | self, from, to, maxcount); |
| 3039 | } |
| 3040 | } |
| 3041 | |
| 3042 | /* Otherwise use the more generic algorithms */ |
| 3043 | if (from_len == 1) { |
| 3044 | return replace_single_character(self, PyString_AS_STRING(from)[0], |
| 3045 | to, maxcount); |
| 3046 | } else { |
| 3047 | /* len('from')>=2, len('to')>=1 */ |
| 3048 | return replace_substring(self, from, to, maxcount); |
| 3049 | } |
| 3050 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3051 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3052 | PyDoc_STRVAR(replace__doc__, |
Fred Drake | d22bb65 | 2003-10-22 02:56:40 +0000 | [diff] [blame] | 3053 | "S.replace (old, new[, count]) -> string\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3054 | \n\ |
| 3055 | Return a copy of string S with all occurrences of substring\n\ |
Fred Drake | d22bb65 | 2003-10-22 02:56:40 +0000 | [diff] [blame] | 3056 | old replaced by new. If the optional argument count is\n\ |
| 3057 | given, only the first count occurrences are replaced."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3058 | |
| 3059 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3060 | string_replace(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3061 | { |
Thomas Wouters | dc5f808 | 2006-04-19 15:38:01 +0000 | [diff] [blame] | 3062 | Py_ssize_t count = -1; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3063 | PyObject *from, *to; |
Jack Diederich | 60cbb3f | 2006-05-25 18:47:15 +0000 | [diff] [blame] | 3064 | const char *tmp_s; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3065 | Py_ssize_t tmp_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3066 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3067 | if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3068 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3069 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3070 | if (PyString_Check(from)) { |
| 3071 | /* Can this be made a '!check' after the Unicode check? */ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3072 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3073 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3074 | if (PyUnicode_Check(from)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3075 | return PyUnicode_Replace((PyObject *)self, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3076 | from, to, count); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3077 | #endif |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3078 | else if (PyObject_AsCharBuffer(from, &tmp_s, &tmp_len)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3079 | return NULL; |
| 3080 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3081 | if (PyString_Check(to)) { |
| 3082 | /* Can this be made a '!check' after the Unicode check? */ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3083 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3084 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3085 | else if (PyUnicode_Check(to)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3086 | return PyUnicode_Replace((PyObject *)self, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3087 | from, to, count); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3088 | #endif |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3089 | else if (PyObject_AsCharBuffer(to, &tmp_s, &tmp_len)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3090 | return NULL; |
| 3091 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3092 | return (PyObject *)replace((PyStringObject *) self, |
| 3093 | (PyStringObject *) from, |
| 3094 | (PyStringObject *) to, count); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3097 | /** End DALKE **/ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3098 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3099 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3100 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3101 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 3102 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 3103 | With optional start, test S beginning at that position.\n\ |
| 3104 | With optional end, stop comparing S at that position."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3105 | |
| 3106 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3107 | string_startswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3108 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3109 | const char* str = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3110 | Py_ssize_t len = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3111 | const char* prefix; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3112 | Py_ssize_t plen; |
| 3113 | Py_ssize_t start = 0; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 3114 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3115 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3116 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 3117 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 3118 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3119 | return NULL; |
| 3120 | if (PyString_Check(subobj)) { |
| 3121 | prefix = PyString_AS_STRING(subobj); |
| 3122 | plen = PyString_GET_SIZE(subobj); |
| 3123 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3124 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3125 | else if (PyUnicode_Check(subobj)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3126 | Py_ssize_t rc; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3127 | rc = PyUnicode_Tailmatch((PyObject *)self, |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3128 | subobj, start, end, -1); |
| 3129 | if (rc == -1) |
| 3130 | return NULL; |
| 3131 | else |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3132 | return PyBool_FromLong((long) rc); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3133 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3134 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3135 | else if (PyObject_AsCharBuffer(subobj, &prefix, &plen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3136 | return NULL; |
| 3137 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 3138 | string_adjust_indices(&start, &end, len); |
| 3139 | |
| 3140 | if (start+plen > len) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3141 | return PyBool_FromLong(0); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3142 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 3143 | if (end-start >= plen) |
| 3144 | return PyBool_FromLong(!memcmp(str+start, prefix, plen)); |
| 3145 | else |
| 3146 | return PyBool_FromLong(0); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3150 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3151 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3152 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 3153 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 3154 | With optional start, test S beginning at that position.\n\ |
| 3155 | With optional end, stop comparing S at that position."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3156 | |
| 3157 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3158 | string_endswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3159 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3160 | const char* str = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3161 | Py_ssize_t len = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3162 | const char* suffix; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3163 | Py_ssize_t slen; |
| 3164 | Py_ssize_t start = 0; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 3165 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3166 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3167 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 3168 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 3169 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3170 | return NULL; |
| 3171 | if (PyString_Check(subobj)) { |
| 3172 | suffix = PyString_AS_STRING(subobj); |
| 3173 | slen = PyString_GET_SIZE(subobj); |
| 3174 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3175 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3176 | else if (PyUnicode_Check(subobj)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3177 | Py_ssize_t rc; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3178 | rc = PyUnicode_Tailmatch((PyObject *)self, |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3179 | subobj, start, end, +1); |
| 3180 | if (rc == -1) |
| 3181 | return NULL; |
| 3182 | else |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3183 | return PyBool_FromLong((long) rc); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3184 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3185 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3186 | else if (PyObject_AsCharBuffer(subobj, &suffix, &slen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3187 | return NULL; |
| 3188 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 3189 | string_adjust_indices(&start, &end, len); |
| 3190 | |
| 3191 | if (end-start < slen || start > len) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3192 | return PyBool_FromLong(0); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3193 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 3194 | if (end-slen > start) |
| 3195 | start = end - slen; |
| 3196 | if (end-start >= slen) |
| 3197 | return PyBool_FromLong(!memcmp(str+start, suffix, slen)); |
| 3198 | else |
| 3199 | return PyBool_FromLong(0); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
| 3202 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3203 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3204 | "S.encode([encoding[,errors]]) -> object\n\ |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3205 | \n\ |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3206 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 3207 | 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] | 3208 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3209 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 3210 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 3211 | codecs.register_error that is able to handle UnicodeEncodeErrors."); |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3212 | |
| 3213 | static PyObject * |
| 3214 | string_encode(PyStringObject *self, PyObject *args) |
| 3215 | { |
| 3216 | char *encoding = NULL; |
| 3217 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3218 | PyObject *v; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 3219 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3220 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 3221 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3222 | v = PyString_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3223 | if (v == NULL) |
| 3224 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3225 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 3226 | PyErr_Format(PyExc_TypeError, |
| 3227 | "encoder did not return a string/unicode object " |
| 3228 | "(type=%.400s)", |
| 3229 | v->ob_type->tp_name); |
| 3230 | Py_DECREF(v); |
| 3231 | return NULL; |
| 3232 | } |
| 3233 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3234 | |
| 3235 | onError: |
| 3236 | return NULL; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3240 | PyDoc_STRVAR(decode__doc__, |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3241 | "S.decode([encoding[,errors]]) -> object\n\ |
| 3242 | \n\ |
| 3243 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 3244 | to the default encoding. errors may be given to set a different error\n\ |
| 3245 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3246 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 3247 | as well as any other name registerd with codecs.register_error that is\n\ |
| 3248 | able to handle UnicodeDecodeErrors."); |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3249 | |
| 3250 | static PyObject * |
| 3251 | string_decode(PyStringObject *self, PyObject *args) |
| 3252 | { |
| 3253 | char *encoding = NULL; |
| 3254 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3255 | PyObject *v; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 3256 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3257 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 3258 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3259 | v = PyString_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3260 | if (v == NULL) |
| 3261 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3262 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 3263 | PyErr_Format(PyExc_TypeError, |
| 3264 | "decoder did not return a string/unicode object " |
| 3265 | "(type=%.400s)", |
| 3266 | v->ob_type->tp_name); |
| 3267 | Py_DECREF(v); |
| 3268 | return NULL; |
| 3269 | } |
| 3270 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3271 | |
| 3272 | onError: |
| 3273 | return NULL; |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3274 | } |
| 3275 | |
| 3276 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3277 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3278 | "S.expandtabs([tabsize]) -> string\n\ |
| 3279 | \n\ |
| 3280 | 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] | 3281 | 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] | 3282 | |
| 3283 | static PyObject* |
| 3284 | string_expandtabs(PyStringObject *self, PyObject *args) |
| 3285 | { |
| 3286 | const char *e, *p; |
| 3287 | char *q; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3288 | Py_ssize_t i, j; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3289 | PyObject *u; |
| 3290 | int tabsize = 8; |
| 3291 | |
| 3292 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 3293 | return NULL; |
| 3294 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 3295 | /* First pass: determine size of output string */ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3296 | i = j = 0; |
| 3297 | e = PyString_AS_STRING(self) + PyString_GET_SIZE(self); |
| 3298 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 3299 | if (*p == '\t') { |
| 3300 | if (tabsize > 0) |
| 3301 | j += tabsize - (j % tabsize); |
| 3302 | } |
| 3303 | else { |
| 3304 | j++; |
| 3305 | if (*p == '\n' || *p == '\r') { |
| 3306 | i += j; |
| 3307 | j = 0; |
| 3308 | } |
| 3309 | } |
| 3310 | |
| 3311 | /* Second pass: create output string and fill it */ |
| 3312 | u = PyString_FromStringAndSize(NULL, i + j); |
| 3313 | if (!u) |
| 3314 | return NULL; |
| 3315 | |
| 3316 | j = 0; |
| 3317 | q = PyString_AS_STRING(u); |
| 3318 | |
| 3319 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 3320 | if (*p == '\t') { |
| 3321 | if (tabsize > 0) { |
| 3322 | i = tabsize - (j % tabsize); |
| 3323 | j += i; |
| 3324 | while (i--) |
| 3325 | *q++ = ' '; |
| 3326 | } |
| 3327 | } |
| 3328 | else { |
| 3329 | j++; |
| 3330 | *q++ = *p; |
| 3331 | if (*p == '\n' || *p == '\r') |
| 3332 | j = 0; |
| 3333 | } |
| 3334 | |
| 3335 | return u; |
| 3336 | } |
| 3337 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 3338 | Py_LOCAL(PyObject *) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3339 | 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] | 3340 | { |
| 3341 | PyObject *u; |
| 3342 | |
| 3343 | if (left < 0) |
| 3344 | left = 0; |
| 3345 | if (right < 0) |
| 3346 | right = 0; |
| 3347 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3348 | if (left == 0 && right == 0 && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3349 | Py_INCREF(self); |
| 3350 | return (PyObject *)self; |
| 3351 | } |
| 3352 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3353 | u = PyString_FromStringAndSize(NULL, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3354 | left + PyString_GET_SIZE(self) + right); |
| 3355 | if (u) { |
| 3356 | if (left) |
| 3357 | memset(PyString_AS_STRING(u), fill, left); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3358 | memcpy(PyString_AS_STRING(u) + left, |
| 3359 | PyString_AS_STRING(self), |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3360 | PyString_GET_SIZE(self)); |
| 3361 | if (right) |
| 3362 | memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self), |
| 3363 | fill, right); |
| 3364 | } |
| 3365 | |
| 3366 | return u; |
| 3367 | } |
| 3368 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3369 | PyDoc_STRVAR(ljust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3370 | "S.ljust(width[, fillchar]) -> string\n" |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3371 | "\n" |
| 3372 | "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] | 3373 | "done using the specified fill character (default is a space)."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3374 | |
| 3375 | static PyObject * |
| 3376 | string_ljust(PyStringObject *self, PyObject *args) |
| 3377 | { |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3378 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3379 | char fillchar = ' '; |
| 3380 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3381 | if (!PyArg_ParseTuple(args, "n|c:ljust", &width, &fillchar)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3382 | return NULL; |
| 3383 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3384 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3385 | Py_INCREF(self); |
| 3386 | return (PyObject*) self; |
| 3387 | } |
| 3388 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3389 | return pad(self, 0, width - PyString_GET_SIZE(self), fillchar); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3390 | } |
| 3391 | |
| 3392 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3393 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3394 | "S.rjust(width[, fillchar]) -> string\n" |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3395 | "\n" |
| 3396 | "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] | 3397 | "done using the specified fill character (default is a space)"); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3398 | |
| 3399 | static PyObject * |
| 3400 | string_rjust(PyStringObject *self, PyObject *args) |
| 3401 | { |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3402 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3403 | char fillchar = ' '; |
| 3404 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3405 | if (!PyArg_ParseTuple(args, "n|c:rjust", &width, &fillchar)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3406 | return NULL; |
| 3407 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3408 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3409 | Py_INCREF(self); |
| 3410 | return (PyObject*) self; |
| 3411 | } |
| 3412 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3413 | return pad(self, width - PyString_GET_SIZE(self), 0, fillchar); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
| 3416 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3417 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3418 | "S.center(width[, fillchar]) -> string\n" |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3419 | "\n" |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3420 | "Return S centered in a string of length width. Padding is\n" |
| 3421 | "done using the specified fill character (default is a space)"); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3422 | |
| 3423 | static PyObject * |
| 3424 | string_center(PyStringObject *self, PyObject *args) |
| 3425 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3426 | Py_ssize_t marg, left; |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3427 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3428 | char fillchar = ' '; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3429 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3430 | if (!PyArg_ParseTuple(args, "n|c:center", &width, &fillchar)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3431 | return NULL; |
| 3432 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3433 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3434 | Py_INCREF(self); |
| 3435 | return (PyObject*) self; |
| 3436 | } |
| 3437 | |
| 3438 | marg = width - PyString_GET_SIZE(self); |
| 3439 | left = marg / 2 + (marg & width & 1); |
| 3440 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3441 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3442 | } |
| 3443 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3444 | PyDoc_STRVAR(zfill__doc__, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3445 | "S.zfill(width) -> string\n" |
| 3446 | "\n" |
| 3447 | "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] | 3448 | "of the specified width. The string S is never truncated."); |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3449 | |
| 3450 | static PyObject * |
| 3451 | string_zfill(PyStringObject *self, PyObject *args) |
| 3452 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3453 | Py_ssize_t fill; |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3454 | PyObject *s; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3455 | char *p; |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3456 | Py_ssize_t width; |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3457 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3458 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3459 | return NULL; |
| 3460 | |
| 3461 | if (PyString_GET_SIZE(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 3462 | if (PyString_CheckExact(self)) { |
| 3463 | Py_INCREF(self); |
| 3464 | return (PyObject*) self; |
| 3465 | } |
| 3466 | else |
| 3467 | return PyString_FromStringAndSize( |
| 3468 | PyString_AS_STRING(self), |
| 3469 | PyString_GET_SIZE(self) |
| 3470 | ); |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3471 | } |
| 3472 | |
| 3473 | fill = width - PyString_GET_SIZE(self); |
| 3474 | |
| 3475 | s = pad(self, fill, 0, '0'); |
| 3476 | |
| 3477 | if (s == NULL) |
| 3478 | return NULL; |
| 3479 | |
| 3480 | p = PyString_AS_STRING(s); |
| 3481 | if (p[fill] == '+' || p[fill] == '-') { |
| 3482 | /* move sign to beginning of string */ |
| 3483 | p[0] = p[fill]; |
| 3484 | p[fill] = '0'; |
| 3485 | } |
| 3486 | |
| 3487 | return (PyObject*) s; |
| 3488 | } |
| 3489 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3490 | PyDoc_STRVAR(isspace__doc__, |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3491 | "S.isspace() -> bool\n\ |
| 3492 | \n\ |
| 3493 | Return True if all characters in S are whitespace\n\ |
| 3494 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3495 | |
| 3496 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3497 | string_isspace(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3498 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3499 | register const unsigned char *p |
| 3500 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3501 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3502 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3503 | /* Shortcut for single character strings */ |
| 3504 | if (PyString_GET_SIZE(self) == 1 && |
| 3505 | isspace(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3506 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3507 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3508 | /* Special case for empty strings */ |
| 3509 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3510 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3511 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3512 | e = p + PyString_GET_SIZE(self); |
| 3513 | for (; p < e; p++) { |
| 3514 | if (!isspace(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3515 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3516 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3517 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3521 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3522 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3523 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3524 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3525 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3526 | |
| 3527 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3528 | string_isalpha(PyStringObject *self) |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3529 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3530 | register const unsigned char *p |
| 3531 | = (unsigned char *) PyString_AS_STRING(self); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3532 | register const unsigned char *e; |
| 3533 | |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3534 | /* Shortcut for single character strings */ |
| 3535 | if (PyString_GET_SIZE(self) == 1 && |
| 3536 | isalpha(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3537 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3538 | |
| 3539 | /* Special case for empty strings */ |
| 3540 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3541 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3542 | |
| 3543 | e = p + PyString_GET_SIZE(self); |
| 3544 | for (; p < e; p++) { |
| 3545 | if (!isalpha(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3546 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3547 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3548 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3549 | } |
| 3550 | |
| 3551 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3552 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3553 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3554 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3555 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3556 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3557 | |
| 3558 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3559 | string_isalnum(PyStringObject *self) |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3560 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3561 | register const unsigned char *p |
| 3562 | = (unsigned char *) PyString_AS_STRING(self); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3563 | register const unsigned char *e; |
| 3564 | |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3565 | /* Shortcut for single character strings */ |
| 3566 | if (PyString_GET_SIZE(self) == 1 && |
| 3567 | isalnum(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3568 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3569 | |
| 3570 | /* Special case for empty strings */ |
| 3571 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3572 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3573 | |
| 3574 | e = p + PyString_GET_SIZE(self); |
| 3575 | for (; p < e; p++) { |
| 3576 | if (!isalnum(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3577 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3578 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3579 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3580 | } |
| 3581 | |
| 3582 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3583 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3584 | "S.isdigit() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3585 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3586 | Return True if all characters in S are digits\n\ |
| 3587 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3588 | |
| 3589 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3590 | string_isdigit(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3591 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3592 | register const unsigned char *p |
| 3593 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3594 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3595 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3596 | /* Shortcut for single character strings */ |
| 3597 | if (PyString_GET_SIZE(self) == 1 && |
| 3598 | isdigit(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3599 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3600 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3601 | /* Special case for empty strings */ |
| 3602 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3603 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3604 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3605 | e = p + PyString_GET_SIZE(self); |
| 3606 | for (; p < e; p++) { |
| 3607 | if (!isdigit(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3608 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3609 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3610 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3614 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3615 | "S.islower() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3616 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3617 | 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] | 3618 | at least one cased character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3619 | |
| 3620 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3621 | string_islower(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3622 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3623 | register const unsigned char *p |
| 3624 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3625 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3626 | int cased; |
| 3627 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3628 | /* Shortcut for single character strings */ |
| 3629 | if (PyString_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3630 | return PyBool_FromLong(islower(*p) != 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3631 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3632 | /* Special case for empty strings */ |
| 3633 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3634 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3635 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3636 | e = p + PyString_GET_SIZE(self); |
| 3637 | cased = 0; |
| 3638 | for (; p < e; p++) { |
| 3639 | if (isupper(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3640 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3641 | else if (!cased && islower(*p)) |
| 3642 | cased = 1; |
| 3643 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3644 | return PyBool_FromLong(cased); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3645 | } |
| 3646 | |
| 3647 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3648 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3649 | "S.isupper() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3650 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3651 | 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] | 3652 | at least one cased character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3653 | |
| 3654 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3655 | string_isupper(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3656 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3657 | register const unsigned char *p |
| 3658 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3659 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3660 | int cased; |
| 3661 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3662 | /* Shortcut for single character strings */ |
| 3663 | if (PyString_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3664 | return PyBool_FromLong(isupper(*p) != 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3665 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3666 | /* Special case for empty strings */ |
| 3667 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3668 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3669 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3670 | e = p + PyString_GET_SIZE(self); |
| 3671 | cased = 0; |
| 3672 | for (; p < e; p++) { |
| 3673 | if (islower(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3674 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3675 | else if (!cased && isupper(*p)) |
| 3676 | cased = 1; |
| 3677 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3678 | return PyBool_FromLong(cased); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3682 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3683 | "S.istitle() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3684 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3685 | Return True if S is a titlecased string and there is at least one\n\ |
| 3686 | character in S, i.e. uppercase characters may only follow uncased\n\ |
| 3687 | characters and lowercase characters only cased ones. Return False\n\ |
| 3688 | otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3689 | |
| 3690 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3691 | string_istitle(PyStringObject *self, PyObject *uncased) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3692 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3693 | register const unsigned char *p |
| 3694 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3695 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3696 | int cased, previous_is_cased; |
| 3697 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3698 | /* Shortcut for single character strings */ |
| 3699 | if (PyString_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3700 | return PyBool_FromLong(isupper(*p) != 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3701 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3702 | /* Special case for empty strings */ |
| 3703 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3704 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3705 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3706 | e = p + PyString_GET_SIZE(self); |
| 3707 | cased = 0; |
| 3708 | previous_is_cased = 0; |
| 3709 | for (; p < e; p++) { |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3710 | register const unsigned char ch = *p; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3711 | |
| 3712 | if (isupper(ch)) { |
| 3713 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3714 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3715 | previous_is_cased = 1; |
| 3716 | cased = 1; |
| 3717 | } |
| 3718 | else if (islower(ch)) { |
| 3719 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3720 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3721 | previous_is_cased = 1; |
| 3722 | cased = 1; |
| 3723 | } |
| 3724 | else |
| 3725 | previous_is_cased = 0; |
| 3726 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3727 | return PyBool_FromLong(cased); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
| 3730 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3731 | PyDoc_STRVAR(splitlines__doc__, |
Fred Drake | 2bae4fa | 2001-10-13 15:57:55 +0000 | [diff] [blame] | 3732 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3733 | \n\ |
| 3734 | 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] | 3735 | 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] | 3736 | is given and true."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3737 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3738 | static PyObject* |
| 3739 | string_splitlines(PyStringObject *self, PyObject *args) |
| 3740 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3741 | register Py_ssize_t i; |
| 3742 | register Py_ssize_t j; |
| 3743 | Py_ssize_t len; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3744 | int keepends = 0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3745 | PyObject *list; |
| 3746 | PyObject *str; |
| 3747 | char *data; |
| 3748 | |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3749 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3750 | return NULL; |
| 3751 | |
| 3752 | data = PyString_AS_STRING(self); |
| 3753 | len = PyString_GET_SIZE(self); |
| 3754 | |
Andrew Dalke | 7e0a62e | 2006-05-26 22:49:03 +0000 | [diff] [blame] | 3755 | /* This does not use the preallocated list because splitlines is |
| 3756 | usually run with hundreds of newlines. The overhead of |
| 3757 | switching between PyList_SET_ITEM and append causes about a |
| 3758 | 2-3% slowdown for that common case. A smarter implementation |
| 3759 | could move the if check out, so the SET_ITEMs are done first |
| 3760 | and the appends only done when the prealloc buffer is full. |
| 3761 | That's too much work for little gain.*/ |
| 3762 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3763 | list = PyList_New(0); |
| 3764 | if (!list) |
| 3765 | goto onError; |
| 3766 | |
| 3767 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3768 | Py_ssize_t eol; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3769 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3770 | /* Find a line and append it */ |
| 3771 | while (i < len && data[i] != '\n' && data[i] != '\r') |
| 3772 | i++; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3773 | |
| 3774 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3775 | eol = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3776 | if (i < len) { |
| 3777 | if (data[i] == '\r' && i + 1 < len && |
| 3778 | data[i+1] == '\n') |
| 3779 | i += 2; |
| 3780 | else |
| 3781 | i++; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3782 | if (keepends) |
| 3783 | eol = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3784 | } |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3785 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3786 | j = i; |
| 3787 | } |
| 3788 | if (j < len) { |
| 3789 | SPLIT_APPEND(data, j, len); |
| 3790 | } |
| 3791 | |
| 3792 | return list; |
| 3793 | |
| 3794 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3795 | Py_XDECREF(list); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3796 | return NULL; |
| 3797 | } |
| 3798 | |
| 3799 | #undef SPLIT_APPEND |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 3800 | #undef SPLIT_ADD |
| 3801 | #undef MAX_PREALLOC |
| 3802 | #undef PREALLOC_SIZE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3803 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3804 | static PyObject * |
| 3805 | string_getnewargs(PyStringObject *v) |
| 3806 | { |
| 3807 | return Py_BuildValue("(s#)", v->ob_sval, v->ob_size); |
| 3808 | } |
| 3809 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3810 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3811 | static PyMethodDef |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3812 | string_methods[] = { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3813 | /* Counterparts of the obsolete stropmodule functions; except |
| 3814 | string.maketrans(). */ |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3815 | {"join", (PyCFunction)string_join, METH_O, join__doc__}, |
| 3816 | {"split", (PyCFunction)string_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 3817 | {"rsplit", (PyCFunction)string_rsplit, METH_VARARGS, rsplit__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3818 | {"lower", (PyCFunction)string_lower, METH_NOARGS, lower__doc__}, |
| 3819 | {"upper", (PyCFunction)string_upper, METH_NOARGS, upper__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3820 | {"islower", (PyCFunction)string_islower, METH_NOARGS, islower__doc__}, |
| 3821 | {"isupper", (PyCFunction)string_isupper, METH_NOARGS, isupper__doc__}, |
| 3822 | {"isspace", (PyCFunction)string_isspace, METH_NOARGS, isspace__doc__}, |
| 3823 | {"isdigit", (PyCFunction)string_isdigit, METH_NOARGS, isdigit__doc__}, |
| 3824 | {"istitle", (PyCFunction)string_istitle, METH_NOARGS, istitle__doc__}, |
| 3825 | {"isalpha", (PyCFunction)string_isalpha, METH_NOARGS, isalpha__doc__}, |
| 3826 | {"isalnum", (PyCFunction)string_isalnum, METH_NOARGS, isalnum__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3827 | {"capitalize", (PyCFunction)string_capitalize, METH_NOARGS, |
| 3828 | capitalize__doc__}, |
| 3829 | {"count", (PyCFunction)string_count, METH_VARARGS, count__doc__}, |
| 3830 | {"endswith", (PyCFunction)string_endswith, METH_VARARGS, |
| 3831 | endswith__doc__}, |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 3832 | {"partition", (PyCFunction)string_partition, METH_O, partition__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3833 | {"find", (PyCFunction)string_find, METH_VARARGS, find__doc__}, |
| 3834 | {"index", (PyCFunction)string_index, METH_VARARGS, index__doc__}, |
| 3835 | {"lstrip", (PyCFunction)string_lstrip, METH_VARARGS, lstrip__doc__}, |
| 3836 | {"replace", (PyCFunction)string_replace, METH_VARARGS, replace__doc__}, |
| 3837 | {"rfind", (PyCFunction)string_rfind, METH_VARARGS, rfind__doc__}, |
| 3838 | {"rindex", (PyCFunction)string_rindex, METH_VARARGS, rindex__doc__}, |
| 3839 | {"rstrip", (PyCFunction)string_rstrip, METH_VARARGS, rstrip__doc__}, |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 3840 | {"rpartition", (PyCFunction)string_rpartition, METH_O, |
| 3841 | rpartition__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3842 | {"startswith", (PyCFunction)string_startswith, METH_VARARGS, |
| 3843 | startswith__doc__}, |
| 3844 | {"strip", (PyCFunction)string_strip, METH_VARARGS, strip__doc__}, |
| 3845 | {"swapcase", (PyCFunction)string_swapcase, METH_NOARGS, |
| 3846 | swapcase__doc__}, |
| 3847 | {"translate", (PyCFunction)string_translate, METH_VARARGS, |
| 3848 | translate__doc__}, |
| 3849 | {"title", (PyCFunction)string_title, METH_NOARGS, title__doc__}, |
| 3850 | {"ljust", (PyCFunction)string_ljust, METH_VARARGS, ljust__doc__}, |
| 3851 | {"rjust", (PyCFunction)string_rjust, METH_VARARGS, rjust__doc__}, |
| 3852 | {"center", (PyCFunction)string_center, METH_VARARGS, center__doc__}, |
| 3853 | {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__}, |
| 3854 | {"encode", (PyCFunction)string_encode, METH_VARARGS, encode__doc__}, |
| 3855 | {"decode", (PyCFunction)string_decode, METH_VARARGS, decode__doc__}, |
| 3856 | {"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, |
| 3857 | expandtabs__doc__}, |
| 3858 | {"splitlines", (PyCFunction)string_splitlines, METH_VARARGS, |
| 3859 | splitlines__doc__}, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3860 | {"__getnewargs__", (PyCFunction)string_getnewargs, METH_NOARGS}, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3861 | {NULL, NULL} /* sentinel */ |
| 3862 | }; |
| 3863 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 3864 | static PyObject * |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3865 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 3866 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3867 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3868 | string_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3869 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3870 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 3871 | static char *kwlist[] = {"object", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3872 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3873 | if (type != &PyString_Type) |
| 3874 | return str_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3875 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x)) |
| 3876 | return NULL; |
| 3877 | if (x == NULL) |
| 3878 | return PyString_FromString(""); |
| 3879 | return PyObject_Str(x); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3880 | } |
| 3881 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3882 | static PyObject * |
| 3883 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3884 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3885 | PyObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3886 | Py_ssize_t n; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3887 | |
| 3888 | assert(PyType_IsSubtype(type, &PyString_Type)); |
| 3889 | tmp = string_new(&PyString_Type, args, kwds); |
| 3890 | if (tmp == NULL) |
| 3891 | return NULL; |
Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 3892 | assert(PyString_CheckExact(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3893 | n = PyString_GET_SIZE(tmp); |
| 3894 | pnew = type->tp_alloc(type, n); |
| 3895 | if (pnew != NULL) { |
| 3896 | memcpy(PyString_AS_STRING(pnew), PyString_AS_STRING(tmp), n+1); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3897 | ((PyStringObject *)pnew)->ob_shash = |
| 3898 | ((PyStringObject *)tmp)->ob_shash; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 3899 | ((PyStringObject *)pnew)->ob_sstate = SSTATE_NOT_INTERNED; |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3900 | } |
Guido van Rossum | 29d55a3 | 2001-08-31 16:11:15 +0000 | [diff] [blame] | 3901 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3902 | return pnew; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 3905 | static PyObject * |
| 3906 | basestring_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3907 | { |
| 3908 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 32a7e7f | 2002-05-31 19:58:02 +0000 | [diff] [blame] | 3909 | "The basestring type cannot be instantiated"); |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 3910 | return NULL; |
| 3911 | } |
| 3912 | |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 3913 | static PyObject * |
| 3914 | string_mod(PyObject *v, PyObject *w) |
| 3915 | { |
| 3916 | if (!PyString_Check(v)) { |
| 3917 | Py_INCREF(Py_NotImplemented); |
| 3918 | return Py_NotImplemented; |
| 3919 | } |
| 3920 | return PyString_Format(v, w); |
| 3921 | } |
| 3922 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3923 | PyDoc_STRVAR(basestring_doc, |
| 3924 | "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] | 3925 | |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 3926 | static PyNumberMethods string_as_number = { |
| 3927 | 0, /*nb_add*/ |
| 3928 | 0, /*nb_subtract*/ |
| 3929 | 0, /*nb_multiply*/ |
| 3930 | 0, /*nb_divide*/ |
| 3931 | string_mod, /*nb_remainder*/ |
| 3932 | }; |
| 3933 | |
| 3934 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 3935 | PyTypeObject PyBaseString_Type = { |
| 3936 | PyObject_HEAD_INIT(&PyType_Type) |
| 3937 | 0, |
Neal Norwitz | 32a7e7f | 2002-05-31 19:58:02 +0000 | [diff] [blame] | 3938 | "basestring", |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 3939 | 0, |
| 3940 | 0, |
| 3941 | 0, /* tp_dealloc */ |
| 3942 | 0, /* tp_print */ |
| 3943 | 0, /* tp_getattr */ |
| 3944 | 0, /* tp_setattr */ |
| 3945 | 0, /* tp_compare */ |
| 3946 | 0, /* tp_repr */ |
| 3947 | 0, /* tp_as_number */ |
| 3948 | 0, /* tp_as_sequence */ |
| 3949 | 0, /* tp_as_mapping */ |
| 3950 | 0, /* tp_hash */ |
| 3951 | 0, /* tp_call */ |
| 3952 | 0, /* tp_str */ |
| 3953 | 0, /* tp_getattro */ |
| 3954 | 0, /* tp_setattro */ |
| 3955 | 0, /* tp_as_buffer */ |
| 3956 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3957 | basestring_doc, /* tp_doc */ |
| 3958 | 0, /* tp_traverse */ |
| 3959 | 0, /* tp_clear */ |
| 3960 | 0, /* tp_richcompare */ |
| 3961 | 0, /* tp_weaklistoffset */ |
| 3962 | 0, /* tp_iter */ |
| 3963 | 0, /* tp_iternext */ |
| 3964 | 0, /* tp_methods */ |
| 3965 | 0, /* tp_members */ |
| 3966 | 0, /* tp_getset */ |
| 3967 | &PyBaseObject_Type, /* tp_base */ |
| 3968 | 0, /* tp_dict */ |
| 3969 | 0, /* tp_descr_get */ |
| 3970 | 0, /* tp_descr_set */ |
| 3971 | 0, /* tp_dictoffset */ |
| 3972 | 0, /* tp_init */ |
| 3973 | 0, /* tp_alloc */ |
| 3974 | basestring_new, /* tp_new */ |
| 3975 | 0, /* tp_free */ |
| 3976 | }; |
| 3977 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3978 | PyDoc_STRVAR(string_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3979 | "str(object) -> string\n\ |
| 3980 | \n\ |
| 3981 | Return a nice string representation of the object.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3982 | 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] | 3983 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3984 | PyTypeObject PyString_Type = { |
| 3985 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3986 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3987 | "str", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3988 | sizeof(PyStringObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3989 | sizeof(char), |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 3990 | string_dealloc, /* tp_dealloc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3991 | (printfunc)string_print, /* tp_print */ |
| 3992 | 0, /* tp_getattr */ |
| 3993 | 0, /* tp_setattr */ |
| 3994 | 0, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 3995 | string_repr, /* tp_repr */ |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 3996 | &string_as_number, /* tp_as_number */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3997 | &string_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 3998 | &string_as_mapping, /* tp_as_mapping */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3999 | (hashfunc)string_hash, /* tp_hash */ |
| 4000 | 0, /* tp_call */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 4001 | string_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4002 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4003 | 0, /* tp_setattro */ |
| 4004 | &string_as_buffer, /* tp_as_buffer */ |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 4005 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 4006 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4007 | string_doc, /* tp_doc */ |
| 4008 | 0, /* tp_traverse */ |
| 4009 | 0, /* tp_clear */ |
| 4010 | (richcmpfunc)string_richcompare, /* tp_richcompare */ |
| 4011 | 0, /* tp_weaklistoffset */ |
| 4012 | 0, /* tp_iter */ |
| 4013 | 0, /* tp_iternext */ |
| 4014 | string_methods, /* tp_methods */ |
| 4015 | 0, /* tp_members */ |
| 4016 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 4017 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4018 | 0, /* tp_dict */ |
| 4019 | 0, /* tp_descr_get */ |
| 4020 | 0, /* tp_descr_set */ |
| 4021 | 0, /* tp_dictoffset */ |
| 4022 | 0, /* tp_init */ |
| 4023 | 0, /* tp_alloc */ |
| 4024 | string_new, /* tp_new */ |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 4025 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4026 | }; |
| 4027 | |
| 4028 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4029 | PyString_Concat(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4030 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4031 | register PyObject *v; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4032 | if (*pv == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4033 | return; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4034 | if (w == NULL || !PyString_Check(*pv)) { |
| 4035 | Py_DECREF(*pv); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4036 | *pv = NULL; |
| 4037 | return; |
| 4038 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4039 | v = string_concat((PyStringObject *) *pv, w); |
| 4040 | Py_DECREF(*pv); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4041 | *pv = v; |
| 4042 | } |
| 4043 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4044 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4045 | PyString_ConcatAndDel(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4046 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4047 | PyString_Concat(pv, w); |
| 4048 | Py_XDECREF(w); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4049 | } |
| 4050 | |
| 4051 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4052 | /* The following function breaks the notion that strings are immutable: |
| 4053 | it changes the size of a string. We get away with this only if there |
| 4054 | is only one module referencing the object. You can also think of it |
| 4055 | as creating a new string object and destroying the old one, only |
| 4056 | 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] | 4057 | already be known to some other part of the code... |
| 4058 | Note that if there's not enough memory to resize the string, the original |
| 4059 | string object at *pv is deallocated, *pv is set to NULL, an "out of |
| 4060 | memory" exception is set, and -1 is returned. Else (on success) 0 is |
| 4061 | returned, and the value in *pv may or may not be the same as on input. |
| 4062 | As always, an extra byte is allocated for a trailing \0 byte (newsize |
| 4063 | does *not* include that), and a trailing \0 byte is stored. |
| 4064 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4065 | |
| 4066 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4067 | _PyString_Resize(PyObject **pv, Py_ssize_t newsize) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4068 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4069 | register PyObject *v; |
| 4070 | register PyStringObject *sv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 4071 | v = *pv; |
Armin Rigo | 618fbf5 | 2004-08-07 20:58:32 +0000 | [diff] [blame] | 4072 | if (!PyString_Check(v) || v->ob_refcnt != 1 || newsize < 0 || |
| 4073 | PyString_CHECK_INTERNED(v)) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4074 | *pv = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4075 | Py_DECREF(v); |
| 4076 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 4077 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4078 | } |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 4079 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 4080 | _Py_DEC_REFTOTAL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4081 | _Py_ForgetReference(v); |
| 4082 | *pv = (PyObject *) |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 4083 | PyObject_REALLOC((char *)v, sizeof(PyStringObject) + newsize); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4084 | if (*pv == NULL) { |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 4085 | PyObject_Del(v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4086 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 4087 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4088 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4089 | _Py_NewReference(*pv); |
| 4090 | sv = (PyStringObject *) *pv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 4091 | sv->ob_size = newsize; |
| 4092 | sv->ob_sval[newsize] = '\0'; |
Raymond Hettinger | 561fbf1 | 2004-10-26 01:52:37 +0000 | [diff] [blame] | 4093 | sv->ob_shash = -1; /* invalidate cached hash value */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4094 | return 0; |
| 4095 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4096 | |
| 4097 | /* Helpers for formatstring */ |
| 4098 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 4099 | Py_LOCAL(PyObject *) |
Thomas Wouters | 977485d | 2006-02-16 15:59:12 +0000 | [diff] [blame] | 4100 | 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] | 4101 | { |
Thomas Wouters | 977485d | 2006-02-16 15:59:12 +0000 | [diff] [blame] | 4102 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4103 | if (argidx < arglen) { |
| 4104 | (*p_argidx)++; |
| 4105 | if (arglen < 0) |
| 4106 | return args; |
| 4107 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4108 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4109 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4110 | PyErr_SetString(PyExc_TypeError, |
| 4111 | "not enough arguments for format string"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4112 | return NULL; |
| 4113 | } |
| 4114 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4115 | /* Format codes |
| 4116 | * F_LJUST '-' |
| 4117 | * F_SIGN '+' |
| 4118 | * F_BLANK ' ' |
| 4119 | * F_ALT '#' |
| 4120 | * F_ZERO '0' |
| 4121 | */ |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4122 | #define F_LJUST (1<<0) |
| 4123 | #define F_SIGN (1<<1) |
| 4124 | #define F_BLANK (1<<2) |
| 4125 | #define F_ALT (1<<3) |
| 4126 | #define F_ZERO (1<<4) |
| 4127 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 4128 | Py_LOCAL(int) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4129 | formatfloat(char *buf, size_t buflen, int flags, |
| 4130 | int prec, int type, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4131 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4132 | /* fmt = '%#.' + `prec` + `type` |
| 4133 | 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] | 4134 | char fmt[20]; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4135 | double x; |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4136 | x = PyFloat_AsDouble(v); |
| 4137 | if (x == -1.0 && PyErr_Occurred()) { |
| 4138 | PyErr_SetString(PyExc_TypeError, "float argument required"); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4139 | return -1; |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4140 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4141 | if (prec < 0) |
| 4142 | prec = 6; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4143 | if (type == 'f' && fabs(x)/1e25 >= 1e25) |
| 4144 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4145 | /* Worst case length calc to ensure no buffer overrun: |
| 4146 | |
| 4147 | 'g' formats: |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4148 | fmt = %#.<prec>g |
| 4149 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4150 | for any double rep.) |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4151 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4152 | |
| 4153 | 'f' formats: |
| 4154 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 4155 | len = 1 + 50 + 1 + prec = 52 + prec |
| 4156 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4157 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 4158 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4159 | |
| 4160 | */ |
| 4161 | if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || |
| 4162 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4163 | PyErr_SetString(PyExc_OverflowError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4164 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4165 | return -1; |
| 4166 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4167 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 4168 | (flags&F_ALT) ? "#" : "", |
| 4169 | prec, type); |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 4170 | PyOS_ascii_formatd(buf, buflen, fmt, x); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4171 | return (int)strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4172 | } |
| 4173 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4174 | /* _PyString_FormatLong emulates the format codes d, u, o, x and X, and |
| 4175 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 4176 | * Python's regular ints. |
| 4177 | * Return value: a new PyString*, or NULL if error. |
| 4178 | * . *pbuf is set to point into it, |
| 4179 | * *plen set to the # of chars following that. |
| 4180 | * Caller must decref it when done using pbuf. |
| 4181 | * The string starting at *pbuf is of the form |
| 4182 | * "-"? ("0x" | "0X")? digit+ |
| 4183 | * "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] | 4184 | * set in flags. The case of hex digits will be correct, |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4185 | * There will be at least prec digits, zero-filled on the left if |
| 4186 | * necessary to get that many. |
| 4187 | * val object to be converted |
| 4188 | * flags bitmask of format flags; only F_ALT is looked at |
| 4189 | * prec minimum number of digits; 0-fill on left if needed |
| 4190 | * type a character in [duoxX]; u acts the same as d |
| 4191 | * |
| 4192 | * CAUTION: o, x and X conversions on regular ints can never |
| 4193 | * produce a '-' sign, but can for Python's unbounded ints. |
| 4194 | */ |
| 4195 | PyObject* |
| 4196 | _PyString_FormatLong(PyObject *val, int flags, int prec, int type, |
| 4197 | char **pbuf, int *plen) |
| 4198 | { |
| 4199 | PyObject *result = NULL; |
| 4200 | char *buf; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4201 | Py_ssize_t i; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4202 | int sign; /* 1 if '-', else 0 */ |
| 4203 | int len; /* number of characters */ |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4204 | Py_ssize_t llen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4205 | int numdigits; /* len == numnondigits + numdigits */ |
| 4206 | int numnondigits = 0; |
| 4207 | |
| 4208 | switch (type) { |
| 4209 | case 'd': |
| 4210 | case 'u': |
| 4211 | result = val->ob_type->tp_str(val); |
| 4212 | break; |
| 4213 | case 'o': |
| 4214 | result = val->ob_type->tp_as_number->nb_oct(val); |
| 4215 | break; |
| 4216 | case 'x': |
| 4217 | case 'X': |
| 4218 | numnondigits = 2; |
| 4219 | result = val->ob_type->tp_as_number->nb_hex(val); |
| 4220 | break; |
| 4221 | default: |
| 4222 | assert(!"'type' not in [duoxX]"); |
| 4223 | } |
| 4224 | if (!result) |
| 4225 | return NULL; |
| 4226 | |
| 4227 | /* To modify the string in-place, there can only be one reference. */ |
| 4228 | if (result->ob_refcnt != 1) { |
| 4229 | PyErr_BadInternalCall(); |
| 4230 | return NULL; |
| 4231 | } |
| 4232 | buf = PyString_AsString(result); |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4233 | llen = PyString_Size(result); |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 4234 | if (llen > PY_SSIZE_T_MAX) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4235 | PyErr_SetString(PyExc_ValueError, "string too large in _PyString_FormatLong"); |
| 4236 | return NULL; |
| 4237 | } |
| 4238 | len = (int)llen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4239 | if (buf[len-1] == 'L') { |
| 4240 | --len; |
| 4241 | buf[len] = '\0'; |
| 4242 | } |
| 4243 | sign = buf[0] == '-'; |
| 4244 | numnondigits += sign; |
| 4245 | numdigits = len - numnondigits; |
| 4246 | assert(numdigits > 0); |
| 4247 | |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4248 | /* Get rid of base marker unless F_ALT */ |
| 4249 | if ((flags & F_ALT) == 0) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4250 | /* Need to skip 0x, 0X or 0. */ |
| 4251 | int skipped = 0; |
| 4252 | switch (type) { |
| 4253 | case 'o': |
| 4254 | assert(buf[sign] == '0'); |
| 4255 | /* If 0 is only digit, leave it alone. */ |
| 4256 | if (numdigits > 1) { |
| 4257 | skipped = 1; |
| 4258 | --numdigits; |
| 4259 | } |
| 4260 | break; |
| 4261 | case 'x': |
| 4262 | case 'X': |
| 4263 | assert(buf[sign] == '0'); |
| 4264 | assert(buf[sign + 1] == 'x'); |
| 4265 | skipped = 2; |
| 4266 | numnondigits -= 2; |
| 4267 | break; |
| 4268 | } |
| 4269 | if (skipped) { |
| 4270 | buf += skipped; |
| 4271 | len -= skipped; |
| 4272 | if (sign) |
| 4273 | buf[0] = '-'; |
| 4274 | } |
| 4275 | assert(len == numnondigits + numdigits); |
| 4276 | assert(numdigits > 0); |
| 4277 | } |
| 4278 | |
| 4279 | /* Fill with leading zeroes to meet minimum width. */ |
| 4280 | if (prec > numdigits) { |
| 4281 | PyObject *r1 = PyString_FromStringAndSize(NULL, |
| 4282 | numnondigits + prec); |
| 4283 | char *b1; |
| 4284 | if (!r1) { |
| 4285 | Py_DECREF(result); |
| 4286 | return NULL; |
| 4287 | } |
| 4288 | b1 = PyString_AS_STRING(r1); |
| 4289 | for (i = 0; i < numnondigits; ++i) |
| 4290 | *b1++ = *buf++; |
| 4291 | for (i = 0; i < prec - numdigits; i++) |
| 4292 | *b1++ = '0'; |
| 4293 | for (i = 0; i < numdigits; i++) |
| 4294 | *b1++ = *buf++; |
| 4295 | *b1 = '\0'; |
| 4296 | Py_DECREF(result); |
| 4297 | result = r1; |
| 4298 | buf = PyString_AS_STRING(result); |
| 4299 | len = numnondigits + prec; |
| 4300 | } |
| 4301 | |
| 4302 | /* Fix up case for hex conversions. */ |
Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 4303 | if (type == 'X') { |
| 4304 | /* Need to convert all lower case letters to upper case. |
| 4305 | and need to convert 0x to 0X (and -0x to -0X). */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4306 | for (i = 0; i < len; i++) |
Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 4307 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 4308 | buf[i] -= 'a'-'A'; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4309 | } |
| 4310 | *pbuf = buf; |
| 4311 | *plen = len; |
| 4312 | return result; |
| 4313 | } |
| 4314 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 4315 | Py_LOCAL(int) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4316 | formatint(char *buf, size_t buflen, int flags, |
| 4317 | int prec, int type, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4318 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4319 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4320 | worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 4321 | + 1 + 1 = 24 */ |
| 4322 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4323 | char *sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4324 | long x; |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4325 | |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4326 | x = PyInt_AsLong(v); |
| 4327 | if (x == -1 && PyErr_Occurred()) { |
| 4328 | PyErr_SetString(PyExc_TypeError, "int argument required"); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4329 | return -1; |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4330 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4331 | if (x < 0 && type == 'u') { |
| 4332 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 4333 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4334 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 4335 | sign = "-"; |
| 4336 | else |
| 4337 | sign = ""; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4338 | if (prec < 0) |
| 4339 | prec = 1; |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4340 | |
| 4341 | if ((flags & F_ALT) && |
| 4342 | (type == 'x' || type == 'X')) { |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4343 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4344 | * of issues that cause pain: |
| 4345 | * - when 0 is being converted, the C standard leaves off |
| 4346 | * the '0x' or '0X', which is inconsistent with other |
| 4347 | * %#x/%#X conversions and inconsistent with Python's |
| 4348 | * hex() function |
| 4349 | * - there are platforms that violate the standard and |
| 4350 | * convert 0 with the '0x' or '0X' |
| 4351 | * (Metrowerks, Compaq Tru64) |
| 4352 | * - there are platforms that give '0x' when converting |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4353 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4354 | * standard (OS/2 EMX) |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4355 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4356 | * We can achieve the desired consistency by inserting our |
| 4357 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 4358 | * of %#x/%#X. |
| 4359 | * |
| 4360 | * Note that this is the same approach as used in |
| 4361 | * formatint() in unicodeobject.c |
| 4362 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4363 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 4364 | sign, type, prec, type); |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4365 | } |
| 4366 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4367 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 4368 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4369 | prec, type); |
| 4370 | } |
| 4371 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4372 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 4373 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4374 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4375 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4376 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4377 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4378 | return -1; |
| 4379 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4380 | if (sign[0]) |
| 4381 | PyOS_snprintf(buf, buflen, fmt, -x); |
| 4382 | else |
| 4383 | PyOS_snprintf(buf, buflen, fmt, x); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4384 | return (int)strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4385 | } |
| 4386 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 4387 | Py_LOCAL(int) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4388 | formatchar(char *buf, size_t buflen, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4389 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4390 | /* presume that the buffer is at least 2 characters long */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4391 | if (PyString_Check(v)) { |
| 4392 | 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] | 4393 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4394 | } |
| 4395 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4396 | 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] | 4397 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4398 | } |
| 4399 | buf[1] = '\0'; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4400 | return 1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4401 | } |
| 4402 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4403 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 4404 | |
| 4405 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 4406 | chars are formatted. XXX This is a magic number. Each formatting |
| 4407 | routine does bounds checking to ensure no overflow, but a better |
| 4408 | solution may be to malloc a buffer of appropriate size for each |
| 4409 | format. For now, the current solution is sufficient. |
| 4410 | */ |
| 4411 | #define FORMATBUFLEN (size_t)120 |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4412 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4413 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4414 | PyString_Format(PyObject *format, PyObject *args) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4415 | { |
| 4416 | char *fmt, *res; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4417 | Py_ssize_t arglen, argidx; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4418 | Py_ssize_t reslen, rescnt, fmtcnt; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4419 | int args_owned = 0; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4420 | PyObject *result, *orig_args; |
| 4421 | #ifdef Py_USING_UNICODE |
| 4422 | PyObject *v, *w; |
| 4423 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4424 | PyObject *dict = NULL; |
| 4425 | if (format == NULL || !PyString_Check(format) || args == NULL) { |
| 4426 | PyErr_BadInternalCall(); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4427 | return NULL; |
| 4428 | } |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4429 | orig_args = args; |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4430 | fmt = PyString_AS_STRING(format); |
| 4431 | fmtcnt = PyString_GET_SIZE(format); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 4432 | reslen = rescnt = fmtcnt + 100; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4433 | result = PyString_FromStringAndSize((char *)NULL, reslen); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4434 | if (result == NULL) |
| 4435 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4436 | res = PyString_AsString(result); |
| 4437 | if (PyTuple_Check(args)) { |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4438 | arglen = PyTuple_GET_SIZE(args); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4439 | argidx = 0; |
| 4440 | } |
| 4441 | else { |
| 4442 | arglen = -1; |
| 4443 | argidx = -2; |
| 4444 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 4445 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 4446 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4447 | dict = args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4448 | while (--fmtcnt >= 0) { |
| 4449 | if (*fmt != '%') { |
| 4450 | if (--rescnt < 0) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 4451 | rescnt = fmtcnt + 100; |
| 4452 | reslen += rescnt; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4453 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4454 | return NULL; |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4455 | res = PyString_AS_STRING(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4456 | + reslen - rescnt; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4457 | --rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4458 | } |
| 4459 | *res++ = *fmt++; |
| 4460 | } |
| 4461 | else { |
| 4462 | /* Got a format specifier */ |
| 4463 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4464 | Py_ssize_t width = -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4465 | int prec = -1; |
Guido van Rossum | 6938a29 | 1993-11-11 14:51:57 +0000 | [diff] [blame] | 4466 | int c = '\0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4467 | int fill; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4468 | PyObject *v = NULL; |
| 4469 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4470 | char *pbuf; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4471 | int sign; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4472 | Py_ssize_t len; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4473 | char formatbuf[FORMATBUFLEN]; |
| 4474 | /* For format{float,int,char}() */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4475 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4476 | char *fmt_start = fmt; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4477 | Py_ssize_t argidx_start = argidx; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4478 | #endif |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4479 | |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 4480 | fmt++; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4481 | if (*fmt == '(') { |
| 4482 | char *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4483 | Py_ssize_t keylen; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4484 | PyObject *key; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4485 | int pcount = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4486 | |
| 4487 | if (dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4488 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4489 | "format requires a mapping"); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4490 | goto error; |
| 4491 | } |
| 4492 | ++fmt; |
| 4493 | --fmtcnt; |
| 4494 | keystart = fmt; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4495 | /* Skip over balanced parentheses */ |
| 4496 | while (pcount > 0 && --fmtcnt >= 0) { |
| 4497 | if (*fmt == ')') |
| 4498 | --pcount; |
| 4499 | else if (*fmt == '(') |
| 4500 | ++pcount; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4501 | fmt++; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4502 | } |
| 4503 | keylen = fmt - keystart - 1; |
| 4504 | if (fmtcnt < 0 || pcount > 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4505 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4506 | "incomplete format key"); |
| 4507 | goto error; |
| 4508 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4509 | key = PyString_FromStringAndSize(keystart, |
| 4510 | keylen); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4511 | if (key == NULL) |
| 4512 | goto error; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4513 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4514 | Py_DECREF(args); |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4515 | args_owned = 0; |
| 4516 | } |
| 4517 | args = PyObject_GetItem(dict, key); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4518 | Py_DECREF(key); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4519 | if (args == NULL) { |
| 4520 | goto error; |
| 4521 | } |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4522 | args_owned = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4523 | arglen = -1; |
| 4524 | argidx = -2; |
| 4525 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4526 | while (--fmtcnt >= 0) { |
| 4527 | switch (c = *fmt++) { |
| 4528 | case '-': flags |= F_LJUST; continue; |
| 4529 | case '+': flags |= F_SIGN; continue; |
| 4530 | case ' ': flags |= F_BLANK; continue; |
| 4531 | case '#': flags |= F_ALT; continue; |
| 4532 | case '0': flags |= F_ZERO; continue; |
| 4533 | } |
| 4534 | break; |
| 4535 | } |
| 4536 | if (c == '*') { |
| 4537 | v = getnextarg(args, arglen, &argidx); |
| 4538 | if (v == NULL) |
| 4539 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4540 | if (!PyInt_Check(v)) { |
| 4541 | PyErr_SetString(PyExc_TypeError, |
| 4542 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4543 | goto error; |
| 4544 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4545 | width = PyInt_AsLong(v); |
Guido van Rossum | 98c9eba | 1999-06-07 15:12:32 +0000 | [diff] [blame] | 4546 | if (width < 0) { |
| 4547 | flags |= F_LJUST; |
| 4548 | width = -width; |
| 4549 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4550 | if (--fmtcnt >= 0) |
| 4551 | c = *fmt++; |
| 4552 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4553 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4554 | width = c - '0'; |
| 4555 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4556 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4557 | if (!isdigit(c)) |
| 4558 | break; |
| 4559 | if ((width*10) / 10 != width) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4560 | PyErr_SetString( |
| 4561 | PyExc_ValueError, |
| 4562 | "width too big"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4563 | goto error; |
| 4564 | } |
| 4565 | width = width*10 + (c - '0'); |
| 4566 | } |
| 4567 | } |
| 4568 | if (c == '.') { |
| 4569 | prec = 0; |
| 4570 | if (--fmtcnt >= 0) |
| 4571 | c = *fmt++; |
| 4572 | if (c == '*') { |
| 4573 | v = getnextarg(args, arglen, &argidx); |
| 4574 | if (v == NULL) |
| 4575 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4576 | if (!PyInt_Check(v)) { |
| 4577 | PyErr_SetString( |
| 4578 | PyExc_TypeError, |
| 4579 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4580 | goto error; |
| 4581 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4582 | prec = PyInt_AsLong(v); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4583 | if (prec < 0) |
| 4584 | prec = 0; |
| 4585 | if (--fmtcnt >= 0) |
| 4586 | c = *fmt++; |
| 4587 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4588 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4589 | prec = c - '0'; |
| 4590 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4591 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4592 | if (!isdigit(c)) |
| 4593 | break; |
| 4594 | if ((prec*10) / 10 != prec) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4595 | PyErr_SetString( |
| 4596 | PyExc_ValueError, |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4597 | "prec too big"); |
| 4598 | goto error; |
| 4599 | } |
| 4600 | prec = prec*10 + (c - '0'); |
| 4601 | } |
| 4602 | } |
| 4603 | } /* prec */ |
| 4604 | if (fmtcnt >= 0) { |
| 4605 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4606 | if (--fmtcnt >= 0) |
| 4607 | c = *fmt++; |
| 4608 | } |
| 4609 | } |
| 4610 | if (fmtcnt < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4611 | PyErr_SetString(PyExc_ValueError, |
| 4612 | "incomplete format"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4613 | goto error; |
| 4614 | } |
| 4615 | if (c != '%') { |
| 4616 | v = getnextarg(args, arglen, &argidx); |
| 4617 | if (v == NULL) |
| 4618 | goto error; |
| 4619 | } |
| 4620 | sign = 0; |
| 4621 | fill = ' '; |
| 4622 | switch (c) { |
| 4623 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4624 | pbuf = "%"; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4625 | len = 1; |
| 4626 | break; |
| 4627 | case 's': |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4628 | #ifdef Py_USING_UNICODE |
Neil Schemenauer | ab61923 | 2005-08-31 23:02:05 +0000 | [diff] [blame] | 4629 | if (PyUnicode_Check(v)) { |
| 4630 | fmt = fmt_start; |
| 4631 | argidx = argidx_start; |
| 4632 | goto unicode; |
| 4633 | } |
Georg Brandl | d45014b | 2005-10-01 17:06:00 +0000 | [diff] [blame] | 4634 | #endif |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 4635 | temp = _PyObject_Str(v); |
Georg Brandl | d45014b | 2005-10-01 17:06:00 +0000 | [diff] [blame] | 4636 | #ifdef Py_USING_UNICODE |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 4637 | if (temp != NULL && PyUnicode_Check(temp)) { |
| 4638 | Py_DECREF(temp); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4639 | fmt = fmt_start; |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 4640 | argidx = argidx_start; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4641 | goto unicode; |
| 4642 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4643 | #endif |
Guido van Rossum | b00c07f | 2002-10-09 19:07:53 +0000 | [diff] [blame] | 4644 | /* Fall through */ |
Walter Dörwald | 9ff3f03 | 2003-06-18 14:17:01 +0000 | [diff] [blame] | 4645 | case 'r': |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 4646 | if (c == 'r') |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 4647 | temp = PyObject_Repr(v); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4648 | if (temp == NULL) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4649 | goto error; |
Guido van Rossum | 4a0144c | 1998-06-09 15:08:41 +0000 | [diff] [blame] | 4650 | if (!PyString_Check(temp)) { |
| 4651 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 8052f89 | 2002-10-09 19:14:30 +0000 | [diff] [blame] | 4652 | "%s argument has non-string str()"); |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4653 | Py_DECREF(temp); |
Guido van Rossum | 4a0144c | 1998-06-09 15:08:41 +0000 | [diff] [blame] | 4654 | goto error; |
| 4655 | } |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4656 | pbuf = PyString_AS_STRING(temp); |
| 4657 | len = PyString_GET_SIZE(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4658 | if (prec >= 0 && len > prec) |
| 4659 | len = prec; |
| 4660 | break; |
| 4661 | case 'i': |
| 4662 | case 'd': |
| 4663 | case 'u': |
| 4664 | case 'o': |
| 4665 | case 'x': |
| 4666 | case 'X': |
| 4667 | if (c == 'i') |
| 4668 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 4669 | if (PyLong_Check(v)) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4670 | int ilen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4671 | temp = _PyString_FormatLong(v, flags, |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4672 | prec, c, &pbuf, &ilen); |
| 4673 | len = ilen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4674 | if (!temp) |
| 4675 | goto error; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4676 | sign = 1; |
Guido van Rossum | 4acdc23 | 1997-01-29 06:00:24 +0000 | [diff] [blame] | 4677 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4678 | else { |
| 4679 | pbuf = formatbuf; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4680 | len = formatint(pbuf, |
| 4681 | sizeof(formatbuf), |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4682 | flags, prec, c, v); |
| 4683 | if (len < 0) |
| 4684 | goto error; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4685 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4686 | } |
| 4687 | if (flags & F_ZERO) |
| 4688 | fill = '0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4689 | break; |
| 4690 | case 'e': |
| 4691 | case 'E': |
| 4692 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 4693 | case 'F': |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4694 | case 'g': |
| 4695 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 4696 | if (c == 'F') |
| 4697 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4698 | pbuf = formatbuf; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4699 | len = formatfloat(pbuf, sizeof(formatbuf), |
| 4700 | flags, prec, c, v); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4701 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4702 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4703 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4704 | if (flags & F_ZERO) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4705 | fill = '0'; |
| 4706 | break; |
| 4707 | case 'c': |
Walter Dörwald | 43440a6 | 2003-03-31 18:07:50 +0000 | [diff] [blame] | 4708 | #ifdef Py_USING_UNICODE |
| 4709 | if (PyUnicode_Check(v)) { |
| 4710 | fmt = fmt_start; |
| 4711 | argidx = argidx_start; |
| 4712 | goto unicode; |
| 4713 | } |
| 4714 | #endif |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4715 | pbuf = formatbuf; |
| 4716 | len = formatchar(pbuf, sizeof(formatbuf), v); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4717 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4718 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4719 | break; |
| 4720 | default: |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4721 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 4722 | "unsupported format character '%c' (0x%x) " |
| 4723 | "at index %i", |
Guido van Rossum | efc1188 | 2002-09-12 14:43:41 +0000 | [diff] [blame] | 4724 | c, c, |
| 4725 | (int)(fmt - 1 - PyString_AsString(format))); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4726 | goto error; |
| 4727 | } |
| 4728 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4729 | if (*pbuf == '-' || *pbuf == '+') { |
| 4730 | sign = *pbuf++; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4731 | len--; |
| 4732 | } |
| 4733 | else if (flags & F_SIGN) |
| 4734 | sign = '+'; |
| 4735 | else if (flags & F_BLANK) |
| 4736 | sign = ' '; |
| 4737 | else |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4738 | sign = 0; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4739 | } |
| 4740 | if (width < len) |
| 4741 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 4742 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 4743 | reslen -= rescnt; |
| 4744 | rescnt = width + fmtcnt + 100; |
| 4745 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 4746 | if (reslen < 0) { |
| 4747 | Py_DECREF(result); |
| 4748 | return PyErr_NoMemory(); |
| 4749 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4750 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4751 | return NULL; |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4752 | res = PyString_AS_STRING(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4753 | + reslen - rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4754 | } |
| 4755 | if (sign) { |
Guido van Rossum | 71e57d0 | 1993-11-11 15:03:51 +0000 | [diff] [blame] | 4756 | if (fill != ' ') |
| 4757 | *res++ = sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4758 | rescnt--; |
| 4759 | if (width > len) |
| 4760 | width--; |
| 4761 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4762 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 4763 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4764 | assert(pbuf[1] == c); |
| 4765 | if (fill != ' ') { |
| 4766 | *res++ = *pbuf++; |
| 4767 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4768 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4769 | rescnt -= 2; |
| 4770 | width -= 2; |
| 4771 | if (width < 0) |
| 4772 | width = 0; |
| 4773 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4774 | } |
| 4775 | if (width > len && !(flags & F_LJUST)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4776 | do { |
| 4777 | --rescnt; |
| 4778 | *res++ = fill; |
| 4779 | } while (--width > len); |
| 4780 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4781 | if (fill == ' ') { |
| 4782 | if (sign) |
| 4783 | *res++ = sign; |
| 4784 | if ((flags & F_ALT) && |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4785 | (c == 'x' || c == 'X')) { |
| 4786 | assert(pbuf[0] == '0'); |
| 4787 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4788 | *res++ = *pbuf++; |
| 4789 | *res++ = *pbuf++; |
| 4790 | } |
| 4791 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4792 | memcpy(res, pbuf, len); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4793 | res += len; |
| 4794 | rescnt -= len; |
| 4795 | while (--width >= len) { |
| 4796 | --rescnt; |
| 4797 | *res++ = ' '; |
| 4798 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4799 | if (dict && (argidx < arglen) && c != '%') { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4800 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 4801 | "not all arguments converted during string formatting"); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4802 | goto error; |
| 4803 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4804 | Py_XDECREF(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4805 | } /* '%' */ |
| 4806 | } /* until end */ |
Guido van Rossum | caeaafc | 1995-02-27 10:13:23 +0000 | [diff] [blame] | 4807 | if (argidx < arglen && !dict) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4808 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 4809 | "not all arguments converted during string formatting"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4810 | goto error; |
| 4811 | } |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4812 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4813 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4814 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4815 | _PyString_Resize(&result, reslen - rescnt); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4816 | return result; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4817 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4818 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4819 | unicode: |
| 4820 | if (args_owned) { |
| 4821 | Py_DECREF(args); |
| 4822 | args_owned = 0; |
| 4823 | } |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 4824 | /* Fiddle args right (remove the first argidx arguments) */ |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4825 | if (PyTuple_Check(orig_args) && argidx > 0) { |
| 4826 | PyObject *v; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4827 | Py_ssize_t n = PyTuple_GET_SIZE(orig_args) - argidx; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4828 | v = PyTuple_New(n); |
| 4829 | if (v == NULL) |
| 4830 | goto error; |
| 4831 | while (--n >= 0) { |
| 4832 | PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx); |
| 4833 | Py_INCREF(w); |
| 4834 | PyTuple_SET_ITEM(v, n, w); |
| 4835 | } |
| 4836 | args = v; |
| 4837 | } else { |
| 4838 | Py_INCREF(orig_args); |
| 4839 | args = orig_args; |
| 4840 | } |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4841 | args_owned = 1; |
| 4842 | /* Take what we have of the result and let the Unicode formatting |
| 4843 | function format the rest of the input. */ |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4844 | rescnt = res - PyString_AS_STRING(result); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4845 | if (_PyString_Resize(&result, rescnt)) |
| 4846 | goto error; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4847 | fmtcnt = PyString_GET_SIZE(format) - \ |
| 4848 | (fmt - PyString_AS_STRING(format)); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4849 | format = PyUnicode_Decode(fmt, fmtcnt, NULL, NULL); |
| 4850 | if (format == NULL) |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4851 | goto error; |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4852 | v = PyUnicode_Format(format, args); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4853 | Py_DECREF(format); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4854 | if (v == NULL) |
| 4855 | goto error; |
| 4856 | /* Paste what we have (result) to what the Unicode formatting |
| 4857 | function returned (v) and return the result (or error) */ |
| 4858 | w = PyUnicode_Concat(result, v); |
| 4859 | Py_DECREF(result); |
| 4860 | Py_DECREF(v); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4861 | Py_DECREF(args); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4862 | return w; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4863 | #endif /* Py_USING_UNICODE */ |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4864 | |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4865 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4866 | Py_DECREF(result); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4867 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4868 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4869 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4870 | return NULL; |
| 4871 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4872 | |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4873 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4874 | PyString_InternInPlace(PyObject **p) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4875 | { |
| 4876 | register PyStringObject *s = (PyStringObject *)(*p); |
| 4877 | PyObject *t; |
| 4878 | if (s == NULL || !PyString_Check(s)) |
| 4879 | Py_FatalError("PyString_InternInPlace: strings only please!"); |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4880 | /* If it's a string subclass, we don't really know what putting |
| 4881 | it in the interned dict might do. */ |
| 4882 | if (!PyString_CheckExact(s)) |
| 4883 | return; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4884 | if (PyString_CHECK_INTERNED(s)) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4885 | return; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4886 | if (interned == NULL) { |
| 4887 | interned = PyDict_New(); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4888 | if (interned == NULL) { |
| 4889 | PyErr_Clear(); /* Don't leave an exception */ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4890 | return; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4891 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4892 | } |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4893 | t = PyDict_GetItem(interned, (PyObject *)s); |
| 4894 | if (t) { |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4895 | Py_INCREF(t); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4896 | Py_DECREF(*p); |
| 4897 | *p = t; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4898 | return; |
| 4899 | } |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4900 | |
Armin Rigo | 79f7ad2 | 2004-08-07 19:27:39 +0000 | [diff] [blame] | 4901 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4902 | PyErr_Clear(); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4903 | return; |
| 4904 | } |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4905 | /* The two references in interned are not counted by refcnt. |
| 4906 | The string deallocator will take care of this */ |
Armin Rigo | 79f7ad2 | 2004-08-07 19:27:39 +0000 | [diff] [blame] | 4907 | s->ob_refcnt -= 2; |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4908 | PyString_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4909 | } |
| 4910 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4911 | void |
| 4912 | PyString_InternImmortal(PyObject **p) |
| 4913 | { |
| 4914 | PyString_InternInPlace(p); |
| 4915 | if (PyString_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 4916 | PyString_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 4917 | Py_INCREF(*p); |
| 4918 | } |
| 4919 | } |
| 4920 | |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4921 | |
| 4922 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4923 | PyString_InternFromString(const char *cp) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4924 | { |
| 4925 | PyObject *s = PyString_FromString(cp); |
| 4926 | if (s == NULL) |
| 4927 | return NULL; |
| 4928 | PyString_InternInPlace(&s); |
| 4929 | return s; |
| 4930 | } |
| 4931 | |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 4932 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4933 | PyString_Fini(void) |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 4934 | { |
| 4935 | int i; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 4936 | for (i = 0; i < UCHAR_MAX + 1; i++) { |
| 4937 | Py_XDECREF(characters[i]); |
| 4938 | characters[i] = NULL; |
| 4939 | } |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 4940 | Py_XDECREF(nullstring); |
| 4941 | nullstring = NULL; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 4942 | } |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 4943 | |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 4944 | void _Py_ReleaseInternedStrings(void) |
| 4945 | { |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4946 | PyObject *keys; |
| 4947 | PyStringObject *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4948 | Py_ssize_t i, n; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4949 | |
| 4950 | if (interned == NULL || !PyDict_Check(interned)) |
| 4951 | return; |
| 4952 | keys = PyDict_Keys(interned); |
| 4953 | if (keys == NULL || !PyList_Check(keys)) { |
| 4954 | PyErr_Clear(); |
| 4955 | return; |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 4956 | } |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4957 | |
| 4958 | /* Since _Py_ReleaseInternedStrings() is intended to help a leak |
| 4959 | detector, interned strings are not forcibly deallocated; rather, we |
| 4960 | give them their stolen references back, and then clear and DECREF |
| 4961 | the interned dict. */ |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 4962 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4963 | fprintf(stderr, "releasing interned strings\n"); |
| 4964 | n = PyList_GET_SIZE(keys); |
| 4965 | for (i = 0; i < n; i++) { |
| 4966 | s = (PyStringObject *) PyList_GET_ITEM(keys, i); |
| 4967 | switch (s->ob_sstate) { |
| 4968 | case SSTATE_NOT_INTERNED: |
| 4969 | /* XXX Shouldn't happen */ |
| 4970 | break; |
| 4971 | case SSTATE_INTERNED_IMMORTAL: |
| 4972 | s->ob_refcnt += 1; |
| 4973 | break; |
| 4974 | case SSTATE_INTERNED_MORTAL: |
| 4975 | s->ob_refcnt += 2; |
| 4976 | break; |
| 4977 | default: |
| 4978 | Py_FatalError("Inconsistent interned string state."); |
| 4979 | } |
| 4980 | s->ob_sstate = SSTATE_NOT_INTERNED; |
| 4981 | } |
| 4982 | Py_DECREF(keys); |
| 4983 | PyDict_Clear(interned); |
| 4984 | Py_DECREF(interned); |
| 4985 | interned = NULL; |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 4986 | } |