Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* String object implementation */ |
| 2 | |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 3 | #define PY_SSIZE_T_CLEAN |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 7 | #include <ctype.h> |
| 8 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 9 | #ifdef COUNT_ALLOCS |
| 10 | int null_strings, one_strings; |
| 11 | #endif |
| 12 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 13 | static PyStringObject *characters[UCHAR_MAX + 1]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 14 | static PyStringObject *nullstring; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 16 | /* This dictionary holds all interned strings. Note that references to |
| 17 | strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 18 | When the interned string reaches a refcnt of 0 the string deallocation |
| 19 | function will delete the reference from this dictionary. |
| 20 | |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 21 | Another way to look at this is that to say that the actual reference |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 22 | count of a string is: s->ob_refcnt + (s->ob_sstate?2:0) |
| 23 | */ |
| 24 | static PyObject *interned; |
| 25 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 26 | /* |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 27 | For both PyString_FromString() and PyString_FromStringAndSize(), the |
| 28 | parameter `size' denotes number of characters to allocate, not counting any |
Martin v. Löwis | 1f803f7 | 2002-01-16 10:53:24 +0000 | [diff] [blame] | 29 | null terminating character. |
Martin v. Löwis | d132750 | 2001-12-02 18:09:41 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 31 | For PyString_FromString(), the parameter `str' points to a null-terminated |
Martin v. Löwis | 1f803f7 | 2002-01-16 10:53:24 +0000 | [diff] [blame] | 32 | string containing exactly `size' bytes. |
Martin v. Löwis | d132750 | 2001-12-02 18:09:41 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 34 | For PyString_FromStringAndSize(), the parameter the parameter `str' is |
| 35 | either NULL or else points to a string containing at least `size' bytes. |
| 36 | For PyString_FromStringAndSize(), the string in the `str' parameter does |
| 37 | not have to be null-terminated. (Therefore it is safe to construct a |
| 38 | substring by calling `PyString_FromStringAndSize(origstring, substrlen)'.) |
| 39 | If `str' is NULL then PyString_FromStringAndSize() will allocate `size+1' |
| 40 | bytes (setting the last byte to the null terminating character) and you can |
| 41 | fill in the data yourself. If `str' is non-NULL then the resulting |
| 42 | PyString object must be treated as immutable and you must not fill in nor |
| 43 | alter the data yourself, since the strings may be shared. |
Martin v. Löwis | 8f1ea71 | 2001-12-03 08:24:52 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 45 | The PyObject member `op->ob_size', which denotes the number of "extra |
| 46 | items" in a variable-size object, will contain the number of bytes |
| 47 | allocated for string data, not counting the null terminating character. It |
| 48 | is therefore equal to the equal to the `size' parameter (for |
| 49 | PyString_FromStringAndSize()) or the length of the string in the `str' |
| 50 | parameter (for PyString_FromString()). |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 51 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 52 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 53 | PyString_FromStringAndSize(const char *str, Py_ssize_t size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 55 | register PyStringObject *op; |
Michael W. Hudson | faa7648 | 2005-01-31 17:09:25 +0000 | [diff] [blame] | 56 | assert(size >= 0); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 57 | if (size == 0 && (op = nullstring) != NULL) { |
| 58 | #ifdef COUNT_ALLOCS |
| 59 | null_strings++; |
| 60 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 61 | Py_INCREF(op); |
| 62 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 63 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 64 | if (size == 1 && str != NULL && |
| 65 | (op = characters[*str & UCHAR_MAX]) != NULL) |
| 66 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 67 | #ifdef COUNT_ALLOCS |
| 68 | one_strings++; |
| 69 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 70 | Py_INCREF(op); |
| 71 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 72 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 74 | /* Inline PyObject_NewVar */ |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 75 | op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 76 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 77 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 78 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 79 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 80 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 81 | if (str != NULL) |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 82 | Py_MEMCPY(op->ob_sval, str, size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 83 | op->ob_sval[size] = '\0'; |
Tim Peters | 8deda70 | 2002-03-30 10:06:07 +0000 | [diff] [blame] | 84 | /* share short strings */ |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 85 | if (size == 0) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 86 | PyObject *t = (PyObject *)op; |
| 87 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 88 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 89 | nullstring = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 90 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 91 | } else if (size == 1 && str != NULL) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 92 | PyObject *t = (PyObject *)op; |
| 93 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 94 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 95 | characters[*str & UCHAR_MAX] = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 96 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 97 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 98 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 101 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 102 | PyString_FromString(const char *str) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | { |
Tim Peters | 62de65b | 2001-12-06 20:29:32 +0000 | [diff] [blame] | 104 | register size_t size; |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 105 | register PyStringObject *op; |
Tim Peters | 62de65b | 2001-12-06 20:29:32 +0000 | [diff] [blame] | 106 | |
| 107 | assert(str != NULL); |
| 108 | size = strlen(str); |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 109 | if (size > PY_SSIZE_T_MAX) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 110 | PyErr_SetString(PyExc_OverflowError, |
| 111 | "string is too long for a Python string"); |
| 112 | return NULL; |
| 113 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 114 | if (size == 0 && (op = nullstring) != NULL) { |
| 115 | #ifdef COUNT_ALLOCS |
| 116 | null_strings++; |
| 117 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 118 | Py_INCREF(op); |
| 119 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 120 | } |
| 121 | if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
| 122 | #ifdef COUNT_ALLOCS |
| 123 | one_strings++; |
| 124 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 125 | Py_INCREF(op); |
| 126 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 127 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 129 | /* Inline PyObject_NewVar */ |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 130 | op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 131 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 132 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 133 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 134 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 135 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 136 | Py_MEMCPY(op->ob_sval, str, size+1); |
Tim Peters | 8deda70 | 2002-03-30 10:06:07 +0000 | [diff] [blame] | 137 | /* share short strings */ |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 138 | if (size == 0) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 139 | PyObject *t = (PyObject *)op; |
| 140 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 141 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 142 | nullstring = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 143 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 144 | } else if (size == 1) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 145 | PyObject *t = (PyObject *)op; |
| 146 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 147 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 148 | characters[*str & UCHAR_MAX] = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 149 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 150 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 151 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 154 | PyObject * |
| 155 | PyString_FromFormatV(const char *format, va_list vargs) |
| 156 | { |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 157 | va_list count; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 158 | Py_ssize_t n = 0; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 159 | const char* f; |
| 160 | char *s; |
| 161 | PyObject* string; |
| 162 | |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 163 | #ifdef VA_LIST_IS_ARRAY |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 164 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 165 | #else |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 166 | #ifdef __va_copy |
| 167 | __va_copy(count, vargs); |
| 168 | #else |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 169 | count = vargs; |
| 170 | #endif |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 171 | #endif |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 172 | /* step 1: figure out how large a buffer we need */ |
| 173 | for (f = format; *f; f++) { |
| 174 | if (*f == '%') { |
| 175 | const char* p = f; |
| 176 | while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
| 177 | ; |
| 178 | |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 179 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 180 | * they don't affect the amount of space we reserve. |
| 181 | */ |
| 182 | if ((*f == 'l' || *f == 'z') && |
| 183 | (f[1] == 'd' || f[1] == 'u')) |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 184 | ++f; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 185 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 186 | switch (*f) { |
| 187 | case 'c': |
| 188 | (void)va_arg(count, int); |
| 189 | /* fall through... */ |
| 190 | case '%': |
| 191 | n++; |
| 192 | break; |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 193 | case 'd': case 'u': case 'i': case 'x': |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 194 | (void) va_arg(count, int); |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 195 | /* 20 bytes is enough to hold a 64-bit |
| 196 | integer. Decimal takes the most space. |
| 197 | This isn't enough for octal. */ |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 198 | n += 20; |
| 199 | break; |
| 200 | case 's': |
| 201 | s = va_arg(count, char*); |
| 202 | n += strlen(s); |
| 203 | break; |
| 204 | case 'p': |
| 205 | (void) va_arg(count, int); |
| 206 | /* maximum 64-bit pointer representation: |
| 207 | * 0xffffffffffffffff |
| 208 | * so 19 characters is enough. |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 209 | * XXX I count 18 -- what's the extra for? |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 210 | */ |
| 211 | n += 19; |
| 212 | break; |
| 213 | default: |
| 214 | /* if we stumble upon an unknown |
| 215 | formatting code, copy the rest of |
| 216 | the format string to the output |
| 217 | string. (we cannot just skip the |
| 218 | code, since there's no way to know |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 219 | what's in the argument list) */ |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 220 | n += strlen(p); |
| 221 | goto expand; |
| 222 | } |
| 223 | } else |
| 224 | n++; |
| 225 | } |
| 226 | expand: |
| 227 | /* step 2: fill the buffer */ |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 228 | /* Since we've analyzed how much space we need for the worst case, |
| 229 | use sprintf directly instead of the slower PyOS_snprintf. */ |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 230 | string = PyString_FromStringAndSize(NULL, n); |
| 231 | if (!string) |
| 232 | return NULL; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 233 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 234 | s = PyString_AsString(string); |
| 235 | |
| 236 | for (f = format; *f; f++) { |
| 237 | if (*f == '%') { |
| 238 | const char* p = f++; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 239 | Py_ssize_t i; |
| 240 | int longflag = 0; |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 241 | int size_tflag = 0; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 242 | /* parse the width.precision part (we're only |
| 243 | interested in the precision value, if any) */ |
| 244 | n = 0; |
| 245 | while (isdigit(Py_CHARMASK(*f))) |
| 246 | n = (n*10) + *f++ - '0'; |
| 247 | if (*f == '.') { |
| 248 | f++; |
| 249 | n = 0; |
| 250 | while (isdigit(Py_CHARMASK(*f))) |
| 251 | n = (n*10) + *f++ - '0'; |
| 252 | } |
| 253 | while (*f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
| 254 | f++; |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 255 | /* handle the long flag, but only for %ld and %lu. |
| 256 | others can be added when necessary. */ |
| 257 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 258 | longflag = 1; |
| 259 | ++f; |
| 260 | } |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 261 | /* handle the size_t flag. */ |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 262 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 263 | size_tflag = 1; |
| 264 | ++f; |
| 265 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 266 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 267 | switch (*f) { |
| 268 | case 'c': |
| 269 | *s++ = va_arg(vargs, int); |
| 270 | break; |
| 271 | case 'd': |
| 272 | if (longflag) |
| 273 | sprintf(s, "%ld", va_arg(vargs, long)); |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 274 | else if (size_tflag) |
Martin v. Löwis | 822f34a | 2006-05-13 13:34:04 +0000 | [diff] [blame] | 275 | sprintf(s, "%" PY_FORMAT_SIZE_T "d", |
| 276 | va_arg(vargs, Py_ssize_t)); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 277 | else |
| 278 | sprintf(s, "%d", va_arg(vargs, int)); |
| 279 | s += strlen(s); |
| 280 | break; |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 281 | case 'u': |
| 282 | if (longflag) |
| 283 | sprintf(s, "%lu", |
| 284 | va_arg(vargs, unsigned long)); |
| 285 | else if (size_tflag) |
| 286 | sprintf(s, "%" PY_FORMAT_SIZE_T "u", |
| 287 | va_arg(vargs, size_t)); |
| 288 | else |
| 289 | sprintf(s, "%u", |
| 290 | va_arg(vargs, unsigned int)); |
| 291 | s += strlen(s); |
| 292 | break; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 293 | case 'i': |
| 294 | sprintf(s, "%i", va_arg(vargs, int)); |
| 295 | s += strlen(s); |
| 296 | break; |
| 297 | case 'x': |
| 298 | sprintf(s, "%x", va_arg(vargs, int)); |
| 299 | s += strlen(s); |
| 300 | break; |
| 301 | case 's': |
| 302 | p = va_arg(vargs, char*); |
| 303 | i = strlen(p); |
| 304 | if (n > 0 && i > n) |
| 305 | i = n; |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 306 | Py_MEMCPY(s, p, i); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 307 | s += i; |
| 308 | break; |
| 309 | case 'p': |
| 310 | sprintf(s, "%p", va_arg(vargs, void*)); |
Tim Peters | 6af5bbb | 2001-08-25 03:02:28 +0000 | [diff] [blame] | 311 | /* %p is ill-defined: ensure leading 0x. */ |
| 312 | if (s[1] == 'X') |
| 313 | s[1] = 'x'; |
| 314 | else if (s[1] != 'x') { |
| 315 | memmove(s+2, s, strlen(s)+1); |
| 316 | s[0] = '0'; |
| 317 | s[1] = 'x'; |
| 318 | } |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 319 | s += strlen(s); |
| 320 | break; |
| 321 | case '%': |
| 322 | *s++ = '%'; |
| 323 | break; |
| 324 | default: |
| 325 | strcpy(s, p); |
| 326 | s += strlen(s); |
| 327 | goto end; |
| 328 | } |
| 329 | } else |
| 330 | *s++ = *f; |
| 331 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 332 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 333 | end: |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 334 | _PyString_Resize(&string, s - PyString_AS_STRING(string)); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 335 | return string; |
| 336 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 337 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 338 | PyObject * |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 339 | PyString_FromFormat(const char *format, ...) |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 340 | { |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 341 | PyObject* ret; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 342 | va_list vargs; |
| 343 | |
| 344 | #ifdef HAVE_STDARG_PROTOTYPES |
| 345 | va_start(vargs, format); |
| 346 | #else |
| 347 | va_start(vargs); |
| 348 | #endif |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 349 | ret = PyString_FromFormatV(format, vargs); |
| 350 | va_end(vargs); |
| 351 | return ret; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 355 | PyObject *PyString_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 356 | Py_ssize_t size, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 357 | const char *encoding, |
| 358 | const char *errors) |
| 359 | { |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 360 | PyObject *v, *str; |
| 361 | |
| 362 | str = PyString_FromStringAndSize(s, size); |
| 363 | if (str == NULL) |
| 364 | return NULL; |
| 365 | v = PyString_AsDecodedString(str, encoding, errors); |
| 366 | Py_DECREF(str); |
| 367 | return v; |
| 368 | } |
| 369 | |
| 370 | PyObject *PyString_AsDecodedObject(PyObject *str, |
| 371 | const char *encoding, |
| 372 | const char *errors) |
| 373 | { |
| 374 | PyObject *v; |
| 375 | |
| 376 | if (!PyString_Check(str)) { |
| 377 | PyErr_BadArgument(); |
| 378 | goto onError; |
| 379 | } |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 380 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 381 | if (encoding == NULL) { |
| 382 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 383 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 384 | #else |
| 385 | PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
| 386 | goto onError; |
| 387 | #endif |
| 388 | } |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 389 | |
| 390 | /* Decode via the codec registry */ |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 391 | v = PyCodec_Decode(str, encoding, errors); |
| 392 | if (v == NULL) |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 393 | goto onError; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 394 | |
| 395 | return v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 396 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 397 | onError: |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | PyObject *PyString_AsDecodedString(PyObject *str, |
| 402 | const char *encoding, |
| 403 | const char *errors) |
| 404 | { |
| 405 | PyObject *v; |
| 406 | |
| 407 | v = PyString_AsDecodedObject(str, encoding, errors); |
| 408 | if (v == NULL) |
| 409 | goto onError; |
| 410 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 411 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 412 | /* Convert Unicode to a string using the default encoding */ |
| 413 | if (PyUnicode_Check(v)) { |
| 414 | PyObject *temp = v; |
| 415 | v = PyUnicode_AsEncodedString(v, NULL, NULL); |
| 416 | Py_DECREF(temp); |
| 417 | if (v == NULL) |
| 418 | goto onError; |
| 419 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 420 | #endif |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 421 | if (!PyString_Check(v)) { |
| 422 | PyErr_Format(PyExc_TypeError, |
| 423 | "decoder did not return a string object (type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 424 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 425 | Py_DECREF(v); |
| 426 | goto onError; |
| 427 | } |
| 428 | |
| 429 | return v; |
| 430 | |
| 431 | onError: |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 432 | return NULL; |
| 433 | } |
| 434 | |
| 435 | PyObject *PyString_Encode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 436 | Py_ssize_t size, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 437 | const char *encoding, |
| 438 | const char *errors) |
| 439 | { |
| 440 | PyObject *v, *str; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 441 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 442 | str = PyString_FromStringAndSize(s, size); |
| 443 | if (str == NULL) |
| 444 | return NULL; |
| 445 | v = PyString_AsEncodedString(str, encoding, errors); |
| 446 | Py_DECREF(str); |
| 447 | return v; |
| 448 | } |
| 449 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 450 | PyObject *PyString_AsEncodedObject(PyObject *str, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 451 | const char *encoding, |
| 452 | const char *errors) |
| 453 | { |
| 454 | PyObject *v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 455 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 456 | if (!PyString_Check(str)) { |
| 457 | PyErr_BadArgument(); |
| 458 | goto onError; |
| 459 | } |
| 460 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 461 | if (encoding == NULL) { |
| 462 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 463 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 464 | #else |
| 465 | PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
| 466 | goto onError; |
| 467 | #endif |
| 468 | } |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 469 | |
| 470 | /* Encode via the codec registry */ |
| 471 | v = PyCodec_Encode(str, encoding, errors); |
| 472 | if (v == NULL) |
| 473 | goto onError; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 474 | |
| 475 | return v; |
| 476 | |
| 477 | onError: |
| 478 | return NULL; |
| 479 | } |
| 480 | |
| 481 | PyObject *PyString_AsEncodedString(PyObject *str, |
| 482 | const char *encoding, |
| 483 | const char *errors) |
| 484 | { |
| 485 | PyObject *v; |
| 486 | |
Marc-André Lemburg | 8c2133d | 2001-06-12 13:14:10 +0000 | [diff] [blame] | 487 | v = PyString_AsEncodedObject(str, encoding, errors); |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 488 | if (v == NULL) |
| 489 | goto onError; |
| 490 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 491 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 492 | /* Convert Unicode to a string using the default encoding */ |
| 493 | if (PyUnicode_Check(v)) { |
| 494 | PyObject *temp = v; |
| 495 | v = PyUnicode_AsEncodedString(v, NULL, NULL); |
| 496 | Py_DECREF(temp); |
| 497 | if (v == NULL) |
| 498 | goto onError; |
| 499 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 500 | #endif |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 501 | if (!PyString_Check(v)) { |
| 502 | PyErr_Format(PyExc_TypeError, |
| 503 | "encoder did not return a string object (type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 504 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 505 | Py_DECREF(v); |
| 506 | goto onError; |
| 507 | } |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 508 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 509 | return v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 510 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 511 | onError: |
| 512 | return NULL; |
| 513 | } |
| 514 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 515 | static void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 516 | string_dealloc(PyObject *op) |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 517 | { |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 518 | switch (PyString_CHECK_INTERNED(op)) { |
| 519 | case SSTATE_NOT_INTERNED: |
| 520 | break; |
| 521 | |
| 522 | case SSTATE_INTERNED_MORTAL: |
| 523 | /* revive dead object temporarily for DelItem */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 524 | Py_REFCNT(op) = 3; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 525 | if (PyDict_DelItem(interned, op) != 0) |
| 526 | Py_FatalError( |
| 527 | "deletion of interned string failed"); |
| 528 | break; |
| 529 | |
| 530 | case SSTATE_INTERNED_IMMORTAL: |
| 531 | Py_FatalError("Immortal interned string died."); |
| 532 | |
| 533 | default: |
| 534 | Py_FatalError("Inconsistent interned string state."); |
| 535 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 536 | Py_TYPE(op)->tp_free(op); |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 539 | /* Unescape a backslash-escaped string. If unicode is non-zero, |
| 540 | the string is a u-literal. If recode_encoding is non-zero, |
| 541 | the string is UTF-8 encoded and should be re-encoded in the |
| 542 | specified encoding. */ |
| 543 | |
| 544 | PyObject *PyString_DecodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 545 | Py_ssize_t len, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 546 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 547 | Py_ssize_t unicode, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 548 | const char *recode_encoding) |
| 549 | { |
| 550 | int c; |
| 551 | char *p, *buf; |
| 552 | const char *end; |
| 553 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 554 | Py_ssize_t newlen = recode_encoding ? 4*len:len; |
Walter Dörwald | 8709a42 | 2002-09-03 13:53:40 +0000 | [diff] [blame] | 555 | v = PyString_FromStringAndSize((char *)NULL, newlen); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 556 | if (v == NULL) |
| 557 | return NULL; |
| 558 | p = buf = PyString_AsString(v); |
| 559 | end = s + len; |
| 560 | while (s < end) { |
| 561 | if (*s != '\\') { |
Martin v. Löwis | 2412853 | 2002-09-09 06:17:05 +0000 | [diff] [blame] | 562 | non_esc: |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 563 | #ifdef Py_USING_UNICODE |
| 564 | if (recode_encoding && (*s & 0x80)) { |
| 565 | PyObject *u, *w; |
| 566 | char *r; |
| 567 | const char* t; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 568 | Py_ssize_t rn; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 569 | t = s; |
| 570 | /* Decode non-ASCII bytes as UTF-8. */ |
| 571 | while (t < end && (*t & 0x80)) t++; |
| 572 | u = PyUnicode_DecodeUTF8(s, t - s, errors); |
| 573 | if(!u) goto failed; |
| 574 | |
| 575 | /* Recode them in target encoding. */ |
| 576 | w = PyUnicode_AsEncodedString( |
| 577 | u, recode_encoding, errors); |
| 578 | Py_DECREF(u); |
| 579 | if (!w) goto failed; |
| 580 | |
| 581 | /* Append bytes to output buffer. */ |
Neal Norwitz | 2aa9a5d | 2006-03-20 01:53:23 +0000 | [diff] [blame] | 582 | assert(PyString_Check(w)); |
| 583 | r = PyString_AS_STRING(w); |
| 584 | rn = PyString_GET_SIZE(w); |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 585 | Py_MEMCPY(p, r, rn); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 586 | p += rn; |
| 587 | Py_DECREF(w); |
| 588 | s = t; |
| 589 | } else { |
| 590 | *p++ = *s++; |
| 591 | } |
| 592 | #else |
| 593 | *p++ = *s++; |
| 594 | #endif |
| 595 | continue; |
| 596 | } |
| 597 | s++; |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 598 | if (s==end) { |
| 599 | PyErr_SetString(PyExc_ValueError, |
| 600 | "Trailing \\ in string"); |
| 601 | goto failed; |
| 602 | } |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 603 | switch (*s++) { |
| 604 | /* XXX This assumes ASCII! */ |
| 605 | case '\n': break; |
| 606 | case '\\': *p++ = '\\'; break; |
| 607 | case '\'': *p++ = '\''; break; |
| 608 | case '\"': *p++ = '\"'; break; |
| 609 | case 'b': *p++ = '\b'; break; |
| 610 | case 'f': *p++ = '\014'; break; /* FF */ |
| 611 | case 't': *p++ = '\t'; break; |
| 612 | case 'n': *p++ = '\n'; break; |
| 613 | case 'r': *p++ = '\r'; break; |
| 614 | case 'v': *p++ = '\013'; break; /* VT */ |
| 615 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 616 | case '0': case '1': case '2': case '3': |
| 617 | case '4': case '5': case '6': case '7': |
| 618 | c = s[-1] - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 619 | if (s < end && '0' <= *s && *s <= '7') { |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 620 | c = (c<<3) + *s++ - '0'; |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 621 | if (s < end && '0' <= *s && *s <= '7') |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 622 | c = (c<<3) + *s++ - '0'; |
| 623 | } |
| 624 | *p++ = c; |
| 625 | break; |
| 626 | case 'x': |
Guido van Rossum | 1c1ac38 | 2007-10-29 22:15:05 +0000 | [diff] [blame] | 627 | if (s+1 < end && |
| 628 | isxdigit(Py_CHARMASK(s[0])) && |
| 629 | isxdigit(Py_CHARMASK(s[1]))) |
| 630 | { |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 631 | unsigned int x = 0; |
| 632 | c = Py_CHARMASK(*s); |
| 633 | s++; |
| 634 | if (isdigit(c)) |
| 635 | x = c - '0'; |
| 636 | else if (islower(c)) |
| 637 | x = 10 + c - 'a'; |
| 638 | else |
| 639 | x = 10 + c - 'A'; |
| 640 | x = x << 4; |
| 641 | c = Py_CHARMASK(*s); |
| 642 | s++; |
| 643 | if (isdigit(c)) |
| 644 | x += c - '0'; |
| 645 | else if (islower(c)) |
| 646 | x += 10 + c - 'a'; |
| 647 | else |
| 648 | x += 10 + c - 'A'; |
| 649 | *p++ = x; |
| 650 | break; |
| 651 | } |
| 652 | if (!errors || strcmp(errors, "strict") == 0) { |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 653 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 654 | "invalid \\x escape"); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 655 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 656 | } |
| 657 | if (strcmp(errors, "replace") == 0) { |
| 658 | *p++ = '?'; |
| 659 | } else if (strcmp(errors, "ignore") == 0) |
| 660 | /* do nothing */; |
| 661 | else { |
| 662 | PyErr_Format(PyExc_ValueError, |
| 663 | "decoding error; " |
| 664 | "unknown error handling code: %.400s", |
| 665 | errors); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 666 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 667 | } |
| 668 | #ifndef Py_USING_UNICODE |
| 669 | case 'u': |
| 670 | case 'U': |
| 671 | case 'N': |
| 672 | if (unicode) { |
Neal Norwitz | b898d9f | 2002-08-16 23:20:39 +0000 | [diff] [blame] | 673 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 674 | "Unicode escapes not legal " |
| 675 | "when Unicode disabled"); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 676 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 677 | } |
| 678 | #endif |
| 679 | default: |
| 680 | *p++ = '\\'; |
Martin v. Löwis | 2412853 | 2002-09-09 06:17:05 +0000 | [diff] [blame] | 681 | s--; |
| 682 | goto non_esc; /* an arbitry number of unescaped |
| 683 | UTF-8 bytes may follow. */ |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 684 | } |
| 685 | } |
Walter Dörwald | 8709a42 | 2002-09-03 13:53:40 +0000 | [diff] [blame] | 686 | if (p-buf < newlen) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 687 | _PyString_Resize(&v, p - buf); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 688 | return v; |
| 689 | failed: |
| 690 | Py_DECREF(v); |
| 691 | return NULL; |
| 692 | } |
| 693 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 694 | /* -------------------------------------------------------------------- */ |
| 695 | /* object api */ |
| 696 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 697 | static Py_ssize_t |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 698 | string_getsize(register PyObject *op) |
| 699 | { |
| 700 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 701 | Py_ssize_t len; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 702 | if (PyString_AsStringAndSize(op, &s, &len)) |
| 703 | return -1; |
| 704 | return len; |
| 705 | } |
| 706 | |
| 707 | static /*const*/ char * |
| 708 | string_getbuffer(register PyObject *op) |
| 709 | { |
| 710 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 711 | Py_ssize_t len; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 712 | if (PyString_AsStringAndSize(op, &s, &len)) |
| 713 | return NULL; |
| 714 | return s; |
| 715 | } |
| 716 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 717 | Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 718 | PyString_Size(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 719 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 720 | if (!PyString_Check(op)) |
| 721 | return string_getsize(op); |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 722 | return Py_SIZE(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | /*const*/ char * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 726 | PyString_AsString(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 727 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 728 | if (!PyString_Check(op)) |
| 729 | return string_getbuffer(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 730 | return ((PyStringObject *)op) -> ob_sval; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 733 | int |
| 734 | PyString_AsStringAndSize(register PyObject *obj, |
| 735 | register char **s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 736 | register Py_ssize_t *len) |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 737 | { |
| 738 | if (s == NULL) { |
| 739 | PyErr_BadInternalCall(); |
| 740 | return -1; |
| 741 | } |
| 742 | |
| 743 | if (!PyString_Check(obj)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 744 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 745 | if (PyUnicode_Check(obj)) { |
| 746 | obj = _PyUnicode_AsDefaultEncodedString(obj, NULL); |
| 747 | if (obj == NULL) |
| 748 | return -1; |
| 749 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 750 | else |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 751 | #endif |
| 752 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 753 | PyErr_Format(PyExc_TypeError, |
| 754 | "expected string or Unicode object, " |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 755 | "%.200s found", Py_TYPE(obj)->tp_name); |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 756 | return -1; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | *s = PyString_AS_STRING(obj); |
| 761 | if (len != NULL) |
| 762 | *len = PyString_GET_SIZE(obj); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 763 | else if (strlen(*s) != (size_t)PyString_GET_SIZE(obj)) { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 764 | PyErr_SetString(PyExc_TypeError, |
| 765 | "expected string without null bytes"); |
| 766 | return -1; |
| 767 | } |
| 768 | return 0; |
| 769 | } |
| 770 | |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 771 | /* -------------------------------------------------------------------- */ |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 772 | /* Methods */ |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 773 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 774 | #define STRINGLIB_CHAR char |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 775 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 776 | #define STRINGLIB_CMP memcmp |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 777 | #define STRINGLIB_LEN PyString_GET_SIZE |
| 778 | #define STRINGLIB_NEW PyString_FromStringAndSize |
| 779 | #define STRINGLIB_STR PyString_AS_STRING |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 780 | |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 781 | #define STRINGLIB_EMPTY nullstring |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 782 | |
Fredrik Lundh | a50d201 | 2006-05-26 17:04:58 +0000 | [diff] [blame] | 783 | #include "stringlib/fastsearch.h" |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 784 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 785 | #include "stringlib/count.h" |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 786 | #include "stringlib/find.h" |
Fredrik Lundh | b947948 | 2006-05-26 17:22:38 +0000 | [diff] [blame] | 787 | #include "stringlib/partition.h" |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 788 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 789 | |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 790 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 791 | string_print(PyStringObject *op, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 792 | { |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 793 | Py_ssize_t i, str_len; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 794 | char c; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 795 | int quote; |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 796 | |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 797 | /* XXX Ought to check for interrupts when writing long strings */ |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 798 | if (! PyString_CheckExact(op)) { |
| 799 | int ret; |
| 800 | /* A str subclass may have its own __str__ method. */ |
| 801 | op = (PyStringObject *) PyObject_Str((PyObject *)op); |
| 802 | if (op == NULL) |
| 803 | return -1; |
| 804 | ret = string_print(op, fp, flags); |
| 805 | Py_DECREF(op); |
| 806 | return ret; |
| 807 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 808 | if (flags & Py_PRINT_RAW) { |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 809 | char *data = op->ob_sval; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 810 | Py_ssize_t size = Py_SIZE(op); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 811 | Py_BEGIN_ALLOW_THREADS |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 812 | while (size > INT_MAX) { |
| 813 | /* Very long strings cannot be written atomically. |
| 814 | * But don't write exactly INT_MAX bytes at a time |
| 815 | * to avoid memory aligment issues. |
| 816 | */ |
| 817 | const int chunk_size = INT_MAX & ~0x3FFF; |
| 818 | fwrite(data, 1, chunk_size, fp); |
| 819 | data += chunk_size; |
| 820 | size -= chunk_size; |
| 821 | } |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 822 | #ifdef __VMS |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 823 | if (size) fwrite(data, (int)size, 1, fp); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 824 | #else |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 825 | fwrite(data, 1, (int)size, fp); |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 826 | #endif |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 827 | Py_END_ALLOW_THREADS |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 828 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 829 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 830 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 831 | /* figure out which quote to use; single is preferred */ |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 832 | quote = '\''; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 833 | if (memchr(op->ob_sval, '\'', Py_SIZE(op)) && |
| 834 | !memchr(op->ob_sval, '"', Py_SIZE(op))) |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 835 | quote = '"'; |
| 836 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 837 | str_len = Py_SIZE(op); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 838 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 839 | fputc(quote, fp); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 840 | for (i = 0; i < str_len; i++) { |
| 841 | /* Since strings are immutable and the caller should have a |
| 842 | reference, accessing the interal buffer should not be an issue |
| 843 | with the GIL released. */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 844 | c = op->ob_sval[i]; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 845 | if (c == quote || c == '\\') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 846 | fprintf(fp, "\\%c", c); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 847 | else if (c == '\t') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 848 | fprintf(fp, "\\t"); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 849 | else if (c == '\n') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 850 | fprintf(fp, "\\n"); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 851 | else if (c == '\r') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 852 | fprintf(fp, "\\r"); |
| 853 | else if (c < ' ' || c >= 0x7f) |
| 854 | fprintf(fp, "\\x%02x", c & 0xff); |
Martin v. Löwis | fed2405 | 2002-10-07 13:55:50 +0000 | [diff] [blame] | 855 | else |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 856 | fputc(c, fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 857 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 858 | fputc(quote, fp); |
Brett Cannon | 0153159 | 2007-09-17 03:28:34 +0000 | [diff] [blame] | 859 | Py_END_ALLOW_THREADS |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 860 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 863 | PyObject * |
| 864 | PyString_Repr(PyObject *obj, int smartquotes) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 865 | { |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 866 | register PyStringObject* op = (PyStringObject*) obj; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 867 | size_t newsize = 2 + 4 * Py_SIZE(op); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 868 | PyObject *v; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 869 | if (newsize > PY_SSIZE_T_MAX || newsize / 4 != Py_SIZE(op)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 870 | PyErr_SetString(PyExc_OverflowError, |
| 871 | "string is too large to make repr"); |
Guido van Rossum | 9b847b4 | 2007-11-06 23:32:56 +0000 | [diff] [blame] | 872 | return NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 873 | } |
| 874 | v = PyString_FromStringAndSize((char *)NULL, newsize); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 875 | if (v == NULL) { |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 876 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 877 | } |
| 878 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 879 | register Py_ssize_t i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 880 | register char c; |
| 881 | register char *p; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 882 | int quote; |
| 883 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 884 | /* figure out which quote to use; single is preferred */ |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 885 | quote = '\''; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 886 | if (smartquotes && |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 887 | memchr(op->ob_sval, '\'', Py_SIZE(op)) && |
| 888 | !memchr(op->ob_sval, '"', Py_SIZE(op))) |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 889 | quote = '"'; |
| 890 | |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 891 | p = PyString_AS_STRING(v); |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 892 | *p++ = quote; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 893 | for (i = 0; i < Py_SIZE(op); i++) { |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 894 | /* There's at least enough room for a hex escape |
| 895 | and a closing quote. */ |
| 896 | assert(newsize - (p - PyString_AS_STRING(v)) >= 5); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 897 | c = op->ob_sval[i]; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 898 | if (c == quote || c == '\\') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 899 | *p++ = '\\', *p++ = c; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 900 | else if (c == '\t') |
| 901 | *p++ = '\\', *p++ = 't'; |
| 902 | else if (c == '\n') |
| 903 | *p++ = '\\', *p++ = 'n'; |
| 904 | else if (c == '\r') |
| 905 | *p++ = '\\', *p++ = 'r'; |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 906 | else if (c < ' ' || c >= 0x7f) { |
| 907 | /* For performance, we don't want to call |
| 908 | PyOS_snprintf here (extra layers of |
| 909 | function call). */ |
| 910 | sprintf(p, "\\x%02x", c & 0xff); |
| 911 | p += 4; |
Martin v. Löwis | fed2405 | 2002-10-07 13:55:50 +0000 | [diff] [blame] | 912 | } |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 913 | else |
| 914 | *p++ = c; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 915 | } |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 916 | assert(newsize - (p - PyString_AS_STRING(v)) >= 1); |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 917 | *p++ = quote; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 918 | *p = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 919 | _PyString_Resize( |
Thomas Wouters | 568f1d0 | 2006-04-21 13:54:43 +0000 | [diff] [blame] | 920 | &v, (p - PyString_AS_STRING(v))); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 921 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 922 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 925 | static PyObject * |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 926 | string_repr(PyObject *op) |
| 927 | { |
| 928 | return PyString_Repr(op, 1); |
| 929 | } |
| 930 | |
| 931 | static PyObject * |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 932 | string_str(PyObject *s) |
| 933 | { |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 934 | assert(PyString_Check(s)); |
| 935 | if (PyString_CheckExact(s)) { |
| 936 | Py_INCREF(s); |
| 937 | return s; |
| 938 | } |
| 939 | else { |
| 940 | /* Subtype -- return genuine string with the same value. */ |
| 941 | PyStringObject *t = (PyStringObject *) s; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 942 | return PyString_FromStringAndSize(t->ob_sval, Py_SIZE(t)); |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 943 | } |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 946 | static Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 947 | string_length(PyStringObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 948 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 949 | return Py_SIZE(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 950 | } |
| 951 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 952 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 953 | string_concat(register PyStringObject *a, register PyObject *bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 954 | { |
Andrew Dalke | 598710c | 2006-05-25 18:18:39 +0000 | [diff] [blame] | 955 | register Py_ssize_t size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 956 | register PyStringObject *op; |
| 957 | if (!PyString_Check(bb)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 958 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 959 | if (PyUnicode_Check(bb)) |
| 960 | return PyUnicode_Concat((PyObject *)a, bb); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 961 | #endif |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 962 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5c66a26 | 2001-10-22 04:12:44 +0000 | [diff] [blame] | 963 | "cannot concatenate 'str' and '%.200s' objects", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 964 | Py_TYPE(bb)->tp_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 965 | return NULL; |
| 966 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 967 | #define b ((PyStringObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 968 | /* Optimize cases with empty left or right operand */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 969 | if ((Py_SIZE(a) == 0 || Py_SIZE(b) == 0) && |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 970 | PyString_CheckExact(a) && PyString_CheckExact(b)) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 971 | if (Py_SIZE(a) == 0) { |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 972 | Py_INCREF(bb); |
| 973 | return bb; |
| 974 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 975 | Py_INCREF(a); |
| 976 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 977 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 978 | size = Py_SIZE(a) + Py_SIZE(b); |
Andrew Dalke | 598710c | 2006-05-25 18:18:39 +0000 | [diff] [blame] | 979 | if (size < 0) { |
| 980 | PyErr_SetString(PyExc_OverflowError, |
| 981 | "strings are too large to concat"); |
| 982 | return NULL; |
| 983 | } |
| 984 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 985 | /* Inline PyObject_NewVar */ |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 986 | op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 987 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 988 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 989 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 990 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 991 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 992 | Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a)); |
| 993 | Py_MEMCPY(op->ob_sval + Py_SIZE(a), b->ob_sval, Py_SIZE(b)); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 994 | op->ob_sval[size] = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 995 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 996 | #undef b |
| 997 | } |
| 998 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 999 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1000 | string_repeat(register PyStringObject *a, register Py_ssize_t n) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1001 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1002 | register Py_ssize_t i; |
| 1003 | register Py_ssize_t j; |
| 1004 | register Py_ssize_t size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1005 | register PyStringObject *op; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 1006 | size_t nbytes; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1007 | if (n < 0) |
| 1008 | n = 0; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 1009 | /* watch out for overflows: the size can overflow int, |
| 1010 | * and the # of bytes needed can overflow size_t |
| 1011 | */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1012 | size = Py_SIZE(a) * n; |
| 1013 | if (n && size / n != Py_SIZE(a)) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 1014 | PyErr_SetString(PyExc_OverflowError, |
| 1015 | "repeated string is too long"); |
| 1016 | return NULL; |
| 1017 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1018 | if (size == Py_SIZE(a) && PyString_CheckExact(a)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1019 | Py_INCREF(a); |
| 1020 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1021 | } |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 1022 | nbytes = (size_t)size; |
| 1023 | if (nbytes + sizeof(PyStringObject) <= nbytes) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 1024 | PyErr_SetString(PyExc_OverflowError, |
| 1025 | "repeated string is too long"); |
| 1026 | return NULL; |
| 1027 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1028 | op = (PyStringObject *) |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 1029 | PyObject_MALLOC(sizeof(PyStringObject) + nbytes); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 1030 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1031 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1032 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1033 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 1034 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Raymond Hettinger | 0a2f849 | 2003-01-06 22:42:41 +0000 | [diff] [blame] | 1035 | op->ob_sval[size] = '\0'; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1036 | if (Py_SIZE(a) == 1 && n > 0) { |
Raymond Hettinger | 0a2f849 | 2003-01-06 22:42:41 +0000 | [diff] [blame] | 1037 | memset(op->ob_sval, a->ob_sval[0] , n); |
| 1038 | return (PyObject *) op; |
| 1039 | } |
Raymond Hettinger | 698258a | 2003-01-06 10:33:56 +0000 | [diff] [blame] | 1040 | i = 0; |
| 1041 | if (i < size) { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1042 | Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a)); |
| 1043 | i = Py_SIZE(a); |
Raymond Hettinger | 698258a | 2003-01-06 10:33:56 +0000 | [diff] [blame] | 1044 | } |
| 1045 | while (i < size) { |
| 1046 | j = (i <= size-i) ? i : size-i; |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 1047 | Py_MEMCPY(op->ob_sval+i, op->ob_sval, j); |
Raymond Hettinger | 698258a | 2003-01-06 10:33:56 +0000 | [diff] [blame] | 1048 | i += j; |
| 1049 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1050 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ |
| 1054 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1055 | static PyObject * |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1056 | string_slice(register PyStringObject *a, register Py_ssize_t i, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1057 | register Py_ssize_t j) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1058 | /* j -- may be negative! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1059 | { |
| 1060 | if (i < 0) |
| 1061 | i = 0; |
| 1062 | if (j < 0) |
| 1063 | j = 0; /* Avoid signed/unsigned bug in next line */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1064 | if (j > Py_SIZE(a)) |
| 1065 | j = Py_SIZE(a); |
| 1066 | if (i == 0 && j == Py_SIZE(a) && PyString_CheckExact(a)) { |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1067 | /* It's the same as a */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1068 | Py_INCREF(a); |
| 1069 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1070 | } |
| 1071 | if (j < i) |
| 1072 | j = i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1073 | return PyString_FromStringAndSize(a->ob_sval + i, j-i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1076 | static int |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1077 | string_contains(PyObject *str_obj, PyObject *sub_obj) |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1078 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1079 | if (!PyString_CheckExact(sub_obj)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1080 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1081 | if (PyUnicode_Check(sub_obj)) |
| 1082 | return PyUnicode_Contains(str_obj, sub_obj); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1083 | #endif |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1084 | if (!PyString_Check(sub_obj)) { |
Georg Brandl | 283a135 | 2006-11-19 08:48:30 +0000 | [diff] [blame] | 1085 | PyErr_Format(PyExc_TypeError, |
| 1086 | "'in <string>' requires string as left operand, " |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1087 | "not %.200s", Py_TYPE(sub_obj)->tp_name); |
Guido van Rossum | bf935fd | 2002-08-24 06:57:49 +0000 | [diff] [blame] | 1088 | return -1; |
| 1089 | } |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1090 | } |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 1091 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1092 | return stringlib_contains_obj(str_obj, sub_obj); |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1095 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1096 | string_item(PyStringObject *a, register Py_ssize_t i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1097 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1098 | char pchar; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1099 | PyObject *v; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1100 | if (i < 0 || i >= Py_SIZE(a)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1101 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1102 | return NULL; |
| 1103 | } |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1104 | pchar = a->ob_sval[i]; |
| 1105 | v = (PyObject *)characters[pchar & UCHAR_MAX]; |
Tim Peters | 5b4d477 | 2001-05-08 22:33:50 +0000 | [diff] [blame] | 1106 | if (v == NULL) |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1107 | v = PyString_FromStringAndSize(&pchar, 1); |
Tim Peters | b4bbcd7 | 2001-05-09 00:31:40 +0000 | [diff] [blame] | 1108 | else { |
| 1109 | #ifdef COUNT_ALLOCS |
| 1110 | one_strings++; |
| 1111 | #endif |
Tim Peters | cf5ad5d | 2001-05-09 00:24:55 +0000 | [diff] [blame] | 1112 | Py_INCREF(v); |
Tim Peters | b4bbcd7 | 2001-05-09 00:31:40 +0000 | [diff] [blame] | 1113 | } |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 1114 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1117 | static PyObject* |
| 1118 | string_richcompare(PyStringObject *a, PyStringObject *b, int op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1119 | { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1120 | int c; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1121 | Py_ssize_t len_a, len_b; |
| 1122 | Py_ssize_t min_len; |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1123 | PyObject *result; |
| 1124 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 1125 | /* Make sure both arguments are strings. */ |
| 1126 | if (!(PyString_Check(a) && PyString_Check(b))) { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1127 | result = Py_NotImplemented; |
| 1128 | goto out; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1129 | } |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1130 | if (a == b) { |
| 1131 | switch (op) { |
| 1132 | case Py_EQ:case Py_LE:case Py_GE: |
| 1133 | result = Py_True; |
| 1134 | goto out; |
| 1135 | case Py_NE:case Py_LT:case Py_GT: |
| 1136 | result = Py_False; |
| 1137 | goto out; |
| 1138 | } |
| 1139 | } |
| 1140 | if (op == Py_EQ) { |
| 1141 | /* Supporting Py_NE here as well does not save |
| 1142 | much time, since Py_NE is rarely used. */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1143 | if (Py_SIZE(a) == Py_SIZE(b) |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1144 | && (a->ob_sval[0] == b->ob_sval[0] |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1145 | && memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0)) { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1146 | result = Py_True; |
| 1147 | } else { |
| 1148 | result = Py_False; |
| 1149 | } |
| 1150 | goto out; |
| 1151 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1152 | len_a = Py_SIZE(a); len_b = Py_SIZE(b); |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1153 | min_len = (len_a < len_b) ? len_a : len_b; |
| 1154 | if (min_len > 0) { |
| 1155 | c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
| 1156 | if (c==0) |
| 1157 | c = memcmp(a->ob_sval, b->ob_sval, min_len); |
Neal Norwitz | 7218c2d | 2007-02-25 15:53:36 +0000 | [diff] [blame] | 1158 | } else |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1159 | c = 0; |
| 1160 | if (c == 0) |
| 1161 | c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
| 1162 | switch (op) { |
| 1163 | case Py_LT: c = c < 0; break; |
| 1164 | case Py_LE: c = c <= 0; break; |
| 1165 | case Py_EQ: assert(0); break; /* unreachable */ |
| 1166 | case Py_NE: c = c != 0; break; |
| 1167 | case Py_GT: c = c > 0; break; |
| 1168 | case Py_GE: c = c >= 0; break; |
| 1169 | default: |
| 1170 | result = Py_NotImplemented; |
| 1171 | goto out; |
| 1172 | } |
| 1173 | result = c ? Py_True : Py_False; |
| 1174 | out: |
| 1175 | Py_INCREF(result); |
| 1176 | return result; |
| 1177 | } |
| 1178 | |
| 1179 | int |
| 1180 | _PyString_Eq(PyObject *o1, PyObject *o2) |
| 1181 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 1182 | PyStringObject *a = (PyStringObject*) o1; |
| 1183 | PyStringObject *b = (PyStringObject*) o2; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1184 | return Py_SIZE(a) == Py_SIZE(b) |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1185 | && *a->ob_sval == *b->ob_sval |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1186 | && memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1189 | static long |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1190 | string_hash(PyStringObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1191 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1192 | register Py_ssize_t len; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1193 | register unsigned char *p; |
| 1194 | register long x; |
| 1195 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1196 | if (a->ob_shash != -1) |
| 1197 | return a->ob_shash; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1198 | len = Py_SIZE(a); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1199 | p = (unsigned char *) a->ob_sval; |
| 1200 | x = *p << 7; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1201 | while (--len >= 0) |
Guido van Rossum | eddcb3b | 1996-09-11 20:22:48 +0000 | [diff] [blame] | 1202 | x = (1000003*x) ^ *p++; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1203 | x ^= Py_SIZE(a); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1204 | if (x == -1) |
| 1205 | x = -2; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1206 | a->ob_shash = x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1207 | return x; |
| 1208 | } |
| 1209 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1210 | static PyObject* |
| 1211 | string_subscript(PyStringObject* self, PyObject* item) |
| 1212 | { |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1213 | if (PyIndex_Check(item)) { |
| 1214 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1215 | if (i == -1 && PyErr_Occurred()) |
| 1216 | return NULL; |
| 1217 | if (i < 0) |
| 1218 | i += PyString_GET_SIZE(self); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1219 | return string_item(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1220 | } |
| 1221 | else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1222 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1223 | char* source_buf; |
| 1224 | char* result_buf; |
| 1225 | PyObject* result; |
| 1226 | |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1227 | if (PySlice_GetIndicesEx((PySliceObject*)item, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1228 | PyString_GET_SIZE(self), |
| 1229 | &start, &stop, &step, &slicelength) < 0) { |
| 1230 | return NULL; |
| 1231 | } |
| 1232 | |
| 1233 | if (slicelength <= 0) { |
| 1234 | return PyString_FromStringAndSize("", 0); |
| 1235 | } |
Thomas Wouters | 3ccec68 | 2007-08-28 15:28:19 +0000 | [diff] [blame] | 1236 | else if (start == 0 && step == 1 && |
| 1237 | slicelength == PyString_GET_SIZE(self) && |
| 1238 | PyString_CheckExact(self)) { |
| 1239 | Py_INCREF(self); |
| 1240 | return (PyObject *)self; |
| 1241 | } |
| 1242 | else if (step == 1) { |
| 1243 | return PyString_FromStringAndSize( |
| 1244 | PyString_AS_STRING(self) + start, |
| 1245 | slicelength); |
| 1246 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1247 | else { |
| 1248 | source_buf = PyString_AsString((PyObject*)self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1249 | result_buf = (char *)PyMem_Malloc(slicelength); |
Neal Norwitz | 95c1e50 | 2005-10-20 04:15:52 +0000 | [diff] [blame] | 1250 | if (result_buf == NULL) |
| 1251 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1252 | |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1253 | for (cur = start, i = 0; i < slicelength; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1254 | cur += step, i++) { |
| 1255 | result_buf[i] = source_buf[cur]; |
| 1256 | } |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1257 | |
| 1258 | result = PyString_FromStringAndSize(result_buf, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1259 | slicelength); |
| 1260 | PyMem_Free(result_buf); |
| 1261 | return result; |
| 1262 | } |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1263 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1264 | else { |
Georg Brandl | 283a135 | 2006-11-19 08:48:30 +0000 | [diff] [blame] | 1265 | PyErr_Format(PyExc_TypeError, |
| 1266 | "string indices must be integers, not %.200s", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1267 | Py_TYPE(item)->tp_name); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1268 | return NULL; |
| 1269 | } |
| 1270 | } |
| 1271 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1272 | static Py_ssize_t |
| 1273 | 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] | 1274 | { |
| 1275 | if ( index != 0 ) { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1276 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 1277 | "accessing non-existent string segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1278 | return -1; |
| 1279 | } |
| 1280 | *ptr = (void *)self->ob_sval; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1281 | return Py_SIZE(self); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1284 | static Py_ssize_t |
| 1285 | 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] | 1286 | { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1287 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 07d7800 | 1998-10-01 15:59:48 +0000 | [diff] [blame] | 1288 | "Cannot use string as modifiable buffer"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1289 | return -1; |
| 1290 | } |
| 1291 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1292 | static Py_ssize_t |
| 1293 | string_buffer_getsegcount(PyStringObject *self, Py_ssize_t *lenp) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1294 | { |
| 1295 | if ( lenp ) |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1296 | *lenp = Py_SIZE(self); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1297 | return 1; |
| 1298 | } |
| 1299 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1300 | static Py_ssize_t |
| 1301 | 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] | 1302 | { |
| 1303 | if ( index != 0 ) { |
| 1304 | PyErr_SetString(PyExc_SystemError, |
| 1305 | "accessing non-existent string segment"); |
| 1306 | return -1; |
| 1307 | } |
| 1308 | *ptr = self->ob_sval; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1309 | return Py_SIZE(self); |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1312 | static PySequenceMethods string_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1313 | (lenfunc)string_length, /*sq_length*/ |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1314 | (binaryfunc)string_concat, /*sq_concat*/ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1315 | (ssizeargfunc)string_repeat, /*sq_repeat*/ |
| 1316 | (ssizeargfunc)string_item, /*sq_item*/ |
| 1317 | (ssizessizeargfunc)string_slice, /*sq_slice*/ |
Guido van Rossum | f380e66 | 1991-06-04 19:36:32 +0000 | [diff] [blame] | 1318 | 0, /*sq_ass_item*/ |
| 1319 | 0, /*sq_ass_slice*/ |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1320 | (objobjproc)string_contains /*sq_contains*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1321 | }; |
| 1322 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1323 | static PyMappingMethods string_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1324 | (lenfunc)string_length, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1325 | (binaryfunc)string_subscript, |
| 1326 | 0, |
| 1327 | }; |
| 1328 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1329 | static PyBufferProcs string_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1330 | (readbufferproc)string_buffer_getreadbuf, |
| 1331 | (writebufferproc)string_buffer_getwritebuf, |
| 1332 | (segcountproc)string_buffer_getsegcount, |
| 1333 | (charbufferproc)string_buffer_getcharbuf, |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1334 | }; |
| 1335 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1336 | |
| 1337 | |
| 1338 | #define LEFTSTRIP 0 |
| 1339 | #define RIGHTSTRIP 1 |
| 1340 | #define BOTHSTRIP 2 |
| 1341 | |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1342 | /* Arrays indexed by above */ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 1343 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 1344 | |
| 1345 | #define STRIPNAME(i) (stripformat[i]+3) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1346 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1347 | |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1348 | /* Don't call if length < 2 */ |
| 1349 | #define Py_STRING_MATCH(target, offset, pattern, length) \ |
| 1350 | (target[offset] == pattern[0] && \ |
| 1351 | target[offset+length-1] == pattern[length-1] && \ |
| 1352 | !memcmp(target+offset+1, pattern+1, length-2) ) |
| 1353 | |
| 1354 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1355 | /* Overallocate the initial list to reduce the number of reallocs for small |
| 1356 | split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three |
| 1357 | resizes, to sizes 4, 8, then 16. Most observed string splits are for human |
| 1358 | text (roughly 11 words per line) and field delimited data (usually 1-10 |
| 1359 | fields). For large strings the split algorithms are bandwidth limited |
| 1360 | so increasing the preallocation likely will not improve things.*/ |
| 1361 | |
| 1362 | #define MAX_PREALLOC 12 |
| 1363 | |
| 1364 | /* 5 splits gives 6 elements */ |
| 1365 | #define PREALLOC_SIZE(maxsplit) \ |
| 1366 | (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1) |
| 1367 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1368 | #define SPLIT_APPEND(data, left, right) \ |
| 1369 | str = PyString_FromStringAndSize((data) + (left), \ |
| 1370 | (right) - (left)); \ |
| 1371 | if (str == NULL) \ |
| 1372 | goto onError; \ |
| 1373 | if (PyList_Append(list, str)) { \ |
| 1374 | Py_DECREF(str); \ |
| 1375 | goto onError; \ |
| 1376 | } \ |
| 1377 | else \ |
| 1378 | Py_DECREF(str); |
| 1379 | |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1380 | #define SPLIT_ADD(data, left, right) { \ |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1381 | str = PyString_FromStringAndSize((data) + (left), \ |
| 1382 | (right) - (left)); \ |
| 1383 | if (str == NULL) \ |
| 1384 | goto onError; \ |
| 1385 | if (count < MAX_PREALLOC) { \ |
| 1386 | PyList_SET_ITEM(list, count, str); \ |
| 1387 | } else { \ |
| 1388 | if (PyList_Append(list, str)) { \ |
| 1389 | Py_DECREF(str); \ |
| 1390 | goto onError; \ |
| 1391 | } \ |
| 1392 | else \ |
| 1393 | Py_DECREF(str); \ |
| 1394 | } \ |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1395 | count++; } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1396 | |
| 1397 | /* Always force the list to the expected size. */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1398 | #define FIX_PREALLOC_SIZE(list) Py_SIZE(list) = count |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1399 | |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1400 | #define SKIP_SPACE(s, i, len) { while (i<len && isspace(Py_CHARMASK(s[i]))) i++; } |
| 1401 | #define SKIP_NONSPACE(s, i, len) { while (i<len && !isspace(Py_CHARMASK(s[i]))) i++; } |
| 1402 | #define RSKIP_SPACE(s, i) { while (i>=0 && isspace(Py_CHARMASK(s[i]))) i--; } |
| 1403 | #define RSKIP_NONSPACE(s, i) { while (i>=0 && !isspace(Py_CHARMASK(s[i]))) i--; } |
| 1404 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 1405 | Py_LOCAL_INLINE(PyObject *) |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1406 | split_whitespace(PyStringObject *self, Py_ssize_t len, Py_ssize_t maxsplit) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1407 | { |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1408 | const char *s = PyString_AS_STRING(self); |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1409 | Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1410 | PyObject *str; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1411 | PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1412 | |
| 1413 | if (list == NULL) |
| 1414 | return NULL; |
| 1415 | |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1416 | i = j = 0; |
| 1417 | |
| 1418 | while (maxsplit-- > 0) { |
| 1419 | SKIP_SPACE(s, i, len); |
| 1420 | if (i==len) break; |
| 1421 | j = i; i++; |
| 1422 | SKIP_NONSPACE(s, i, len); |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1423 | if (j == 0 && i == len && PyString_CheckExact(self)) { |
| 1424 | /* No whitespace in self, so just use it as list[0] */ |
| 1425 | Py_INCREF(self); |
| 1426 | PyList_SET_ITEM(list, 0, (PyObject *)self); |
| 1427 | count++; |
| 1428 | break; |
| 1429 | } |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1430 | SPLIT_ADD(s, j, i); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1431 | } |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1432 | |
| 1433 | if (i < len) { |
| 1434 | /* Only occurs when maxsplit was reached */ |
| 1435 | /* Skip any remaining whitespace and copy to end of string */ |
| 1436 | SKIP_SPACE(s, i, len); |
| 1437 | if (i != len) |
| 1438 | SPLIT_ADD(s, i, len); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1439 | } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1440 | FIX_PREALLOC_SIZE(list); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1441 | return list; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1442 | onError: |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1443 | Py_DECREF(list); |
| 1444 | return NULL; |
| 1445 | } |
| 1446 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 1447 | Py_LOCAL_INLINE(PyObject *) |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1448 | split_char(PyStringObject *self, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1449 | { |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1450 | const char *s = PyString_AS_STRING(self); |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1451 | register Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1452 | PyObject *str; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1453 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1454 | |
| 1455 | if (list == NULL) |
| 1456 | return NULL; |
| 1457 | |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1458 | i = j = 0; |
| 1459 | while ((j < len) && (maxcount-- > 0)) { |
| 1460 | for(; j<len; j++) { |
| 1461 | /* I found that using memchr makes no difference */ |
| 1462 | if (s[j] == ch) { |
| 1463 | SPLIT_ADD(s, i, j); |
| 1464 | i = j = j + 1; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1465 | break; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1466 | } |
| 1467 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1468 | } |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1469 | if (i == 0 && count == 0 && PyString_CheckExact(self)) { |
| 1470 | /* ch not in self, so just use self as list[0] */ |
| 1471 | Py_INCREF(self); |
| 1472 | PyList_SET_ITEM(list, 0, (PyObject *)self); |
| 1473 | count++; |
| 1474 | } |
| 1475 | else if (i <= len) { |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1476 | SPLIT_ADD(s, i, len); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1477 | } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1478 | FIX_PREALLOC_SIZE(list); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1479 | return list; |
| 1480 | |
| 1481 | onError: |
| 1482 | Py_DECREF(list); |
| 1483 | return NULL; |
| 1484 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1485 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1486 | PyDoc_STRVAR(split__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1487 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 1488 | \n\ |
| 1489 | 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] | 1490 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Raymond Hettinger | bc552ce | 2002-08-05 06:28:21 +0000 | [diff] [blame] | 1491 | splits are done. If sep is not specified or is None, any\n\ |
| 1492 | whitespace string is a separator."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1493 | |
| 1494 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1495 | string_split(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1496 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1497 | Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1498 | Py_ssize_t maxsplit = -1, count=0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1499 | const char *s = PyString_AS_STRING(self), *sub; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1500 | PyObject *list, *str, *subobj = Py_None; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1501 | #ifdef USE_FAST |
| 1502 | Py_ssize_t pos; |
| 1503 | #endif |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1504 | |
Martin v. Löwis | 9c83076 | 2006-04-13 08:37:17 +0000 | [diff] [blame] | 1505 | if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1506 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1507 | if (maxsplit < 0) |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1508 | maxsplit = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1509 | if (subobj == Py_None) |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1510 | return split_whitespace(self, len, maxsplit); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1511 | if (PyString_Check(subobj)) { |
| 1512 | sub = PyString_AS_STRING(subobj); |
| 1513 | n = PyString_GET_SIZE(subobj); |
| 1514 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1515 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1516 | else if (PyUnicode_Check(subobj)) |
| 1517 | return PyUnicode_Split((PyObject *)self, subobj, maxsplit); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1518 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1519 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 1520 | return NULL; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1521 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1522 | if (n == 0) { |
| 1523 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 1524 | return NULL; |
| 1525 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1526 | else if (n == 1) |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1527 | return split_char(self, len, sub[0], maxsplit); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1528 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1529 | list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1530 | if (list == NULL) |
| 1531 | return NULL; |
| 1532 | |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1533 | #ifdef USE_FAST |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1534 | i = j = 0; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1535 | while (maxsplit-- > 0) { |
| 1536 | pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH); |
| 1537 | if (pos < 0) |
| 1538 | break; |
| 1539 | j = i+pos; |
| 1540 | SPLIT_ADD(s, i, j); |
| 1541 | i = j + n; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1542 | } |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1543 | #else |
| 1544 | i = j = 0; |
| 1545 | while ((j+n <= len) && (maxsplit-- > 0)) { |
| 1546 | for (; j+n <= len; j++) { |
| 1547 | if (Py_STRING_MATCH(s, j, sub, n)) { |
| 1548 | SPLIT_ADD(s, i, j); |
| 1549 | i = j = j + n; |
| 1550 | break; |
| 1551 | } |
| 1552 | } |
| 1553 | } |
| 1554 | #endif |
| 1555 | SPLIT_ADD(s, i, len); |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1556 | FIX_PREALLOC_SIZE(list); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1557 | return list; |
| 1558 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1559 | onError: |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1560 | Py_DECREF(list); |
| 1561 | return NULL; |
| 1562 | } |
| 1563 | |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1564 | PyDoc_STRVAR(partition__doc__, |
| 1565 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 1566 | \n\ |
| 1567 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 1568 | the separator itself, and the part after it. If the separator is not\n\ |
| 1569 | found, returns S and two empty strings."); |
| 1570 | |
| 1571 | static PyObject * |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 1572 | string_partition(PyStringObject *self, PyObject *sep_obj) |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1573 | { |
Fredrik Lundh | c2032fb | 2006-05-26 17:26:39 +0000 | [diff] [blame] | 1574 | const char *sep; |
| 1575 | Py_ssize_t sep_len; |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1576 | |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 1577 | if (PyString_Check(sep_obj)) { |
| 1578 | sep = PyString_AS_STRING(sep_obj); |
| 1579 | sep_len = PyString_GET_SIZE(sep_obj); |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1580 | } |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 1581 | #ifdef Py_USING_UNICODE |
| 1582 | else if (PyUnicode_Check(sep_obj)) |
Fredrik Lundh | c2032fb | 2006-05-26 17:26:39 +0000 | [diff] [blame] | 1583 | return PyUnicode_Partition((PyObject *) self, sep_obj); |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1584 | #endif |
Fredrik Lundh | 06a69dd | 2006-05-26 08:54:28 +0000 | [diff] [blame] | 1585 | else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1586 | return NULL; |
| 1587 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 1588 | return stringlib_partition( |
Fredrik Lundh | c2032fb | 2006-05-26 17:26:39 +0000 | [diff] [blame] | 1589 | (PyObject*) self, |
| 1590 | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
| 1591 | sep_obj, sep, sep_len |
| 1592 | ); |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1593 | } |
| 1594 | |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 1595 | PyDoc_STRVAR(rpartition__doc__, |
Raymond Hettinger | a0c95fa | 2006-09-04 15:32:48 +0000 | [diff] [blame] | 1596 | "S.rpartition(sep) -> (tail, sep, head)\n\ |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 1597 | \n\ |
| 1598 | Searches for the separator sep in S, starting at the end of S, and returns\n\ |
| 1599 | the part before it, the separator itself, and the part after it. If the\n\ |
Raymond Hettinger | a0c95fa | 2006-09-04 15:32:48 +0000 | [diff] [blame] | 1600 | separator is not found, returns two empty strings and S."); |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 1601 | |
| 1602 | static PyObject * |
| 1603 | string_rpartition(PyStringObject *self, PyObject *sep_obj) |
| 1604 | { |
| 1605 | const char *sep; |
| 1606 | Py_ssize_t sep_len; |
| 1607 | |
| 1608 | if (PyString_Check(sep_obj)) { |
| 1609 | sep = PyString_AS_STRING(sep_obj); |
| 1610 | sep_len = PyString_GET_SIZE(sep_obj); |
| 1611 | } |
| 1612 | #ifdef Py_USING_UNICODE |
| 1613 | else if (PyUnicode_Check(sep_obj)) |
| 1614 | return PyUnicode_Partition((PyObject *) self, sep_obj); |
| 1615 | #endif |
| 1616 | else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
| 1617 | return NULL; |
| 1618 | |
Fredrik Lundh | 58b5e84 | 2006-05-26 19:24:53 +0000 | [diff] [blame] | 1619 | return stringlib_rpartition( |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 1620 | (PyObject*) self, |
| 1621 | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
| 1622 | sep_obj, sep, sep_len |
| 1623 | ); |
| 1624 | } |
| 1625 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 1626 | Py_LOCAL_INLINE(PyObject *) |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1627 | rsplit_whitespace(PyStringObject *self, Py_ssize_t len, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1628 | { |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1629 | const char *s = PyString_AS_STRING(self); |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1630 | Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1631 | PyObject *str; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1632 | PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1633 | |
| 1634 | if (list == NULL) |
| 1635 | return NULL; |
| 1636 | |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1637 | i = j = len-1; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 1638 | |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1639 | while (maxsplit-- > 0) { |
| 1640 | RSKIP_SPACE(s, i); |
| 1641 | if (i<0) break; |
| 1642 | j = i; i--; |
| 1643 | RSKIP_NONSPACE(s, i); |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1644 | if (j == len-1 && i < 0 && PyString_CheckExact(self)) { |
| 1645 | /* No whitespace in self, so just use it as list[0] */ |
| 1646 | Py_INCREF(self); |
| 1647 | PyList_SET_ITEM(list, 0, (PyObject *)self); |
| 1648 | count++; |
| 1649 | break; |
| 1650 | } |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1651 | SPLIT_ADD(s, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1652 | } |
Andrew Dalke | 02758d6 | 2006-05-26 15:21:01 +0000 | [diff] [blame] | 1653 | if (i >= 0) { |
| 1654 | /* Only occurs when maxsplit was reached */ |
| 1655 | /* Skip any remaining whitespace and copy to beginning of string */ |
| 1656 | RSKIP_SPACE(s, i); |
| 1657 | if (i >= 0) |
| 1658 | SPLIT_ADD(s, 0, i + 1); |
| 1659 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1660 | } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1661 | FIX_PREALLOC_SIZE(list); |
Fredrik Lundh | 554da41 | 2006-05-25 19:19:05 +0000 | [diff] [blame] | 1662 | if (PyList_Reverse(list) < 0) |
| 1663 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1664 | return list; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1665 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1666 | Py_DECREF(list); |
| 1667 | return NULL; |
| 1668 | } |
| 1669 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 1670 | Py_LOCAL_INLINE(PyObject *) |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1671 | rsplit_char(PyStringObject *self, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1672 | { |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1673 | const char *s = PyString_AS_STRING(self); |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1674 | register Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1675 | PyObject *str; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1676 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1677 | |
| 1678 | if (list == NULL) |
| 1679 | return NULL; |
| 1680 | |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1681 | i = j = len - 1; |
| 1682 | while ((i >= 0) && (maxcount-- > 0)) { |
| 1683 | for (; i >= 0; i--) { |
| 1684 | if (s[i] == ch) { |
| 1685 | SPLIT_ADD(s, i + 1, j + 1); |
| 1686 | j = i = i - 1; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1687 | break; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1688 | } |
| 1689 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1690 | } |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1691 | if (i < 0 && count == 0 && PyString_CheckExact(self)) { |
| 1692 | /* ch not in self, so just use self as list[0] */ |
| 1693 | Py_INCREF(self); |
| 1694 | PyList_SET_ITEM(list, 0, (PyObject *)self); |
| 1695 | count++; |
| 1696 | } |
| 1697 | else if (j >= -1) { |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1698 | SPLIT_ADD(s, 0, j + 1); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1699 | } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1700 | FIX_PREALLOC_SIZE(list); |
Fredrik Lundh | 554da41 | 2006-05-25 19:19:05 +0000 | [diff] [blame] | 1701 | if (PyList_Reverse(list) < 0) |
| 1702 | goto onError; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1703 | return list; |
| 1704 | |
| 1705 | onError: |
| 1706 | Py_DECREF(list); |
| 1707 | return NULL; |
| 1708 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1709 | |
| 1710 | PyDoc_STRVAR(rsplit__doc__, |
| 1711 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 1712 | \n\ |
| 1713 | Return a list of the words in the string S, using sep as the\n\ |
| 1714 | delimiter string, starting at the end of the string and working\n\ |
| 1715 | to the front. If maxsplit is given, at most maxsplit splits are\n\ |
| 1716 | done. If sep is not specified or is None, any whitespace string\n\ |
| 1717 | is a separator."); |
| 1718 | |
| 1719 | static PyObject * |
| 1720 | string_rsplit(PyStringObject *self, PyObject *args) |
| 1721 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1722 | Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1723 | Py_ssize_t maxsplit = -1, count=0; |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1724 | const char *s, *sub; |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1725 | PyObject *list, *str, *subobj = Py_None; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1726 | |
Martin v. Löwis | 9c83076 | 2006-04-13 08:37:17 +0000 | [diff] [blame] | 1727 | if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1728 | return NULL; |
| 1729 | if (maxsplit < 0) |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1730 | maxsplit = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1731 | if (subobj == Py_None) |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1732 | return rsplit_whitespace(self, len, maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1733 | if (PyString_Check(subobj)) { |
| 1734 | sub = PyString_AS_STRING(subobj); |
| 1735 | n = PyString_GET_SIZE(subobj); |
| 1736 | } |
| 1737 | #ifdef Py_USING_UNICODE |
| 1738 | else if (PyUnicode_Check(subobj)) |
| 1739 | return PyUnicode_RSplit((PyObject *)self, subobj, maxsplit); |
| 1740 | #endif |
| 1741 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 1742 | return NULL; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1743 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1744 | if (n == 0) { |
| 1745 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 1746 | return NULL; |
| 1747 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1748 | else if (n == 1) |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1749 | return rsplit_char(self, len, sub[0], maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1750 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1751 | list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1752 | if (list == NULL) |
| 1753 | return NULL; |
| 1754 | |
| 1755 | j = len; |
| 1756 | i = j - n; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1757 | |
Skip Montanaro | 2601549 | 2007-12-08 15:33:24 +0000 | [diff] [blame] | 1758 | s = PyString_AS_STRING(self); |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1759 | while ( (i >= 0) && (maxsplit-- > 0) ) { |
| 1760 | for (; i>=0; i--) { |
| 1761 | if (Py_STRING_MATCH(s, i, sub, n)) { |
| 1762 | SPLIT_ADD(s, i + n, j); |
| 1763 | j = i; |
| 1764 | i -= n; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1765 | break; |
Andrew Dalke | c5da53b | 2006-05-26 19:02:09 +0000 | [diff] [blame] | 1766 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1767 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1768 | } |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1769 | SPLIT_ADD(s, 0, j); |
| 1770 | FIX_PREALLOC_SIZE(list); |
| 1771 | if (PyList_Reverse(list) < 0) |
| 1772 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1773 | return list; |
| 1774 | |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 1775 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1776 | Py_DECREF(list); |
| 1777 | return NULL; |
| 1778 | } |
| 1779 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1780 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1781 | PyDoc_STRVAR(join__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1782 | "S.join(sequence) -> string\n\ |
| 1783 | \n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1784 | 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] | 1785 | sequence. The separator between elements is S."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1786 | |
| 1787 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1788 | string_join(PyStringObject *self, PyObject *orig) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1789 | { |
| 1790 | char *sep = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1791 | const Py_ssize_t seplen = PyString_GET_SIZE(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1792 | PyObject *res = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1793 | char *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1794 | Py_ssize_t seqlen = 0; |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1795 | size_t sz = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1796 | Py_ssize_t i; |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1797 | PyObject *seq, *item; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1798 | |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1799 | seq = PySequence_Fast(orig, ""); |
| 1800 | if (seq == NULL) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1801 | return NULL; |
| 1802 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1803 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1804 | seqlen = PySequence_Size(seq); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1805 | if (seqlen == 0) { |
| 1806 | Py_DECREF(seq); |
| 1807 | return PyString_FromString(""); |
| 1808 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1809 | if (seqlen == 1) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1810 | item = PySequence_Fast_GET_ITEM(seq, 0); |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1811 | if (PyString_CheckExact(item) || PyUnicode_CheckExact(item)) { |
| 1812 | Py_INCREF(item); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1813 | Py_DECREF(seq); |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1814 | return item; |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1815 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1816 | } |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1817 | |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1818 | /* 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] | 1819 | * of the builtin types in the sequence. |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1820 | * Do a pre-pass to figure out the total amount of space we'll |
| 1821 | * need (sz), see whether any argument is absurd, and defer to |
| 1822 | * the Unicode join if appropriate. |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1823 | */ |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1824 | for (i = 0; i < seqlen; i++) { |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1825 | const size_t old_sz = sz; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1826 | item = PySequence_Fast_GET_ITEM(seq, i); |
| 1827 | if (!PyString_Check(item)){ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1828 | #ifdef Py_USING_UNICODE |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1829 | if (PyUnicode_Check(item)) { |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 1830 | /* Defer to Unicode join. |
| 1831 | * CAUTION: There's no gurantee that the |
| 1832 | * original sequence can be iterated over |
| 1833 | * again, so we must pass seq here. |
| 1834 | */ |
| 1835 | PyObject *result; |
| 1836 | result = PyUnicode_Join((PyObject *)self, seq); |
Barry Warsaw | 771d067 | 2000-07-11 04:58:12 +0000 | [diff] [blame] | 1837 | Py_DECREF(seq); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 1838 | return result; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1839 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1840 | #endif |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1841 | PyErr_Format(PyExc_TypeError, |
Neal Norwitz | 0e2cbab | 2006-04-17 05:56:32 +0000 | [diff] [blame] | 1842 | "sequence item %zd: expected string," |
Jeremy Hylton | 88887aa | 2000-07-11 20:55:38 +0000 | [diff] [blame] | 1843 | " %.80s found", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 1844 | i, Py_TYPE(item)->tp_name); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1845 | Py_DECREF(seq); |
| 1846 | return NULL; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1847 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1848 | sz += PyString_GET_SIZE(item); |
| 1849 | if (i != 0) |
| 1850 | sz += seplen; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1851 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1852 | PyErr_SetString(PyExc_OverflowError, |
Georg Brandl | 90e27d3 | 2006-06-10 06:40:50 +0000 | [diff] [blame] | 1853 | "join() result is too long for a Python string"); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1854 | Py_DECREF(seq); |
| 1855 | return NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1856 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | /* Allocate result space. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1860 | res = PyString_FromStringAndSize((char*)NULL, sz); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1861 | if (res == NULL) { |
| 1862 | Py_DECREF(seq); |
| 1863 | return NULL; |
| 1864 | } |
| 1865 | |
| 1866 | /* Catenate everything. */ |
| 1867 | p = PyString_AS_STRING(res); |
| 1868 | for (i = 0; i < seqlen; ++i) { |
| 1869 | size_t n; |
| 1870 | item = PySequence_Fast_GET_ITEM(seq, i); |
| 1871 | n = PyString_GET_SIZE(item); |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 1872 | Py_MEMCPY(p, PyString_AS_STRING(item), n); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1873 | p += n; |
| 1874 | if (i < seqlen - 1) { |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 1875 | Py_MEMCPY(p, sep, seplen); |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1876 | p += seplen; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1877 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1878 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1879 | |
Jeremy Hylton | 4904829 | 2000-07-11 03:28:17 +0000 | [diff] [blame] | 1880 | Py_DECREF(seq); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1881 | return res; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Tim Peters | 52e155e | 2001-06-16 05:42:57 +0000 | [diff] [blame] | 1884 | PyObject * |
| 1885 | _PyString_Join(PyObject *sep, PyObject *x) |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1886 | { |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1887 | assert(sep != NULL && PyString_Check(sep)); |
| 1888 | assert(x != NULL); |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1889 | return string_join((PyStringObject *)sep, x); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 1892 | Py_LOCAL_INLINE(void) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1893 | 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] | 1894 | { |
| 1895 | if (*end > len) |
| 1896 | *end = len; |
| 1897 | else if (*end < 0) |
| 1898 | *end += len; |
| 1899 | if (*end < 0) |
| 1900 | *end = 0; |
| 1901 | if (*start < 0) |
| 1902 | *start += len; |
| 1903 | if (*start < 0) |
| 1904 | *start = 0; |
| 1905 | } |
| 1906 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 1907 | Py_LOCAL_INLINE(Py_ssize_t) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1908 | string_find_internal(PyStringObject *self, PyObject *args, int dir) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1909 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1910 | PyObject *subobj; |
Fredrik Lundh | 0b7ef46 | 2006-05-27 15:26:19 +0000 | [diff] [blame] | 1911 | const char *sub; |
| 1912 | Py_ssize_t sub_len; |
| 1913 | Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 1914 | PyObject *obj_start=Py_None, *obj_end=Py_None; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1915 | |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 1916 | if (!PyArg_ParseTuple(args, "O|OO:find/rfind/index/rindex", &subobj, |
| 1917 | &obj_start, &obj_end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1918 | return -2; |
Facundo Batista | 57d5669 | 2007-11-16 18:04:14 +0000 | [diff] [blame] | 1919 | /* To support None in "start" and "end" arguments, meaning |
| 1920 | the same as if they were not passed. |
| 1921 | */ |
| 1922 | if (obj_start != Py_None) |
| 1923 | if (!_PyEval_SliceIndex(obj_start, &start)) |
| 1924 | return -2; |
| 1925 | if (obj_end != Py_None) |
| 1926 | if (!_PyEval_SliceIndex(obj_end, &end)) |
| 1927 | return -2; |
| 1928 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1929 | if (PyString_Check(subobj)) { |
| 1930 | sub = PyString_AS_STRING(subobj); |
Fredrik Lundh | 0b7ef46 | 2006-05-27 15:26:19 +0000 | [diff] [blame] | 1931 | sub_len = PyString_GET_SIZE(subobj); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1932 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1933 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1934 | else if (PyUnicode_Check(subobj)) |
Fredrik Lundh | 0b7ef46 | 2006-05-27 15:26:19 +0000 | [diff] [blame] | 1935 | return PyUnicode_Find( |
| 1936 | (PyObject *)self, subobj, start, end, dir); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1937 | #endif |
Fredrik Lundh | 0b7ef46 | 2006-05-27 15:26:19 +0000 | [diff] [blame] | 1938 | else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len)) |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 1939 | /* XXX - the "expected a character buffer object" is pretty |
| 1940 | confusing for a non-expert. remap to something else ? */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1941 | return -2; |
| 1942 | |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 1943 | if (dir > 0) |
Fredrik Lundh | 0b7ef46 | 2006-05-27 15:26:19 +0000 | [diff] [blame] | 1944 | return stringlib_find_slice( |
| 1945 | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
| 1946 | sub, sub_len, start, end); |
Fredrik Lundh | e6e43c8 | 2006-05-26 19:48:07 +0000 | [diff] [blame] | 1947 | else |
Fredrik Lundh | 0b7ef46 | 2006-05-27 15:26:19 +0000 | [diff] [blame] | 1948 | return stringlib_rfind_slice( |
| 1949 | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
| 1950 | sub, sub_len, start, end); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1954 | PyDoc_STRVAR(find__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1955 | "S.find(sub [,start [,end]]) -> int\n\ |
| 1956 | \n\ |
| 1957 | Return the lowest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 1958 | such that sub is contained within s[start:end]. Optional\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1959 | arguments start and end are interpreted as in slice notation.\n\ |
| 1960 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1961 | Return -1 on failure."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1962 | |
| 1963 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1964 | string_find(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1965 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1966 | Py_ssize_t result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1967 | if (result == -2) |
| 1968 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1969 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1973 | PyDoc_STRVAR(index__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1974 | "S.index(sub [,start [,end]]) -> int\n\ |
| 1975 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1976 | Like S.find() but raise ValueError when the substring is not found."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1977 | |
| 1978 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1979 | string_index(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1980 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1981 | Py_ssize_t result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1982 | if (result == -2) |
| 1983 | return NULL; |
| 1984 | if (result == -1) { |
| 1985 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 5d5e7c0 | 2003-01-15 05:32:57 +0000 | [diff] [blame] | 1986 | "substring not found"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1987 | return NULL; |
| 1988 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1989 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1993 | PyDoc_STRVAR(rfind__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1994 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 1995 | \n\ |
| 1996 | Return the highest index in S where substring sub is found,\n\ |
Georg Brandl | 9efd9b6 | 2007-07-29 17:38:35 +0000 | [diff] [blame] | 1997 | such that sub is contained within s[start:end]. Optional\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1998 | arguments start and end are interpreted as in slice notation.\n\ |
| 1999 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2000 | Return -1 on failure."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2001 | |
| 2002 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2003 | string_rfind(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2004 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2005 | Py_ssize_t result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2006 | if (result == -2) |
| 2007 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2008 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2012 | PyDoc_STRVAR(rindex__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2013 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 2014 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2015 | Like S.rfind() but raise ValueError when the substring is not found."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2016 | |
| 2017 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2018 | string_rindex(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2019 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2020 | Py_ssize_t result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2021 | if (result == -2) |
| 2022 | return NULL; |
| 2023 | if (result == -1) { |
| 2024 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 5d5e7c0 | 2003-01-15 05:32:57 +0000 | [diff] [blame] | 2025 | "substring not found"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2026 | return NULL; |
| 2027 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2028 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2032 | Py_LOCAL_INLINE(PyObject *) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2033 | do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj) |
| 2034 | { |
| 2035 | char *s = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2036 | Py_ssize_t len = PyString_GET_SIZE(self); |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2037 | char *sep = PyString_AS_STRING(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2038 | Py_ssize_t seplen = PyString_GET_SIZE(sepobj); |
| 2039 | Py_ssize_t i, j; |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2040 | |
| 2041 | i = 0; |
| 2042 | if (striptype != RIGHTSTRIP) { |
| 2043 | while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) { |
| 2044 | i++; |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | j = len; |
| 2049 | if (striptype != LEFTSTRIP) { |
| 2050 | do { |
| 2051 | j--; |
| 2052 | } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen)); |
| 2053 | j++; |
| 2054 | } |
| 2055 | |
| 2056 | if (i == 0 && j == len && PyString_CheckExact(self)) { |
| 2057 | Py_INCREF(self); |
| 2058 | return (PyObject*)self; |
| 2059 | } |
| 2060 | else |
| 2061 | return PyString_FromStringAndSize(s+i, j-i); |
| 2062 | } |
| 2063 | |
| 2064 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2065 | Py_LOCAL_INLINE(PyObject *) |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2066 | do_strip(PyStringObject *self, int striptype) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2067 | { |
| 2068 | char *s = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2069 | Py_ssize_t len = PyString_GET_SIZE(self), i, j; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2070 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2071 | i = 0; |
| 2072 | if (striptype != RIGHTSTRIP) { |
| 2073 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
| 2074 | i++; |
| 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | j = len; |
| 2079 | if (striptype != LEFTSTRIP) { |
| 2080 | do { |
| 2081 | j--; |
| 2082 | } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
| 2083 | j++; |
| 2084 | } |
| 2085 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2086 | if (i == 0 && j == len && PyString_CheckExact(self)) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2087 | Py_INCREF(self); |
| 2088 | return (PyObject*)self; |
| 2089 | } |
| 2090 | else |
| 2091 | return PyString_FromStringAndSize(s+i, j-i); |
| 2092 | } |
| 2093 | |
| 2094 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2095 | Py_LOCAL_INLINE(PyObject *) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2096 | do_argstrip(PyStringObject *self, int striptype, PyObject *args) |
| 2097 | { |
| 2098 | PyObject *sep = NULL; |
| 2099 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 2100 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2101 | return NULL; |
| 2102 | |
| 2103 | if (sep != NULL && sep != Py_None) { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 2104 | if (PyString_Check(sep)) |
| 2105 | return do_xstrip(self, striptype, sep); |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2106 | #ifdef Py_USING_UNICODE |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 2107 | else if (PyUnicode_Check(sep)) { |
| 2108 | PyObject *uniself = PyUnicode_FromObject((PyObject *)self); |
| 2109 | PyObject *res; |
| 2110 | if (uniself==NULL) |
| 2111 | return NULL; |
| 2112 | res = _PyUnicode_XStrip((PyUnicodeObject *)uniself, |
| 2113 | striptype, sep); |
| 2114 | Py_DECREF(uniself); |
| 2115 | return res; |
| 2116 | } |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2117 | #endif |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2118 | PyErr_Format(PyExc_TypeError, |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2119 | #ifdef Py_USING_UNICODE |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2120 | "%s arg must be None, str or unicode", |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2121 | #else |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2122 | "%s arg must be None or str", |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2123 | #endif |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2124 | STRIPNAME(striptype)); |
| 2125 | return NULL; |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | return do_strip(self, striptype); |
| 2129 | } |
| 2130 | |
| 2131 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2132 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2133 | "S.strip([chars]) -> string or unicode\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2134 | \n\ |
| 2135 | 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] | 2136 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2137 | If chars is given and not None, remove characters in chars instead.\n\ |
| 2138 | If chars is unicode, S will be converted to unicode before stripping"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2139 | |
| 2140 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2141 | string_strip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2142 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2143 | if (PyTuple_GET_SIZE(args) == 0) |
| 2144 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 2145 | else |
| 2146 | return do_argstrip(self, BOTHSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2150 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2151 | "S.lstrip([chars]) -> string or unicode\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2152 | \n\ |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2153 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2154 | If chars is given and not None, remove characters in chars instead.\n\ |
| 2155 | If chars is unicode, S will be converted to unicode before stripping"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2156 | |
| 2157 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2158 | string_lstrip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2159 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2160 | if (PyTuple_GET_SIZE(args) == 0) |
| 2161 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 2162 | else |
| 2163 | return do_argstrip(self, LEFTSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2167 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2168 | "S.rstrip([chars]) -> string or unicode\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2169 | \n\ |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2170 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2171 | If chars is given and not None, remove characters in chars instead.\n\ |
| 2172 | If chars is unicode, S will be converted to unicode before stripping"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2173 | |
| 2174 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2175 | string_rstrip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2176 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2177 | if (PyTuple_GET_SIZE(args) == 0) |
| 2178 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 2179 | else |
| 2180 | return do_argstrip(self, RIGHTSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
| 2183 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2184 | PyDoc_STRVAR(lower__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2185 | "S.lower() -> string\n\ |
| 2186 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2187 | Return a copy of the string S converted to lowercase."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2188 | |
Fredrik Lundh | dfe503d | 2006-05-25 16:10:12 +0000 | [diff] [blame] | 2189 | /* _tolower and _toupper are defined by SUSv2, but they're not ISO C */ |
| 2190 | #ifndef _tolower |
| 2191 | #define _tolower tolower |
| 2192 | #endif |
| 2193 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2194 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2195 | string_lower(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2196 | { |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2197 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2198 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2199 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2200 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2201 | newobj = PyString_FromStringAndSize(NULL, n); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2202 | if (!newobj) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2203 | return NULL; |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2204 | |
| 2205 | s = PyString_AS_STRING(newobj); |
| 2206 | |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2207 | Py_MEMCPY(s, PyString_AS_STRING(self), n); |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2208 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2209 | for (i = 0; i < n; i++) { |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2210 | int c = Py_CHARMASK(s[i]); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2211 | if (isupper(c)) |
| 2212 | s[i] = _tolower(c); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2213 | } |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2214 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2215 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2218 | PyDoc_STRVAR(upper__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2219 | "S.upper() -> string\n\ |
| 2220 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2221 | Return a copy of the string S converted to uppercase."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2222 | |
Fredrik Lundh | dfe503d | 2006-05-25 16:10:12 +0000 | [diff] [blame] | 2223 | #ifndef _toupper |
| 2224 | #define _toupper toupper |
| 2225 | #endif |
| 2226 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2227 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2228 | string_upper(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2229 | { |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2230 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2231 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2232 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2233 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2234 | newobj = PyString_FromStringAndSize(NULL, n); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2235 | if (!newobj) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2236 | return NULL; |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2237 | |
| 2238 | s = PyString_AS_STRING(newobj); |
| 2239 | |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2240 | Py_MEMCPY(s, PyString_AS_STRING(self), n); |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2241 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2242 | for (i = 0; i < n; i++) { |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2243 | int c = Py_CHARMASK(s[i]); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2244 | if (islower(c)) |
| 2245 | s[i] = _toupper(c); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2246 | } |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2247 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2248 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2251 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2252 | "S.title() -> string\n\ |
| 2253 | \n\ |
| 2254 | 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] | 2255 | characters, all remaining cased characters have lowercase."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2256 | |
| 2257 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2258 | string_title(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2259 | { |
| 2260 | char *s = PyString_AS_STRING(self), *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2261 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2262 | int previous_is_cased = 0; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2263 | PyObject *newobj; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2264 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2265 | newobj = PyString_FromStringAndSize(NULL, n); |
| 2266 | if (newobj == NULL) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2267 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2268 | s_new = PyString_AsString(newobj); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2269 | for (i = 0; i < n; i++) { |
| 2270 | int c = Py_CHARMASK(*s++); |
| 2271 | if (islower(c)) { |
| 2272 | if (!previous_is_cased) |
| 2273 | c = toupper(c); |
| 2274 | previous_is_cased = 1; |
| 2275 | } else if (isupper(c)) { |
| 2276 | if (previous_is_cased) |
| 2277 | c = tolower(c); |
| 2278 | previous_is_cased = 1; |
| 2279 | } else |
| 2280 | previous_is_cased = 0; |
| 2281 | *s_new++ = c; |
| 2282 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2283 | return newobj; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2286 | PyDoc_STRVAR(capitalize__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2287 | "S.capitalize() -> string\n\ |
| 2288 | \n\ |
| 2289 | 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] | 2290 | capitalized."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2291 | |
| 2292 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2293 | string_capitalize(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2294 | { |
| 2295 | char *s = PyString_AS_STRING(self), *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2296 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2297 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2298 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2299 | newobj = PyString_FromStringAndSize(NULL, n); |
| 2300 | if (newobj == NULL) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2301 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2302 | s_new = PyString_AsString(newobj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2303 | if (0 < n) { |
| 2304 | int c = Py_CHARMASK(*s++); |
| 2305 | if (islower(c)) |
| 2306 | *s_new = toupper(c); |
| 2307 | else |
| 2308 | *s_new = c; |
| 2309 | s_new++; |
| 2310 | } |
| 2311 | for (i = 1; i < n; i++) { |
| 2312 | int c = Py_CHARMASK(*s++); |
| 2313 | if (isupper(c)) |
| 2314 | *s_new = tolower(c); |
| 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(count__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2324 | "S.count(sub[, start[, end]]) -> int\n\ |
| 2325 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 2326 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 2327 | string S[start:end]. Optional arguments start and end are interpreted\n\ |
| 2328 | as in slice notation."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2329 | |
| 2330 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2331 | string_count(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2332 | { |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2333 | PyObject *sub_obj; |
| 2334 | const char *str = PyString_AS_STRING(self), *sub; |
| 2335 | Py_ssize_t sub_len; |
| 2336 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2337 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2338 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj, |
| 2339 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2340 | return NULL; |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 2341 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2342 | if (PyString_Check(sub_obj)) { |
| 2343 | sub = PyString_AS_STRING(sub_obj); |
| 2344 | sub_len = PyString_GET_SIZE(sub_obj); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2345 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2346 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2347 | else if (PyUnicode_Check(sub_obj)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2348 | Py_ssize_t count; |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2349 | count = PyUnicode_Count((PyObject *)self, sub_obj, start, end); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2350 | if (count == -1) |
| 2351 | return NULL; |
| 2352 | else |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2353 | return PyInt_FromSsize_t(count); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2354 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2355 | #endif |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2356 | else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2357 | return NULL; |
| 2358 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2359 | string_adjust_indices(&start, &end, PyString_GET_SIZE(self)); |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 2360 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2361 | return PyInt_FromSsize_t( |
| 2362 | stringlib_count(str + start, end - start, sub, sub_len) |
| 2363 | ); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2366 | PyDoc_STRVAR(swapcase__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2367 | "S.swapcase() -> string\n\ |
| 2368 | \n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2369 | 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] | 2370 | converted to lowercase and vice versa."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2371 | |
| 2372 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2373 | string_swapcase(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2374 | { |
| 2375 | char *s = PyString_AS_STRING(self), *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2376 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2377 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2378 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2379 | newobj = PyString_FromStringAndSize(NULL, n); |
| 2380 | if (newobj == NULL) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2381 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2382 | s_new = PyString_AsString(newobj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2383 | for (i = 0; i < n; i++) { |
| 2384 | int c = Py_CHARMASK(*s++); |
| 2385 | if (islower(c)) { |
| 2386 | *s_new = toupper(c); |
| 2387 | } |
| 2388 | else if (isupper(c)) { |
| 2389 | *s_new = tolower(c); |
| 2390 | } |
| 2391 | else |
| 2392 | *s_new = c; |
| 2393 | s_new++; |
| 2394 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2395 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2399 | PyDoc_STRVAR(translate__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2400 | "S.translate(table [,deletechars]) -> string\n\ |
| 2401 | \n\ |
| 2402 | Return a copy of the string S, where all characters occurring\n\ |
| 2403 | in the optional argument deletechars are removed, and the\n\ |
| 2404 | remaining characters have been mapped through the given\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2405 | translation table, which must be a string of length 256."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2406 | |
| 2407 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2408 | string_translate(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2409 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2410 | register char *input, *output; |
Raymond Hettinger | 4db5fe9 | 2007-04-12 04:10:00 +0000 | [diff] [blame] | 2411 | const char *table; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2412 | register Py_ssize_t i, c, changed = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2413 | PyObject *input_obj = (PyObject*)self; |
Raymond Hettinger | 4db5fe9 | 2007-04-12 04:10:00 +0000 | [diff] [blame] | 2414 | const char *output_start, *del_table=NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2415 | Py_ssize_t inlen, tablen, dellen = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2416 | PyObject *result; |
| 2417 | int trans_table[256]; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2418 | PyObject *tableobj, *delobj = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2419 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 2420 | if (!PyArg_UnpackTuple(args, "translate", 1, 2, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2421 | &tableobj, &delobj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2422 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2423 | |
| 2424 | if (PyString_Check(tableobj)) { |
Raymond Hettinger | 4db5fe9 | 2007-04-12 04:10:00 +0000 | [diff] [blame] | 2425 | table = PyString_AS_STRING(tableobj); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2426 | tablen = PyString_GET_SIZE(tableobj); |
| 2427 | } |
Raymond Hettinger | 4db5fe9 | 2007-04-12 04:10:00 +0000 | [diff] [blame] | 2428 | else if (tableobj == Py_None) { |
| 2429 | table = NULL; |
| 2430 | tablen = 256; |
| 2431 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2432 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2433 | else if (PyUnicode_Check(tableobj)) { |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2434 | /* Unicode .translate() does not support the deletechars |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2435 | parameter; instead a mapping to None will cause characters |
| 2436 | to be deleted. */ |
| 2437 | if (delobj != NULL) { |
| 2438 | PyErr_SetString(PyExc_TypeError, |
| 2439 | "deletions are implemented differently for unicode"); |
| 2440 | return NULL; |
| 2441 | } |
| 2442 | return PyUnicode_Translate((PyObject *)self, tableobj, NULL); |
| 2443 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2444 | #endif |
Raymond Hettinger | 4db5fe9 | 2007-04-12 04:10:00 +0000 | [diff] [blame] | 2445 | else if (PyObject_AsCharBuffer(tableobj, &table, &tablen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2446 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2447 | |
Martin v. Löwis | 00b6127 | 2002-12-12 20:03:19 +0000 | [diff] [blame] | 2448 | if (tablen != 256) { |
| 2449 | PyErr_SetString(PyExc_ValueError, |
| 2450 | "translation table must be 256 characters long"); |
| 2451 | return NULL; |
| 2452 | } |
| 2453 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2454 | if (delobj != NULL) { |
| 2455 | if (PyString_Check(delobj)) { |
| 2456 | del_table = PyString_AS_STRING(delobj); |
| 2457 | dellen = PyString_GET_SIZE(delobj); |
| 2458 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2459 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2460 | else if (PyUnicode_Check(delobj)) { |
| 2461 | PyErr_SetString(PyExc_TypeError, |
| 2462 | "deletions are implemented differently for unicode"); |
| 2463 | return NULL; |
| 2464 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2465 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2466 | else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen)) |
| 2467 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2468 | } |
| 2469 | else { |
| 2470 | del_table = NULL; |
| 2471 | dellen = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
Neal Norwitz | 2aa9a5d | 2006-03-20 01:53:23 +0000 | [diff] [blame] | 2474 | inlen = PyString_GET_SIZE(input_obj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2475 | result = PyString_FromStringAndSize((char *)NULL, inlen); |
| 2476 | if (result == NULL) |
| 2477 | return NULL; |
| 2478 | output_start = output = PyString_AsString(result); |
Neal Norwitz | 2aa9a5d | 2006-03-20 01:53:23 +0000 | [diff] [blame] | 2479 | input = PyString_AS_STRING(input_obj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2480 | |
Raymond Hettinger | 4db5fe9 | 2007-04-12 04:10:00 +0000 | [diff] [blame] | 2481 | if (dellen == 0 && table != NULL) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2482 | /* If no deletions are required, use faster code */ |
| 2483 | for (i = inlen; --i >= 0; ) { |
| 2484 | c = Py_CHARMASK(*input++); |
| 2485 | if (Py_CHARMASK((*output++ = table[c])) != c) |
| 2486 | changed = 1; |
| 2487 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2488 | if (changed || !PyString_CheckExact(input_obj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2489 | return result; |
| 2490 | Py_DECREF(result); |
| 2491 | Py_INCREF(input_obj); |
| 2492 | return input_obj; |
| 2493 | } |
| 2494 | |
Raymond Hettinger | 4db5fe9 | 2007-04-12 04:10:00 +0000 | [diff] [blame] | 2495 | if (table == NULL) { |
| 2496 | for (i = 0; i < 256; i++) |
| 2497 | trans_table[i] = Py_CHARMASK(i); |
| 2498 | } else { |
| 2499 | for (i = 0; i < 256; i++) |
| 2500 | trans_table[i] = Py_CHARMASK(table[i]); |
| 2501 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2502 | |
| 2503 | for (i = 0; i < dellen; i++) |
| 2504 | trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
| 2505 | |
| 2506 | for (i = inlen; --i >= 0; ) { |
| 2507 | c = Py_CHARMASK(*input++); |
| 2508 | if (trans_table[c] != -1) |
| 2509 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 2510 | continue; |
| 2511 | changed = 1; |
| 2512 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2513 | if (!changed && PyString_CheckExact(input_obj)) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2514 | Py_DECREF(result); |
| 2515 | Py_INCREF(input_obj); |
| 2516 | return input_obj; |
| 2517 | } |
| 2518 | /* Fix the size of the resulting string */ |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2519 | if (inlen > 0) |
| 2520 | _PyString_Resize(&result, output - output_start); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2521 | return result; |
| 2522 | } |
| 2523 | |
| 2524 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2525 | #define FORWARD 1 |
| 2526 | #define REVERSE -1 |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2527 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2528 | /* find and count characters and substrings */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2529 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2530 | #define findchar(target, target_len, c) \ |
| 2531 | ((char *)memchr((const void *)(target), c, target_len)) |
| 2532 | |
| 2533 | /* String ops must return a string. */ |
| 2534 | /* If the object is subclass of string, create a copy */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2535 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2536 | return_self(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2537 | { |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2538 | if (PyString_CheckExact(self)) { |
| 2539 | Py_INCREF(self); |
| 2540 | return self; |
| 2541 | } |
| 2542 | return (PyStringObject *)PyString_FromStringAndSize( |
| 2543 | PyString_AS_STRING(self), |
| 2544 | PyString_GET_SIZE(self)); |
| 2545 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2546 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2547 | Py_LOCAL_INLINE(Py_ssize_t) |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2548 | countchar(const char *target, int target_len, char c, Py_ssize_t maxcount) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2549 | { |
| 2550 | Py_ssize_t count=0; |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2551 | const char *start=target; |
| 2552 | const char *end=target+target_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2553 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2554 | while ( (start=findchar(start, end-start, c)) != NULL ) { |
| 2555 | count++; |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2556 | if (count >= maxcount) |
| 2557 | break; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2558 | start += 1; |
| 2559 | } |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2560 | return count; |
| 2561 | } |
| 2562 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2563 | Py_LOCAL(Py_ssize_t) |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2564 | findstring(const char *target, Py_ssize_t target_len, |
| 2565 | const char *pattern, Py_ssize_t pattern_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2566 | Py_ssize_t start, |
| 2567 | Py_ssize_t end, |
| 2568 | int direction) |
| 2569 | { |
| 2570 | if (start < 0) { |
| 2571 | start += target_len; |
| 2572 | if (start < 0) |
| 2573 | start = 0; |
| 2574 | } |
| 2575 | if (end > target_len) { |
| 2576 | end = target_len; |
| 2577 | } else if (end < 0) { |
| 2578 | end += target_len; |
| 2579 | if (end < 0) |
| 2580 | end = 0; |
| 2581 | } |
| 2582 | |
| 2583 | /* zero-length substrings always match at the first attempt */ |
| 2584 | if (pattern_len == 0) |
| 2585 | return (direction > 0) ? start : end; |
| 2586 | |
| 2587 | end -= pattern_len; |
| 2588 | |
| 2589 | if (direction < 0) { |
| 2590 | for (; end >= start; end--) |
| 2591 | if (Py_STRING_MATCH(target, end, pattern, pattern_len)) |
| 2592 | return end; |
| 2593 | } else { |
| 2594 | for (; start <= end; start++) |
| 2595 | if (Py_STRING_MATCH(target, start, pattern, pattern_len)) |
| 2596 | return start; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2597 | } |
| 2598 | return -1; |
| 2599 | } |
| 2600 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 2601 | Py_LOCAL_INLINE(Py_ssize_t) |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2602 | countstring(const char *target, Py_ssize_t target_len, |
| 2603 | const char *pattern, Py_ssize_t pattern_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2604 | Py_ssize_t start, |
| 2605 | Py_ssize_t end, |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2606 | int direction, Py_ssize_t maxcount) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2607 | { |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2608 | Py_ssize_t count=0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2609 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2610 | if (start < 0) { |
| 2611 | start += target_len; |
| 2612 | if (start < 0) |
| 2613 | start = 0; |
| 2614 | } |
| 2615 | if (end > target_len) { |
| 2616 | end = target_len; |
| 2617 | } else if (end < 0) { |
| 2618 | end += target_len; |
| 2619 | if (end < 0) |
| 2620 | end = 0; |
| 2621 | } |
| 2622 | |
| 2623 | /* zero-length substrings match everywhere */ |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2624 | if (pattern_len == 0 || maxcount == 0) { |
| 2625 | if (target_len+1 < maxcount) |
| 2626 | return target_len+1; |
| 2627 | return maxcount; |
| 2628 | } |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2629 | |
| 2630 | end -= pattern_len; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2631 | if (direction < 0) { |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2632 | for (; (end >= start); end--) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2633 | if (Py_STRING_MATCH(target, end, pattern, pattern_len)) { |
| 2634 | count++; |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2635 | if (--maxcount <= 0) break; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2636 | end -= pattern_len-1; |
| 2637 | } |
| 2638 | } else { |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2639 | for (; (start <= end); start++) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2640 | if (Py_STRING_MATCH(target, start, pattern, pattern_len)) { |
| 2641 | count++; |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2642 | if (--maxcount <= 0) |
| 2643 | break; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2644 | start += pattern_len-1; |
| 2645 | } |
| 2646 | } |
| 2647 | return count; |
| 2648 | } |
| 2649 | |
| 2650 | |
Fredrik Lundh | 2d23d5b | 2006-05-27 10:05:10 +0000 | [diff] [blame] | 2651 | /* Algorithms for different cases of string replacement */ |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2652 | |
| 2653 | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2654 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2655 | replace_interleave(PyStringObject *self, |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2656 | const char *to_s, Py_ssize_t to_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2657 | Py_ssize_t maxcount) |
| 2658 | { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2659 | char *self_s, *result_s; |
| 2660 | Py_ssize_t self_len, result_len; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2661 | Py_ssize_t count, i, product; |
| 2662 | PyStringObject *result; |
| 2663 | |
| 2664 | self_len = PyString_GET_SIZE(self); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2665 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2666 | /* 1 at the end plus 1 after every character */ |
| 2667 | count = self_len+1; |
| 2668 | if (maxcount < count) |
| 2669 | count = maxcount; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2670 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2671 | /* Check for overflow */ |
| 2672 | /* result_len = count * to_len + self_len; */ |
| 2673 | product = count * to_len; |
| 2674 | if (product / to_len != count) { |
| 2675 | PyErr_SetString(PyExc_OverflowError, |
| 2676 | "replace string is too long"); |
| 2677 | return NULL; |
| 2678 | } |
| 2679 | result_len = product + self_len; |
| 2680 | if (result_len < 0) { |
| 2681 | PyErr_SetString(PyExc_OverflowError, |
| 2682 | "replace string is too long"); |
| 2683 | return NULL; |
| 2684 | } |
| 2685 | |
| 2686 | if (! (result = (PyStringObject *) |
| 2687 | PyString_FromStringAndSize(NULL, result_len)) ) |
| 2688 | return NULL; |
| 2689 | |
| 2690 | self_s = PyString_AS_STRING(self); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2691 | result_s = PyString_AS_STRING(result); |
| 2692 | |
| 2693 | /* TODO: special case single character, which doesn't need memcpy */ |
| 2694 | |
| 2695 | /* Lay the first one down (guaranteed this will occur) */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2696 | Py_MEMCPY(result_s, to_s, to_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2697 | result_s += to_len; |
| 2698 | count -= 1; |
| 2699 | |
| 2700 | for (i=0; i<count; i++) { |
| 2701 | *result_s++ = *self_s++; |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2702 | Py_MEMCPY(result_s, to_s, to_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2703 | result_s += to_len; |
| 2704 | } |
| 2705 | |
| 2706 | /* Copy the rest of the original string */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2707 | Py_MEMCPY(result_s, self_s, self_len-i); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2708 | |
| 2709 | return result; |
| 2710 | } |
| 2711 | |
| 2712 | /* Special case for deleting a single character */ |
| 2713 | /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2714 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2715 | replace_delete_single_character(PyStringObject *self, |
| 2716 | char from_c, Py_ssize_t maxcount) |
| 2717 | { |
| 2718 | char *self_s, *result_s; |
| 2719 | char *start, *next, *end; |
| 2720 | Py_ssize_t self_len, result_len; |
| 2721 | Py_ssize_t count; |
| 2722 | PyStringObject *result; |
| 2723 | |
| 2724 | self_len = PyString_GET_SIZE(self); |
| 2725 | self_s = PyString_AS_STRING(self); |
| 2726 | |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2727 | count = countchar(self_s, self_len, from_c, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2728 | if (count == 0) { |
| 2729 | return return_self(self); |
| 2730 | } |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2731 | |
| 2732 | result_len = self_len - count; /* from_len == 1 */ |
| 2733 | assert(result_len>=0); |
| 2734 | |
| 2735 | if ( (result = (PyStringObject *) |
| 2736 | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
| 2737 | return NULL; |
| 2738 | result_s = PyString_AS_STRING(result); |
| 2739 | |
| 2740 | start = self_s; |
| 2741 | end = self_s + self_len; |
| 2742 | while (count-- > 0) { |
| 2743 | next = findchar(start, end-start, from_c); |
| 2744 | if (next == NULL) |
| 2745 | break; |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2746 | Py_MEMCPY(result_s, start, next-start); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2747 | result_s += (next-start); |
| 2748 | start = next+1; |
| 2749 | } |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2750 | Py_MEMCPY(result_s, start, end-start); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2751 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2752 | return result; |
| 2753 | } |
| 2754 | |
| 2755 | /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
| 2756 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2757 | Py_LOCAL(PyStringObject *) |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2758 | replace_delete_substring(PyStringObject *self, |
| 2759 | const char *from_s, Py_ssize_t from_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2760 | Py_ssize_t maxcount) { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2761 | char *self_s, *result_s; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2762 | char *start, *next, *end; |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2763 | Py_ssize_t self_len, result_len; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2764 | Py_ssize_t count, offset; |
| 2765 | PyStringObject *result; |
| 2766 | |
| 2767 | self_len = PyString_GET_SIZE(self); |
| 2768 | self_s = PyString_AS_STRING(self); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2769 | |
| 2770 | count = countstring(self_s, self_len, |
| 2771 | from_s, from_len, |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2772 | 0, self_len, 1, |
| 2773 | maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2774 | |
| 2775 | if (count == 0) { |
| 2776 | /* no matches */ |
| 2777 | return return_self(self); |
| 2778 | } |
| 2779 | |
| 2780 | result_len = self_len - (count * from_len); |
| 2781 | assert (result_len>=0); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2782 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2783 | if ( (result = (PyStringObject *) |
| 2784 | PyString_FromStringAndSize(NULL, result_len)) == NULL ) |
| 2785 | return NULL; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2786 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2787 | result_s = PyString_AS_STRING(result); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2788 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2789 | start = self_s; |
| 2790 | end = self_s + self_len; |
| 2791 | while (count-- > 0) { |
| 2792 | offset = findstring(start, end-start, |
| 2793 | from_s, from_len, |
| 2794 | 0, end-start, FORWARD); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2795 | if (offset == -1) |
| 2796 | break; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2797 | next = start + offset; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2798 | |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2799 | Py_MEMCPY(result_s, start, next-start); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2800 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2801 | result_s += (next-start); |
| 2802 | start = next+from_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2803 | } |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2804 | Py_MEMCPY(result_s, start, end-start); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2805 | return result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2808 | /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2809 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2810 | replace_single_character_in_place(PyStringObject *self, |
| 2811 | char from_c, char to_c, |
| 2812 | Py_ssize_t maxcount) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2813 | { |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2814 | char *self_s, *result_s, *start, *end, *next; |
| 2815 | Py_ssize_t self_len; |
| 2816 | PyStringObject *result; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2817 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2818 | /* The result string will be the same size */ |
| 2819 | self_s = PyString_AS_STRING(self); |
| 2820 | self_len = PyString_GET_SIZE(self); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2821 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2822 | next = findchar(self_s, self_len, from_c); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2823 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2824 | if (next == NULL) { |
| 2825 | /* No matches; return the original string */ |
| 2826 | return return_self(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2827 | } |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2828 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2829 | /* Need to make a new string */ |
Andrew Dalke | 8c90910 | 2006-05-25 17:53:00 +0000 | [diff] [blame] | 2830 | result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2831 | if (result == NULL) |
| 2832 | return NULL; |
| 2833 | result_s = PyString_AS_STRING(result); |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2834 | Py_MEMCPY(result_s, self_s, self_len); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2835 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2836 | /* change everything in-place, starting with this one */ |
| 2837 | start = result_s + (next-self_s); |
| 2838 | *start = to_c; |
| 2839 | start++; |
| 2840 | end = result_s + self_len; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2841 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2842 | while (--maxcount > 0) { |
| 2843 | next = findchar(start, end-start, from_c); |
| 2844 | if (next == NULL) |
| 2845 | break; |
| 2846 | *next = to_c; |
| 2847 | start = next+1; |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 2848 | } |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2849 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2850 | return result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2853 | /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2854 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2855 | replace_substring_in_place(PyStringObject *self, |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2856 | const char *from_s, Py_ssize_t from_len, |
| 2857 | const char *to_s, Py_ssize_t to_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2858 | Py_ssize_t maxcount) |
| 2859 | { |
| 2860 | char *result_s, *start, *end; |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2861 | char *self_s; |
| 2862 | Py_ssize_t self_len, offset; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2863 | PyStringObject *result; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2864 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2865 | /* The result string will be the same size */ |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2866 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2867 | self_s = PyString_AS_STRING(self); |
| 2868 | self_len = PyString_GET_SIZE(self); |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2869 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2870 | offset = findstring(self_s, self_len, |
| 2871 | from_s, from_len, |
| 2872 | 0, self_len, FORWARD); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2873 | if (offset == -1) { |
| 2874 | /* No matches; return the original string */ |
| 2875 | return return_self(self); |
| 2876 | } |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2877 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2878 | /* Need to make a new string */ |
Andrew Dalke | 8c90910 | 2006-05-25 17:53:00 +0000 | [diff] [blame] | 2879 | result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2880 | if (result == NULL) |
| 2881 | return NULL; |
| 2882 | result_s = PyString_AS_STRING(result); |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2883 | Py_MEMCPY(result_s, self_s, self_len); |
Andrew Dalke | 8c90910 | 2006-05-25 17:53:00 +0000 | [diff] [blame] | 2884 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2885 | /* change everything in-place, starting with this one */ |
| 2886 | start = result_s + offset; |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2887 | Py_MEMCPY(start, to_s, from_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2888 | start += from_len; |
| 2889 | end = result_s + self_len; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2890 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2891 | while ( --maxcount > 0) { |
| 2892 | offset = findstring(start, end-start, |
| 2893 | from_s, from_len, |
| 2894 | 0, end-start, FORWARD); |
| 2895 | if (offset==-1) |
| 2896 | break; |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2897 | Py_MEMCPY(start+offset, to_s, from_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2898 | start += offset+from_len; |
| 2899 | } |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2900 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2901 | return result; |
| 2902 | } |
| 2903 | |
| 2904 | /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2905 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2906 | replace_single_character(PyStringObject *self, |
| 2907 | char from_c, |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2908 | const char *to_s, Py_ssize_t to_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2909 | Py_ssize_t maxcount) |
| 2910 | { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2911 | char *self_s, *result_s; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2912 | char *start, *next, *end; |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2913 | Py_ssize_t self_len, result_len; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2914 | Py_ssize_t count, product; |
| 2915 | PyStringObject *result; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2916 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2917 | self_s = PyString_AS_STRING(self); |
| 2918 | self_len = PyString_GET_SIZE(self); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2919 | |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2920 | count = countchar(self_s, self_len, from_c, 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 | } |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2925 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2926 | /* use the difference between current and new, hence the "-1" */ |
| 2927 | /* result_len = self_len + count * (to_len-1) */ |
| 2928 | product = count * (to_len-1); |
| 2929 | if (product / (to_len-1) != count) { |
| 2930 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 2931 | return NULL; |
| 2932 | } |
| 2933 | result_len = self_len + product; |
| 2934 | if (result_len < 0) { |
| 2935 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 2936 | return NULL; |
| 2937 | } |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2938 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2939 | if ( (result = (PyStringObject *) |
| 2940 | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
| 2941 | return NULL; |
| 2942 | result_s = PyString_AS_STRING(result); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2943 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2944 | start = self_s; |
| 2945 | end = self_s + self_len; |
| 2946 | while (count-- > 0) { |
| 2947 | next = findchar(start, end-start, from_c); |
| 2948 | if (next == NULL) |
| 2949 | break; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2950 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2951 | if (next == start) { |
| 2952 | /* replace with the 'to' */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2953 | Py_MEMCPY(result_s, to_s, to_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2954 | result_s += to_len; |
| 2955 | start += 1; |
| 2956 | } else { |
| 2957 | /* copy the unchanged old then the 'to' */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2958 | Py_MEMCPY(result_s, start, next-start); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2959 | result_s += (next-start); |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2960 | Py_MEMCPY(result_s, to_s, to_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2961 | result_s += to_len; |
| 2962 | start = next+1; |
| 2963 | } |
| 2964 | } |
| 2965 | /* Copy the remainder of the remaining string */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 2966 | Py_MEMCPY(result_s, start, end-start); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2967 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2968 | return result; |
| 2969 | } |
| 2970 | |
| 2971 | /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 2972 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2973 | replace_substring(PyStringObject *self, |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2974 | const char *from_s, Py_ssize_t from_len, |
| 2975 | const char *to_s, Py_ssize_t to_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2976 | Py_ssize_t maxcount) { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2977 | char *self_s, *result_s; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2978 | char *start, *next, *end; |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2979 | Py_ssize_t self_len, result_len; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2980 | Py_ssize_t count, offset, product; |
| 2981 | PyStringObject *result; |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 2982 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2983 | self_s = PyString_AS_STRING(self); |
| 2984 | self_len = PyString_GET_SIZE(self); |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2985 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2986 | count = countstring(self_s, self_len, |
| 2987 | from_s, from_len, |
Andrew Dalke | 5132407 | 2006-05-26 20:25:22 +0000 | [diff] [blame] | 2988 | 0, self_len, FORWARD, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2989 | if (count == 0) { |
| 2990 | /* no matches, return unchanged */ |
| 2991 | return return_self(self); |
| 2992 | } |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 2993 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2994 | /* Check for overflow */ |
| 2995 | /* result_len = self_len + count * (to_len-from_len) */ |
| 2996 | product = count * (to_len-from_len); |
| 2997 | if (product / (to_len-from_len) != count) { |
| 2998 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 2999 | return NULL; |
| 3000 | } |
| 3001 | result_len = self_len + product; |
| 3002 | if (result_len < 0) { |
| 3003 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 3004 | return NULL; |
| 3005 | } |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 3006 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3007 | if ( (result = (PyStringObject *) |
| 3008 | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
| 3009 | return NULL; |
| 3010 | result_s = PyString_AS_STRING(result); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 3011 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3012 | start = self_s; |
| 3013 | end = self_s + self_len; |
| 3014 | while (count-- > 0) { |
| 3015 | offset = findstring(start, end-start, |
| 3016 | from_s, from_len, |
| 3017 | 0, end-start, FORWARD); |
| 3018 | if (offset == -1) |
| 3019 | break; |
| 3020 | next = start+offset; |
| 3021 | if (next == start) { |
| 3022 | /* replace with the 'to' */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 3023 | Py_MEMCPY(result_s, to_s, to_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3024 | result_s += to_len; |
| 3025 | start += from_len; |
| 3026 | } else { |
| 3027 | /* copy the unchanged old then the 'to' */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 3028 | Py_MEMCPY(result_s, start, next-start); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3029 | result_s += (next-start); |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 3030 | Py_MEMCPY(result_s, to_s, to_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3031 | result_s += to_len; |
| 3032 | start = next+from_len; |
| 3033 | } |
| 3034 | } |
| 3035 | /* Copy the remainder of the remaining string */ |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 3036 | Py_MEMCPY(result_s, start, end-start); |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 3037 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3038 | return result; |
| 3039 | } |
| 3040 | |
| 3041 | |
Fredrik Lundh | 7c940d1 | 2006-05-26 16:32:42 +0000 | [diff] [blame] | 3042 | Py_LOCAL(PyStringObject *) |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3043 | replace(PyStringObject *self, |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3044 | const char *from_s, Py_ssize_t from_len, |
| 3045 | const char *to_s, Py_ssize_t to_len, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3046 | Py_ssize_t maxcount) |
| 3047 | { |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3048 | if (maxcount < 0) { |
| 3049 | maxcount = PY_SSIZE_T_MAX; |
| 3050 | } else if (maxcount == 0 || PyString_GET_SIZE(self) == 0) { |
| 3051 | /* nothing to do; return the original string */ |
| 3052 | return return_self(self); |
| 3053 | } |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3054 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3055 | if (maxcount == 0 || |
| 3056 | (from_len == 0 && to_len == 0)) { |
| 3057 | /* nothing to do; return the original string */ |
| 3058 | return return_self(self); |
| 3059 | } |
| 3060 | |
| 3061 | /* Handle zero-length special cases */ |
Neal Norwitz | a7edb11 | 2006-07-30 06:59:13 +0000 | [diff] [blame] | 3062 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3063 | if (from_len == 0) { |
| 3064 | /* insert the 'to' string everywhere. */ |
| 3065 | /* >>> "Python".replace("", ".") */ |
| 3066 | /* '.P.y.t.h.o.n.' */ |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3067 | return replace_interleave(self, to_s, to_len, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
| 3070 | /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
| 3071 | /* point for an empty self string to generate a non-empty string */ |
| 3072 | /* Special case so the remaining code always gets a non-empty string */ |
| 3073 | if (PyString_GET_SIZE(self) == 0) { |
| 3074 | return return_self(self); |
| 3075 | } |
| 3076 | |
| 3077 | if (to_len == 0) { |
| 3078 | /* delete all occurances of 'from' string */ |
| 3079 | if (from_len == 1) { |
| 3080 | return replace_delete_single_character( |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3081 | self, from_s[0], maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3082 | } else { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3083 | return replace_delete_substring(self, from_s, from_len, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3084 | } |
| 3085 | } |
| 3086 | |
| 3087 | /* Handle special case where both strings have the same length */ |
| 3088 | |
| 3089 | if (from_len == to_len) { |
| 3090 | if (from_len == 1) { |
| 3091 | return replace_single_character_in_place( |
| 3092 | self, |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3093 | from_s[0], |
| 3094 | to_s[0], |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3095 | maxcount); |
| 3096 | } else { |
| 3097 | return replace_substring_in_place( |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3098 | self, from_s, from_len, to_s, to_len, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | /* Otherwise use the more generic algorithms */ |
| 3103 | if (from_len == 1) { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3104 | return replace_single_character(self, from_s[0], |
| 3105 | to_s, to_len, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3106 | } else { |
| 3107 | /* len('from')>=2, len('to')>=1 */ |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3108 | return replace_substring(self, from_s, from_len, to_s, to_len, maxcount); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3109 | } |
| 3110 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3111 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3112 | PyDoc_STRVAR(replace__doc__, |
Fred Drake | d22bb65 | 2003-10-22 02:56:40 +0000 | [diff] [blame] | 3113 | "S.replace (old, new[, count]) -> string\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3114 | \n\ |
| 3115 | Return a copy of string S with all occurrences of substring\n\ |
Fred Drake | d22bb65 | 2003-10-22 02:56:40 +0000 | [diff] [blame] | 3116 | old replaced by new. If the optional argument count is\n\ |
| 3117 | given, only the first count occurrences are replaced."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3118 | |
| 3119 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3120 | string_replace(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3121 | { |
Thomas Wouters | dc5f808 | 2006-04-19 15:38:01 +0000 | [diff] [blame] | 3122 | Py_ssize_t count = -1; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3123 | PyObject *from, *to; |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3124 | const char *from_s, *to_s; |
| 3125 | Py_ssize_t from_len, to_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3126 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3127 | if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3128 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3129 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3130 | if (PyString_Check(from)) { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3131 | from_s = PyString_AS_STRING(from); |
| 3132 | from_len = PyString_GET_SIZE(from); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3133 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3134 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3135 | if (PyUnicode_Check(from)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3136 | return PyUnicode_Replace((PyObject *)self, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3137 | from, to, count); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3138 | #endif |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3139 | else if (PyObject_AsCharBuffer(from, &from_s, &from_len)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3140 | return NULL; |
| 3141 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3142 | if (PyString_Check(to)) { |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3143 | to_s = PyString_AS_STRING(to); |
| 3144 | to_len = PyString_GET_SIZE(to); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3145 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3146 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3147 | else if (PyUnicode_Check(to)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3148 | return PyUnicode_Replace((PyObject *)self, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3149 | from, to, count); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3150 | #endif |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3151 | else if (PyObject_AsCharBuffer(to, &to_s, &to_len)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3152 | return NULL; |
| 3153 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3154 | return (PyObject *)replace((PyStringObject *) self, |
Neal Norwitz | f71ec5a | 2006-07-30 06:57:04 +0000 | [diff] [blame] | 3155 | from_s, from_len, |
| 3156 | to_s, to_len, count); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3157 | } |
| 3158 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3159 | /** End DALKE **/ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3160 | |
Neal Norwitz | 8e6675a | 2006-06-11 05:47:14 +0000 | [diff] [blame] | 3161 | /* Matches the end (direction >= 0) or start (direction < 0) of self |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3162 | * against substr, using the start and end arguments. Returns |
| 3163 | * -1 on error, 0 if not found and 1 if found. |
| 3164 | */ |
| 3165 | Py_LOCAL(int) |
| 3166 | _string_tailmatch(PyStringObject *self, PyObject *substr, Py_ssize_t start, |
| 3167 | Py_ssize_t end, int direction) |
| 3168 | { |
| 3169 | Py_ssize_t len = PyString_GET_SIZE(self); |
| 3170 | Py_ssize_t slen; |
| 3171 | const char* sub; |
| 3172 | const char* str; |
| 3173 | |
| 3174 | if (PyString_Check(substr)) { |
| 3175 | sub = PyString_AS_STRING(substr); |
| 3176 | slen = PyString_GET_SIZE(substr); |
| 3177 | } |
| 3178 | #ifdef Py_USING_UNICODE |
| 3179 | else if (PyUnicode_Check(substr)) |
| 3180 | return PyUnicode_Tailmatch((PyObject *)self, |
| 3181 | substr, start, end, direction); |
| 3182 | #endif |
| 3183 | else if (PyObject_AsCharBuffer(substr, &sub, &slen)) |
| 3184 | return -1; |
| 3185 | str = PyString_AS_STRING(self); |
| 3186 | |
| 3187 | string_adjust_indices(&start, &end, len); |
| 3188 | |
| 3189 | if (direction < 0) { |
| 3190 | /* startswith */ |
| 3191 | if (start+slen > len) |
| 3192 | return 0; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3193 | } else { |
| 3194 | /* endswith */ |
| 3195 | if (end-start < slen || start > len) |
| 3196 | return 0; |
| 3197 | |
| 3198 | if (end-slen > start) |
| 3199 | start = end - slen; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3200 | } |
Neal Norwitz | 8e6675a | 2006-06-11 05:47:14 +0000 | [diff] [blame] | 3201 | if (end-start >= slen) |
| 3202 | return ! memcmp(str+start, sub, slen); |
| 3203 | return 0; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
| 3206 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3207 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3208 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3209 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 3210 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 3211 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3212 | With optional end, stop comparing S at that position.\n\ |
| 3213 | prefix can also be a tuple of strings to try."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3214 | |
| 3215 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3216 | string_startswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3217 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3218 | Py_ssize_t start = 0; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 3219 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3220 | PyObject *subobj; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3221 | int result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3222 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 3223 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 3224 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3225 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3226 | if (PyTuple_Check(subobj)) { |
| 3227 | Py_ssize_t i; |
| 3228 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 3229 | result = _string_tailmatch(self, |
| 3230 | PyTuple_GET_ITEM(subobj, i), |
| 3231 | start, end, -1); |
| 3232 | if (result == -1) |
| 3233 | return NULL; |
| 3234 | else if (result) { |
| 3235 | Py_RETURN_TRUE; |
| 3236 | } |
| 3237 | } |
| 3238 | Py_RETURN_FALSE; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3239 | } |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3240 | result = _string_tailmatch(self, subobj, start, end, -1); |
| 3241 | if (result == -1) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3242 | return NULL; |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 3243 | else |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3244 | return PyBool_FromLong(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3248 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3249 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3250 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 3251 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 3252 | With optional start, test S beginning at that position.\n\ |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3253 | With optional end, stop comparing S at that position.\n\ |
| 3254 | suffix can also be a tuple of strings to try."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3255 | |
| 3256 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3257 | string_endswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3258 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3259 | Py_ssize_t start = 0; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 3260 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3261 | PyObject *subobj; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3262 | int result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3263 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 3264 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 3265 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3266 | return NULL; |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3267 | if (PyTuple_Check(subobj)) { |
| 3268 | Py_ssize_t i; |
| 3269 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 3270 | result = _string_tailmatch(self, |
| 3271 | PyTuple_GET_ITEM(subobj, i), |
| 3272 | start, end, +1); |
| 3273 | if (result == -1) |
| 3274 | return NULL; |
| 3275 | else if (result) { |
| 3276 | Py_RETURN_TRUE; |
| 3277 | } |
| 3278 | } |
| 3279 | Py_RETURN_FALSE; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3280 | } |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3281 | result = _string_tailmatch(self, subobj, start, end, +1); |
| 3282 | if (result == -1) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3283 | return NULL; |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 3284 | else |
Georg Brandl | 2425081 | 2006-06-09 18:45:48 +0000 | [diff] [blame] | 3285 | return PyBool_FromLong(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3289 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3290 | "S.encode([encoding[,errors]]) -> object\n\ |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3291 | \n\ |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3292 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 3293 | 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] | 3294 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3295 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 3296 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 3297 | codecs.register_error that is able to handle UnicodeEncodeErrors."); |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3298 | |
| 3299 | static PyObject * |
| 3300 | string_encode(PyStringObject *self, PyObject *args) |
| 3301 | { |
| 3302 | char *encoding = NULL; |
| 3303 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3304 | PyObject *v; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 3305 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3306 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 3307 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3308 | v = PyString_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3309 | if (v == NULL) |
| 3310 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3311 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 3312 | PyErr_Format(PyExc_TypeError, |
| 3313 | "encoder did not return a string/unicode object " |
| 3314 | "(type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3315 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3316 | Py_DECREF(v); |
| 3317 | return NULL; |
| 3318 | } |
| 3319 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3320 | |
| 3321 | onError: |
| 3322 | return NULL; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3326 | PyDoc_STRVAR(decode__doc__, |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3327 | "S.decode([encoding[,errors]]) -> object\n\ |
| 3328 | \n\ |
| 3329 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 3330 | to the default encoding. errors may be given to set a different error\n\ |
| 3331 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3332 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 3333 | as well as any other name registerd with codecs.register_error that is\n\ |
| 3334 | able to handle UnicodeDecodeErrors."); |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3335 | |
| 3336 | static PyObject * |
| 3337 | string_decode(PyStringObject *self, PyObject *args) |
| 3338 | { |
| 3339 | char *encoding = NULL; |
| 3340 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3341 | PyObject *v; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 3342 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3343 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 3344 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3345 | v = PyString_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3346 | if (v == NULL) |
| 3347 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3348 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 3349 | PyErr_Format(PyExc_TypeError, |
| 3350 | "decoder did not return a string/unicode object " |
| 3351 | "(type=%.400s)", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3352 | Py_TYPE(v)->tp_name); |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3353 | Py_DECREF(v); |
| 3354 | return NULL; |
| 3355 | } |
| 3356 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3357 | |
| 3358 | onError: |
| 3359 | return NULL; |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
| 3362 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3363 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3364 | "S.expandtabs([tabsize]) -> string\n\ |
| 3365 | \n\ |
| 3366 | 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] | 3367 | 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] | 3368 | |
| 3369 | static PyObject* |
| 3370 | string_expandtabs(PyStringObject *self, PyObject *args) |
| 3371 | { |
| 3372 | const char *e, *p; |
| 3373 | char *q; |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 3374 | Py_ssize_t i, j, old_j; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3375 | PyObject *u; |
| 3376 | int tabsize = 8; |
| 3377 | |
| 3378 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 3379 | return NULL; |
| 3380 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 3381 | /* First pass: determine size of output string */ |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 3382 | i = j = old_j = 0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3383 | e = PyString_AS_STRING(self) + PyString_GET_SIZE(self); |
| 3384 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 3385 | if (*p == '\t') { |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 3386 | if (tabsize > 0) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3387 | j += tabsize - (j % tabsize); |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 3388 | if (old_j > j) { |
Neal Norwitz | 5c9a81a | 2007-06-11 02:16:10 +0000 | [diff] [blame] | 3389 | PyErr_SetString(PyExc_OverflowError, |
| 3390 | "new string is too long"); |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 3391 | return NULL; |
| 3392 | } |
| 3393 | old_j = j; |
| 3394 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3395 | } |
| 3396 | else { |
| 3397 | j++; |
| 3398 | if (*p == '\n' || *p == '\r') { |
| 3399 | i += j; |
Neal Norwitz | 5c9a81a | 2007-06-11 02:16:10 +0000 | [diff] [blame] | 3400 | old_j = j = 0; |
| 3401 | if (i < 0) { |
| 3402 | PyErr_SetString(PyExc_OverflowError, |
| 3403 | "new string is too long"); |
| 3404 | return NULL; |
| 3405 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3406 | } |
| 3407 | } |
| 3408 | |
Neal Norwitz | 7dbd2a3 | 2007-06-09 03:36:34 +0000 | [diff] [blame] | 3409 | if ((i + j) < 0) { |
| 3410 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
| 3411 | return NULL; |
| 3412 | } |
| 3413 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3414 | /* Second pass: create output string and fill it */ |
| 3415 | u = PyString_FromStringAndSize(NULL, i + j); |
| 3416 | if (!u) |
| 3417 | return NULL; |
| 3418 | |
| 3419 | j = 0; |
| 3420 | q = PyString_AS_STRING(u); |
| 3421 | |
| 3422 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 3423 | if (*p == '\t') { |
| 3424 | if (tabsize > 0) { |
| 3425 | i = tabsize - (j % tabsize); |
| 3426 | j += i; |
| 3427 | while (i--) |
| 3428 | *q++ = ' '; |
| 3429 | } |
| 3430 | } |
| 3431 | else { |
| 3432 | j++; |
| 3433 | *q++ = *p; |
| 3434 | if (*p == '\n' || *p == '\r') |
| 3435 | j = 0; |
| 3436 | } |
| 3437 | |
| 3438 | return u; |
| 3439 | } |
| 3440 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 3441 | Py_LOCAL_INLINE(PyObject *) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3442 | 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] | 3443 | { |
| 3444 | PyObject *u; |
| 3445 | |
| 3446 | if (left < 0) |
| 3447 | left = 0; |
| 3448 | if (right < 0) |
| 3449 | right = 0; |
| 3450 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3451 | if (left == 0 && right == 0 && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3452 | Py_INCREF(self); |
| 3453 | return (PyObject *)self; |
| 3454 | } |
| 3455 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3456 | u = PyString_FromStringAndSize(NULL, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3457 | left + PyString_GET_SIZE(self) + right); |
| 3458 | if (u) { |
| 3459 | if (left) |
| 3460 | memset(PyString_AS_STRING(u), fill, left); |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 3461 | Py_MEMCPY(PyString_AS_STRING(u) + left, |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3462 | PyString_AS_STRING(self), |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3463 | PyString_GET_SIZE(self)); |
| 3464 | if (right) |
| 3465 | memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self), |
| 3466 | fill, right); |
| 3467 | } |
| 3468 | |
| 3469 | return u; |
| 3470 | } |
| 3471 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3472 | PyDoc_STRVAR(ljust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3473 | "S.ljust(width[, fillchar]) -> string\n" |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3474 | "\n" |
| 3475 | "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] | 3476 | "done using the specified fill character (default is a space)."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3477 | |
| 3478 | static PyObject * |
| 3479 | string_ljust(PyStringObject *self, PyObject *args) |
| 3480 | { |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3481 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3482 | char fillchar = ' '; |
| 3483 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3484 | if (!PyArg_ParseTuple(args, "n|c:ljust", &width, &fillchar)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3485 | return NULL; |
| 3486 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3487 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3488 | Py_INCREF(self); |
| 3489 | return (PyObject*) self; |
| 3490 | } |
| 3491 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3492 | return pad(self, 0, width - PyString_GET_SIZE(self), fillchar); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
| 3495 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3496 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3497 | "S.rjust(width[, fillchar]) -> string\n" |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3498 | "\n" |
| 3499 | "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] | 3500 | "done using the specified fill character (default is a space)"); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3501 | |
| 3502 | static PyObject * |
| 3503 | string_rjust(PyStringObject *self, PyObject *args) |
| 3504 | { |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3505 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3506 | char fillchar = ' '; |
| 3507 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3508 | if (!PyArg_ParseTuple(args, "n|c:rjust", &width, &fillchar)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3509 | return NULL; |
| 3510 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3511 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3512 | Py_INCREF(self); |
| 3513 | return (PyObject*) self; |
| 3514 | } |
| 3515 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3516 | return pad(self, width - PyString_GET_SIZE(self), 0, fillchar); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3517 | } |
| 3518 | |
| 3519 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3520 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3521 | "S.center(width[, fillchar]) -> string\n" |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3522 | "\n" |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3523 | "Return S centered in a string of length width. Padding is\n" |
| 3524 | "done using the specified fill character (default is a space)"); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3525 | |
| 3526 | static PyObject * |
| 3527 | string_center(PyStringObject *self, PyObject *args) |
| 3528 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3529 | Py_ssize_t marg, left; |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3530 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3531 | char fillchar = ' '; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3532 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3533 | if (!PyArg_ParseTuple(args, "n|c:center", &width, &fillchar)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3534 | return NULL; |
| 3535 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3536 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3537 | Py_INCREF(self); |
| 3538 | return (PyObject*) self; |
| 3539 | } |
| 3540 | |
| 3541 | marg = width - PyString_GET_SIZE(self); |
| 3542 | left = marg / 2 + (marg & width & 1); |
| 3543 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3544 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3545 | } |
| 3546 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3547 | PyDoc_STRVAR(zfill__doc__, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3548 | "S.zfill(width) -> string\n" |
| 3549 | "\n" |
| 3550 | "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] | 3551 | "of the specified width. The string S is never truncated."); |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3552 | |
| 3553 | static PyObject * |
| 3554 | string_zfill(PyStringObject *self, PyObject *args) |
| 3555 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3556 | Py_ssize_t fill; |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3557 | PyObject *s; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3558 | char *p; |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3559 | Py_ssize_t width; |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3560 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3561 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3562 | return NULL; |
| 3563 | |
| 3564 | if (PyString_GET_SIZE(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 3565 | if (PyString_CheckExact(self)) { |
| 3566 | Py_INCREF(self); |
| 3567 | return (PyObject*) self; |
| 3568 | } |
| 3569 | else |
| 3570 | return PyString_FromStringAndSize( |
| 3571 | PyString_AS_STRING(self), |
| 3572 | PyString_GET_SIZE(self) |
| 3573 | ); |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3574 | } |
| 3575 | |
| 3576 | fill = width - PyString_GET_SIZE(self); |
| 3577 | |
| 3578 | s = pad(self, fill, 0, '0'); |
| 3579 | |
| 3580 | if (s == NULL) |
| 3581 | return NULL; |
| 3582 | |
| 3583 | p = PyString_AS_STRING(s); |
| 3584 | if (p[fill] == '+' || p[fill] == '-') { |
| 3585 | /* move sign to beginning of string */ |
| 3586 | p[0] = p[fill]; |
| 3587 | p[fill] = '0'; |
| 3588 | } |
| 3589 | |
| 3590 | return (PyObject*) s; |
| 3591 | } |
| 3592 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3593 | PyDoc_STRVAR(isspace__doc__, |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3594 | "S.isspace() -> bool\n\ |
| 3595 | \n\ |
| 3596 | Return True if all characters in S are whitespace\n\ |
| 3597 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3598 | |
| 3599 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3600 | string_isspace(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3601 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3602 | register const unsigned char *p |
| 3603 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3604 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3605 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3606 | /* Shortcut for single character strings */ |
| 3607 | if (PyString_GET_SIZE(self) == 1 && |
| 3608 | isspace(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3609 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3610 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3611 | /* Special case for empty strings */ |
| 3612 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3613 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3614 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3615 | e = p + PyString_GET_SIZE(self); |
| 3616 | for (; p < e; p++) { |
| 3617 | if (!isspace(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3618 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3619 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3620 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3621 | } |
| 3622 | |
| 3623 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3624 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3625 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3626 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3627 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3628 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3629 | |
| 3630 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3631 | string_isalpha(PyStringObject *self) |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3632 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3633 | register const unsigned char *p |
| 3634 | = (unsigned char *) PyString_AS_STRING(self); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3635 | register const unsigned char *e; |
| 3636 | |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3637 | /* Shortcut for single character strings */ |
| 3638 | if (PyString_GET_SIZE(self) == 1 && |
| 3639 | isalpha(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3640 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3641 | |
| 3642 | /* Special case for empty strings */ |
| 3643 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3644 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3645 | |
| 3646 | e = p + PyString_GET_SIZE(self); |
| 3647 | for (; p < e; p++) { |
| 3648 | if (!isalpha(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3649 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3650 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3651 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3655 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3656 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3657 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3658 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3659 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3660 | |
| 3661 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3662 | string_isalnum(PyStringObject *self) |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3663 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3664 | register const unsigned char *p |
| 3665 | = (unsigned char *) PyString_AS_STRING(self); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3666 | register const unsigned char *e; |
| 3667 | |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3668 | /* Shortcut for single character strings */ |
| 3669 | if (PyString_GET_SIZE(self) == 1 && |
| 3670 | isalnum(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3671 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3672 | |
| 3673 | /* Special case for empty strings */ |
| 3674 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3675 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3676 | |
| 3677 | e = p + PyString_GET_SIZE(self); |
| 3678 | for (; p < e; p++) { |
| 3679 | if (!isalnum(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3680 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3681 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3682 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3683 | } |
| 3684 | |
| 3685 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3686 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3687 | "S.isdigit() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3688 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3689 | Return True if all characters in S are digits\n\ |
| 3690 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3691 | |
| 3692 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3693 | string_isdigit(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3694 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3695 | register const unsigned char *p |
| 3696 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3697 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3698 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3699 | /* Shortcut for single character strings */ |
| 3700 | if (PyString_GET_SIZE(self) == 1 && |
| 3701 | isdigit(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3702 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3703 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3704 | /* Special case for empty strings */ |
| 3705 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3706 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3707 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3708 | e = p + PyString_GET_SIZE(self); |
| 3709 | for (; p < e; p++) { |
| 3710 | if (!isdigit(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3711 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3712 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3713 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3714 | } |
| 3715 | |
| 3716 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3717 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3718 | "S.islower() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3719 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3720 | 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] | 3721 | at least one cased character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3722 | |
| 3723 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3724 | string_islower(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3725 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3726 | register const unsigned char *p |
| 3727 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3728 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3729 | int cased; |
| 3730 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3731 | /* Shortcut for single character strings */ |
| 3732 | if (PyString_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3733 | return PyBool_FromLong(islower(*p) != 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3734 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3735 | /* Special case for empty strings */ |
| 3736 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3737 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3738 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3739 | e = p + PyString_GET_SIZE(self); |
| 3740 | cased = 0; |
| 3741 | for (; p < e; p++) { |
| 3742 | if (isupper(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3743 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3744 | else if (!cased && islower(*p)) |
| 3745 | cased = 1; |
| 3746 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3747 | return PyBool_FromLong(cased); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3748 | } |
| 3749 | |
| 3750 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3751 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3752 | "S.isupper() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3753 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3754 | 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] | 3755 | at least one cased character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3756 | |
| 3757 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3758 | string_isupper(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3759 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3760 | register const unsigned char *p |
| 3761 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3762 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3763 | int cased; |
| 3764 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3765 | /* Shortcut for single character strings */ |
| 3766 | if (PyString_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3767 | return PyBool_FromLong(isupper(*p) != 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3768 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3769 | /* Special case for empty strings */ |
| 3770 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3771 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3772 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3773 | e = p + PyString_GET_SIZE(self); |
| 3774 | cased = 0; |
| 3775 | for (; p < e; p++) { |
| 3776 | if (islower(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3777 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3778 | else if (!cased && isupper(*p)) |
| 3779 | cased = 1; |
| 3780 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3781 | return PyBool_FromLong(cased); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
| 3784 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3785 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3786 | "S.istitle() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3787 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3788 | Return True if S is a titlecased string and there is at least one\n\ |
| 3789 | character in S, i.e. uppercase characters may only follow uncased\n\ |
| 3790 | characters and lowercase characters only cased ones. Return False\n\ |
| 3791 | otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3792 | |
| 3793 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3794 | string_istitle(PyStringObject *self, PyObject *uncased) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3795 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3796 | register const unsigned char *p |
| 3797 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3798 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3799 | int cased, previous_is_cased; |
| 3800 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3801 | /* Shortcut for single character strings */ |
| 3802 | if (PyString_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3803 | return PyBool_FromLong(isupper(*p) != 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3804 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3805 | /* Special case for empty strings */ |
| 3806 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3807 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3808 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3809 | e = p + PyString_GET_SIZE(self); |
| 3810 | cased = 0; |
| 3811 | previous_is_cased = 0; |
| 3812 | for (; p < e; p++) { |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3813 | register const unsigned char ch = *p; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3814 | |
| 3815 | if (isupper(ch)) { |
| 3816 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3817 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3818 | previous_is_cased = 1; |
| 3819 | cased = 1; |
| 3820 | } |
| 3821 | else if (islower(ch)) { |
| 3822 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3823 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3824 | previous_is_cased = 1; |
| 3825 | cased = 1; |
| 3826 | } |
| 3827 | else |
| 3828 | previous_is_cased = 0; |
| 3829 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3830 | return PyBool_FromLong(cased); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3831 | } |
| 3832 | |
| 3833 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3834 | PyDoc_STRVAR(splitlines__doc__, |
Fred Drake | 2bae4fa | 2001-10-13 15:57:55 +0000 | [diff] [blame] | 3835 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3836 | \n\ |
| 3837 | 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] | 3838 | 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] | 3839 | is given and true."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3840 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3841 | static PyObject* |
| 3842 | string_splitlines(PyStringObject *self, PyObject *args) |
| 3843 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3844 | register Py_ssize_t i; |
| 3845 | register Py_ssize_t j; |
| 3846 | Py_ssize_t len; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3847 | int keepends = 0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3848 | PyObject *list; |
| 3849 | PyObject *str; |
| 3850 | char *data; |
| 3851 | |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3852 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3853 | return NULL; |
| 3854 | |
| 3855 | data = PyString_AS_STRING(self); |
| 3856 | len = PyString_GET_SIZE(self); |
| 3857 | |
Andrew Dalke | 7e0a62e | 2006-05-26 22:49:03 +0000 | [diff] [blame] | 3858 | /* This does not use the preallocated list because splitlines is |
| 3859 | usually run with hundreds of newlines. The overhead of |
| 3860 | switching between PyList_SET_ITEM and append causes about a |
| 3861 | 2-3% slowdown for that common case. A smarter implementation |
| 3862 | could move the if check out, so the SET_ITEMs are done first |
| 3863 | and the appends only done when the prealloc buffer is full. |
| 3864 | That's too much work for little gain.*/ |
| 3865 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3866 | list = PyList_New(0); |
| 3867 | if (!list) |
| 3868 | goto onError; |
| 3869 | |
| 3870 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3871 | Py_ssize_t eol; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3872 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3873 | /* Find a line and append it */ |
| 3874 | while (i < len && data[i] != '\n' && data[i] != '\r') |
| 3875 | i++; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3876 | |
| 3877 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3878 | eol = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3879 | if (i < len) { |
| 3880 | if (data[i] == '\r' && i + 1 < len && |
| 3881 | data[i+1] == '\n') |
| 3882 | i += 2; |
| 3883 | else |
| 3884 | i++; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3885 | if (keepends) |
| 3886 | eol = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3887 | } |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3888 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3889 | j = i; |
| 3890 | } |
| 3891 | if (j < len) { |
| 3892 | SPLIT_APPEND(data, j, len); |
| 3893 | } |
| 3894 | |
| 3895 | return list; |
| 3896 | |
| 3897 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3898 | Py_XDECREF(list); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3899 | return NULL; |
| 3900 | } |
| 3901 | |
| 3902 | #undef SPLIT_APPEND |
Andrew Dalke | 525eab3 | 2006-05-26 14:00:45 +0000 | [diff] [blame] | 3903 | #undef SPLIT_ADD |
| 3904 | #undef MAX_PREALLOC |
| 3905 | #undef PREALLOC_SIZE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3906 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3907 | static PyObject * |
| 3908 | string_getnewargs(PyStringObject *v) |
| 3909 | { |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 3910 | return Py_BuildValue("(s#)", v->ob_sval, Py_SIZE(v)); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3911 | } |
| 3912 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3913 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3914 | static PyMethodDef |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3915 | string_methods[] = { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3916 | /* Counterparts of the obsolete stropmodule functions; except |
| 3917 | string.maketrans(). */ |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3918 | {"join", (PyCFunction)string_join, METH_O, join__doc__}, |
| 3919 | {"split", (PyCFunction)string_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 3920 | {"rsplit", (PyCFunction)string_rsplit, METH_VARARGS, rsplit__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3921 | {"lower", (PyCFunction)string_lower, METH_NOARGS, lower__doc__}, |
| 3922 | {"upper", (PyCFunction)string_upper, METH_NOARGS, upper__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3923 | {"islower", (PyCFunction)string_islower, METH_NOARGS, islower__doc__}, |
| 3924 | {"isupper", (PyCFunction)string_isupper, METH_NOARGS, isupper__doc__}, |
| 3925 | {"isspace", (PyCFunction)string_isspace, METH_NOARGS, isspace__doc__}, |
| 3926 | {"isdigit", (PyCFunction)string_isdigit, METH_NOARGS, isdigit__doc__}, |
| 3927 | {"istitle", (PyCFunction)string_istitle, METH_NOARGS, istitle__doc__}, |
| 3928 | {"isalpha", (PyCFunction)string_isalpha, METH_NOARGS, isalpha__doc__}, |
| 3929 | {"isalnum", (PyCFunction)string_isalnum, METH_NOARGS, isalnum__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3930 | {"capitalize", (PyCFunction)string_capitalize, METH_NOARGS, |
| 3931 | capitalize__doc__}, |
| 3932 | {"count", (PyCFunction)string_count, METH_VARARGS, count__doc__}, |
| 3933 | {"endswith", (PyCFunction)string_endswith, METH_VARARGS, |
| 3934 | endswith__doc__}, |
Fredrik Lundh | 450277f | 2006-05-26 09:46:59 +0000 | [diff] [blame] | 3935 | {"partition", (PyCFunction)string_partition, METH_O, partition__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3936 | {"find", (PyCFunction)string_find, METH_VARARGS, find__doc__}, |
| 3937 | {"index", (PyCFunction)string_index, METH_VARARGS, index__doc__}, |
| 3938 | {"lstrip", (PyCFunction)string_lstrip, METH_VARARGS, lstrip__doc__}, |
| 3939 | {"replace", (PyCFunction)string_replace, METH_VARARGS, replace__doc__}, |
| 3940 | {"rfind", (PyCFunction)string_rfind, METH_VARARGS, rfind__doc__}, |
| 3941 | {"rindex", (PyCFunction)string_rindex, METH_VARARGS, rindex__doc__}, |
| 3942 | {"rstrip", (PyCFunction)string_rstrip, METH_VARARGS, rstrip__doc__}, |
Fredrik Lundh | b3167cb | 2006-05-26 18:15:38 +0000 | [diff] [blame] | 3943 | {"rpartition", (PyCFunction)string_rpartition, METH_O, |
| 3944 | rpartition__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3945 | {"startswith", (PyCFunction)string_startswith, METH_VARARGS, |
| 3946 | startswith__doc__}, |
| 3947 | {"strip", (PyCFunction)string_strip, METH_VARARGS, strip__doc__}, |
| 3948 | {"swapcase", (PyCFunction)string_swapcase, METH_NOARGS, |
| 3949 | swapcase__doc__}, |
| 3950 | {"translate", (PyCFunction)string_translate, METH_VARARGS, |
| 3951 | translate__doc__}, |
| 3952 | {"title", (PyCFunction)string_title, METH_NOARGS, title__doc__}, |
| 3953 | {"ljust", (PyCFunction)string_ljust, METH_VARARGS, ljust__doc__}, |
| 3954 | {"rjust", (PyCFunction)string_rjust, METH_VARARGS, rjust__doc__}, |
| 3955 | {"center", (PyCFunction)string_center, METH_VARARGS, center__doc__}, |
| 3956 | {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__}, |
| 3957 | {"encode", (PyCFunction)string_encode, METH_VARARGS, encode__doc__}, |
| 3958 | {"decode", (PyCFunction)string_decode, METH_VARARGS, decode__doc__}, |
| 3959 | {"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, |
| 3960 | expandtabs__doc__}, |
| 3961 | {"splitlines", (PyCFunction)string_splitlines, METH_VARARGS, |
| 3962 | splitlines__doc__}, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3963 | {"__getnewargs__", (PyCFunction)string_getnewargs, METH_NOARGS}, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3964 | {NULL, NULL} /* sentinel */ |
| 3965 | }; |
| 3966 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 3967 | static PyObject * |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3968 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 3969 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3970 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3971 | string_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3972 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3973 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 3974 | static char *kwlist[] = {"object", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3975 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3976 | if (type != &PyString_Type) |
| 3977 | return str_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3978 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x)) |
| 3979 | return NULL; |
| 3980 | if (x == NULL) |
| 3981 | return PyString_FromString(""); |
| 3982 | return PyObject_Str(x); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3983 | } |
| 3984 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3985 | static PyObject * |
| 3986 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3987 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3988 | PyObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3989 | Py_ssize_t n; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3990 | |
| 3991 | assert(PyType_IsSubtype(type, &PyString_Type)); |
| 3992 | tmp = string_new(&PyString_Type, args, kwds); |
| 3993 | if (tmp == NULL) |
| 3994 | return NULL; |
Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 3995 | assert(PyString_CheckExact(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3996 | n = PyString_GET_SIZE(tmp); |
| 3997 | pnew = type->tp_alloc(type, n); |
| 3998 | if (pnew != NULL) { |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 3999 | Py_MEMCPY(PyString_AS_STRING(pnew), PyString_AS_STRING(tmp), n+1); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 4000 | ((PyStringObject *)pnew)->ob_shash = |
| 4001 | ((PyStringObject *)tmp)->ob_shash; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4002 | ((PyStringObject *)pnew)->ob_sstate = SSTATE_NOT_INTERNED; |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 4003 | } |
Guido van Rossum | 29d55a3 | 2001-08-31 16:11:15 +0000 | [diff] [blame] | 4004 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 4005 | return pnew; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 4006 | } |
| 4007 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 4008 | static PyObject * |
| 4009 | basestring_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4010 | { |
| 4011 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 32a7e7f | 2002-05-31 19:58:02 +0000 | [diff] [blame] | 4012 | "The basestring type cannot be instantiated"); |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 4013 | return NULL; |
| 4014 | } |
| 4015 | |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 4016 | static PyObject * |
| 4017 | string_mod(PyObject *v, PyObject *w) |
| 4018 | { |
| 4019 | if (!PyString_Check(v)) { |
| 4020 | Py_INCREF(Py_NotImplemented); |
| 4021 | return Py_NotImplemented; |
| 4022 | } |
| 4023 | return PyString_Format(v, w); |
| 4024 | } |
| 4025 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4026 | PyDoc_STRVAR(basestring_doc, |
| 4027 | "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] | 4028 | |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 4029 | static PyNumberMethods string_as_number = { |
| 4030 | 0, /*nb_add*/ |
| 4031 | 0, /*nb_subtract*/ |
| 4032 | 0, /*nb_multiply*/ |
| 4033 | 0, /*nb_divide*/ |
| 4034 | string_mod, /*nb_remainder*/ |
| 4035 | }; |
| 4036 | |
| 4037 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 4038 | PyTypeObject PyBaseString_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4039 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Neal Norwitz | 32a7e7f | 2002-05-31 19:58:02 +0000 | [diff] [blame] | 4040 | "basestring", |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 4041 | 0, |
| 4042 | 0, |
| 4043 | 0, /* tp_dealloc */ |
| 4044 | 0, /* tp_print */ |
| 4045 | 0, /* tp_getattr */ |
| 4046 | 0, /* tp_setattr */ |
| 4047 | 0, /* tp_compare */ |
| 4048 | 0, /* tp_repr */ |
| 4049 | 0, /* tp_as_number */ |
| 4050 | 0, /* tp_as_sequence */ |
| 4051 | 0, /* tp_as_mapping */ |
| 4052 | 0, /* tp_hash */ |
| 4053 | 0, /* tp_call */ |
| 4054 | 0, /* tp_str */ |
| 4055 | 0, /* tp_getattro */ |
| 4056 | 0, /* tp_setattro */ |
| 4057 | 0, /* tp_as_buffer */ |
| 4058 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4059 | basestring_doc, /* tp_doc */ |
| 4060 | 0, /* tp_traverse */ |
| 4061 | 0, /* tp_clear */ |
| 4062 | 0, /* tp_richcompare */ |
| 4063 | 0, /* tp_weaklistoffset */ |
| 4064 | 0, /* tp_iter */ |
| 4065 | 0, /* tp_iternext */ |
| 4066 | 0, /* tp_methods */ |
| 4067 | 0, /* tp_members */ |
| 4068 | 0, /* tp_getset */ |
| 4069 | &PyBaseObject_Type, /* tp_base */ |
| 4070 | 0, /* tp_dict */ |
| 4071 | 0, /* tp_descr_get */ |
| 4072 | 0, /* tp_descr_set */ |
| 4073 | 0, /* tp_dictoffset */ |
| 4074 | 0, /* tp_init */ |
| 4075 | 0, /* tp_alloc */ |
| 4076 | basestring_new, /* tp_new */ |
| 4077 | 0, /* tp_free */ |
| 4078 | }; |
| 4079 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4080 | PyDoc_STRVAR(string_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4081 | "str(object) -> string\n\ |
| 4082 | \n\ |
| 4083 | Return a nice string representation of the object.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4084 | 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] | 4085 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4086 | PyTypeObject PyString_Type = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 4087 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4088 | "str", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4089 | sizeof(PyStringObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4090 | sizeof(char), |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 4091 | string_dealloc, /* tp_dealloc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4092 | (printfunc)string_print, /* tp_print */ |
| 4093 | 0, /* tp_getattr */ |
| 4094 | 0, /* tp_setattr */ |
| 4095 | 0, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 4096 | string_repr, /* tp_repr */ |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 4097 | &string_as_number, /* tp_as_number */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4098 | &string_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 4099 | &string_as_mapping, /* tp_as_mapping */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4100 | (hashfunc)string_hash, /* tp_hash */ |
| 4101 | 0, /* tp_call */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 4102 | string_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4103 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4104 | 0, /* tp_setattro */ |
| 4105 | &string_as_buffer, /* tp_as_buffer */ |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 4106 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
Neal Norwitz | ee3a1b5 | 2007-02-25 19:44:48 +0000 | [diff] [blame] | 4107 | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_STRING_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4108 | string_doc, /* tp_doc */ |
| 4109 | 0, /* tp_traverse */ |
| 4110 | 0, /* tp_clear */ |
| 4111 | (richcmpfunc)string_richcompare, /* tp_richcompare */ |
| 4112 | 0, /* tp_weaklistoffset */ |
| 4113 | 0, /* tp_iter */ |
| 4114 | 0, /* tp_iternext */ |
| 4115 | string_methods, /* tp_methods */ |
| 4116 | 0, /* tp_members */ |
| 4117 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 4118 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4119 | 0, /* tp_dict */ |
| 4120 | 0, /* tp_descr_get */ |
| 4121 | 0, /* tp_descr_set */ |
| 4122 | 0, /* tp_dictoffset */ |
| 4123 | 0, /* tp_init */ |
| 4124 | 0, /* tp_alloc */ |
| 4125 | string_new, /* tp_new */ |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 4126 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4127 | }; |
| 4128 | |
| 4129 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4130 | PyString_Concat(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4131 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4132 | register PyObject *v; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4133 | if (*pv == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4134 | return; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4135 | if (w == NULL || !PyString_Check(*pv)) { |
| 4136 | Py_DECREF(*pv); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4137 | *pv = NULL; |
| 4138 | return; |
| 4139 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4140 | v = string_concat((PyStringObject *) *pv, w); |
| 4141 | Py_DECREF(*pv); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4142 | *pv = v; |
| 4143 | } |
| 4144 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4145 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4146 | PyString_ConcatAndDel(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4147 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4148 | PyString_Concat(pv, w); |
| 4149 | Py_XDECREF(w); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4150 | } |
| 4151 | |
| 4152 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4153 | /* The following function breaks the notion that strings are immutable: |
| 4154 | it changes the size of a string. We get away with this only if there |
| 4155 | is only one module referencing the object. You can also think of it |
| 4156 | as creating a new string object and destroying the old one, only |
| 4157 | 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] | 4158 | already be known to some other part of the code... |
| 4159 | Note that if there's not enough memory to resize the string, the original |
| 4160 | string object at *pv is deallocated, *pv is set to NULL, an "out of |
| 4161 | memory" exception is set, and -1 is returned. Else (on success) 0 is |
| 4162 | returned, and the value in *pv may or may not be the same as on input. |
| 4163 | As always, an extra byte is allocated for a trailing \0 byte (newsize |
| 4164 | does *not* include that), and a trailing \0 byte is stored. |
| 4165 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4166 | |
| 4167 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4168 | _PyString_Resize(PyObject **pv, Py_ssize_t newsize) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4169 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4170 | register PyObject *v; |
| 4171 | register PyStringObject *sv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 4172 | v = *pv; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4173 | if (!PyString_Check(v) || Py_REFCNT(v) != 1 || newsize < 0 || |
Armin Rigo | 618fbf5 | 2004-08-07 20:58:32 +0000 | [diff] [blame] | 4174 | PyString_CHECK_INTERNED(v)) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4175 | *pv = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4176 | Py_DECREF(v); |
| 4177 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 4178 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4179 | } |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 4180 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 4181 | _Py_DEC_REFTOTAL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4182 | _Py_ForgetReference(v); |
| 4183 | *pv = (PyObject *) |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 4184 | PyObject_REALLOC((char *)v, sizeof(PyStringObject) + newsize); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4185 | if (*pv == NULL) { |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 4186 | PyObject_Del(v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4187 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 4188 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4189 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4190 | _Py_NewReference(*pv); |
| 4191 | sv = (PyStringObject *) *pv; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4192 | Py_SIZE(sv) = newsize; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 4193 | sv->ob_sval[newsize] = '\0'; |
Raymond Hettinger | 561fbf1 | 2004-10-26 01:52:37 +0000 | [diff] [blame] | 4194 | sv->ob_shash = -1; /* invalidate cached hash value */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4195 | return 0; |
| 4196 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4197 | |
| 4198 | /* Helpers for formatstring */ |
| 4199 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 4200 | Py_LOCAL_INLINE(PyObject *) |
Thomas Wouters | 977485d | 2006-02-16 15:59:12 +0000 | [diff] [blame] | 4201 | 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] | 4202 | { |
Thomas Wouters | 977485d | 2006-02-16 15:59:12 +0000 | [diff] [blame] | 4203 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4204 | if (argidx < arglen) { |
| 4205 | (*p_argidx)++; |
| 4206 | if (arglen < 0) |
| 4207 | return args; |
| 4208 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4209 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4210 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4211 | PyErr_SetString(PyExc_TypeError, |
| 4212 | "not enough arguments for format string"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4213 | return NULL; |
| 4214 | } |
| 4215 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4216 | /* Format codes |
| 4217 | * F_LJUST '-' |
| 4218 | * F_SIGN '+' |
| 4219 | * F_BLANK ' ' |
| 4220 | * F_ALT '#' |
| 4221 | * F_ZERO '0' |
| 4222 | */ |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4223 | #define F_LJUST (1<<0) |
| 4224 | #define F_SIGN (1<<1) |
| 4225 | #define F_BLANK (1<<2) |
| 4226 | #define F_ALT (1<<3) |
| 4227 | #define F_ZERO (1<<4) |
| 4228 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 4229 | Py_LOCAL_INLINE(int) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4230 | formatfloat(char *buf, size_t buflen, int flags, |
| 4231 | int prec, int type, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4232 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4233 | /* fmt = '%#.' + `prec` + `type` |
| 4234 | 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] | 4235 | char fmt[20]; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4236 | double x; |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4237 | x = PyFloat_AsDouble(v); |
| 4238 | if (x == -1.0 && PyErr_Occurred()) { |
Georg Brandl | 283a135 | 2006-11-19 08:48:30 +0000 | [diff] [blame] | 4239 | PyErr_Format(PyExc_TypeError, "float argument required, " |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4240 | "not %.200s", Py_TYPE(v)->tp_name); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4241 | return -1; |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4242 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4243 | if (prec < 0) |
| 4244 | prec = 6; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4245 | if (type == 'f' && fabs(x)/1e25 >= 1e25) |
| 4246 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4247 | /* Worst case length calc to ensure no buffer overrun: |
| 4248 | |
| 4249 | 'g' formats: |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4250 | fmt = %#.<prec>g |
| 4251 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4252 | for any double rep.) |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4253 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4254 | |
| 4255 | 'f' formats: |
| 4256 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 4257 | len = 1 + 50 + 1 + prec = 52 + prec |
| 4258 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4259 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 4260 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4261 | |
| 4262 | */ |
Georg Brandl | 7c3b50d | 2007-07-12 08:38:00 +0000 | [diff] [blame] | 4263 | if (((type == 'g' || type == 'G') && |
| 4264 | buflen <= (size_t)10 + (size_t)prec) || |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4265 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4266 | PyErr_SetString(PyExc_OverflowError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4267 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4268 | return -1; |
| 4269 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4270 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 4271 | (flags&F_ALT) ? "#" : "", |
| 4272 | prec, type); |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 4273 | PyOS_ascii_formatd(buf, buflen, fmt, x); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4274 | return (int)strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4277 | /* _PyString_FormatLong emulates the format codes d, u, o, x and X, and |
| 4278 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 4279 | * Python's regular ints. |
| 4280 | * Return value: a new PyString*, or NULL if error. |
| 4281 | * . *pbuf is set to point into it, |
| 4282 | * *plen set to the # of chars following that. |
| 4283 | * Caller must decref it when done using pbuf. |
| 4284 | * The string starting at *pbuf is of the form |
| 4285 | * "-"? ("0x" | "0X")? digit+ |
| 4286 | * "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] | 4287 | * set in flags. The case of hex digits will be correct, |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4288 | * There will be at least prec digits, zero-filled on the left if |
| 4289 | * necessary to get that many. |
| 4290 | * val object to be converted |
| 4291 | * flags bitmask of format flags; only F_ALT is looked at |
| 4292 | * prec minimum number of digits; 0-fill on left if needed |
| 4293 | * type a character in [duoxX]; u acts the same as d |
| 4294 | * |
| 4295 | * CAUTION: o, x and X conversions on regular ints can never |
| 4296 | * produce a '-' sign, but can for Python's unbounded ints. |
| 4297 | */ |
| 4298 | PyObject* |
| 4299 | _PyString_FormatLong(PyObject *val, int flags, int prec, int type, |
| 4300 | char **pbuf, int *plen) |
| 4301 | { |
| 4302 | PyObject *result = NULL; |
| 4303 | char *buf; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4304 | Py_ssize_t i; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4305 | int sign; /* 1 if '-', else 0 */ |
| 4306 | int len; /* number of characters */ |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4307 | Py_ssize_t llen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4308 | int numdigits; /* len == numnondigits + numdigits */ |
| 4309 | int numnondigits = 0; |
| 4310 | |
| 4311 | switch (type) { |
| 4312 | case 'd': |
| 4313 | case 'u': |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4314 | result = Py_TYPE(val)->tp_str(val); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4315 | break; |
| 4316 | case 'o': |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4317 | result = Py_TYPE(val)->tp_as_number->nb_oct(val); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4318 | break; |
| 4319 | case 'x': |
| 4320 | case 'X': |
| 4321 | numnondigits = 2; |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4322 | result = Py_TYPE(val)->tp_as_number->nb_hex(val); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4323 | break; |
| 4324 | default: |
| 4325 | assert(!"'type' not in [duoxX]"); |
| 4326 | } |
| 4327 | if (!result) |
| 4328 | return NULL; |
| 4329 | |
Neal Norwitz | 56423e5 | 2006-08-13 18:11:08 +0000 | [diff] [blame] | 4330 | buf = PyString_AsString(result); |
Georg Brandl | 26a07b5 | 2006-08-14 20:25:39 +0000 | [diff] [blame] | 4331 | if (!buf) { |
| 4332 | Py_DECREF(result); |
Neal Norwitz | 56423e5 | 2006-08-13 18:11:08 +0000 | [diff] [blame] | 4333 | return NULL; |
Georg Brandl | 26a07b5 | 2006-08-14 20:25:39 +0000 | [diff] [blame] | 4334 | } |
Neal Norwitz | 56423e5 | 2006-08-13 18:11:08 +0000 | [diff] [blame] | 4335 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4336 | /* To modify the string in-place, there can only be one reference. */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4337 | if (Py_REFCNT(result) != 1) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4338 | PyErr_BadInternalCall(); |
| 4339 | return NULL; |
| 4340 | } |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4341 | llen = PyString_Size(result); |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 4342 | if (llen > INT_MAX) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4343 | PyErr_SetString(PyExc_ValueError, "string too large in _PyString_FormatLong"); |
| 4344 | return NULL; |
| 4345 | } |
| 4346 | len = (int)llen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4347 | if (buf[len-1] == 'L') { |
| 4348 | --len; |
| 4349 | buf[len] = '\0'; |
| 4350 | } |
| 4351 | sign = buf[0] == '-'; |
| 4352 | numnondigits += sign; |
| 4353 | numdigits = len - numnondigits; |
| 4354 | assert(numdigits > 0); |
| 4355 | |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4356 | /* Get rid of base marker unless F_ALT */ |
| 4357 | if ((flags & F_ALT) == 0) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4358 | /* Need to skip 0x, 0X or 0. */ |
| 4359 | int skipped = 0; |
| 4360 | switch (type) { |
| 4361 | case 'o': |
| 4362 | assert(buf[sign] == '0'); |
| 4363 | /* If 0 is only digit, leave it alone. */ |
| 4364 | if (numdigits > 1) { |
| 4365 | skipped = 1; |
| 4366 | --numdigits; |
| 4367 | } |
| 4368 | break; |
| 4369 | case 'x': |
| 4370 | case 'X': |
| 4371 | assert(buf[sign] == '0'); |
| 4372 | assert(buf[sign + 1] == 'x'); |
| 4373 | skipped = 2; |
| 4374 | numnondigits -= 2; |
| 4375 | break; |
| 4376 | } |
| 4377 | if (skipped) { |
| 4378 | buf += skipped; |
| 4379 | len -= skipped; |
| 4380 | if (sign) |
| 4381 | buf[0] = '-'; |
| 4382 | } |
| 4383 | assert(len == numnondigits + numdigits); |
| 4384 | assert(numdigits > 0); |
| 4385 | } |
| 4386 | |
| 4387 | /* Fill with leading zeroes to meet minimum width. */ |
| 4388 | if (prec > numdigits) { |
| 4389 | PyObject *r1 = PyString_FromStringAndSize(NULL, |
| 4390 | numnondigits + prec); |
| 4391 | char *b1; |
| 4392 | if (!r1) { |
| 4393 | Py_DECREF(result); |
| 4394 | return NULL; |
| 4395 | } |
| 4396 | b1 = PyString_AS_STRING(r1); |
| 4397 | for (i = 0; i < numnondigits; ++i) |
| 4398 | *b1++ = *buf++; |
| 4399 | for (i = 0; i < prec - numdigits; i++) |
| 4400 | *b1++ = '0'; |
| 4401 | for (i = 0; i < numdigits; i++) |
| 4402 | *b1++ = *buf++; |
| 4403 | *b1 = '\0'; |
| 4404 | Py_DECREF(result); |
| 4405 | result = r1; |
| 4406 | buf = PyString_AS_STRING(result); |
| 4407 | len = numnondigits + prec; |
| 4408 | } |
| 4409 | |
| 4410 | /* Fix up case for hex conversions. */ |
Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 4411 | if (type == 'X') { |
| 4412 | /* Need to convert all lower case letters to upper case. |
| 4413 | and need to convert 0x to 0X (and -0x to -0X). */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4414 | for (i = 0; i < len; i++) |
Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 4415 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 4416 | buf[i] -= 'a'-'A'; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4417 | } |
| 4418 | *pbuf = buf; |
| 4419 | *plen = len; |
| 4420 | return result; |
| 4421 | } |
| 4422 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 4423 | Py_LOCAL_INLINE(int) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4424 | formatint(char *buf, size_t buflen, int flags, |
| 4425 | int prec, int type, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4426 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4427 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4428 | worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 4429 | + 1 + 1 = 24 */ |
| 4430 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4431 | char *sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4432 | long x; |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4433 | |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4434 | x = PyInt_AsLong(v); |
| 4435 | if (x == -1 && PyErr_Occurred()) { |
Georg Brandl | 283a135 | 2006-11-19 08:48:30 +0000 | [diff] [blame] | 4436 | PyErr_Format(PyExc_TypeError, "int argument required, not %.200s", |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4437 | Py_TYPE(v)->tp_name); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4438 | return -1; |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4439 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4440 | if (x < 0 && type == 'u') { |
| 4441 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 4442 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4443 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 4444 | sign = "-"; |
| 4445 | else |
| 4446 | sign = ""; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4447 | if (prec < 0) |
| 4448 | prec = 1; |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4449 | |
| 4450 | if ((flags & F_ALT) && |
| 4451 | (type == 'x' || type == 'X')) { |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4452 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4453 | * of issues that cause pain: |
| 4454 | * - when 0 is being converted, the C standard leaves off |
| 4455 | * the '0x' or '0X', which is inconsistent with other |
| 4456 | * %#x/%#X conversions and inconsistent with Python's |
| 4457 | * hex() function |
| 4458 | * - there are platforms that violate the standard and |
| 4459 | * convert 0 with the '0x' or '0X' |
| 4460 | * (Metrowerks, Compaq Tru64) |
| 4461 | * - there are platforms that give '0x' when converting |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4462 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4463 | * standard (OS/2 EMX) |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4464 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4465 | * We can achieve the desired consistency by inserting our |
| 4466 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 4467 | * of %#x/%#X. |
| 4468 | * |
| 4469 | * Note that this is the same approach as used in |
| 4470 | * formatint() in unicodeobject.c |
| 4471 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4472 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 4473 | sign, type, prec, type); |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4474 | } |
| 4475 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4476 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 4477 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4478 | prec, type); |
| 4479 | } |
| 4480 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4481 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 4482 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4483 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4484 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4485 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4486 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4487 | return -1; |
| 4488 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4489 | if (sign[0]) |
| 4490 | PyOS_snprintf(buf, buflen, fmt, -x); |
| 4491 | else |
| 4492 | PyOS_snprintf(buf, buflen, fmt, x); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4493 | return (int)strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4494 | } |
| 4495 | |
Fredrik Lundh | c2d29c5 | 2006-05-27 14:58:20 +0000 | [diff] [blame] | 4496 | Py_LOCAL_INLINE(int) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4497 | formatchar(char *buf, size_t buflen, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4498 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4499 | /* presume that the buffer is at least 2 characters long */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4500 | if (PyString_Check(v)) { |
| 4501 | 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] | 4502 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4503 | } |
| 4504 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4505 | 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] | 4506 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4507 | } |
| 4508 | buf[1] = '\0'; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4509 | return 1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4510 | } |
| 4511 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4512 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 4513 | |
| 4514 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 4515 | chars are formatted. XXX This is a magic number. Each formatting |
| 4516 | routine does bounds checking to ensure no overflow, but a better |
| 4517 | solution may be to malloc a buffer of appropriate size for each |
| 4518 | format. For now, the current solution is sufficient. |
| 4519 | */ |
| 4520 | #define FORMATBUFLEN (size_t)120 |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4521 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4522 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4523 | PyString_Format(PyObject *format, PyObject *args) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4524 | { |
| 4525 | char *fmt, *res; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4526 | Py_ssize_t arglen, argidx; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4527 | Py_ssize_t reslen, rescnt, fmtcnt; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4528 | int args_owned = 0; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4529 | PyObject *result, *orig_args; |
| 4530 | #ifdef Py_USING_UNICODE |
| 4531 | PyObject *v, *w; |
| 4532 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4533 | PyObject *dict = NULL; |
| 4534 | if (format == NULL || !PyString_Check(format) || args == NULL) { |
| 4535 | PyErr_BadInternalCall(); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4536 | return NULL; |
| 4537 | } |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4538 | orig_args = args; |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4539 | fmt = PyString_AS_STRING(format); |
| 4540 | fmtcnt = PyString_GET_SIZE(format); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 4541 | reslen = rescnt = fmtcnt + 100; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4542 | result = PyString_FromStringAndSize((char *)NULL, reslen); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4543 | if (result == NULL) |
| 4544 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4545 | res = PyString_AsString(result); |
| 4546 | if (PyTuple_Check(args)) { |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4547 | arglen = PyTuple_GET_SIZE(args); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4548 | argidx = 0; |
| 4549 | } |
| 4550 | else { |
| 4551 | arglen = -1; |
| 4552 | argidx = -2; |
| 4553 | } |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 4554 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 4555 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4556 | dict = args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4557 | while (--fmtcnt >= 0) { |
| 4558 | if (*fmt != '%') { |
| 4559 | if (--rescnt < 0) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 4560 | rescnt = fmtcnt + 100; |
| 4561 | reslen += rescnt; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4562 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4563 | return NULL; |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4564 | res = PyString_AS_STRING(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4565 | + reslen - rescnt; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4566 | --rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4567 | } |
| 4568 | *res++ = *fmt++; |
| 4569 | } |
| 4570 | else { |
| 4571 | /* Got a format specifier */ |
| 4572 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4573 | Py_ssize_t width = -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4574 | int prec = -1; |
Guido van Rossum | 6938a29 | 1993-11-11 14:51:57 +0000 | [diff] [blame] | 4575 | int c = '\0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4576 | int fill; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4577 | PyObject *v = NULL; |
| 4578 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4579 | char *pbuf; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4580 | int sign; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4581 | Py_ssize_t len; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4582 | char formatbuf[FORMATBUFLEN]; |
| 4583 | /* For format{float,int,char}() */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4584 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4585 | char *fmt_start = fmt; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4586 | Py_ssize_t argidx_start = argidx; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4587 | #endif |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4588 | |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 4589 | fmt++; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4590 | if (*fmt == '(') { |
| 4591 | char *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4592 | Py_ssize_t keylen; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4593 | PyObject *key; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4594 | int pcount = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4595 | |
| 4596 | if (dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4597 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4598 | "format requires a mapping"); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4599 | goto error; |
| 4600 | } |
| 4601 | ++fmt; |
| 4602 | --fmtcnt; |
| 4603 | keystart = fmt; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4604 | /* Skip over balanced parentheses */ |
| 4605 | while (pcount > 0 && --fmtcnt >= 0) { |
| 4606 | if (*fmt == ')') |
| 4607 | --pcount; |
| 4608 | else if (*fmt == '(') |
| 4609 | ++pcount; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4610 | fmt++; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4611 | } |
| 4612 | keylen = fmt - keystart - 1; |
| 4613 | if (fmtcnt < 0 || pcount > 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4614 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4615 | "incomplete format key"); |
| 4616 | goto error; |
| 4617 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4618 | key = PyString_FromStringAndSize(keystart, |
| 4619 | keylen); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4620 | if (key == NULL) |
| 4621 | goto error; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4622 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4623 | Py_DECREF(args); |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4624 | args_owned = 0; |
| 4625 | } |
| 4626 | args = PyObject_GetItem(dict, key); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4627 | Py_DECREF(key); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4628 | if (args == NULL) { |
| 4629 | goto error; |
| 4630 | } |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4631 | args_owned = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4632 | arglen = -1; |
| 4633 | argidx = -2; |
| 4634 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4635 | while (--fmtcnt >= 0) { |
| 4636 | switch (c = *fmt++) { |
| 4637 | case '-': flags |= F_LJUST; continue; |
| 4638 | case '+': flags |= F_SIGN; continue; |
| 4639 | case ' ': flags |= F_BLANK; continue; |
| 4640 | case '#': flags |= F_ALT; continue; |
| 4641 | case '0': flags |= F_ZERO; continue; |
| 4642 | } |
| 4643 | break; |
| 4644 | } |
| 4645 | if (c == '*') { |
| 4646 | v = getnextarg(args, arglen, &argidx); |
| 4647 | if (v == NULL) |
| 4648 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4649 | if (!PyInt_Check(v)) { |
| 4650 | PyErr_SetString(PyExc_TypeError, |
| 4651 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4652 | goto error; |
| 4653 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4654 | width = PyInt_AsLong(v); |
Guido van Rossum | 98c9eba | 1999-06-07 15:12:32 +0000 | [diff] [blame] | 4655 | if (width < 0) { |
| 4656 | flags |= F_LJUST; |
| 4657 | width = -width; |
| 4658 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4659 | if (--fmtcnt >= 0) |
| 4660 | c = *fmt++; |
| 4661 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4662 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4663 | width = c - '0'; |
| 4664 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4665 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4666 | if (!isdigit(c)) |
| 4667 | break; |
| 4668 | if ((width*10) / 10 != width) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4669 | PyErr_SetString( |
| 4670 | PyExc_ValueError, |
| 4671 | "width too big"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4672 | goto error; |
| 4673 | } |
| 4674 | width = width*10 + (c - '0'); |
| 4675 | } |
| 4676 | } |
| 4677 | if (c == '.') { |
| 4678 | prec = 0; |
| 4679 | if (--fmtcnt >= 0) |
| 4680 | c = *fmt++; |
| 4681 | if (c == '*') { |
| 4682 | v = getnextarg(args, arglen, &argidx); |
| 4683 | if (v == NULL) |
| 4684 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4685 | if (!PyInt_Check(v)) { |
| 4686 | PyErr_SetString( |
| 4687 | PyExc_TypeError, |
| 4688 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4689 | goto error; |
| 4690 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4691 | prec = PyInt_AsLong(v); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4692 | if (prec < 0) |
| 4693 | prec = 0; |
| 4694 | if (--fmtcnt >= 0) |
| 4695 | c = *fmt++; |
| 4696 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4697 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4698 | prec = c - '0'; |
| 4699 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4700 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4701 | if (!isdigit(c)) |
| 4702 | break; |
| 4703 | if ((prec*10) / 10 != prec) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4704 | PyErr_SetString( |
| 4705 | PyExc_ValueError, |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4706 | "prec too big"); |
| 4707 | goto error; |
| 4708 | } |
| 4709 | prec = prec*10 + (c - '0'); |
| 4710 | } |
| 4711 | } |
| 4712 | } /* prec */ |
| 4713 | if (fmtcnt >= 0) { |
| 4714 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4715 | if (--fmtcnt >= 0) |
| 4716 | c = *fmt++; |
| 4717 | } |
| 4718 | } |
| 4719 | if (fmtcnt < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4720 | PyErr_SetString(PyExc_ValueError, |
| 4721 | "incomplete format"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4722 | goto error; |
| 4723 | } |
| 4724 | if (c != '%') { |
| 4725 | v = getnextarg(args, arglen, &argidx); |
| 4726 | if (v == NULL) |
| 4727 | goto error; |
| 4728 | } |
| 4729 | sign = 0; |
| 4730 | fill = ' '; |
| 4731 | switch (c) { |
| 4732 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4733 | pbuf = "%"; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4734 | len = 1; |
| 4735 | break; |
| 4736 | case 's': |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4737 | #ifdef Py_USING_UNICODE |
Neil Schemenauer | ab61923 | 2005-08-31 23:02:05 +0000 | [diff] [blame] | 4738 | if (PyUnicode_Check(v)) { |
| 4739 | fmt = fmt_start; |
| 4740 | argidx = argidx_start; |
| 4741 | goto unicode; |
| 4742 | } |
Georg Brandl | d45014b | 2005-10-01 17:06:00 +0000 | [diff] [blame] | 4743 | #endif |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 4744 | temp = _PyObject_Str(v); |
Georg Brandl | d45014b | 2005-10-01 17:06:00 +0000 | [diff] [blame] | 4745 | #ifdef Py_USING_UNICODE |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 4746 | if (temp != NULL && PyUnicode_Check(temp)) { |
| 4747 | Py_DECREF(temp); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4748 | fmt = fmt_start; |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 4749 | argidx = argidx_start; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4750 | goto unicode; |
| 4751 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4752 | #endif |
Guido van Rossum | b00c07f | 2002-10-09 19:07:53 +0000 | [diff] [blame] | 4753 | /* Fall through */ |
Walter Dörwald | 9ff3f03 | 2003-06-18 14:17:01 +0000 | [diff] [blame] | 4754 | case 'r': |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 4755 | if (c == 'r') |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 4756 | temp = PyObject_Repr(v); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4757 | if (temp == NULL) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4758 | goto error; |
Guido van Rossum | 4a0144c | 1998-06-09 15:08:41 +0000 | [diff] [blame] | 4759 | if (!PyString_Check(temp)) { |
| 4760 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 8052f89 | 2002-10-09 19:14:30 +0000 | [diff] [blame] | 4761 | "%s argument has non-string str()"); |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4762 | Py_DECREF(temp); |
Guido van Rossum | 4a0144c | 1998-06-09 15:08:41 +0000 | [diff] [blame] | 4763 | goto error; |
| 4764 | } |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4765 | pbuf = PyString_AS_STRING(temp); |
| 4766 | len = PyString_GET_SIZE(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4767 | if (prec >= 0 && len > prec) |
| 4768 | len = prec; |
| 4769 | break; |
| 4770 | case 'i': |
| 4771 | case 'd': |
| 4772 | case 'u': |
| 4773 | case 'o': |
| 4774 | case 'x': |
| 4775 | case 'X': |
| 4776 | if (c == 'i') |
| 4777 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 4778 | if (PyLong_Check(v)) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4779 | int ilen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4780 | temp = _PyString_FormatLong(v, flags, |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4781 | prec, c, &pbuf, &ilen); |
| 4782 | len = ilen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4783 | if (!temp) |
| 4784 | goto error; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4785 | sign = 1; |
Guido van Rossum | 4acdc23 | 1997-01-29 06:00:24 +0000 | [diff] [blame] | 4786 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4787 | else { |
| 4788 | pbuf = formatbuf; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4789 | len = formatint(pbuf, |
| 4790 | sizeof(formatbuf), |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4791 | flags, prec, c, v); |
| 4792 | if (len < 0) |
| 4793 | goto error; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4794 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4795 | } |
| 4796 | if (flags & F_ZERO) |
| 4797 | fill = '0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4798 | break; |
| 4799 | case 'e': |
| 4800 | case 'E': |
| 4801 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 4802 | case 'F': |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4803 | case 'g': |
| 4804 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 4805 | if (c == 'F') |
| 4806 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4807 | pbuf = formatbuf; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4808 | len = formatfloat(pbuf, sizeof(formatbuf), |
| 4809 | flags, prec, c, v); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4810 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4811 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4812 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4813 | if (flags & F_ZERO) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4814 | fill = '0'; |
| 4815 | break; |
| 4816 | case 'c': |
Walter Dörwald | 43440a6 | 2003-03-31 18:07:50 +0000 | [diff] [blame] | 4817 | #ifdef Py_USING_UNICODE |
| 4818 | if (PyUnicode_Check(v)) { |
| 4819 | fmt = fmt_start; |
| 4820 | argidx = argidx_start; |
| 4821 | goto unicode; |
| 4822 | } |
| 4823 | #endif |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4824 | pbuf = formatbuf; |
| 4825 | len = formatchar(pbuf, sizeof(formatbuf), v); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4826 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4827 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4828 | break; |
| 4829 | default: |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4830 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 4831 | "unsupported format character '%c' (0x%x) " |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 4832 | "at index %zd", |
Guido van Rossum | efc1188 | 2002-09-12 14:43:41 +0000 | [diff] [blame] | 4833 | c, c, |
Armin Rigo | 7ccbca9 | 2006-10-04 12:17:45 +0000 | [diff] [blame] | 4834 | (Py_ssize_t)(fmt - 1 - |
| 4835 | PyString_AsString(format))); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4836 | goto error; |
| 4837 | } |
| 4838 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4839 | if (*pbuf == '-' || *pbuf == '+') { |
| 4840 | sign = *pbuf++; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4841 | len--; |
| 4842 | } |
| 4843 | else if (flags & F_SIGN) |
| 4844 | sign = '+'; |
| 4845 | else if (flags & F_BLANK) |
| 4846 | sign = ' '; |
| 4847 | else |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4848 | sign = 0; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4849 | } |
| 4850 | if (width < len) |
| 4851 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 4852 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 4853 | reslen -= rescnt; |
| 4854 | rescnt = width + fmtcnt + 100; |
| 4855 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 4856 | if (reslen < 0) { |
| 4857 | Py_DECREF(result); |
Georg Brandl | 10a4b0e | 2007-02-26 13:51:29 +0000 | [diff] [blame] | 4858 | Py_XDECREF(temp); |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 4859 | return PyErr_NoMemory(); |
| 4860 | } |
Georg Brandl | 10a4b0e | 2007-02-26 13:51:29 +0000 | [diff] [blame] | 4861 | if (_PyString_Resize(&result, reslen) < 0) { |
| 4862 | Py_XDECREF(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4863 | return NULL; |
Georg Brandl | 10a4b0e | 2007-02-26 13:51:29 +0000 | [diff] [blame] | 4864 | } |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4865 | res = PyString_AS_STRING(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4866 | + reslen - rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4867 | } |
| 4868 | if (sign) { |
Guido van Rossum | 71e57d0 | 1993-11-11 15:03:51 +0000 | [diff] [blame] | 4869 | if (fill != ' ') |
| 4870 | *res++ = sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4871 | rescnt--; |
| 4872 | if (width > len) |
| 4873 | width--; |
| 4874 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4875 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 4876 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4877 | assert(pbuf[1] == c); |
| 4878 | if (fill != ' ') { |
| 4879 | *res++ = *pbuf++; |
| 4880 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4881 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4882 | rescnt -= 2; |
| 4883 | width -= 2; |
| 4884 | if (width < 0) |
| 4885 | width = 0; |
| 4886 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4887 | } |
| 4888 | if (width > len && !(flags & F_LJUST)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4889 | do { |
| 4890 | --rescnt; |
| 4891 | *res++ = fill; |
| 4892 | } while (--width > len); |
| 4893 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4894 | if (fill == ' ') { |
| 4895 | if (sign) |
| 4896 | *res++ = sign; |
| 4897 | if ((flags & F_ALT) && |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4898 | (c == 'x' || c == 'X')) { |
| 4899 | assert(pbuf[0] == '0'); |
| 4900 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4901 | *res++ = *pbuf++; |
| 4902 | *res++ = *pbuf++; |
| 4903 | } |
| 4904 | } |
Fredrik Lundh | 80f8e80 | 2006-05-28 12:06:46 +0000 | [diff] [blame] | 4905 | Py_MEMCPY(res, pbuf, len); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4906 | res += len; |
| 4907 | rescnt -= len; |
| 4908 | while (--width >= len) { |
| 4909 | --rescnt; |
| 4910 | *res++ = ' '; |
| 4911 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4912 | if (dict && (argidx < arglen) && c != '%') { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4913 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 4914 | "not all arguments converted during string formatting"); |
Georg Brandl | 10a4b0e | 2007-02-26 13:51:29 +0000 | [diff] [blame] | 4915 | Py_XDECREF(temp); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4916 | goto error; |
| 4917 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4918 | Py_XDECREF(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4919 | } /* '%' */ |
| 4920 | } /* until end */ |
Guido van Rossum | caeaafc | 1995-02-27 10:13:23 +0000 | [diff] [blame] | 4921 | if (argidx < arglen && !dict) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4922 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 4923 | "not all arguments converted during string formatting"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4924 | goto error; |
| 4925 | } |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4926 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4927 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4928 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4929 | _PyString_Resize(&result, reslen - rescnt); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4930 | return result; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4931 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4932 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4933 | unicode: |
| 4934 | if (args_owned) { |
| 4935 | Py_DECREF(args); |
| 4936 | args_owned = 0; |
| 4937 | } |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 4938 | /* Fiddle args right (remove the first argidx arguments) */ |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4939 | if (PyTuple_Check(orig_args) && argidx > 0) { |
| 4940 | PyObject *v; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4941 | Py_ssize_t n = PyTuple_GET_SIZE(orig_args) - argidx; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4942 | v = PyTuple_New(n); |
| 4943 | if (v == NULL) |
| 4944 | goto error; |
| 4945 | while (--n >= 0) { |
| 4946 | PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx); |
| 4947 | Py_INCREF(w); |
| 4948 | PyTuple_SET_ITEM(v, n, w); |
| 4949 | } |
| 4950 | args = v; |
| 4951 | } else { |
| 4952 | Py_INCREF(orig_args); |
| 4953 | args = orig_args; |
| 4954 | } |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4955 | args_owned = 1; |
| 4956 | /* Take what we have of the result and let the Unicode formatting |
| 4957 | function format the rest of the input. */ |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4958 | rescnt = res - PyString_AS_STRING(result); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4959 | if (_PyString_Resize(&result, rescnt)) |
| 4960 | goto error; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4961 | fmtcnt = PyString_GET_SIZE(format) - \ |
| 4962 | (fmt - PyString_AS_STRING(format)); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4963 | format = PyUnicode_Decode(fmt, fmtcnt, NULL, NULL); |
| 4964 | if (format == NULL) |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4965 | goto error; |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4966 | v = PyUnicode_Format(format, args); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4967 | Py_DECREF(format); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4968 | if (v == NULL) |
| 4969 | goto error; |
| 4970 | /* Paste what we have (result) to what the Unicode formatting |
| 4971 | function returned (v) and return the result (or error) */ |
| 4972 | w = PyUnicode_Concat(result, v); |
| 4973 | Py_DECREF(result); |
| 4974 | Py_DECREF(v); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4975 | Py_DECREF(args); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4976 | return w; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4977 | #endif /* Py_USING_UNICODE */ |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4978 | |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4979 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4980 | Py_DECREF(result); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4981 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4982 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4983 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4984 | return NULL; |
| 4985 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4986 | |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4987 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4988 | PyString_InternInPlace(PyObject **p) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4989 | { |
| 4990 | register PyStringObject *s = (PyStringObject *)(*p); |
| 4991 | PyObject *t; |
| 4992 | if (s == NULL || !PyString_Check(s)) |
| 4993 | Py_FatalError("PyString_InternInPlace: strings only please!"); |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4994 | /* If it's a string subclass, we don't really know what putting |
| 4995 | it in the interned dict might do. */ |
| 4996 | if (!PyString_CheckExact(s)) |
| 4997 | return; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4998 | if (PyString_CHECK_INTERNED(s)) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4999 | return; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5000 | if (interned == NULL) { |
| 5001 | interned = PyDict_New(); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5002 | if (interned == NULL) { |
| 5003 | PyErr_Clear(); /* Don't leave an exception */ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5004 | return; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5005 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5006 | } |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 5007 | t = PyDict_GetItem(interned, (PyObject *)s); |
| 5008 | if (t) { |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5009 | Py_INCREF(t); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5010 | Py_DECREF(*p); |
| 5011 | *p = t; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5012 | return; |
| 5013 | } |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5014 | |
Armin Rigo | 79f7ad2 | 2004-08-07 19:27:39 +0000 | [diff] [blame] | 5015 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 5016 | PyErr_Clear(); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5017 | return; |
| 5018 | } |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 5019 | /* The two references in interned are not counted by refcnt. |
| 5020 | The string deallocator will take care of this */ |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5021 | Py_REFCNT(s) -= 2; |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 5022 | PyString_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5025 | void |
| 5026 | PyString_InternImmortal(PyObject **p) |
| 5027 | { |
| 5028 | PyString_InternInPlace(p); |
| 5029 | if (PyString_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 5030 | PyString_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 5031 | Py_INCREF(*p); |
| 5032 | } |
| 5033 | } |
| 5034 | |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5035 | |
| 5036 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 5037 | PyString_InternFromString(const char *cp) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5038 | { |
| 5039 | PyObject *s = PyString_FromString(cp); |
| 5040 | if (s == NULL) |
| 5041 | return NULL; |
| 5042 | PyString_InternInPlace(&s); |
| 5043 | return s; |
| 5044 | } |
| 5045 | |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 5046 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 5047 | PyString_Fini(void) |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 5048 | { |
| 5049 | int i; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 5050 | for (i = 0; i < UCHAR_MAX + 1; i++) { |
| 5051 | Py_XDECREF(characters[i]); |
| 5052 | characters[i] = NULL; |
| 5053 | } |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 5054 | Py_XDECREF(nullstring); |
| 5055 | nullstring = NULL; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 5056 | } |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 5057 | |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 5058 | void _Py_ReleaseInternedStrings(void) |
| 5059 | { |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5060 | PyObject *keys; |
| 5061 | PyStringObject *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5062 | Py_ssize_t i, n; |
Neal Norwitz | 1c1a1c5 | 2007-02-25 15:52:27 +0000 | [diff] [blame] | 5063 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5064 | |
| 5065 | if (interned == NULL || !PyDict_Check(interned)) |
| 5066 | return; |
| 5067 | keys = PyDict_Keys(interned); |
| 5068 | if (keys == NULL || !PyList_Check(keys)) { |
| 5069 | PyErr_Clear(); |
| 5070 | return; |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 5071 | } |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5072 | |
| 5073 | /* Since _Py_ReleaseInternedStrings() is intended to help a leak |
| 5074 | detector, interned strings are not forcibly deallocated; rather, we |
| 5075 | give them their stolen references back, and then clear and DECREF |
| 5076 | the interned dict. */ |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 5077 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5078 | n = PyList_GET_SIZE(keys); |
Neal Norwitz | 1c1a1c5 | 2007-02-25 15:52:27 +0000 | [diff] [blame] | 5079 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
| 5080 | n); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5081 | for (i = 0; i < n; i++) { |
| 5082 | s = (PyStringObject *) PyList_GET_ITEM(keys, i); |
| 5083 | switch (s->ob_sstate) { |
| 5084 | case SSTATE_NOT_INTERNED: |
| 5085 | /* XXX Shouldn't happen */ |
| 5086 | break; |
| 5087 | case SSTATE_INTERNED_IMMORTAL: |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5088 | Py_REFCNT(s) += 1; |
| 5089 | immortal_size += Py_SIZE(s); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5090 | break; |
| 5091 | case SSTATE_INTERNED_MORTAL: |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 5092 | Py_REFCNT(s) += 2; |
| 5093 | mortal_size += Py_SIZE(s); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5094 | break; |
| 5095 | default: |
| 5096 | Py_FatalError("Inconsistent interned string state."); |
| 5097 | } |
| 5098 | s->ob_sstate = SSTATE_NOT_INTERNED; |
| 5099 | } |
Neal Norwitz | 1c1a1c5 | 2007-02-25 15:52:27 +0000 | [diff] [blame] | 5100 | fprintf(stderr, "total size of all interned strings: " |
| 5101 | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
| 5102 | "mortal/immortal\n", mortal_size, immortal_size); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5103 | Py_DECREF(keys); |
| 5104 | PyDict_Clear(interned); |
| 5105 | Py_DECREF(interned); |
| 5106 | interned = NULL; |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 5107 | } |