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 |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 6 | #include <ctype.h> |
| 7 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 8 | #ifdef COUNT_ALLOCS |
| 9 | int null_strings, one_strings; |
| 10 | #endif |
| 11 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 12 | static PyStringObject *characters[UCHAR_MAX + 1]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 13 | static PyStringObject *nullstring; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 15 | /* This dictionary holds all interned strings. Note that references to |
| 16 | strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 17 | When the interned string reaches a refcnt of 0 the string deallocation |
| 18 | function will delete the reference from this dictionary. |
| 19 | |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 20 | 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] | 21 | count of a string is: s->ob_refcnt + (s->ob_sstate?2:0) |
| 22 | */ |
| 23 | static PyObject *interned; |
| 24 | |
| 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) |
| 82 | memcpy(op->ob_sval, str, size); |
| 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; |
Guido van Rossum | 169192e | 2001-12-10 15:45:54 +0000 | [diff] [blame] | 136 | 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 |
| 164 | memcpy(count, vargs, sizeof(va_list)); |
| 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; |
| 306 | memcpy(s, p, i); |
| 307 | s += i; |
| 308 | break; |
| 309 | case 'p': |
| 310 | sprintf(s, "%p", va_arg(vargs, void*)); |
Tim Peters | 6af5bbb | 2001-08-25 03:02:28 +0000 | [diff] [blame] | 311 | /* %p is ill-defined: ensure leading 0x. */ |
| 312 | if (s[1] == 'X') |
| 313 | s[1] = 'x'; |
| 314 | else if (s[1] != 'x') { |
| 315 | memmove(s+2, s, strlen(s)+1); |
| 316 | s[0] = '0'; |
| 317 | s[1] = 'x'; |
| 318 | } |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 319 | s += strlen(s); |
| 320 | break; |
| 321 | case '%': |
| 322 | *s++ = '%'; |
| 323 | break; |
| 324 | default: |
| 325 | strcpy(s, p); |
| 326 | s += strlen(s); |
| 327 | goto end; |
| 328 | } |
| 329 | } else |
| 330 | *s++ = *f; |
| 331 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 332 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 333 | end: |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 334 | _PyString_Resize(&string, s - PyString_AS_STRING(string)); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 335 | return string; |
| 336 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 337 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 338 | PyObject * |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 339 | PyString_FromFormat(const char *format, ...) |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 340 | { |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 341 | PyObject* ret; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 342 | va_list vargs; |
| 343 | |
| 344 | #ifdef HAVE_STDARG_PROTOTYPES |
| 345 | va_start(vargs, format); |
| 346 | #else |
| 347 | va_start(vargs); |
| 348 | #endif |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 349 | ret = PyString_FromFormatV(format, vargs); |
| 350 | va_end(vargs); |
| 351 | return ret; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 355 | PyObject *PyString_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 356 | Py_ssize_t size, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 357 | const char *encoding, |
| 358 | const char *errors) |
| 359 | { |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 360 | PyObject *v, *str; |
| 361 | |
| 362 | str = PyString_FromStringAndSize(s, size); |
| 363 | if (str == NULL) |
| 364 | return NULL; |
| 365 | v = PyString_AsDecodedString(str, encoding, errors); |
| 366 | Py_DECREF(str); |
| 367 | return v; |
| 368 | } |
| 369 | |
| 370 | PyObject *PyString_AsDecodedObject(PyObject *str, |
| 371 | const char *encoding, |
| 372 | const char *errors) |
| 373 | { |
| 374 | PyObject *v; |
| 375 | |
| 376 | if (!PyString_Check(str)) { |
| 377 | PyErr_BadArgument(); |
| 378 | goto onError; |
| 379 | } |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 380 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 381 | if (encoding == NULL) { |
| 382 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 383 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 384 | #else |
| 385 | PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
| 386 | goto onError; |
| 387 | #endif |
| 388 | } |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 389 | |
| 390 | /* Decode via the codec registry */ |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 391 | v = PyCodec_Decode(str, encoding, errors); |
| 392 | if (v == NULL) |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 393 | goto onError; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 394 | |
| 395 | return v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 396 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 397 | onError: |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | PyObject *PyString_AsDecodedString(PyObject *str, |
| 402 | const char *encoding, |
| 403 | const char *errors) |
| 404 | { |
| 405 | PyObject *v; |
| 406 | |
| 407 | v = PyString_AsDecodedObject(str, encoding, errors); |
| 408 | if (v == NULL) |
| 409 | goto onError; |
| 410 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 411 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 412 | /* Convert Unicode to a string using the default encoding */ |
| 413 | if (PyUnicode_Check(v)) { |
| 414 | PyObject *temp = v; |
| 415 | v = PyUnicode_AsEncodedString(v, NULL, NULL); |
| 416 | Py_DECREF(temp); |
| 417 | if (v == NULL) |
| 418 | goto onError; |
| 419 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 420 | #endif |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 421 | if (!PyString_Check(v)) { |
| 422 | PyErr_Format(PyExc_TypeError, |
| 423 | "decoder did not return a string object (type=%.400s)", |
| 424 | v->ob_type->tp_name); |
| 425 | Py_DECREF(v); |
| 426 | goto onError; |
| 427 | } |
| 428 | |
| 429 | return v; |
| 430 | |
| 431 | onError: |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 432 | return NULL; |
| 433 | } |
| 434 | |
| 435 | PyObject *PyString_Encode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 436 | Py_ssize_t size, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 437 | const char *encoding, |
| 438 | const char *errors) |
| 439 | { |
| 440 | PyObject *v, *str; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 441 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 442 | str = PyString_FromStringAndSize(s, size); |
| 443 | if (str == NULL) |
| 444 | return NULL; |
| 445 | v = PyString_AsEncodedString(str, encoding, errors); |
| 446 | Py_DECREF(str); |
| 447 | return v; |
| 448 | } |
| 449 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 450 | PyObject *PyString_AsEncodedObject(PyObject *str, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 451 | const char *encoding, |
| 452 | const char *errors) |
| 453 | { |
| 454 | PyObject *v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 455 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 456 | if (!PyString_Check(str)) { |
| 457 | PyErr_BadArgument(); |
| 458 | goto onError; |
| 459 | } |
| 460 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 461 | if (encoding == NULL) { |
| 462 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 463 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 464 | #else |
| 465 | PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
| 466 | goto onError; |
| 467 | #endif |
| 468 | } |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 469 | |
| 470 | /* Encode via the codec registry */ |
| 471 | v = PyCodec_Encode(str, encoding, errors); |
| 472 | if (v == NULL) |
| 473 | goto onError; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 474 | |
| 475 | return v; |
| 476 | |
| 477 | onError: |
| 478 | return NULL; |
| 479 | } |
| 480 | |
| 481 | PyObject *PyString_AsEncodedString(PyObject *str, |
| 482 | const char *encoding, |
| 483 | const char *errors) |
| 484 | { |
| 485 | PyObject *v; |
| 486 | |
Marc-André Lemburg | 8c2133d | 2001-06-12 13:14:10 +0000 | [diff] [blame] | 487 | v = PyString_AsEncodedObject(str, encoding, errors); |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 488 | if (v == NULL) |
| 489 | goto onError; |
| 490 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 491 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 492 | /* Convert Unicode to a string using the default encoding */ |
| 493 | if (PyUnicode_Check(v)) { |
| 494 | PyObject *temp = v; |
| 495 | v = PyUnicode_AsEncodedString(v, NULL, NULL); |
| 496 | Py_DECREF(temp); |
| 497 | if (v == NULL) |
| 498 | goto onError; |
| 499 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 500 | #endif |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 501 | if (!PyString_Check(v)) { |
| 502 | PyErr_Format(PyExc_TypeError, |
| 503 | "encoder did not return a string object (type=%.400s)", |
| 504 | v->ob_type->tp_name); |
| 505 | Py_DECREF(v); |
| 506 | goto onError; |
| 507 | } |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 508 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 509 | return v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 510 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 511 | onError: |
| 512 | return NULL; |
| 513 | } |
| 514 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 515 | static void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 516 | string_dealloc(PyObject *op) |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 517 | { |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 518 | switch (PyString_CHECK_INTERNED(op)) { |
| 519 | case SSTATE_NOT_INTERNED: |
| 520 | break; |
| 521 | |
| 522 | case SSTATE_INTERNED_MORTAL: |
| 523 | /* revive dead object temporarily for DelItem */ |
| 524 | op->ob_refcnt = 3; |
| 525 | if (PyDict_DelItem(interned, op) != 0) |
| 526 | Py_FatalError( |
| 527 | "deletion of interned string failed"); |
| 528 | break; |
| 529 | |
| 530 | case SSTATE_INTERNED_IMMORTAL: |
| 531 | Py_FatalError("Immortal interned string died."); |
| 532 | |
| 533 | default: |
| 534 | Py_FatalError("Inconsistent interned string state."); |
| 535 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 536 | op->ob_type->tp_free(op); |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 539 | /* Unescape a backslash-escaped string. If unicode is non-zero, |
| 540 | the string is a u-literal. If recode_encoding is non-zero, |
| 541 | the string is UTF-8 encoded and should be re-encoded in the |
| 542 | specified encoding. */ |
| 543 | |
| 544 | PyObject *PyString_DecodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 545 | Py_ssize_t len, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 546 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 547 | Py_ssize_t unicode, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 548 | const char *recode_encoding) |
| 549 | { |
| 550 | int c; |
| 551 | char *p, *buf; |
| 552 | const char *end; |
| 553 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 554 | Py_ssize_t newlen = recode_encoding ? 4*len:len; |
Walter Dörwald | 8709a42 | 2002-09-03 13:53:40 +0000 | [diff] [blame] | 555 | v = PyString_FromStringAndSize((char *)NULL, newlen); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 556 | if (v == NULL) |
| 557 | return NULL; |
| 558 | p = buf = PyString_AsString(v); |
| 559 | end = s + len; |
| 560 | while (s < end) { |
| 561 | if (*s != '\\') { |
Martin v. Löwis | 2412853 | 2002-09-09 06:17:05 +0000 | [diff] [blame] | 562 | non_esc: |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 563 | #ifdef Py_USING_UNICODE |
| 564 | if (recode_encoding && (*s & 0x80)) { |
| 565 | PyObject *u, *w; |
| 566 | char *r; |
| 567 | const char* t; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 568 | Py_ssize_t rn; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 569 | t = s; |
| 570 | /* Decode non-ASCII bytes as UTF-8. */ |
| 571 | while (t < end && (*t & 0x80)) t++; |
| 572 | u = PyUnicode_DecodeUTF8(s, t - s, errors); |
| 573 | if(!u) goto failed; |
| 574 | |
| 575 | /* Recode them in target encoding. */ |
| 576 | w = PyUnicode_AsEncodedString( |
| 577 | u, recode_encoding, errors); |
| 578 | Py_DECREF(u); |
| 579 | if (!w) goto failed; |
| 580 | |
| 581 | /* Append bytes to output buffer. */ |
Neal Norwitz | 2aa9a5d | 2006-03-20 01:53:23 +0000 | [diff] [blame] | 582 | assert(PyString_Check(w)); |
| 583 | r = PyString_AS_STRING(w); |
| 584 | rn = PyString_GET_SIZE(w); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 585 | memcpy(p, r, rn); |
| 586 | p += rn; |
| 587 | Py_DECREF(w); |
| 588 | s = t; |
| 589 | } else { |
| 590 | *p++ = *s++; |
| 591 | } |
| 592 | #else |
| 593 | *p++ = *s++; |
| 594 | #endif |
| 595 | continue; |
| 596 | } |
| 597 | s++; |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 598 | if (s==end) { |
| 599 | PyErr_SetString(PyExc_ValueError, |
| 600 | "Trailing \\ in string"); |
| 601 | goto failed; |
| 602 | } |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 603 | switch (*s++) { |
| 604 | /* XXX This assumes ASCII! */ |
| 605 | case '\n': break; |
| 606 | case '\\': *p++ = '\\'; break; |
| 607 | case '\'': *p++ = '\''; break; |
| 608 | case '\"': *p++ = '\"'; break; |
| 609 | case 'b': *p++ = '\b'; break; |
| 610 | case 'f': *p++ = '\014'; break; /* FF */ |
| 611 | case 't': *p++ = '\t'; break; |
| 612 | case 'n': *p++ = '\n'; break; |
| 613 | case 'r': *p++ = '\r'; break; |
| 614 | case 'v': *p++ = '\013'; break; /* VT */ |
| 615 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 616 | case '0': case '1': case '2': case '3': |
| 617 | case '4': case '5': case '6': case '7': |
| 618 | c = s[-1] - '0'; |
| 619 | if ('0' <= *s && *s <= '7') { |
| 620 | c = (c<<3) + *s++ - '0'; |
| 621 | if ('0' <= *s && *s <= '7') |
| 622 | c = (c<<3) + *s++ - '0'; |
| 623 | } |
| 624 | *p++ = c; |
| 625 | break; |
| 626 | case 'x': |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 627 | if (isxdigit(Py_CHARMASK(s[0])) |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 628 | && isxdigit(Py_CHARMASK(s[1]))) { |
| 629 | unsigned int x = 0; |
| 630 | c = Py_CHARMASK(*s); |
| 631 | s++; |
| 632 | if (isdigit(c)) |
| 633 | x = c - '0'; |
| 634 | else if (islower(c)) |
| 635 | x = 10 + c - 'a'; |
| 636 | else |
| 637 | x = 10 + c - 'A'; |
| 638 | x = x << 4; |
| 639 | c = Py_CHARMASK(*s); |
| 640 | s++; |
| 641 | if (isdigit(c)) |
| 642 | x += c - '0'; |
| 643 | else if (islower(c)) |
| 644 | x += 10 + c - 'a'; |
| 645 | else |
| 646 | x += 10 + c - 'A'; |
| 647 | *p++ = x; |
| 648 | break; |
| 649 | } |
| 650 | if (!errors || strcmp(errors, "strict") == 0) { |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 651 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 652 | "invalid \\x escape"); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 653 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 654 | } |
| 655 | if (strcmp(errors, "replace") == 0) { |
| 656 | *p++ = '?'; |
| 657 | } else if (strcmp(errors, "ignore") == 0) |
| 658 | /* do nothing */; |
| 659 | else { |
| 660 | PyErr_Format(PyExc_ValueError, |
| 661 | "decoding error; " |
| 662 | "unknown error handling code: %.400s", |
| 663 | errors); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 664 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 665 | } |
| 666 | #ifndef Py_USING_UNICODE |
| 667 | case 'u': |
| 668 | case 'U': |
| 669 | case 'N': |
| 670 | if (unicode) { |
Neal Norwitz | b898d9f | 2002-08-16 23:20:39 +0000 | [diff] [blame] | 671 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 672 | "Unicode escapes not legal " |
| 673 | "when Unicode disabled"); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 674 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 675 | } |
| 676 | #endif |
| 677 | default: |
| 678 | *p++ = '\\'; |
Martin v. Löwis | 2412853 | 2002-09-09 06:17:05 +0000 | [diff] [blame] | 679 | s--; |
| 680 | goto non_esc; /* an arbitry number of unescaped |
| 681 | UTF-8 bytes may follow. */ |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 682 | } |
| 683 | } |
Walter Dörwald | 8709a42 | 2002-09-03 13:53:40 +0000 | [diff] [blame] | 684 | if (p-buf < newlen) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 685 | _PyString_Resize(&v, p - buf); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 686 | return v; |
| 687 | failed: |
| 688 | Py_DECREF(v); |
| 689 | return NULL; |
| 690 | } |
| 691 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 692 | static Py_ssize_t |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 693 | string_getsize(register PyObject *op) |
| 694 | { |
| 695 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 696 | Py_ssize_t len; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 697 | if (PyString_AsStringAndSize(op, &s, &len)) |
| 698 | return -1; |
| 699 | return len; |
| 700 | } |
| 701 | |
| 702 | static /*const*/ char * |
| 703 | string_getbuffer(register PyObject *op) |
| 704 | { |
| 705 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 706 | Py_ssize_t len; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 707 | if (PyString_AsStringAndSize(op, &s, &len)) |
| 708 | return NULL; |
| 709 | return s; |
| 710 | } |
| 711 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 712 | Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 713 | PyString_Size(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 714 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 715 | if (!PyString_Check(op)) |
| 716 | return string_getsize(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 717 | return ((PyStringObject *)op) -> ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /*const*/ char * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 721 | PyString_AsString(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 722 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 723 | if (!PyString_Check(op)) |
| 724 | return string_getbuffer(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 725 | return ((PyStringObject *)op) -> ob_sval; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 728 | int |
| 729 | PyString_AsStringAndSize(register PyObject *obj, |
| 730 | register char **s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 731 | register Py_ssize_t *len) |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 732 | { |
| 733 | if (s == NULL) { |
| 734 | PyErr_BadInternalCall(); |
| 735 | return -1; |
| 736 | } |
| 737 | |
| 738 | if (!PyString_Check(obj)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 739 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 740 | if (PyUnicode_Check(obj)) { |
| 741 | obj = _PyUnicode_AsDefaultEncodedString(obj, NULL); |
| 742 | if (obj == NULL) |
| 743 | return -1; |
| 744 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 745 | else |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 746 | #endif |
| 747 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 748 | PyErr_Format(PyExc_TypeError, |
| 749 | "expected string or Unicode object, " |
| 750 | "%.200s found", obj->ob_type->tp_name); |
| 751 | return -1; |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | *s = PyString_AS_STRING(obj); |
| 756 | if (len != NULL) |
| 757 | *len = PyString_GET_SIZE(obj); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 758 | else if (strlen(*s) != (size_t)PyString_GET_SIZE(obj)) { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 759 | PyErr_SetString(PyExc_TypeError, |
| 760 | "expected string without null bytes"); |
| 761 | return -1; |
| 762 | } |
| 763 | return 0; |
| 764 | } |
| 765 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 766 | /* Methods */ |
| 767 | |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 768 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 769 | string_print(PyStringObject *op, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 770 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 771 | Py_ssize_t i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 772 | char c; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 773 | int quote; |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 774 | |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 775 | /* XXX Ought to check for interrupts when writing long strings */ |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 776 | if (! PyString_CheckExact(op)) { |
| 777 | int ret; |
| 778 | /* A str subclass may have its own __str__ method. */ |
| 779 | op = (PyStringObject *) PyObject_Str((PyObject *)op); |
| 780 | if (op == NULL) |
| 781 | return -1; |
| 782 | ret = string_print(op, fp, flags); |
| 783 | Py_DECREF(op); |
| 784 | return ret; |
| 785 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 786 | if (flags & Py_PRINT_RAW) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 787 | #ifdef __VMS |
| 788 | if (op->ob_size) fwrite(op->ob_sval, (int) op->ob_size, 1, fp); |
| 789 | #else |
| 790 | fwrite(op->ob_sval, 1, (int) op->ob_size, fp); |
| 791 | #endif |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 792 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 793 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 794 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 795 | /* figure out which quote to use; single is preferred */ |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 796 | quote = '\''; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 797 | if (memchr(op->ob_sval, '\'', op->ob_size) && |
| 798 | !memchr(op->ob_sval, '"', op->ob_size)) |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 799 | quote = '"'; |
| 800 | |
| 801 | fputc(quote, fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 802 | for (i = 0; i < op->ob_size; i++) { |
| 803 | c = op->ob_sval[i]; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 804 | if (c == quote || c == '\\') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 805 | fprintf(fp, "\\%c", c); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 806 | else if (c == '\t') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 807 | fprintf(fp, "\\t"); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 808 | else if (c == '\n') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 809 | fprintf(fp, "\\n"); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 810 | else if (c == '\r') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 811 | fprintf(fp, "\\r"); |
| 812 | else if (c < ' ' || c >= 0x7f) |
| 813 | fprintf(fp, "\\x%02x", c & 0xff); |
Martin v. Löwis | fed2405 | 2002-10-07 13:55:50 +0000 | [diff] [blame] | 814 | else |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 815 | fputc(c, fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 816 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 817 | fputc(quote, fp); |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 818 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 821 | PyObject * |
| 822 | PyString_Repr(PyObject *obj, int smartquotes) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 823 | { |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 824 | register PyStringObject* op = (PyStringObject*) obj; |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 825 | size_t newsize = 2 + 4 * op->ob_size; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 826 | PyObject *v; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 827 | if (newsize > PY_SSIZE_T_MAX) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 828 | PyErr_SetString(PyExc_OverflowError, |
| 829 | "string is too large to make repr"); |
| 830 | } |
| 831 | v = PyString_FromStringAndSize((char *)NULL, newsize); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 832 | if (v == NULL) { |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 833 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 834 | } |
| 835 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 836 | register Py_ssize_t i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 837 | register char c; |
| 838 | register char *p; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 839 | int quote; |
| 840 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 841 | /* figure out which quote to use; single is preferred */ |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 842 | quote = '\''; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 843 | if (smartquotes && |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 844 | memchr(op->ob_sval, '\'', op->ob_size) && |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 845 | !memchr(op->ob_sval, '"', op->ob_size)) |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 846 | quote = '"'; |
| 847 | |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 848 | p = PyString_AS_STRING(v); |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 849 | *p++ = quote; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 850 | for (i = 0; i < op->ob_size; i++) { |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 851 | /* There's at least enough room for a hex escape |
| 852 | and a closing quote. */ |
| 853 | assert(newsize - (p - PyString_AS_STRING(v)) >= 5); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 854 | c = op->ob_sval[i]; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 855 | if (c == quote || c == '\\') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 856 | *p++ = '\\', *p++ = c; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 857 | else if (c == '\t') |
| 858 | *p++ = '\\', *p++ = 't'; |
| 859 | else if (c == '\n') |
| 860 | *p++ = '\\', *p++ = 'n'; |
| 861 | else if (c == '\r') |
| 862 | *p++ = '\\', *p++ = 'r'; |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 863 | else if (c < ' ' || c >= 0x7f) { |
| 864 | /* For performance, we don't want to call |
| 865 | PyOS_snprintf here (extra layers of |
| 866 | function call). */ |
| 867 | sprintf(p, "\\x%02x", c & 0xff); |
| 868 | p += 4; |
Martin v. Löwis | fed2405 | 2002-10-07 13:55:50 +0000 | [diff] [blame] | 869 | } |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 870 | else |
| 871 | *p++ = c; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 872 | } |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 873 | assert(newsize - (p - PyString_AS_STRING(v)) >= 1); |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 874 | *p++ = quote; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 875 | *p = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 876 | _PyString_Resize( |
Thomas Wouters | 568f1d0 | 2006-04-21 13:54:43 +0000 | [diff] [blame] | 877 | &v, (p - PyString_AS_STRING(v))); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 878 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 879 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 880 | } |
| 881 | |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 882 | static PyObject * |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 883 | string_repr(PyObject *op) |
| 884 | { |
| 885 | return PyString_Repr(op, 1); |
| 886 | } |
| 887 | |
| 888 | static PyObject * |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 889 | string_str(PyObject *s) |
| 890 | { |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 891 | assert(PyString_Check(s)); |
| 892 | if (PyString_CheckExact(s)) { |
| 893 | Py_INCREF(s); |
| 894 | return s; |
| 895 | } |
| 896 | else { |
| 897 | /* Subtype -- return genuine string with the same value. */ |
| 898 | PyStringObject *t = (PyStringObject *) s; |
| 899 | return PyString_FromStringAndSize(t->ob_sval, t->ob_size); |
| 900 | } |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 903 | static Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 904 | string_length(PyStringObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 905 | { |
| 906 | return a->ob_size; |
| 907 | } |
| 908 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 909 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 910 | string_concat(register PyStringObject *a, register PyObject *bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 911 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 912 | register size_t size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 913 | register PyStringObject *op; |
| 914 | if (!PyString_Check(bb)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 915 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 916 | if (PyUnicode_Check(bb)) |
| 917 | return PyUnicode_Concat((PyObject *)a, bb); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 918 | #endif |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 919 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5c66a26 | 2001-10-22 04:12:44 +0000 | [diff] [blame] | 920 | "cannot concatenate 'str' and '%.200s' objects", |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 921 | bb->ob_type->tp_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 922 | return NULL; |
| 923 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 924 | #define b ((PyStringObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 925 | /* Optimize cases with empty left or right operand */ |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 926 | if ((a->ob_size == 0 || b->ob_size == 0) && |
| 927 | PyString_CheckExact(a) && PyString_CheckExact(b)) { |
| 928 | if (a->ob_size == 0) { |
| 929 | Py_INCREF(bb); |
| 930 | return bb; |
| 931 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 932 | Py_INCREF(a); |
| 933 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 934 | } |
| 935 | size = a->ob_size + b->ob_size; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 936 | /* XXX check overflow */ |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 937 | /* Inline PyObject_NewVar */ |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 938 | op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 939 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 940 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 941 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 942 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 943 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 944 | memcpy(op->ob_sval, a->ob_sval, a->ob_size); |
| 945 | memcpy(op->ob_sval + a->ob_size, b->ob_sval, b->ob_size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 946 | op->ob_sval[size] = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 947 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 948 | #undef b |
| 949 | } |
| 950 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 951 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 952 | string_repeat(register PyStringObject *a, register Py_ssize_t n) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 953 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 954 | register Py_ssize_t i; |
| 955 | register Py_ssize_t j; |
| 956 | register Py_ssize_t size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 957 | register PyStringObject *op; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 958 | size_t nbytes; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 959 | if (n < 0) |
| 960 | n = 0; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 961 | /* watch out for overflows: the size can overflow int, |
| 962 | * and the # of bytes needed can overflow size_t |
| 963 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 964 | size = a->ob_size * n; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 965 | if (n && size / n != a->ob_size) { |
| 966 | PyErr_SetString(PyExc_OverflowError, |
| 967 | "repeated string is too long"); |
| 968 | return NULL; |
| 969 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 970 | if (size == a->ob_size && PyString_CheckExact(a)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 971 | Py_INCREF(a); |
| 972 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 973 | } |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 974 | nbytes = (size_t)size; |
| 975 | if (nbytes + sizeof(PyStringObject) <= nbytes) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 976 | PyErr_SetString(PyExc_OverflowError, |
| 977 | "repeated string is too long"); |
| 978 | return NULL; |
| 979 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 980 | op = (PyStringObject *) |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 981 | PyObject_MALLOC(sizeof(PyStringObject) + nbytes); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 982 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 983 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 984 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 985 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 986 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Raymond Hettinger | 0a2f849 | 2003-01-06 22:42:41 +0000 | [diff] [blame] | 987 | op->ob_sval[size] = '\0'; |
| 988 | if (a->ob_size == 1 && n > 0) { |
| 989 | memset(op->ob_sval, a->ob_sval[0] , n); |
| 990 | return (PyObject *) op; |
| 991 | } |
Raymond Hettinger | 698258a | 2003-01-06 10:33:56 +0000 | [diff] [blame] | 992 | i = 0; |
| 993 | if (i < size) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 994 | memcpy(op->ob_sval, a->ob_sval, a->ob_size); |
| 995 | i = a->ob_size; |
Raymond Hettinger | 698258a | 2003-01-06 10:33:56 +0000 | [diff] [blame] | 996 | } |
| 997 | while (i < size) { |
| 998 | j = (i <= size-i) ? i : size-i; |
| 999 | memcpy(op->ob_sval+i, op->ob_sval, j); |
| 1000 | i += j; |
| 1001 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1002 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ |
| 1006 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1007 | static PyObject * |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1008 | string_slice(register PyStringObject *a, register Py_ssize_t i, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1009 | register Py_ssize_t j) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1010 | /* j -- may be negative! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1011 | { |
| 1012 | if (i < 0) |
| 1013 | i = 0; |
| 1014 | if (j < 0) |
| 1015 | j = 0; /* Avoid signed/unsigned bug in next line */ |
| 1016 | if (j > a->ob_size) |
| 1017 | j = a->ob_size; |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1018 | if (i == 0 && j == a->ob_size && PyString_CheckExact(a)) { |
| 1019 | /* It's the same as a */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1020 | Py_INCREF(a); |
| 1021 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1022 | } |
| 1023 | if (j < i) |
| 1024 | j = i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1025 | return PyString_FromStringAndSize(a->ob_sval + i, j-i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1028 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1029 | string_contains(PyObject *a, PyObject *el) |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1030 | { |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 1031 | char *s = PyString_AS_STRING(a); |
| 1032 | const char *sub = PyString_AS_STRING(el); |
| 1033 | char *last; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1034 | Py_ssize_t len_sub = PyString_GET_SIZE(el); |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 1035 | Py_ssize_t shortsub; |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 1036 | char firstchar, lastchar; |
Guido van Rossum | bf935fd | 2002-08-24 06:57:49 +0000 | [diff] [blame] | 1037 | |
| 1038 | if (!PyString_CheckExact(el)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1039 | #ifdef Py_USING_UNICODE |
Guido van Rossum | bf935fd | 2002-08-24 06:57:49 +0000 | [diff] [blame] | 1040 | if (PyUnicode_Check(el)) |
| 1041 | return PyUnicode_Contains(a, el); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1042 | #endif |
Guido van Rossum | bf935fd | 2002-08-24 06:57:49 +0000 | [diff] [blame] | 1043 | if (!PyString_Check(el)) { |
| 1044 | PyErr_SetString(PyExc_TypeError, |
| 1045 | "'in <string>' requires string as left operand"); |
| 1046 | return -1; |
| 1047 | } |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1048 | } |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 1049 | |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 1050 | if (len_sub == 0) |
| 1051 | return 1; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1052 | /* last points to one char beyond the start of the rightmost |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 1053 | substring. When s<last, there is still room for a possible match |
| 1054 | and s[0] through s[len_sub-1] will be in bounds. |
| 1055 | shortsub is len_sub minus the last character which is checked |
| 1056 | separately just before the memcmp(). That check helps prevent |
| 1057 | false starts and saves the setup time for memcmp(). |
| 1058 | */ |
| 1059 | firstchar = sub[0]; |
| 1060 | shortsub = len_sub - 1; |
| 1061 | lastchar = sub[shortsub]; |
| 1062 | last = s + PyString_GET_SIZE(a) - len_sub + 1; |
| 1063 | while (s < last) { |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1064 | s = (char *)memchr(s, firstchar, last-s); |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 1065 | if (s == NULL) |
| 1066 | return 0; |
| 1067 | assert(s < last); |
| 1068 | if (s[shortsub] == lastchar && memcmp(s, sub, shortsub) == 0) |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1069 | return 1; |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 1070 | s++; |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1071 | } |
| 1072 | return 0; |
| 1073 | } |
| 1074 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1075 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1076 | string_item(PyStringObject *a, register Py_ssize_t i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1077 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1078 | PyObject *v; |
Tim Peters | 5b4d477 | 2001-05-08 22:33:50 +0000 | [diff] [blame] | 1079 | char *pchar; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1080 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1081 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1082 | return NULL; |
| 1083 | } |
Tim Peters | 5b4d477 | 2001-05-08 22:33:50 +0000 | [diff] [blame] | 1084 | pchar = a->ob_sval + i; |
Tim Peters | cf5ad5d | 2001-05-09 00:24:55 +0000 | [diff] [blame] | 1085 | v = (PyObject *)characters[*pchar & UCHAR_MAX]; |
Tim Peters | 5b4d477 | 2001-05-08 22:33:50 +0000 | [diff] [blame] | 1086 | if (v == NULL) |
| 1087 | v = PyString_FromStringAndSize(pchar, 1); |
Tim Peters | b4bbcd7 | 2001-05-09 00:31:40 +0000 | [diff] [blame] | 1088 | else { |
| 1089 | #ifdef COUNT_ALLOCS |
| 1090 | one_strings++; |
| 1091 | #endif |
Tim Peters | cf5ad5d | 2001-05-09 00:24:55 +0000 | [diff] [blame] | 1092 | Py_INCREF(v); |
Tim Peters | b4bbcd7 | 2001-05-09 00:31:40 +0000 | [diff] [blame] | 1093 | } |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 1094 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1097 | static PyObject* |
| 1098 | string_richcompare(PyStringObject *a, PyStringObject *b, int op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1099 | { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1100 | int c; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1101 | Py_ssize_t len_a, len_b; |
| 1102 | Py_ssize_t min_len; |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1103 | PyObject *result; |
| 1104 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 1105 | /* Make sure both arguments are strings. */ |
| 1106 | if (!(PyString_Check(a) && PyString_Check(b))) { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1107 | result = Py_NotImplemented; |
| 1108 | goto out; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1109 | } |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1110 | if (a == b) { |
| 1111 | switch (op) { |
| 1112 | case Py_EQ:case Py_LE:case Py_GE: |
| 1113 | result = Py_True; |
| 1114 | goto out; |
| 1115 | case Py_NE:case Py_LT:case Py_GT: |
| 1116 | result = Py_False; |
| 1117 | goto out; |
| 1118 | } |
| 1119 | } |
| 1120 | if (op == Py_EQ) { |
| 1121 | /* Supporting Py_NE here as well does not save |
| 1122 | much time, since Py_NE is rarely used. */ |
| 1123 | if (a->ob_size == b->ob_size |
| 1124 | && (a->ob_sval[0] == b->ob_sval[0] |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 1125 | && memcmp(a->ob_sval, b->ob_sval, |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1126 | a->ob_size) == 0)) { |
| 1127 | result = Py_True; |
| 1128 | } else { |
| 1129 | result = Py_False; |
| 1130 | } |
| 1131 | goto out; |
| 1132 | } |
| 1133 | len_a = a->ob_size; len_b = b->ob_size; |
| 1134 | min_len = (len_a < len_b) ? len_a : len_b; |
| 1135 | if (min_len > 0) { |
| 1136 | c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
| 1137 | if (c==0) |
| 1138 | c = memcmp(a->ob_sval, b->ob_sval, min_len); |
| 1139 | }else |
| 1140 | c = 0; |
| 1141 | if (c == 0) |
| 1142 | c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
| 1143 | switch (op) { |
| 1144 | case Py_LT: c = c < 0; break; |
| 1145 | case Py_LE: c = c <= 0; break; |
| 1146 | case Py_EQ: assert(0); break; /* unreachable */ |
| 1147 | case Py_NE: c = c != 0; break; |
| 1148 | case Py_GT: c = c > 0; break; |
| 1149 | case Py_GE: c = c >= 0; break; |
| 1150 | default: |
| 1151 | result = Py_NotImplemented; |
| 1152 | goto out; |
| 1153 | } |
| 1154 | result = c ? Py_True : Py_False; |
| 1155 | out: |
| 1156 | Py_INCREF(result); |
| 1157 | return result; |
| 1158 | } |
| 1159 | |
| 1160 | int |
| 1161 | _PyString_Eq(PyObject *o1, PyObject *o2) |
| 1162 | { |
| 1163 | PyStringObject *a, *b; |
| 1164 | a = (PyStringObject*)o1; |
| 1165 | b = (PyStringObject*)o2; |
| 1166 | return a->ob_size == b->ob_size |
| 1167 | && *a->ob_sval == *b->ob_sval |
| 1168 | && memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1171 | static long |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1172 | string_hash(PyStringObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1173 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1174 | register Py_ssize_t len; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1175 | register unsigned char *p; |
| 1176 | register long x; |
| 1177 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1178 | if (a->ob_shash != -1) |
| 1179 | return a->ob_shash; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1180 | len = a->ob_size; |
| 1181 | p = (unsigned char *) a->ob_sval; |
| 1182 | x = *p << 7; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1183 | while (--len >= 0) |
Guido van Rossum | eddcb3b | 1996-09-11 20:22:48 +0000 | [diff] [blame] | 1184 | x = (1000003*x) ^ *p++; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1185 | x ^= a->ob_size; |
| 1186 | if (x == -1) |
| 1187 | x = -2; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1188 | a->ob_shash = x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1189 | return x; |
| 1190 | } |
| 1191 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1192 | #define HASINDEX(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_HAVE_INDEX) |
| 1193 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1194 | static PyObject* |
| 1195 | string_subscript(PyStringObject* self, PyObject* item) |
| 1196 | { |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1197 | PyNumberMethods *nb = item->ob_type->tp_as_number; |
| 1198 | if (nb != NULL && HASINDEX(item) && nb->nb_index != NULL) { |
| 1199 | Py_ssize_t i = nb->nb_index(item); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1200 | if (i == -1 && PyErr_Occurred()) |
| 1201 | return NULL; |
| 1202 | if (i < 0) |
| 1203 | i += PyString_GET_SIZE(self); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1204 | return string_item(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1205 | } |
| 1206 | else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1207 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1208 | char* source_buf; |
| 1209 | char* result_buf; |
| 1210 | PyObject* result; |
| 1211 | |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1212 | if (PySlice_GetIndicesEx((PySliceObject*)item, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1213 | PyString_GET_SIZE(self), |
| 1214 | &start, &stop, &step, &slicelength) < 0) { |
| 1215 | return NULL; |
| 1216 | } |
| 1217 | |
| 1218 | if (slicelength <= 0) { |
| 1219 | return PyString_FromStringAndSize("", 0); |
| 1220 | } |
| 1221 | else { |
| 1222 | source_buf = PyString_AsString((PyObject*)self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1223 | result_buf = (char *)PyMem_Malloc(slicelength); |
Neal Norwitz | 95c1e50 | 2005-10-20 04:15:52 +0000 | [diff] [blame] | 1224 | if (result_buf == NULL) |
| 1225 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1226 | |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1227 | for (cur = start, i = 0; i < slicelength; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1228 | cur += step, i++) { |
| 1229 | result_buf[i] = source_buf[cur]; |
| 1230 | } |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1231 | |
| 1232 | result = PyString_FromStringAndSize(result_buf, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1233 | slicelength); |
| 1234 | PyMem_Free(result_buf); |
| 1235 | return result; |
| 1236 | } |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1237 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1238 | else { |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1239 | PyErr_SetString(PyExc_TypeError, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1240 | "string indices must be integers"); |
| 1241 | return NULL; |
| 1242 | } |
| 1243 | } |
| 1244 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1245 | static Py_ssize_t |
| 1246 | 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] | 1247 | { |
| 1248 | if ( index != 0 ) { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1249 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 1250 | "accessing non-existent string segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1251 | return -1; |
| 1252 | } |
| 1253 | *ptr = (void *)self->ob_sval; |
| 1254 | return self->ob_size; |
| 1255 | } |
| 1256 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1257 | static Py_ssize_t |
| 1258 | 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] | 1259 | { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1260 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 07d7800 | 1998-10-01 15:59:48 +0000 | [diff] [blame] | 1261 | "Cannot use string as modifiable buffer"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1262 | return -1; |
| 1263 | } |
| 1264 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1265 | static Py_ssize_t |
| 1266 | string_buffer_getsegcount(PyStringObject *self, Py_ssize_t *lenp) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1267 | { |
| 1268 | if ( lenp ) |
| 1269 | *lenp = self->ob_size; |
| 1270 | return 1; |
| 1271 | } |
| 1272 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1273 | static Py_ssize_t |
| 1274 | 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] | 1275 | { |
| 1276 | if ( index != 0 ) { |
| 1277 | PyErr_SetString(PyExc_SystemError, |
| 1278 | "accessing non-existent string segment"); |
| 1279 | return -1; |
| 1280 | } |
| 1281 | *ptr = self->ob_sval; |
| 1282 | return self->ob_size; |
| 1283 | } |
| 1284 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1285 | static PySequenceMethods string_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1286 | (lenfunc)string_length, /*sq_length*/ |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1287 | (binaryfunc)string_concat, /*sq_concat*/ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1288 | (ssizeargfunc)string_repeat, /*sq_repeat*/ |
| 1289 | (ssizeargfunc)string_item, /*sq_item*/ |
| 1290 | (ssizessizeargfunc)string_slice, /*sq_slice*/ |
Guido van Rossum | f380e66 | 1991-06-04 19:36:32 +0000 | [diff] [blame] | 1291 | 0, /*sq_ass_item*/ |
| 1292 | 0, /*sq_ass_slice*/ |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1293 | (objobjproc)string_contains /*sq_contains*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1294 | }; |
| 1295 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1296 | static PyMappingMethods string_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1297 | (lenfunc)string_length, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1298 | (binaryfunc)string_subscript, |
| 1299 | 0, |
| 1300 | }; |
| 1301 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1302 | static PyBufferProcs string_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1303 | (readbufferproc)string_buffer_getreadbuf, |
| 1304 | (writebufferproc)string_buffer_getwritebuf, |
| 1305 | (segcountproc)string_buffer_getsegcount, |
| 1306 | (charbufferproc)string_buffer_getcharbuf, |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1307 | }; |
| 1308 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1309 | |
| 1310 | |
| 1311 | #define LEFTSTRIP 0 |
| 1312 | #define RIGHTSTRIP 1 |
| 1313 | #define BOTHSTRIP 2 |
| 1314 | |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1315 | /* Arrays indexed by above */ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 1316 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 1317 | |
| 1318 | #define STRIPNAME(i) (stripformat[i]+3) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1319 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1320 | #define SPLIT_APPEND(data, left, right) \ |
| 1321 | str = PyString_FromStringAndSize((data) + (left), \ |
| 1322 | (right) - (left)); \ |
| 1323 | if (str == NULL) \ |
| 1324 | goto onError; \ |
| 1325 | if (PyList_Append(list, str)) { \ |
| 1326 | Py_DECREF(str); \ |
| 1327 | goto onError; \ |
| 1328 | } \ |
| 1329 | else \ |
| 1330 | Py_DECREF(str); |
| 1331 | |
| 1332 | #define SPLIT_INSERT(data, left, right) \ |
| 1333 | str = PyString_FromStringAndSize((data) + (left), \ |
| 1334 | (right) - (left)); \ |
| 1335 | if (str == NULL) \ |
| 1336 | goto onError; \ |
| 1337 | if (PyList_Insert(list, 0, str)) { \ |
| 1338 | Py_DECREF(str); \ |
| 1339 | goto onError; \ |
| 1340 | } \ |
| 1341 | else \ |
| 1342 | Py_DECREF(str); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1343 | |
| 1344 | static PyObject * |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1345 | split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1346 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1347 | Py_ssize_t i, j; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1348 | PyObject *str; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1349 | PyObject *list = PyList_New(0); |
| 1350 | |
| 1351 | if (list == NULL) |
| 1352 | return NULL; |
| 1353 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1354 | for (i = j = 0; i < len; ) { |
| 1355 | while (i < len && isspace(Py_CHARMASK(s[i]))) |
| 1356 | i++; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1357 | j = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1358 | while (i < len && !isspace(Py_CHARMASK(s[i]))) |
| 1359 | i++; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1360 | if (j < i) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1361 | if (maxsplit-- <= 0) |
| 1362 | break; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1363 | SPLIT_APPEND(s, j, i); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1364 | while (i < len && isspace(Py_CHARMASK(s[i]))) |
| 1365 | i++; |
| 1366 | j = i; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1367 | } |
| 1368 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1369 | if (j < len) { |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1370 | SPLIT_APPEND(s, j, len); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1371 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1372 | return list; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1373 | onError: |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1374 | Py_DECREF(list); |
| 1375 | return NULL; |
| 1376 | } |
| 1377 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1378 | static PyObject * |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1379 | split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1380 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1381 | register Py_ssize_t i, j; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1382 | PyObject *str; |
| 1383 | PyObject *list = PyList_New(0); |
| 1384 | |
| 1385 | if (list == NULL) |
| 1386 | return NULL; |
| 1387 | |
| 1388 | for (i = j = 0; i < len; ) { |
| 1389 | if (s[i] == ch) { |
| 1390 | if (maxcount-- <= 0) |
| 1391 | break; |
| 1392 | SPLIT_APPEND(s, j, i); |
| 1393 | i = j = i + 1; |
| 1394 | } else |
| 1395 | i++; |
| 1396 | } |
| 1397 | if (j <= len) { |
| 1398 | SPLIT_APPEND(s, j, len); |
| 1399 | } |
| 1400 | return list; |
| 1401 | |
| 1402 | onError: |
| 1403 | Py_DECREF(list); |
| 1404 | return NULL; |
| 1405 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1406 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1407 | PyDoc_STRVAR(split__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1408 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 1409 | \n\ |
| 1410 | 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] | 1411 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Raymond Hettinger | bc552ce | 2002-08-05 06:28:21 +0000 | [diff] [blame] | 1412 | splits are done. If sep is not specified or is None, any\n\ |
| 1413 | whitespace string is a separator."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1414 | |
| 1415 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1416 | string_split(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1417 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1418 | Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; |
| 1419 | int err; |
Martin v. Löwis | 9c83076 | 2006-04-13 08:37:17 +0000 | [diff] [blame] | 1420 | Py_ssize_t maxsplit = -1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1421 | const char *s = PyString_AS_STRING(self), *sub; |
| 1422 | PyObject *list, *item, *subobj = Py_None; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1423 | |
Martin v. Löwis | 9c83076 | 2006-04-13 08:37:17 +0000 | [diff] [blame] | 1424 | if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1425 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1426 | if (maxsplit < 0) |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1427 | maxsplit = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1428 | if (subobj == Py_None) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1429 | return split_whitespace(s, len, maxsplit); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1430 | if (PyString_Check(subobj)) { |
| 1431 | sub = PyString_AS_STRING(subobj); |
| 1432 | n = PyString_GET_SIZE(subobj); |
| 1433 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1434 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1435 | else if (PyUnicode_Check(subobj)) |
| 1436 | return PyUnicode_Split((PyObject *)self, subobj, maxsplit); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1437 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1438 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 1439 | return NULL; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1440 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1441 | if (n == 0) { |
| 1442 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 1443 | return NULL; |
| 1444 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1445 | else if (n == 1) |
| 1446 | return split_char(s, len, sub[0], maxsplit); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1447 | |
| 1448 | list = PyList_New(0); |
| 1449 | if (list == NULL) |
| 1450 | return NULL; |
| 1451 | |
| 1452 | i = j = 0; |
| 1453 | while (i+n <= len) { |
Fred Drake | 396f6e0 | 2000-06-20 15:47:54 +0000 | [diff] [blame] | 1454 | if (s[i] == sub[0] && memcmp(s+i, sub, n) == 0) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1455 | if (maxsplit-- <= 0) |
| 1456 | break; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1457 | item = PyString_FromStringAndSize(s+j, i-j); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1458 | if (item == NULL) |
| 1459 | goto fail; |
| 1460 | err = PyList_Append(list, item); |
| 1461 | Py_DECREF(item); |
| 1462 | if (err < 0) |
| 1463 | goto fail; |
| 1464 | i = j = i + n; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1465 | } |
| 1466 | else |
| 1467 | i++; |
| 1468 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1469 | item = PyString_FromStringAndSize(s+j, len-j); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1470 | if (item == NULL) |
| 1471 | goto fail; |
| 1472 | err = PyList_Append(list, item); |
| 1473 | Py_DECREF(item); |
| 1474 | if (err < 0) |
| 1475 | goto fail; |
| 1476 | |
| 1477 | return list; |
| 1478 | |
| 1479 | fail: |
| 1480 | Py_DECREF(list); |
| 1481 | return NULL; |
| 1482 | } |
| 1483 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1484 | static PyObject * |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1485 | rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1486 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1487 | Py_ssize_t i, j; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1488 | PyObject *str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1489 | PyObject *list = PyList_New(0); |
| 1490 | |
| 1491 | if (list == NULL) |
| 1492 | return NULL; |
| 1493 | |
| 1494 | for (i = j = len - 1; i >= 0; ) { |
| 1495 | while (i >= 0 && isspace(Py_CHARMASK(s[i]))) |
| 1496 | i--; |
| 1497 | j = i; |
| 1498 | while (i >= 0 && !isspace(Py_CHARMASK(s[i]))) |
| 1499 | i--; |
| 1500 | if (j > i) { |
| 1501 | if (maxsplit-- <= 0) |
| 1502 | break; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1503 | SPLIT_INSERT(s, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1504 | while (i >= 0 && isspace(Py_CHARMASK(s[i]))) |
| 1505 | i--; |
| 1506 | j = i; |
| 1507 | } |
| 1508 | } |
| 1509 | if (j >= 0) { |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1510 | SPLIT_INSERT(s, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1511 | } |
| 1512 | return list; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1513 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1514 | Py_DECREF(list); |
| 1515 | return NULL; |
| 1516 | } |
| 1517 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1518 | static PyObject * |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1519 | rsplit_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1520 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1521 | register Py_ssize_t i, j; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1522 | PyObject *str; |
| 1523 | PyObject *list = PyList_New(0); |
| 1524 | |
| 1525 | if (list == NULL) |
| 1526 | return NULL; |
| 1527 | |
| 1528 | for (i = j = len - 1; i >= 0; ) { |
| 1529 | if (s[i] == ch) { |
| 1530 | if (maxcount-- <= 0) |
| 1531 | break; |
| 1532 | SPLIT_INSERT(s, i + 1, j + 1); |
| 1533 | j = i = i - 1; |
| 1534 | } else |
| 1535 | i--; |
| 1536 | } |
| 1537 | if (j >= -1) { |
| 1538 | SPLIT_INSERT(s, 0, j + 1); |
| 1539 | } |
| 1540 | return list; |
| 1541 | |
| 1542 | onError: |
| 1543 | Py_DECREF(list); |
| 1544 | return NULL; |
| 1545 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1546 | |
| 1547 | PyDoc_STRVAR(rsplit__doc__, |
| 1548 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 1549 | \n\ |
| 1550 | Return a list of the words in the string S, using sep as the\n\ |
| 1551 | delimiter string, starting at the end of the string and working\n\ |
| 1552 | to the front. If maxsplit is given, at most maxsplit splits are\n\ |
| 1553 | done. If sep is not specified or is None, any whitespace string\n\ |
| 1554 | is a separator."); |
| 1555 | |
| 1556 | static PyObject * |
| 1557 | string_rsplit(PyStringObject *self, PyObject *args) |
| 1558 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1559 | Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; |
| 1560 | int err; |
Martin v. Löwis | 9c83076 | 2006-04-13 08:37:17 +0000 | [diff] [blame] | 1561 | Py_ssize_t maxsplit = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1562 | const char *s = PyString_AS_STRING(self), *sub; |
| 1563 | PyObject *list, *item, *subobj = Py_None; |
| 1564 | |
Martin v. Löwis | 9c83076 | 2006-04-13 08:37:17 +0000 | [diff] [blame] | 1565 | if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1566 | return NULL; |
| 1567 | if (maxsplit < 0) |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1568 | maxsplit = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1569 | if (subobj == Py_None) |
| 1570 | return rsplit_whitespace(s, len, maxsplit); |
| 1571 | if (PyString_Check(subobj)) { |
| 1572 | sub = PyString_AS_STRING(subobj); |
| 1573 | n = PyString_GET_SIZE(subobj); |
| 1574 | } |
| 1575 | #ifdef Py_USING_UNICODE |
| 1576 | else if (PyUnicode_Check(subobj)) |
| 1577 | return PyUnicode_RSplit((PyObject *)self, subobj, maxsplit); |
| 1578 | #endif |
| 1579 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 1580 | return NULL; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1581 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1582 | if (n == 0) { |
| 1583 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 1584 | return NULL; |
| 1585 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1586 | else if (n == 1) |
| 1587 | return rsplit_char(s, len, sub[0], maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1588 | |
| 1589 | list = PyList_New(0); |
| 1590 | if (list == NULL) |
| 1591 | return NULL; |
| 1592 | |
| 1593 | j = len; |
| 1594 | i = j - n; |
| 1595 | while (i >= 0) { |
| 1596 | if (s[i] == sub[0] && memcmp(s+i, sub, n) == 0) { |
| 1597 | if (maxsplit-- <= 0) |
| 1598 | break; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1599 | item = PyString_FromStringAndSize(s+i+n, j-i-n); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1600 | if (item == NULL) |
| 1601 | goto fail; |
| 1602 | err = PyList_Insert(list, 0, item); |
| 1603 | Py_DECREF(item); |
| 1604 | if (err < 0) |
| 1605 | goto fail; |
| 1606 | j = i; |
| 1607 | i -= n; |
| 1608 | } |
| 1609 | else |
| 1610 | i--; |
| 1611 | } |
| 1612 | item = PyString_FromStringAndSize(s, j); |
| 1613 | if (item == NULL) |
| 1614 | goto fail; |
| 1615 | err = PyList_Insert(list, 0, item); |
| 1616 | Py_DECREF(item); |
| 1617 | if (err < 0) |
| 1618 | goto fail; |
| 1619 | |
| 1620 | return list; |
| 1621 | |
| 1622 | fail: |
| 1623 | Py_DECREF(list); |
| 1624 | return NULL; |
| 1625 | } |
| 1626 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1627 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1628 | PyDoc_STRVAR(join__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1629 | "S.join(sequence) -> string\n\ |
| 1630 | \n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1631 | 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] | 1632 | sequence. The separator between elements is S."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1633 | |
| 1634 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1635 | string_join(PyStringObject *self, PyObject *orig) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1636 | { |
| 1637 | char *sep = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1638 | const Py_ssize_t seplen = PyString_GET_SIZE(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1639 | PyObject *res = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1640 | char *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1641 | Py_ssize_t seqlen = 0; |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1642 | size_t sz = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1643 | Py_ssize_t i; |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1644 | PyObject *seq, *item; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1645 | |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1646 | seq = PySequence_Fast(orig, ""); |
| 1647 | if (seq == NULL) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1648 | return NULL; |
| 1649 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1650 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1651 | seqlen = PySequence_Size(seq); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1652 | if (seqlen == 0) { |
| 1653 | Py_DECREF(seq); |
| 1654 | return PyString_FromString(""); |
| 1655 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1656 | if (seqlen == 1) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1657 | item = PySequence_Fast_GET_ITEM(seq, 0); |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1658 | if (PyString_CheckExact(item) || PyUnicode_CheckExact(item)) { |
| 1659 | Py_INCREF(item); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1660 | Py_DECREF(seq); |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1661 | return item; |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1662 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1663 | } |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1664 | |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1665 | /* 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] | 1666 | * of the builtin types in the sequence. |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1667 | * Do a pre-pass to figure out the total amount of space we'll |
| 1668 | * need (sz), see whether any argument is absurd, and defer to |
| 1669 | * the Unicode join if appropriate. |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1670 | */ |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1671 | for (i = 0; i < seqlen; i++) { |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1672 | const size_t old_sz = sz; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1673 | item = PySequence_Fast_GET_ITEM(seq, i); |
| 1674 | if (!PyString_Check(item)){ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1675 | #ifdef Py_USING_UNICODE |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1676 | if (PyUnicode_Check(item)) { |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 1677 | /* Defer to Unicode join. |
| 1678 | * CAUTION: There's no gurantee that the |
| 1679 | * original sequence can be iterated over |
| 1680 | * again, so we must pass seq here. |
| 1681 | */ |
| 1682 | PyObject *result; |
| 1683 | result = PyUnicode_Join((PyObject *)self, seq); |
Barry Warsaw | 771d067 | 2000-07-11 04:58:12 +0000 | [diff] [blame] | 1684 | Py_DECREF(seq); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 1685 | return result; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1686 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1687 | #endif |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1688 | PyErr_Format(PyExc_TypeError, |
Neal Norwitz | 0e2cbab | 2006-04-17 05:56:32 +0000 | [diff] [blame] | 1689 | "sequence item %zd: expected string," |
Jeremy Hylton | 88887aa | 2000-07-11 20:55:38 +0000 | [diff] [blame] | 1690 | " %.80s found", |
Neal Norwitz | 0e2cbab | 2006-04-17 05:56:32 +0000 | [diff] [blame] | 1691 | i, item->ob_type->tp_name); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1692 | Py_DECREF(seq); |
| 1693 | return NULL; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1694 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1695 | sz += PyString_GET_SIZE(item); |
| 1696 | if (i != 0) |
| 1697 | sz += seplen; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1698 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1699 | PyErr_SetString(PyExc_OverflowError, |
| 1700 | "join() is too long for a Python string"); |
| 1701 | Py_DECREF(seq); |
| 1702 | return NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1703 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
| 1706 | /* Allocate result space. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1707 | res = PyString_FromStringAndSize((char*)NULL, sz); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1708 | if (res == NULL) { |
| 1709 | Py_DECREF(seq); |
| 1710 | return NULL; |
| 1711 | } |
| 1712 | |
| 1713 | /* Catenate everything. */ |
| 1714 | p = PyString_AS_STRING(res); |
| 1715 | for (i = 0; i < seqlen; ++i) { |
| 1716 | size_t n; |
| 1717 | item = PySequence_Fast_GET_ITEM(seq, i); |
| 1718 | n = PyString_GET_SIZE(item); |
| 1719 | memcpy(p, PyString_AS_STRING(item), n); |
| 1720 | p += n; |
| 1721 | if (i < seqlen - 1) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1722 | memcpy(p, sep, seplen); |
| 1723 | p += seplen; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1724 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1725 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1726 | |
Jeremy Hylton | 4904829 | 2000-07-11 03:28:17 +0000 | [diff] [blame] | 1727 | Py_DECREF(seq); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1728 | return res; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Tim Peters | 52e155e | 2001-06-16 05:42:57 +0000 | [diff] [blame] | 1731 | PyObject * |
| 1732 | _PyString_Join(PyObject *sep, PyObject *x) |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1733 | { |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1734 | assert(sep != NULL && PyString_Check(sep)); |
| 1735 | assert(x != NULL); |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1736 | return string_join((PyStringObject *)sep, x); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 1739 | static void |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1740 | 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] | 1741 | { |
| 1742 | if (*end > len) |
| 1743 | *end = len; |
| 1744 | else if (*end < 0) |
| 1745 | *end += len; |
| 1746 | if (*end < 0) |
| 1747 | *end = 0; |
| 1748 | if (*start < 0) |
| 1749 | *start += len; |
| 1750 | if (*start < 0) |
| 1751 | *start = 0; |
| 1752 | } |
| 1753 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1754 | static Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1755 | string_find_internal(PyStringObject *self, PyObject *args, int dir) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1756 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1757 | const char *s = PyString_AS_STRING(self), *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1758 | Py_ssize_t len = PyString_GET_SIZE(self); |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1759 | Py_ssize_t n, i = 0, last = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1760 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1761 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1762 | /* XXX ssize_t i */ |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1763 | if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 1764 | &subobj, _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1765 | return -2; |
| 1766 | if (PyString_Check(subobj)) { |
| 1767 | sub = PyString_AS_STRING(subobj); |
| 1768 | n = PyString_GET_SIZE(subobj); |
| 1769 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1770 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1771 | else if (PyUnicode_Check(subobj)) |
Guido van Rossum | 76afbd9 | 2002-08-20 17:29:29 +0000 | [diff] [blame] | 1772 | return PyUnicode_Find((PyObject *)self, subobj, i, last, dir); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1773 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1774 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1775 | return -2; |
| 1776 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 1777 | string_adjust_indices(&i, &last, len); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1778 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1779 | if (dir > 0) { |
| 1780 | if (n == 0 && i <= last) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1781 | return (long)i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1782 | last -= n; |
| 1783 | for (; i <= last; ++i) |
Fred Drake | 396f6e0 | 2000-06-20 15:47:54 +0000 | [diff] [blame] | 1784 | if (s[i] == sub[0] && memcmp(&s[i], sub, n) == 0) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1785 | return (long)i; |
| 1786 | } |
| 1787 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1788 | Py_ssize_t j; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1789 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1790 | if (n == 0 && i <= last) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1791 | return last; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1792 | for (j = last-n; j >= i; --j) |
Fred Drake | 396f6e0 | 2000-06-20 15:47:54 +0000 | [diff] [blame] | 1793 | if (s[j] == sub[0] && memcmp(&s[j], sub, n) == 0) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1794 | return j; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1795 | } |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1796 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1797 | return -1; |
| 1798 | } |
| 1799 | |
| 1800 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1801 | PyDoc_STRVAR(find__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1802 | "S.find(sub [,start [,end]]) -> int\n\ |
| 1803 | \n\ |
| 1804 | Return the lowest index in S where substring sub is found,\n\ |
| 1805 | such that sub is contained within s[start,end]. Optional\n\ |
| 1806 | arguments start and end are interpreted as in slice notation.\n\ |
| 1807 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1808 | Return -1 on failure."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1809 | |
| 1810 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1811 | string_find(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1812 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1813 | Py_ssize_t result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1814 | if (result == -2) |
| 1815 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1816 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1820 | PyDoc_STRVAR(index__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1821 | "S.index(sub [,start [,end]]) -> int\n\ |
| 1822 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1823 | Like S.find() but raise ValueError when the substring is not found."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1824 | |
| 1825 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1826 | string_index(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1827 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1828 | Py_ssize_t result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1829 | if (result == -2) |
| 1830 | return NULL; |
| 1831 | if (result == -1) { |
| 1832 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 5d5e7c0 | 2003-01-15 05:32:57 +0000 | [diff] [blame] | 1833 | "substring not found"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1834 | return NULL; |
| 1835 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1836 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1840 | PyDoc_STRVAR(rfind__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1841 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 1842 | \n\ |
| 1843 | Return the highest index in S where substring sub is found,\n\ |
| 1844 | such that sub is contained within s[start,end]. Optional\n\ |
| 1845 | arguments start and end are interpreted as in slice notation.\n\ |
| 1846 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1847 | Return -1 on failure."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1848 | |
| 1849 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1850 | string_rfind(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1851 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1852 | Py_ssize_t result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1853 | if (result == -2) |
| 1854 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1855 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1859 | PyDoc_STRVAR(rindex__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1860 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 1861 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1862 | Like S.rfind() but raise ValueError when the substring is not found."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1863 | |
| 1864 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1865 | string_rindex(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1866 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1867 | Py_ssize_t result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1868 | if (result == -2) |
| 1869 | return NULL; |
| 1870 | if (result == -1) { |
| 1871 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 5d5e7c0 | 2003-01-15 05:32:57 +0000 | [diff] [blame] | 1872 | "substring not found"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1873 | return NULL; |
| 1874 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1875 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
| 1878 | |
| 1879 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1880 | do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj) |
| 1881 | { |
| 1882 | char *s = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1883 | Py_ssize_t len = PyString_GET_SIZE(self); |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1884 | char *sep = PyString_AS_STRING(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1885 | Py_ssize_t seplen = PyString_GET_SIZE(sepobj); |
| 1886 | Py_ssize_t i, j; |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1887 | |
| 1888 | i = 0; |
| 1889 | if (striptype != RIGHTSTRIP) { |
| 1890 | while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) { |
| 1891 | i++; |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | j = len; |
| 1896 | if (striptype != LEFTSTRIP) { |
| 1897 | do { |
| 1898 | j--; |
| 1899 | } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen)); |
| 1900 | j++; |
| 1901 | } |
| 1902 | |
| 1903 | if (i == 0 && j == len && PyString_CheckExact(self)) { |
| 1904 | Py_INCREF(self); |
| 1905 | return (PyObject*)self; |
| 1906 | } |
| 1907 | else |
| 1908 | return PyString_FromStringAndSize(s+i, j-i); |
| 1909 | } |
| 1910 | |
| 1911 | |
| 1912 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1913 | do_strip(PyStringObject *self, int striptype) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1914 | { |
| 1915 | char *s = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1916 | Py_ssize_t len = PyString_GET_SIZE(self), i, j; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1917 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1918 | i = 0; |
| 1919 | if (striptype != RIGHTSTRIP) { |
| 1920 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
| 1921 | i++; |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | j = len; |
| 1926 | if (striptype != LEFTSTRIP) { |
| 1927 | do { |
| 1928 | j--; |
| 1929 | } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
| 1930 | j++; |
| 1931 | } |
| 1932 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1933 | if (i == 0 && j == len && PyString_CheckExact(self)) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1934 | Py_INCREF(self); |
| 1935 | return (PyObject*)self; |
| 1936 | } |
| 1937 | else |
| 1938 | return PyString_FromStringAndSize(s+i, j-i); |
| 1939 | } |
| 1940 | |
| 1941 | |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1942 | static PyObject * |
| 1943 | do_argstrip(PyStringObject *self, int striptype, PyObject *args) |
| 1944 | { |
| 1945 | PyObject *sep = NULL; |
| 1946 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 1947 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1948 | return NULL; |
| 1949 | |
| 1950 | if (sep != NULL && sep != Py_None) { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 1951 | if (PyString_Check(sep)) |
| 1952 | return do_xstrip(self, striptype, sep); |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 1953 | #ifdef Py_USING_UNICODE |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 1954 | else if (PyUnicode_Check(sep)) { |
| 1955 | PyObject *uniself = PyUnicode_FromObject((PyObject *)self); |
| 1956 | PyObject *res; |
| 1957 | if (uniself==NULL) |
| 1958 | return NULL; |
| 1959 | res = _PyUnicode_XStrip((PyUnicodeObject *)uniself, |
| 1960 | striptype, sep); |
| 1961 | Py_DECREF(uniself); |
| 1962 | return res; |
| 1963 | } |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 1964 | #endif |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 1965 | PyErr_Format(PyExc_TypeError, |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 1966 | #ifdef Py_USING_UNICODE |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 1967 | "%s arg must be None, str or unicode", |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 1968 | #else |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 1969 | "%s arg must be None or str", |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 1970 | #endif |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 1971 | STRIPNAME(striptype)); |
| 1972 | return NULL; |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | return do_strip(self, striptype); |
| 1976 | } |
| 1977 | |
| 1978 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1979 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 1980 | "S.strip([chars]) -> string or unicode\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1981 | \n\ |
| 1982 | 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] | 1983 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 1984 | If chars is given and not None, remove characters in chars instead.\n\ |
| 1985 | If chars is unicode, S will be converted to unicode before stripping"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1986 | |
| 1987 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1988 | string_strip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1989 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1990 | if (PyTuple_GET_SIZE(args) == 0) |
| 1991 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 1992 | else |
| 1993 | return do_argstrip(self, BOTHSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1997 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 1998 | "S.lstrip([chars]) -> string or unicode\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1999 | \n\ |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2000 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2001 | If chars is given and not None, remove characters in chars instead.\n\ |
| 2002 | If chars is unicode, S will be converted to unicode before stripping"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2003 | |
| 2004 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2005 | string_lstrip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2006 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2007 | if (PyTuple_GET_SIZE(args) == 0) |
| 2008 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 2009 | else |
| 2010 | return do_argstrip(self, LEFTSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2014 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2015 | "S.rstrip([chars]) -> string or unicode\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2016 | \n\ |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2017 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2018 | If chars is given and not None, remove characters in chars instead.\n\ |
| 2019 | If chars is unicode, S will be converted to unicode before stripping"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2020 | |
| 2021 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2022 | string_rstrip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2023 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2024 | if (PyTuple_GET_SIZE(args) == 0) |
| 2025 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 2026 | else |
| 2027 | return do_argstrip(self, RIGHTSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2031 | PyDoc_STRVAR(lower__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2032 | "S.lower() -> string\n\ |
| 2033 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2034 | Return a copy of the string S converted to lowercase."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2035 | |
Fredrik Lundh | dfe503d | 2006-05-25 16:10:12 +0000 | [diff] [blame] | 2036 | /* _tolower and _toupper are defined by SUSv2, but they're not ISO C */ |
| 2037 | #ifndef _tolower |
| 2038 | #define _tolower tolower |
| 2039 | #endif |
| 2040 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2041 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2042 | string_lower(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2043 | { |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2044 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2045 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2046 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2047 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2048 | newobj = PyString_FromStringAndSize(NULL, n); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2049 | if (!newobj) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2050 | return NULL; |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2051 | |
| 2052 | s = PyString_AS_STRING(newobj); |
| 2053 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2054 | memcpy(s, PyString_AS_STRING(self), n); |
| 2055 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2056 | for (i = 0; i < n; i++) { |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2057 | int c = Py_CHARMASK(s[i]); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2058 | if (isupper(c)) |
| 2059 | s[i] = _tolower(c); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2060 | } |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2061 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2062 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2063 | } |
| 2064 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2065 | PyDoc_STRVAR(upper__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2066 | "S.upper() -> string\n\ |
| 2067 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2068 | Return a copy of the string S converted to uppercase."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2069 | |
Fredrik Lundh | dfe503d | 2006-05-25 16:10:12 +0000 | [diff] [blame] | 2070 | #ifndef _toupper |
| 2071 | #define _toupper toupper |
| 2072 | #endif |
| 2073 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2074 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2075 | string_upper(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2076 | { |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2077 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2078 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2079 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2080 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2081 | newobj = PyString_FromStringAndSize(NULL, n); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2082 | if (!newobj) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2083 | return NULL; |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2084 | |
| 2085 | s = PyString_AS_STRING(newobj); |
| 2086 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2087 | memcpy(s, PyString_AS_STRING(self), n); |
| 2088 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2089 | for (i = 0; i < n; i++) { |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2090 | int c = Py_CHARMASK(s[i]); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2091 | if (islower(c)) |
| 2092 | s[i] = _toupper(c); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2093 | } |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2094 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2095 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2098 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2099 | "S.title() -> string\n\ |
| 2100 | \n\ |
| 2101 | 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] | 2102 | characters, all remaining cased characters have lowercase."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2103 | |
| 2104 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2105 | string_title(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2106 | { |
| 2107 | char *s = PyString_AS_STRING(self), *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2108 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2109 | int previous_is_cased = 0; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2110 | PyObject *newobj; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2111 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2112 | newobj = PyString_FromStringAndSize(NULL, n); |
| 2113 | if (newobj == NULL) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2114 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2115 | s_new = PyString_AsString(newobj); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2116 | for (i = 0; i < n; i++) { |
| 2117 | int c = Py_CHARMASK(*s++); |
| 2118 | if (islower(c)) { |
| 2119 | if (!previous_is_cased) |
| 2120 | c = toupper(c); |
| 2121 | previous_is_cased = 1; |
| 2122 | } else if (isupper(c)) { |
| 2123 | if (previous_is_cased) |
| 2124 | c = tolower(c); |
| 2125 | previous_is_cased = 1; |
| 2126 | } else |
| 2127 | previous_is_cased = 0; |
| 2128 | *s_new++ = c; |
| 2129 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2130 | return newobj; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2133 | PyDoc_STRVAR(capitalize__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2134 | "S.capitalize() -> string\n\ |
| 2135 | \n\ |
| 2136 | 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] | 2137 | capitalized."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2138 | |
| 2139 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2140 | string_capitalize(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2141 | { |
| 2142 | char *s = PyString_AS_STRING(self), *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2143 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2144 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2145 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2146 | newobj = PyString_FromStringAndSize(NULL, n); |
| 2147 | if (newobj == NULL) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2148 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2149 | s_new = PyString_AsString(newobj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2150 | if (0 < n) { |
| 2151 | int c = Py_CHARMASK(*s++); |
| 2152 | if (islower(c)) |
| 2153 | *s_new = toupper(c); |
| 2154 | else |
| 2155 | *s_new = c; |
| 2156 | s_new++; |
| 2157 | } |
| 2158 | for (i = 1; i < n; i++) { |
| 2159 | int c = Py_CHARMASK(*s++); |
| 2160 | if (isupper(c)) |
| 2161 | *s_new = tolower(c); |
| 2162 | else |
| 2163 | *s_new = c; |
| 2164 | s_new++; |
| 2165 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2166 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2170 | PyDoc_STRVAR(count__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2171 | "S.count(sub[, start[, end]]) -> int\n\ |
| 2172 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 2173 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 2174 | string S[start:end]. Optional arguments start and end are interpreted\n\ |
| 2175 | as in slice notation."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2176 | |
| 2177 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2178 | string_count(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2179 | { |
Raymond Hettinger | 57e7447 | 2005-02-20 09:54:53 +0000 | [diff] [blame] | 2180 | const char *s = PyString_AS_STRING(self), *sub, *t; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2181 | Py_ssize_t len = PyString_GET_SIZE(self), n; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 2182 | Py_ssize_t i = 0, last = PY_SSIZE_T_MAX; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2183 | Py_ssize_t m, r; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2184 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2185 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 2186 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &subobj, |
| 2187 | _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2188 | return NULL; |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 2189 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2190 | if (PyString_Check(subobj)) { |
| 2191 | sub = PyString_AS_STRING(subobj); |
| 2192 | n = PyString_GET_SIZE(subobj); |
| 2193 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2194 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2195 | else if (PyUnicode_Check(subobj)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2196 | Py_ssize_t count; |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2197 | count = PyUnicode_Count((PyObject *)self, subobj, i, last); |
| 2198 | if (count == -1) |
| 2199 | return NULL; |
| 2200 | else |
| 2201 | return PyInt_FromLong((long) count); |
| 2202 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2203 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2204 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 2205 | return NULL; |
| 2206 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 2207 | string_adjust_indices(&i, &last, len); |
| 2208 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2209 | m = last + 1 - n; |
| 2210 | if (n == 0) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2211 | return PyInt_FromSsize_t(m-i); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2212 | |
| 2213 | r = 0; |
| 2214 | while (i < m) { |
| 2215 | if (!memcmp(s+i, sub, n)) { |
| 2216 | r++; |
| 2217 | i += n; |
| 2218 | } else { |
| 2219 | i++; |
| 2220 | } |
Raymond Hettinger | 57e7447 | 2005-02-20 09:54:53 +0000 | [diff] [blame] | 2221 | if (i >= m) |
| 2222 | break; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2223 | t = (const char *)memchr(s+i, sub[0], m-i); |
Raymond Hettinger | 57e7447 | 2005-02-20 09:54:53 +0000 | [diff] [blame] | 2224 | if (t == NULL) |
| 2225 | break; |
| 2226 | i = t - s; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2227 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2228 | return PyInt_FromSsize_t(r); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2231 | PyDoc_STRVAR(swapcase__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2232 | "S.swapcase() -> string\n\ |
| 2233 | \n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2234 | 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] | 2235 | converted to lowercase and vice versa."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2236 | |
| 2237 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2238 | string_swapcase(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2239 | { |
| 2240 | char *s = PyString_AS_STRING(self), *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2241 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2242 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2243 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2244 | newobj = PyString_FromStringAndSize(NULL, n); |
| 2245 | if (newobj == NULL) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2246 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2247 | s_new = PyString_AsString(newobj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2248 | for (i = 0; i < n; i++) { |
| 2249 | int c = Py_CHARMASK(*s++); |
| 2250 | if (islower(c)) { |
| 2251 | *s_new = toupper(c); |
| 2252 | } |
| 2253 | else if (isupper(c)) { |
| 2254 | *s_new = tolower(c); |
| 2255 | } |
| 2256 | else |
| 2257 | *s_new = c; |
| 2258 | s_new++; |
| 2259 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2260 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2264 | PyDoc_STRVAR(translate__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2265 | "S.translate(table [,deletechars]) -> string\n\ |
| 2266 | \n\ |
| 2267 | Return a copy of the string S, where all characters occurring\n\ |
| 2268 | in the optional argument deletechars are removed, and the\n\ |
| 2269 | remaining characters have been mapped through the given\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2270 | translation table, which must be a string of length 256."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2271 | |
| 2272 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2273 | string_translate(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2274 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2275 | register char *input, *output; |
| 2276 | register const char *table; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2277 | register Py_ssize_t i, c, changed = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2278 | PyObject *input_obj = (PyObject*)self; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2279 | const char *table1, *output_start, *del_table=NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2280 | Py_ssize_t inlen, tablen, dellen = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2281 | PyObject *result; |
| 2282 | int trans_table[256]; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2283 | PyObject *tableobj, *delobj = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2284 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 2285 | if (!PyArg_UnpackTuple(args, "translate", 1, 2, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2286 | &tableobj, &delobj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2287 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2288 | |
| 2289 | if (PyString_Check(tableobj)) { |
| 2290 | table1 = PyString_AS_STRING(tableobj); |
| 2291 | tablen = PyString_GET_SIZE(tableobj); |
| 2292 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2293 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2294 | else if (PyUnicode_Check(tableobj)) { |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2295 | /* Unicode .translate() does not support the deletechars |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2296 | parameter; instead a mapping to None will cause characters |
| 2297 | to be deleted. */ |
| 2298 | if (delobj != NULL) { |
| 2299 | PyErr_SetString(PyExc_TypeError, |
| 2300 | "deletions are implemented differently for unicode"); |
| 2301 | return NULL; |
| 2302 | } |
| 2303 | return PyUnicode_Translate((PyObject *)self, tableobj, NULL); |
| 2304 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2305 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2306 | else if (PyObject_AsCharBuffer(tableobj, &table1, &tablen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2307 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2308 | |
Martin v. Löwis | 00b6127 | 2002-12-12 20:03:19 +0000 | [diff] [blame] | 2309 | if (tablen != 256) { |
| 2310 | PyErr_SetString(PyExc_ValueError, |
| 2311 | "translation table must be 256 characters long"); |
| 2312 | return NULL; |
| 2313 | } |
| 2314 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2315 | if (delobj != NULL) { |
| 2316 | if (PyString_Check(delobj)) { |
| 2317 | del_table = PyString_AS_STRING(delobj); |
| 2318 | dellen = PyString_GET_SIZE(delobj); |
| 2319 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2320 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2321 | else if (PyUnicode_Check(delobj)) { |
| 2322 | PyErr_SetString(PyExc_TypeError, |
| 2323 | "deletions are implemented differently for unicode"); |
| 2324 | return NULL; |
| 2325 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2326 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2327 | else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen)) |
| 2328 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2329 | } |
| 2330 | else { |
| 2331 | del_table = NULL; |
| 2332 | dellen = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | table = table1; |
Neal Norwitz | 2aa9a5d | 2006-03-20 01:53:23 +0000 | [diff] [blame] | 2336 | inlen = PyString_GET_SIZE(input_obj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2337 | result = PyString_FromStringAndSize((char *)NULL, inlen); |
| 2338 | if (result == NULL) |
| 2339 | return NULL; |
| 2340 | output_start = output = PyString_AsString(result); |
Neal Norwitz | 2aa9a5d | 2006-03-20 01:53:23 +0000 | [diff] [blame] | 2341 | input = PyString_AS_STRING(input_obj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2342 | |
| 2343 | if (dellen == 0) { |
| 2344 | /* If no deletions are required, use faster code */ |
| 2345 | for (i = inlen; --i >= 0; ) { |
| 2346 | c = Py_CHARMASK(*input++); |
| 2347 | if (Py_CHARMASK((*output++ = table[c])) != c) |
| 2348 | changed = 1; |
| 2349 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2350 | if (changed || !PyString_CheckExact(input_obj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2351 | return result; |
| 2352 | Py_DECREF(result); |
| 2353 | Py_INCREF(input_obj); |
| 2354 | return input_obj; |
| 2355 | } |
| 2356 | |
| 2357 | for (i = 0; i < 256; i++) |
| 2358 | trans_table[i] = Py_CHARMASK(table[i]); |
| 2359 | |
| 2360 | for (i = 0; i < dellen; i++) |
| 2361 | trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
| 2362 | |
| 2363 | for (i = inlen; --i >= 0; ) { |
| 2364 | c = Py_CHARMASK(*input++); |
| 2365 | if (trans_table[c] != -1) |
| 2366 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 2367 | continue; |
| 2368 | changed = 1; |
| 2369 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2370 | if (!changed && PyString_CheckExact(input_obj)) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2371 | Py_DECREF(result); |
| 2372 | Py_INCREF(input_obj); |
| 2373 | return input_obj; |
| 2374 | } |
| 2375 | /* Fix the size of the resulting string */ |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2376 | if (inlen > 0) |
| 2377 | _PyString_Resize(&result, output - output_start); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2378 | return result; |
| 2379 | } |
| 2380 | |
| 2381 | |
| 2382 | /* What follows is used for implementing replace(). Perry Stoll. */ |
| 2383 | |
| 2384 | /* |
| 2385 | mymemfind |
| 2386 | |
| 2387 | strstr replacement for arbitrary blocks of memory. |
| 2388 | |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 2389 | Locates the first occurrence in the memory pointed to by MEM of the |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2390 | contents of memory pointed to by PAT. Returns the index into MEM if |
| 2391 | found, or -1 if not found. If len of PAT is greater than length of |
| 2392 | MEM, the function returns -1. |
| 2393 | */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2394 | static Py_ssize_t |
| 2395 | mymemfind(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2396 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2397 | register Py_ssize_t ii; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2398 | |
| 2399 | /* pattern can not occur in the last pat_len-1 chars */ |
| 2400 | len -= pat_len; |
| 2401 | |
| 2402 | for (ii = 0; ii <= len; ii++) { |
Fred Drake | 396f6e0 | 2000-06-20 15:47:54 +0000 | [diff] [blame] | 2403 | if (mem[ii] == pat[0] && memcmp(&mem[ii], pat, pat_len) == 0) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2404 | return ii; |
| 2405 | } |
| 2406 | } |
| 2407 | return -1; |
| 2408 | } |
| 2409 | |
| 2410 | /* |
| 2411 | mymemcnt |
| 2412 | |
| 2413 | Return the number of distinct times PAT is found in MEM. |
| 2414 | meaning mem=1111 and pat==11 returns 2. |
| 2415 | mem=11111 and pat==11 also return 2. |
| 2416 | */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2417 | static Py_ssize_t |
| 2418 | mymemcnt(const char *mem, Py_ssize_t len, const char *pat, Py_ssize_t pat_len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2419 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2420 | register Py_ssize_t offset = 0; |
| 2421 | Py_ssize_t nfound = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2422 | |
| 2423 | while (len >= 0) { |
| 2424 | offset = mymemfind(mem, len, pat, pat_len); |
| 2425 | if (offset == -1) |
| 2426 | break; |
| 2427 | mem += offset + pat_len; |
| 2428 | len -= offset + pat_len; |
| 2429 | nfound++; |
| 2430 | } |
| 2431 | return nfound; |
| 2432 | } |
| 2433 | |
| 2434 | /* |
| 2435 | mymemreplace |
| 2436 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 2437 | Return a string in which all occurrences of PAT in memory STR are |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2438 | replaced with SUB. |
| 2439 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 2440 | If length of PAT is less than length of STR or there are no occurrences |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2441 | of PAT in STR, then the original string is returned. Otherwise, a new |
| 2442 | string is allocated here and returned. |
| 2443 | |
| 2444 | on return, out_len is: |
| 2445 | the length of output string, or |
| 2446 | -1 if the input string is returned, or |
| 2447 | unchanged if an error occurs (no memory). |
| 2448 | |
| 2449 | return value is: |
| 2450 | the new string allocated locally, or |
| 2451 | NULL if an error occurred. |
| 2452 | */ |
| 2453 | static char * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2454 | mymemreplace(const char *str, Py_ssize_t len, /* input string */ |
| 2455 | const char *pat, Py_ssize_t pat_len, /* pattern string to find */ |
| 2456 | const char *sub, Py_ssize_t sub_len, /* substitution string */ |
| 2457 | Py_ssize_t count, /* number of replacements */ |
| 2458 | Py_ssize_t *out_len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2459 | { |
| 2460 | char *out_s; |
| 2461 | char *new_s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2462 | Py_ssize_t nfound, offset, new_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2463 | |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 2464 | if (len == 0 || (pat_len == 0 && sub_len == 0) || pat_len > len) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2465 | goto return_same; |
| 2466 | |
| 2467 | /* find length of output string */ |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 2468 | nfound = (pat_len > 0) ? mymemcnt(str, len, pat, pat_len) : len + 1; |
Tim Peters | 9c012af | 2001-05-10 00:32:57 +0000 | [diff] [blame] | 2469 | if (count < 0) |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 2470 | count = PY_SSIZE_T_MAX; |
Tim Peters | 9c012af | 2001-05-10 00:32:57 +0000 | [diff] [blame] | 2471 | else if (nfound > count) |
| 2472 | nfound = count; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2473 | if (nfound == 0) |
| 2474 | goto return_same; |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 2475 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2476 | new_len = len + nfound*(sub_len - pat_len); |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 2477 | if (new_len == 0) { |
| 2478 | /* Have to allocate something for the caller to free(). */ |
| 2479 | out_s = (char *)PyMem_MALLOC(1); |
Tim Peters | 9c012af | 2001-05-10 00:32:57 +0000 | [diff] [blame] | 2480 | if (out_s == NULL) |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 2481 | return NULL; |
| 2482 | out_s[0] = '\0'; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2483 | } |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 2484 | else { |
| 2485 | assert(new_len > 0); |
| 2486 | new_s = (char *)PyMem_MALLOC(new_len); |
| 2487 | if (new_s == NULL) |
| 2488 | return NULL; |
| 2489 | out_s = new_s; |
| 2490 | |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 2491 | if (pat_len > 0) { |
| 2492 | for (; nfound > 0; --nfound) { |
| 2493 | /* find index of next instance of pattern */ |
| 2494 | offset = mymemfind(str, len, pat, pat_len); |
| 2495 | if (offset == -1) |
| 2496 | break; |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 2497 | |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 2498 | /* copy non matching part of input string */ |
| 2499 | memcpy(new_s, str, offset); |
| 2500 | str += offset + pat_len; |
| 2501 | len -= offset + pat_len; |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 2502 | |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 2503 | /* copy substitute into the output string */ |
| 2504 | new_s += offset; |
| 2505 | memcpy(new_s, sub, sub_len); |
| 2506 | new_s += sub_len; |
| 2507 | } |
| 2508 | /* copy any remaining values into output string */ |
| 2509 | if (len > 0) |
| 2510 | memcpy(new_s, str, len); |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 2511 | } |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 2512 | else { |
| 2513 | for (;;++str, --len) { |
| 2514 | memcpy(new_s, sub, sub_len); |
| 2515 | new_s += sub_len; |
| 2516 | if (--nfound <= 0) { |
| 2517 | memcpy(new_s, str, len); |
| 2518 | break; |
| 2519 | } |
| 2520 | *new_s++ = *str; |
| 2521 | } |
| 2522 | } |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 2523 | } |
| 2524 | *out_len = new_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2525 | return out_s; |
| 2526 | |
| 2527 | return_same: |
| 2528 | *out_len = -1; |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 2529 | return (char *)str; /* cast away const */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2533 | PyDoc_STRVAR(replace__doc__, |
Fred Drake | d22bb65 | 2003-10-22 02:56:40 +0000 | [diff] [blame] | 2534 | "S.replace (old, new[, count]) -> string\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2535 | \n\ |
| 2536 | Return a copy of string S with all occurrences of substring\n\ |
Fred Drake | d22bb65 | 2003-10-22 02:56:40 +0000 | [diff] [blame] | 2537 | old replaced by new. If the optional argument count is\n\ |
| 2538 | given, only the first count occurrences are replaced."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2539 | |
| 2540 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2541 | string_replace(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2542 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2543 | const char *str = PyString_AS_STRING(self), *sub, *repl; |
| 2544 | char *new_s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2545 | const Py_ssize_t len = PyString_GET_SIZE(self); |
| 2546 | Py_ssize_t sub_len, repl_len, out_len; |
Thomas Wouters | dc5f808 | 2006-04-19 15:38:01 +0000 | [diff] [blame] | 2547 | Py_ssize_t count = -1; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2548 | PyObject *newobj; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2549 | PyObject *subobj, *replobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2550 | |
Thomas Wouters | dc5f808 | 2006-04-19 15:38:01 +0000 | [diff] [blame] | 2551 | if (!PyArg_ParseTuple(args, "OO|n:replace", |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2552 | &subobj, &replobj, &count)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2553 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2554 | |
| 2555 | if (PyString_Check(subobj)) { |
| 2556 | sub = PyString_AS_STRING(subobj); |
| 2557 | sub_len = PyString_GET_SIZE(subobj); |
| 2558 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2559 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2560 | else if (PyUnicode_Check(subobj)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2561 | return PyUnicode_Replace((PyObject *)self, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2562 | subobj, replobj, count); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2563 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2564 | else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len)) |
| 2565 | return NULL; |
| 2566 | |
| 2567 | if (PyString_Check(replobj)) { |
| 2568 | repl = PyString_AS_STRING(replobj); |
| 2569 | repl_len = PyString_GET_SIZE(replobj); |
| 2570 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2571 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2572 | else if (PyUnicode_Check(replobj)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2573 | return PyUnicode_Replace((PyObject *)self, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2574 | subobj, replobj, count); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2575 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2576 | else if (PyObject_AsCharBuffer(replobj, &repl, &repl_len)) |
| 2577 | return NULL; |
| 2578 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2579 | new_s = mymemreplace(str,len,sub,sub_len,repl,repl_len,count,&out_len); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2580 | if (new_s == NULL) { |
| 2581 | PyErr_NoMemory(); |
| 2582 | return NULL; |
| 2583 | } |
| 2584 | if (out_len == -1) { |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2585 | if (PyString_CheckExact(self)) { |
| 2586 | /* we're returning another reference to self */ |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2587 | newobj = (PyObject*)self; |
| 2588 | Py_INCREF(newobj); |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2589 | } |
| 2590 | else { |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2591 | newobj = PyString_FromStringAndSize(str, len); |
| 2592 | if (newobj == NULL) |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2593 | return NULL; |
| 2594 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2595 | } |
| 2596 | else { |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2597 | newobj = PyString_FromStringAndSize(new_s, out_len); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2598 | PyMem_FREE(new_s); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2599 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2600 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2604 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2605 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2606 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 2607 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 2608 | With optional start, test S beginning at that position.\n\ |
| 2609 | With optional end, stop comparing S at that position."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2610 | |
| 2611 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2612 | string_startswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2613 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2614 | const char* str = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2615 | Py_ssize_t len = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2616 | const char* prefix; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2617 | Py_ssize_t plen; |
| 2618 | Py_ssize_t start = 0; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 2619 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2620 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2621 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 2622 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 2623 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2624 | return NULL; |
| 2625 | if (PyString_Check(subobj)) { |
| 2626 | prefix = PyString_AS_STRING(subobj); |
| 2627 | plen = PyString_GET_SIZE(subobj); |
| 2628 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2629 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2630 | else if (PyUnicode_Check(subobj)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2631 | Py_ssize_t rc; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2632 | rc = PyUnicode_Tailmatch((PyObject *)self, |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2633 | subobj, start, end, -1); |
| 2634 | if (rc == -1) |
| 2635 | return NULL; |
| 2636 | else |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2637 | return PyBool_FromLong((long) rc); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2638 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2639 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2640 | else if (PyObject_AsCharBuffer(subobj, &prefix, &plen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2641 | return NULL; |
| 2642 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 2643 | string_adjust_indices(&start, &end, len); |
| 2644 | |
| 2645 | if (start+plen > len) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2646 | return PyBool_FromLong(0); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2647 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 2648 | if (end-start >= plen) |
| 2649 | return PyBool_FromLong(!memcmp(str+start, prefix, plen)); |
| 2650 | else |
| 2651 | return PyBool_FromLong(0); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
| 2654 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2655 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2656 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2657 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 2658 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 2659 | With optional start, test S beginning at that position.\n\ |
| 2660 | With optional end, stop comparing S at that position."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2661 | |
| 2662 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2663 | string_endswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2664 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2665 | const char* str = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2666 | Py_ssize_t len = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2667 | const char* suffix; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2668 | Py_ssize_t slen; |
| 2669 | Py_ssize_t start = 0; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 2670 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2671 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2672 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 2673 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 2674 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2675 | return NULL; |
| 2676 | if (PyString_Check(subobj)) { |
| 2677 | suffix = PyString_AS_STRING(subobj); |
| 2678 | slen = PyString_GET_SIZE(subobj); |
| 2679 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2680 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2681 | else if (PyUnicode_Check(subobj)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2682 | Py_ssize_t rc; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2683 | rc = PyUnicode_Tailmatch((PyObject *)self, |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2684 | subobj, start, end, +1); |
| 2685 | if (rc == -1) |
| 2686 | return NULL; |
| 2687 | else |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2688 | return PyBool_FromLong((long) rc); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2689 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2690 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2691 | else if (PyObject_AsCharBuffer(subobj, &suffix, &slen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2692 | return NULL; |
| 2693 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 2694 | string_adjust_indices(&start, &end, len); |
| 2695 | |
| 2696 | if (end-start < slen || start > len) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2697 | return PyBool_FromLong(0); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2698 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 2699 | if (end-slen > start) |
| 2700 | start = end - slen; |
| 2701 | if (end-start >= slen) |
| 2702 | return PyBool_FromLong(!memcmp(str+start, suffix, slen)); |
| 2703 | else |
| 2704 | return PyBool_FromLong(0); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
| 2707 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2708 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2709 | "S.encode([encoding[,errors]]) -> object\n\ |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 2710 | \n\ |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2711 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 2712 | 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] | 2713 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2714 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 2715 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 2716 | codecs.register_error that is able to handle UnicodeEncodeErrors."); |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 2717 | |
| 2718 | static PyObject * |
| 2719 | string_encode(PyStringObject *self, PyObject *args) |
| 2720 | { |
| 2721 | char *encoding = NULL; |
| 2722 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2723 | PyObject *v; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 2724 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 2725 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 2726 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2727 | v = PyString_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 2728 | if (v == NULL) |
| 2729 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2730 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 2731 | PyErr_Format(PyExc_TypeError, |
| 2732 | "encoder did not return a string/unicode object " |
| 2733 | "(type=%.400s)", |
| 2734 | v->ob_type->tp_name); |
| 2735 | Py_DECREF(v); |
| 2736 | return NULL; |
| 2737 | } |
| 2738 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 2739 | |
| 2740 | onError: |
| 2741 | return NULL; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2745 | PyDoc_STRVAR(decode__doc__, |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2746 | "S.decode([encoding[,errors]]) -> object\n\ |
| 2747 | \n\ |
| 2748 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 2749 | to the default encoding. errors may be given to set a different error\n\ |
| 2750 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2751 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 2752 | as well as any other name registerd with codecs.register_error that is\n\ |
| 2753 | able to handle UnicodeDecodeErrors."); |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2754 | |
| 2755 | static PyObject * |
| 2756 | string_decode(PyStringObject *self, PyObject *args) |
| 2757 | { |
| 2758 | char *encoding = NULL; |
| 2759 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2760 | PyObject *v; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 2761 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2762 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 2763 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2764 | v = PyString_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 2765 | if (v == NULL) |
| 2766 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 2767 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 2768 | PyErr_Format(PyExc_TypeError, |
| 2769 | "decoder did not return a string/unicode object " |
| 2770 | "(type=%.400s)", |
| 2771 | v->ob_type->tp_name); |
| 2772 | Py_DECREF(v); |
| 2773 | return NULL; |
| 2774 | } |
| 2775 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 2776 | |
| 2777 | onError: |
| 2778 | return NULL; |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
| 2781 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2782 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2783 | "S.expandtabs([tabsize]) -> string\n\ |
| 2784 | \n\ |
| 2785 | 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] | 2786 | 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] | 2787 | |
| 2788 | static PyObject* |
| 2789 | string_expandtabs(PyStringObject *self, PyObject *args) |
| 2790 | { |
| 2791 | const char *e, *p; |
| 2792 | char *q; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2793 | Py_ssize_t i, j; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2794 | PyObject *u; |
| 2795 | int tabsize = 8; |
| 2796 | |
| 2797 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 2798 | return NULL; |
| 2799 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 2800 | /* First pass: determine size of output string */ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2801 | i = j = 0; |
| 2802 | e = PyString_AS_STRING(self) + PyString_GET_SIZE(self); |
| 2803 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 2804 | if (*p == '\t') { |
| 2805 | if (tabsize > 0) |
| 2806 | j += tabsize - (j % tabsize); |
| 2807 | } |
| 2808 | else { |
| 2809 | j++; |
| 2810 | if (*p == '\n' || *p == '\r') { |
| 2811 | i += j; |
| 2812 | j = 0; |
| 2813 | } |
| 2814 | } |
| 2815 | |
| 2816 | /* Second pass: create output string and fill it */ |
| 2817 | u = PyString_FromStringAndSize(NULL, i + j); |
| 2818 | if (!u) |
| 2819 | return NULL; |
| 2820 | |
| 2821 | j = 0; |
| 2822 | q = PyString_AS_STRING(u); |
| 2823 | |
| 2824 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 2825 | if (*p == '\t') { |
| 2826 | if (tabsize > 0) { |
| 2827 | i = tabsize - (j % tabsize); |
| 2828 | j += i; |
| 2829 | while (i--) |
| 2830 | *q++ = ' '; |
| 2831 | } |
| 2832 | } |
| 2833 | else { |
| 2834 | j++; |
| 2835 | *q++ = *p; |
| 2836 | if (*p == '\n' || *p == '\r') |
| 2837 | j = 0; |
| 2838 | } |
| 2839 | |
| 2840 | return u; |
| 2841 | } |
| 2842 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2843 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2844 | 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] | 2845 | { |
| 2846 | PyObject *u; |
| 2847 | |
| 2848 | if (left < 0) |
| 2849 | left = 0; |
| 2850 | if (right < 0) |
| 2851 | right = 0; |
| 2852 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2853 | if (left == 0 && right == 0 && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2854 | Py_INCREF(self); |
| 2855 | return (PyObject *)self; |
| 2856 | } |
| 2857 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2858 | u = PyString_FromStringAndSize(NULL, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2859 | left + PyString_GET_SIZE(self) + right); |
| 2860 | if (u) { |
| 2861 | if (left) |
| 2862 | memset(PyString_AS_STRING(u), fill, left); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2863 | memcpy(PyString_AS_STRING(u) + left, |
| 2864 | PyString_AS_STRING(self), |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2865 | PyString_GET_SIZE(self)); |
| 2866 | if (right) |
| 2867 | memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self), |
| 2868 | fill, right); |
| 2869 | } |
| 2870 | |
| 2871 | return u; |
| 2872 | } |
| 2873 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2874 | PyDoc_STRVAR(ljust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 2875 | "S.ljust(width[, fillchar]) -> string\n" |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2876 | "\n" |
| 2877 | "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] | 2878 | "done using the specified fill character (default is a space)."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2879 | |
| 2880 | static PyObject * |
| 2881 | string_ljust(PyStringObject *self, PyObject *args) |
| 2882 | { |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 2883 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 2884 | char fillchar = ' '; |
| 2885 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 2886 | if (!PyArg_ParseTuple(args, "n|c:ljust", &width, &fillchar)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2887 | return NULL; |
| 2888 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2889 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2890 | Py_INCREF(self); |
| 2891 | return (PyObject*) self; |
| 2892 | } |
| 2893 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 2894 | return pad(self, 0, width - PyString_GET_SIZE(self), fillchar); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
| 2897 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2898 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 2899 | "S.rjust(width[, fillchar]) -> string\n" |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2900 | "\n" |
| 2901 | "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] | 2902 | "done using the specified fill character (default is a space)"); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2903 | |
| 2904 | static PyObject * |
| 2905 | string_rjust(PyStringObject *self, PyObject *args) |
| 2906 | { |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 2907 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 2908 | char fillchar = ' '; |
| 2909 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 2910 | if (!PyArg_ParseTuple(args, "n|c:rjust", &width, &fillchar)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2911 | return NULL; |
| 2912 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2913 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2914 | Py_INCREF(self); |
| 2915 | return (PyObject*) self; |
| 2916 | } |
| 2917 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 2918 | return pad(self, width - PyString_GET_SIZE(self), 0, fillchar); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2922 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 2923 | "S.center(width[, fillchar]) -> string\n" |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2924 | "\n" |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 2925 | "Return S centered in a string of length width. Padding is\n" |
| 2926 | "done using the specified fill character (default is a space)"); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2927 | |
| 2928 | static PyObject * |
| 2929 | string_center(PyStringObject *self, PyObject *args) |
| 2930 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2931 | Py_ssize_t marg, left; |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 2932 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 2933 | char fillchar = ' '; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2934 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 2935 | if (!PyArg_ParseTuple(args, "n|c:center", &width, &fillchar)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2936 | return NULL; |
| 2937 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2938 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2939 | Py_INCREF(self); |
| 2940 | return (PyObject*) self; |
| 2941 | } |
| 2942 | |
| 2943 | marg = width - PyString_GET_SIZE(self); |
| 2944 | left = marg / 2 + (marg & width & 1); |
| 2945 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 2946 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2949 | PyDoc_STRVAR(zfill__doc__, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 2950 | "S.zfill(width) -> string\n" |
| 2951 | "\n" |
| 2952 | "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] | 2953 | "of the specified width. The string S is never truncated."); |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 2954 | |
| 2955 | static PyObject * |
| 2956 | string_zfill(PyStringObject *self, PyObject *args) |
| 2957 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 2958 | Py_ssize_t fill; |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 2959 | PyObject *s; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 2960 | char *p; |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 2961 | Py_ssize_t width; |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 2962 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 2963 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 2964 | return NULL; |
| 2965 | |
| 2966 | if (PyString_GET_SIZE(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 2967 | if (PyString_CheckExact(self)) { |
| 2968 | Py_INCREF(self); |
| 2969 | return (PyObject*) self; |
| 2970 | } |
| 2971 | else |
| 2972 | return PyString_FromStringAndSize( |
| 2973 | PyString_AS_STRING(self), |
| 2974 | PyString_GET_SIZE(self) |
| 2975 | ); |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | fill = width - PyString_GET_SIZE(self); |
| 2979 | |
| 2980 | s = pad(self, fill, 0, '0'); |
| 2981 | |
| 2982 | if (s == NULL) |
| 2983 | return NULL; |
| 2984 | |
| 2985 | p = PyString_AS_STRING(s); |
| 2986 | if (p[fill] == '+' || p[fill] == '-') { |
| 2987 | /* move sign to beginning of string */ |
| 2988 | p[0] = p[fill]; |
| 2989 | p[fill] = '0'; |
| 2990 | } |
| 2991 | |
| 2992 | return (PyObject*) s; |
| 2993 | } |
| 2994 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2995 | PyDoc_STRVAR(isspace__doc__, |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 2996 | "S.isspace() -> bool\n\ |
| 2997 | \n\ |
| 2998 | Return True if all characters in S are whitespace\n\ |
| 2999 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3000 | |
| 3001 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3002 | string_isspace(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3003 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3004 | register const unsigned char *p |
| 3005 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3006 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3007 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3008 | /* Shortcut for single character strings */ |
| 3009 | if (PyString_GET_SIZE(self) == 1 && |
| 3010 | isspace(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3011 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3012 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3013 | /* Special case for empty strings */ |
| 3014 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3015 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3016 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3017 | e = p + PyString_GET_SIZE(self); |
| 3018 | for (; p < e; p++) { |
| 3019 | if (!isspace(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3020 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3021 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3022 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
| 3025 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3026 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3027 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3028 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3029 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3030 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3031 | |
| 3032 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3033 | string_isalpha(PyStringObject *self) |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3034 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3035 | register const unsigned char *p |
| 3036 | = (unsigned char *) PyString_AS_STRING(self); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3037 | register const unsigned char *e; |
| 3038 | |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3039 | /* Shortcut for single character strings */ |
| 3040 | if (PyString_GET_SIZE(self) == 1 && |
| 3041 | isalpha(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3042 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3043 | |
| 3044 | /* Special case for empty strings */ |
| 3045 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3046 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3047 | |
| 3048 | e = p + PyString_GET_SIZE(self); |
| 3049 | for (; p < e; p++) { |
| 3050 | if (!isalpha(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3051 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3052 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3053 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3057 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3058 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3059 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3060 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3061 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3062 | |
| 3063 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3064 | string_isalnum(PyStringObject *self) |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3065 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3066 | register const unsigned char *p |
| 3067 | = (unsigned char *) PyString_AS_STRING(self); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3068 | register const unsigned char *e; |
| 3069 | |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3070 | /* Shortcut for single character strings */ |
| 3071 | if (PyString_GET_SIZE(self) == 1 && |
| 3072 | isalnum(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3073 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3074 | |
| 3075 | /* Special case for empty strings */ |
| 3076 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3077 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3078 | |
| 3079 | e = p + PyString_GET_SIZE(self); |
| 3080 | for (; p < e; p++) { |
| 3081 | if (!isalnum(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3082 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3083 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3084 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
| 3087 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3088 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3089 | "S.isdigit() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3090 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3091 | Return True if all characters in S are digits\n\ |
| 3092 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3093 | |
| 3094 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3095 | string_isdigit(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3096 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3097 | register const unsigned char *p |
| 3098 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3099 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3100 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3101 | /* Shortcut for single character strings */ |
| 3102 | if (PyString_GET_SIZE(self) == 1 && |
| 3103 | isdigit(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3104 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3105 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3106 | /* Special case for empty strings */ |
| 3107 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3108 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3109 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3110 | e = p + PyString_GET_SIZE(self); |
| 3111 | for (; p < e; p++) { |
| 3112 | if (!isdigit(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3113 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3114 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3115 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3119 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3120 | "S.islower() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3121 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3122 | 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] | 3123 | at least one cased character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3124 | |
| 3125 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3126 | string_islower(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3127 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3128 | register const unsigned char *p |
| 3129 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3130 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3131 | int cased; |
| 3132 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3133 | /* Shortcut for single character strings */ |
| 3134 | if (PyString_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3135 | return PyBool_FromLong(islower(*p) != 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3136 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3137 | /* Special case for empty strings */ |
| 3138 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3139 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3140 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3141 | e = p + PyString_GET_SIZE(self); |
| 3142 | cased = 0; |
| 3143 | for (; p < e; p++) { |
| 3144 | if (isupper(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3145 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3146 | else if (!cased && islower(*p)) |
| 3147 | cased = 1; |
| 3148 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3149 | return PyBool_FromLong(cased); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3153 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3154 | "S.isupper() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3155 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3156 | 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] | 3157 | at least one cased character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3158 | |
| 3159 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3160 | string_isupper(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3161 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3162 | register const unsigned char *p |
| 3163 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3164 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3165 | int cased; |
| 3166 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3167 | /* Shortcut for single character strings */ |
| 3168 | if (PyString_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3169 | return PyBool_FromLong(isupper(*p) != 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3170 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3171 | /* Special case for empty strings */ |
| 3172 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3173 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3174 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3175 | e = p + PyString_GET_SIZE(self); |
| 3176 | cased = 0; |
| 3177 | for (; p < e; p++) { |
| 3178 | if (islower(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3179 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3180 | else if (!cased && isupper(*p)) |
| 3181 | cased = 1; |
| 3182 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3183 | return PyBool_FromLong(cased); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3187 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3188 | "S.istitle() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3189 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3190 | Return True if S is a titlecased string and there is at least one\n\ |
| 3191 | character in S, i.e. uppercase characters may only follow uncased\n\ |
| 3192 | characters and lowercase characters only cased ones. Return False\n\ |
| 3193 | otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3194 | |
| 3195 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3196 | string_istitle(PyStringObject *self, PyObject *uncased) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3197 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3198 | register const unsigned char *p |
| 3199 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3200 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3201 | int cased, previous_is_cased; |
| 3202 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3203 | /* Shortcut for single character strings */ |
| 3204 | if (PyString_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3205 | return PyBool_FromLong(isupper(*p) != 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3206 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3207 | /* Special case for empty strings */ |
| 3208 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3209 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3210 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3211 | e = p + PyString_GET_SIZE(self); |
| 3212 | cased = 0; |
| 3213 | previous_is_cased = 0; |
| 3214 | for (; p < e; p++) { |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3215 | register const unsigned char ch = *p; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3216 | |
| 3217 | if (isupper(ch)) { |
| 3218 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3219 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3220 | previous_is_cased = 1; |
| 3221 | cased = 1; |
| 3222 | } |
| 3223 | else if (islower(ch)) { |
| 3224 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3225 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3226 | previous_is_cased = 1; |
| 3227 | cased = 1; |
| 3228 | } |
| 3229 | else |
| 3230 | previous_is_cased = 0; |
| 3231 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3232 | return PyBool_FromLong(cased); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3236 | PyDoc_STRVAR(splitlines__doc__, |
Fred Drake | 2bae4fa | 2001-10-13 15:57:55 +0000 | [diff] [blame] | 3237 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3238 | \n\ |
| 3239 | 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] | 3240 | 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] | 3241 | is given and true."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3242 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3243 | static PyObject* |
| 3244 | string_splitlines(PyStringObject *self, PyObject *args) |
| 3245 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3246 | register Py_ssize_t i; |
| 3247 | register Py_ssize_t j; |
| 3248 | Py_ssize_t len; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3249 | int keepends = 0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3250 | PyObject *list; |
| 3251 | PyObject *str; |
| 3252 | char *data; |
| 3253 | |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3254 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3255 | return NULL; |
| 3256 | |
| 3257 | data = PyString_AS_STRING(self); |
| 3258 | len = PyString_GET_SIZE(self); |
| 3259 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3260 | list = PyList_New(0); |
| 3261 | if (!list) |
| 3262 | goto onError; |
| 3263 | |
| 3264 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3265 | Py_ssize_t eol; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3266 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3267 | /* Find a line and append it */ |
| 3268 | while (i < len && data[i] != '\n' && data[i] != '\r') |
| 3269 | i++; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3270 | |
| 3271 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3272 | eol = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3273 | if (i < len) { |
| 3274 | if (data[i] == '\r' && i + 1 < len && |
| 3275 | data[i+1] == '\n') |
| 3276 | i += 2; |
| 3277 | else |
| 3278 | i++; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3279 | if (keepends) |
| 3280 | eol = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3281 | } |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3282 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3283 | j = i; |
| 3284 | } |
| 3285 | if (j < len) { |
| 3286 | SPLIT_APPEND(data, j, len); |
| 3287 | } |
| 3288 | |
| 3289 | return list; |
| 3290 | |
| 3291 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3292 | Py_XDECREF(list); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3293 | return NULL; |
| 3294 | } |
| 3295 | |
| 3296 | #undef SPLIT_APPEND |
| 3297 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3298 | static PyObject * |
| 3299 | string_getnewargs(PyStringObject *v) |
| 3300 | { |
| 3301 | return Py_BuildValue("(s#)", v->ob_sval, v->ob_size); |
| 3302 | } |
| 3303 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3304 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3305 | static PyMethodDef |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3306 | string_methods[] = { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3307 | /* Counterparts of the obsolete stropmodule functions; except |
| 3308 | string.maketrans(). */ |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3309 | {"join", (PyCFunction)string_join, METH_O, join__doc__}, |
| 3310 | {"split", (PyCFunction)string_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 3311 | {"rsplit", (PyCFunction)string_rsplit, METH_VARARGS, rsplit__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3312 | {"lower", (PyCFunction)string_lower, METH_NOARGS, lower__doc__}, |
| 3313 | {"upper", (PyCFunction)string_upper, METH_NOARGS, upper__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3314 | {"islower", (PyCFunction)string_islower, METH_NOARGS, islower__doc__}, |
| 3315 | {"isupper", (PyCFunction)string_isupper, METH_NOARGS, isupper__doc__}, |
| 3316 | {"isspace", (PyCFunction)string_isspace, METH_NOARGS, isspace__doc__}, |
| 3317 | {"isdigit", (PyCFunction)string_isdigit, METH_NOARGS, isdigit__doc__}, |
| 3318 | {"istitle", (PyCFunction)string_istitle, METH_NOARGS, istitle__doc__}, |
| 3319 | {"isalpha", (PyCFunction)string_isalpha, METH_NOARGS, isalpha__doc__}, |
| 3320 | {"isalnum", (PyCFunction)string_isalnum, METH_NOARGS, isalnum__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3321 | {"capitalize", (PyCFunction)string_capitalize, METH_NOARGS, |
| 3322 | capitalize__doc__}, |
| 3323 | {"count", (PyCFunction)string_count, METH_VARARGS, count__doc__}, |
| 3324 | {"endswith", (PyCFunction)string_endswith, METH_VARARGS, |
| 3325 | endswith__doc__}, |
| 3326 | {"find", (PyCFunction)string_find, METH_VARARGS, find__doc__}, |
| 3327 | {"index", (PyCFunction)string_index, METH_VARARGS, index__doc__}, |
| 3328 | {"lstrip", (PyCFunction)string_lstrip, METH_VARARGS, lstrip__doc__}, |
| 3329 | {"replace", (PyCFunction)string_replace, METH_VARARGS, replace__doc__}, |
| 3330 | {"rfind", (PyCFunction)string_rfind, METH_VARARGS, rfind__doc__}, |
| 3331 | {"rindex", (PyCFunction)string_rindex, METH_VARARGS, rindex__doc__}, |
| 3332 | {"rstrip", (PyCFunction)string_rstrip, METH_VARARGS, rstrip__doc__}, |
| 3333 | {"startswith", (PyCFunction)string_startswith, METH_VARARGS, |
| 3334 | startswith__doc__}, |
| 3335 | {"strip", (PyCFunction)string_strip, METH_VARARGS, strip__doc__}, |
| 3336 | {"swapcase", (PyCFunction)string_swapcase, METH_NOARGS, |
| 3337 | swapcase__doc__}, |
| 3338 | {"translate", (PyCFunction)string_translate, METH_VARARGS, |
| 3339 | translate__doc__}, |
| 3340 | {"title", (PyCFunction)string_title, METH_NOARGS, title__doc__}, |
| 3341 | {"ljust", (PyCFunction)string_ljust, METH_VARARGS, ljust__doc__}, |
| 3342 | {"rjust", (PyCFunction)string_rjust, METH_VARARGS, rjust__doc__}, |
| 3343 | {"center", (PyCFunction)string_center, METH_VARARGS, center__doc__}, |
| 3344 | {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__}, |
| 3345 | {"encode", (PyCFunction)string_encode, METH_VARARGS, encode__doc__}, |
| 3346 | {"decode", (PyCFunction)string_decode, METH_VARARGS, decode__doc__}, |
| 3347 | {"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, |
| 3348 | expandtabs__doc__}, |
| 3349 | {"splitlines", (PyCFunction)string_splitlines, METH_VARARGS, |
| 3350 | splitlines__doc__}, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3351 | {"__getnewargs__", (PyCFunction)string_getnewargs, METH_NOARGS}, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3352 | {NULL, NULL} /* sentinel */ |
| 3353 | }; |
| 3354 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 3355 | static PyObject * |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3356 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 3357 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3358 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3359 | string_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3360 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3361 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 3362 | static char *kwlist[] = {"object", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3363 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3364 | if (type != &PyString_Type) |
| 3365 | return str_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3366 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x)) |
| 3367 | return NULL; |
| 3368 | if (x == NULL) |
| 3369 | return PyString_FromString(""); |
| 3370 | return PyObject_Str(x); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3371 | } |
| 3372 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3373 | static PyObject * |
| 3374 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3375 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3376 | PyObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3377 | Py_ssize_t n; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3378 | |
| 3379 | assert(PyType_IsSubtype(type, &PyString_Type)); |
| 3380 | tmp = string_new(&PyString_Type, args, kwds); |
| 3381 | if (tmp == NULL) |
| 3382 | return NULL; |
Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 3383 | assert(PyString_CheckExact(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3384 | n = PyString_GET_SIZE(tmp); |
| 3385 | pnew = type->tp_alloc(type, n); |
| 3386 | if (pnew != NULL) { |
| 3387 | memcpy(PyString_AS_STRING(pnew), PyString_AS_STRING(tmp), n+1); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3388 | ((PyStringObject *)pnew)->ob_shash = |
| 3389 | ((PyStringObject *)tmp)->ob_shash; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 3390 | ((PyStringObject *)pnew)->ob_sstate = SSTATE_NOT_INTERNED; |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3391 | } |
Guido van Rossum | 29d55a3 | 2001-08-31 16:11:15 +0000 | [diff] [blame] | 3392 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3393 | return pnew; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3394 | } |
| 3395 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 3396 | static PyObject * |
| 3397 | basestring_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3398 | { |
| 3399 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 32a7e7f | 2002-05-31 19:58:02 +0000 | [diff] [blame] | 3400 | "The basestring type cannot be instantiated"); |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 3401 | return NULL; |
| 3402 | } |
| 3403 | |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 3404 | static PyObject * |
| 3405 | string_mod(PyObject *v, PyObject *w) |
| 3406 | { |
| 3407 | if (!PyString_Check(v)) { |
| 3408 | Py_INCREF(Py_NotImplemented); |
| 3409 | return Py_NotImplemented; |
| 3410 | } |
| 3411 | return PyString_Format(v, w); |
| 3412 | } |
| 3413 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3414 | PyDoc_STRVAR(basestring_doc, |
| 3415 | "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] | 3416 | |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 3417 | static PyNumberMethods string_as_number = { |
| 3418 | 0, /*nb_add*/ |
| 3419 | 0, /*nb_subtract*/ |
| 3420 | 0, /*nb_multiply*/ |
| 3421 | 0, /*nb_divide*/ |
| 3422 | string_mod, /*nb_remainder*/ |
| 3423 | }; |
| 3424 | |
| 3425 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 3426 | PyTypeObject PyBaseString_Type = { |
| 3427 | PyObject_HEAD_INIT(&PyType_Type) |
| 3428 | 0, |
Neal Norwitz | 32a7e7f | 2002-05-31 19:58:02 +0000 | [diff] [blame] | 3429 | "basestring", |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 3430 | 0, |
| 3431 | 0, |
| 3432 | 0, /* tp_dealloc */ |
| 3433 | 0, /* tp_print */ |
| 3434 | 0, /* tp_getattr */ |
| 3435 | 0, /* tp_setattr */ |
| 3436 | 0, /* tp_compare */ |
| 3437 | 0, /* tp_repr */ |
| 3438 | 0, /* tp_as_number */ |
| 3439 | 0, /* tp_as_sequence */ |
| 3440 | 0, /* tp_as_mapping */ |
| 3441 | 0, /* tp_hash */ |
| 3442 | 0, /* tp_call */ |
| 3443 | 0, /* tp_str */ |
| 3444 | 0, /* tp_getattro */ |
| 3445 | 0, /* tp_setattro */ |
| 3446 | 0, /* tp_as_buffer */ |
| 3447 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3448 | basestring_doc, /* tp_doc */ |
| 3449 | 0, /* tp_traverse */ |
| 3450 | 0, /* tp_clear */ |
| 3451 | 0, /* tp_richcompare */ |
| 3452 | 0, /* tp_weaklistoffset */ |
| 3453 | 0, /* tp_iter */ |
| 3454 | 0, /* tp_iternext */ |
| 3455 | 0, /* tp_methods */ |
| 3456 | 0, /* tp_members */ |
| 3457 | 0, /* tp_getset */ |
| 3458 | &PyBaseObject_Type, /* tp_base */ |
| 3459 | 0, /* tp_dict */ |
| 3460 | 0, /* tp_descr_get */ |
| 3461 | 0, /* tp_descr_set */ |
| 3462 | 0, /* tp_dictoffset */ |
| 3463 | 0, /* tp_init */ |
| 3464 | 0, /* tp_alloc */ |
| 3465 | basestring_new, /* tp_new */ |
| 3466 | 0, /* tp_free */ |
| 3467 | }; |
| 3468 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3469 | PyDoc_STRVAR(string_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3470 | "str(object) -> string\n\ |
| 3471 | \n\ |
| 3472 | Return a nice string representation of the object.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3473 | 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] | 3474 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3475 | PyTypeObject PyString_Type = { |
| 3476 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3477 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3478 | "str", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3479 | sizeof(PyStringObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3480 | sizeof(char), |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 3481 | string_dealloc, /* tp_dealloc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3482 | (printfunc)string_print, /* tp_print */ |
| 3483 | 0, /* tp_getattr */ |
| 3484 | 0, /* tp_setattr */ |
| 3485 | 0, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 3486 | string_repr, /* tp_repr */ |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 3487 | &string_as_number, /* tp_as_number */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3488 | &string_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 3489 | &string_as_mapping, /* tp_as_mapping */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3490 | (hashfunc)string_hash, /* tp_hash */ |
| 3491 | 0, /* tp_call */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 3492 | string_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3493 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3494 | 0, /* tp_setattro */ |
| 3495 | &string_as_buffer, /* tp_as_buffer */ |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 3496 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 3497 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3498 | string_doc, /* tp_doc */ |
| 3499 | 0, /* tp_traverse */ |
| 3500 | 0, /* tp_clear */ |
| 3501 | (richcmpfunc)string_richcompare, /* tp_richcompare */ |
| 3502 | 0, /* tp_weaklistoffset */ |
| 3503 | 0, /* tp_iter */ |
| 3504 | 0, /* tp_iternext */ |
| 3505 | string_methods, /* tp_methods */ |
| 3506 | 0, /* tp_members */ |
| 3507 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 3508 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3509 | 0, /* tp_dict */ |
| 3510 | 0, /* tp_descr_get */ |
| 3511 | 0, /* tp_descr_set */ |
| 3512 | 0, /* tp_dictoffset */ |
| 3513 | 0, /* tp_init */ |
| 3514 | 0, /* tp_alloc */ |
| 3515 | string_new, /* tp_new */ |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 3516 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3517 | }; |
| 3518 | |
| 3519 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3520 | PyString_Concat(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3521 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3522 | register PyObject *v; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3523 | if (*pv == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3524 | return; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3525 | if (w == NULL || !PyString_Check(*pv)) { |
| 3526 | Py_DECREF(*pv); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3527 | *pv = NULL; |
| 3528 | return; |
| 3529 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3530 | v = string_concat((PyStringObject *) *pv, w); |
| 3531 | Py_DECREF(*pv); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3532 | *pv = v; |
| 3533 | } |
| 3534 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3535 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3536 | PyString_ConcatAndDel(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3537 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3538 | PyString_Concat(pv, w); |
| 3539 | Py_XDECREF(w); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3543 | /* The following function breaks the notion that strings are immutable: |
| 3544 | it changes the size of a string. We get away with this only if there |
| 3545 | is only one module referencing the object. You can also think of it |
| 3546 | as creating a new string object and destroying the old one, only |
| 3547 | 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] | 3548 | already be known to some other part of the code... |
| 3549 | Note that if there's not enough memory to resize the string, the original |
| 3550 | string object at *pv is deallocated, *pv is set to NULL, an "out of |
| 3551 | memory" exception is set, and -1 is returned. Else (on success) 0 is |
| 3552 | returned, and the value in *pv may or may not be the same as on input. |
| 3553 | As always, an extra byte is allocated for a trailing \0 byte (newsize |
| 3554 | does *not* include that), and a trailing \0 byte is stored. |
| 3555 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3556 | |
| 3557 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3558 | _PyString_Resize(PyObject **pv, Py_ssize_t newsize) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3559 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3560 | register PyObject *v; |
| 3561 | register PyStringObject *sv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 3562 | v = *pv; |
Armin Rigo | 618fbf5 | 2004-08-07 20:58:32 +0000 | [diff] [blame] | 3563 | if (!PyString_Check(v) || v->ob_refcnt != 1 || newsize < 0 || |
| 3564 | PyString_CHECK_INTERNED(v)) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3565 | *pv = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3566 | Py_DECREF(v); |
| 3567 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 3568 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3569 | } |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 3570 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 3571 | _Py_DEC_REFTOTAL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3572 | _Py_ForgetReference(v); |
| 3573 | *pv = (PyObject *) |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 3574 | PyObject_REALLOC((char *)v, sizeof(PyStringObject) + newsize); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3575 | if (*pv == NULL) { |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 3576 | PyObject_Del(v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3577 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 3578 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3579 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3580 | _Py_NewReference(*pv); |
| 3581 | sv = (PyStringObject *) *pv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 3582 | sv->ob_size = newsize; |
| 3583 | sv->ob_sval[newsize] = '\0'; |
Raymond Hettinger | 561fbf1 | 2004-10-26 01:52:37 +0000 | [diff] [blame] | 3584 | sv->ob_shash = -1; /* invalidate cached hash value */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3585 | return 0; |
| 3586 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3587 | |
| 3588 | /* Helpers for formatstring */ |
| 3589 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3590 | static PyObject * |
Thomas Wouters | 977485d | 2006-02-16 15:59:12 +0000 | [diff] [blame] | 3591 | 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] | 3592 | { |
Thomas Wouters | 977485d | 2006-02-16 15:59:12 +0000 | [diff] [blame] | 3593 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3594 | if (argidx < arglen) { |
| 3595 | (*p_argidx)++; |
| 3596 | if (arglen < 0) |
| 3597 | return args; |
| 3598 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3599 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3600 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3601 | PyErr_SetString(PyExc_TypeError, |
| 3602 | "not enough arguments for format string"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3603 | return NULL; |
| 3604 | } |
| 3605 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3606 | /* Format codes |
| 3607 | * F_LJUST '-' |
| 3608 | * F_SIGN '+' |
| 3609 | * F_BLANK ' ' |
| 3610 | * F_ALT '#' |
| 3611 | * F_ZERO '0' |
| 3612 | */ |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3613 | #define F_LJUST (1<<0) |
| 3614 | #define F_SIGN (1<<1) |
| 3615 | #define F_BLANK (1<<2) |
| 3616 | #define F_ALT (1<<3) |
| 3617 | #define F_ZERO (1<<4) |
| 3618 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3619 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3620 | formatfloat(char *buf, size_t buflen, int flags, |
| 3621 | int prec, int type, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3622 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3623 | /* fmt = '%#.' + `prec` + `type` |
| 3624 | 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] | 3625 | char fmt[20]; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3626 | double x; |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 3627 | x = PyFloat_AsDouble(v); |
| 3628 | if (x == -1.0 && PyErr_Occurred()) { |
| 3629 | PyErr_SetString(PyExc_TypeError, "float argument required"); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3630 | return -1; |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 3631 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3632 | if (prec < 0) |
| 3633 | prec = 6; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3634 | if (type == 'f' && fabs(x)/1e25 >= 1e25) |
| 3635 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 3636 | /* Worst case length calc to ensure no buffer overrun: |
| 3637 | |
| 3638 | 'g' formats: |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3639 | fmt = %#.<prec>g |
| 3640 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3641 | for any double rep.) |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3642 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 3643 | |
| 3644 | 'f' formats: |
| 3645 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 3646 | len = 1 + 50 + 1 + prec = 52 + prec |
| 3647 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3648 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 3649 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 3650 | |
| 3651 | */ |
| 3652 | if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || |
| 3653 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3654 | PyErr_SetString(PyExc_OverflowError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 3655 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3656 | return -1; |
| 3657 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 3658 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 3659 | (flags&F_ALT) ? "#" : "", |
| 3660 | prec, type); |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 3661 | PyOS_ascii_formatd(buf, buflen, fmt, x); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3662 | return (int)strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3663 | } |
| 3664 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3665 | /* _PyString_FormatLong emulates the format codes d, u, o, x and X, and |
| 3666 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 3667 | * Python's regular ints. |
| 3668 | * Return value: a new PyString*, or NULL if error. |
| 3669 | * . *pbuf is set to point into it, |
| 3670 | * *plen set to the # of chars following that. |
| 3671 | * Caller must decref it when done using pbuf. |
| 3672 | * The string starting at *pbuf is of the form |
| 3673 | * "-"? ("0x" | "0X")? digit+ |
| 3674 | * "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] | 3675 | * set in flags. The case of hex digits will be correct, |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3676 | * There will be at least prec digits, zero-filled on the left if |
| 3677 | * necessary to get that many. |
| 3678 | * val object to be converted |
| 3679 | * flags bitmask of format flags; only F_ALT is looked at |
| 3680 | * prec minimum number of digits; 0-fill on left if needed |
| 3681 | * type a character in [duoxX]; u acts the same as d |
| 3682 | * |
| 3683 | * CAUTION: o, x and X conversions on regular ints can never |
| 3684 | * produce a '-' sign, but can for Python's unbounded ints. |
| 3685 | */ |
| 3686 | PyObject* |
| 3687 | _PyString_FormatLong(PyObject *val, int flags, int prec, int type, |
| 3688 | char **pbuf, int *plen) |
| 3689 | { |
| 3690 | PyObject *result = NULL; |
| 3691 | char *buf; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3692 | Py_ssize_t i; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3693 | int sign; /* 1 if '-', else 0 */ |
| 3694 | int len; /* number of characters */ |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3695 | Py_ssize_t llen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3696 | int numdigits; /* len == numnondigits + numdigits */ |
| 3697 | int numnondigits = 0; |
| 3698 | |
| 3699 | switch (type) { |
| 3700 | case 'd': |
| 3701 | case 'u': |
| 3702 | result = val->ob_type->tp_str(val); |
| 3703 | break; |
| 3704 | case 'o': |
| 3705 | result = val->ob_type->tp_as_number->nb_oct(val); |
| 3706 | break; |
| 3707 | case 'x': |
| 3708 | case 'X': |
| 3709 | numnondigits = 2; |
| 3710 | result = val->ob_type->tp_as_number->nb_hex(val); |
| 3711 | break; |
| 3712 | default: |
| 3713 | assert(!"'type' not in [duoxX]"); |
| 3714 | } |
| 3715 | if (!result) |
| 3716 | return NULL; |
| 3717 | |
| 3718 | /* To modify the string in-place, there can only be one reference. */ |
| 3719 | if (result->ob_refcnt != 1) { |
| 3720 | PyErr_BadInternalCall(); |
| 3721 | return NULL; |
| 3722 | } |
| 3723 | buf = PyString_AsString(result); |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3724 | llen = PyString_Size(result); |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 3725 | if (llen > PY_SSIZE_T_MAX) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3726 | PyErr_SetString(PyExc_ValueError, "string too large in _PyString_FormatLong"); |
| 3727 | return NULL; |
| 3728 | } |
| 3729 | len = (int)llen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3730 | if (buf[len-1] == 'L') { |
| 3731 | --len; |
| 3732 | buf[len] = '\0'; |
| 3733 | } |
| 3734 | sign = buf[0] == '-'; |
| 3735 | numnondigits += sign; |
| 3736 | numdigits = len - numnondigits; |
| 3737 | assert(numdigits > 0); |
| 3738 | |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 3739 | /* Get rid of base marker unless F_ALT */ |
| 3740 | if ((flags & F_ALT) == 0) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3741 | /* Need to skip 0x, 0X or 0. */ |
| 3742 | int skipped = 0; |
| 3743 | switch (type) { |
| 3744 | case 'o': |
| 3745 | assert(buf[sign] == '0'); |
| 3746 | /* If 0 is only digit, leave it alone. */ |
| 3747 | if (numdigits > 1) { |
| 3748 | skipped = 1; |
| 3749 | --numdigits; |
| 3750 | } |
| 3751 | break; |
| 3752 | case 'x': |
| 3753 | case 'X': |
| 3754 | assert(buf[sign] == '0'); |
| 3755 | assert(buf[sign + 1] == 'x'); |
| 3756 | skipped = 2; |
| 3757 | numnondigits -= 2; |
| 3758 | break; |
| 3759 | } |
| 3760 | if (skipped) { |
| 3761 | buf += skipped; |
| 3762 | len -= skipped; |
| 3763 | if (sign) |
| 3764 | buf[0] = '-'; |
| 3765 | } |
| 3766 | assert(len == numnondigits + numdigits); |
| 3767 | assert(numdigits > 0); |
| 3768 | } |
| 3769 | |
| 3770 | /* Fill with leading zeroes to meet minimum width. */ |
| 3771 | if (prec > numdigits) { |
| 3772 | PyObject *r1 = PyString_FromStringAndSize(NULL, |
| 3773 | numnondigits + prec); |
| 3774 | char *b1; |
| 3775 | if (!r1) { |
| 3776 | Py_DECREF(result); |
| 3777 | return NULL; |
| 3778 | } |
| 3779 | b1 = PyString_AS_STRING(r1); |
| 3780 | for (i = 0; i < numnondigits; ++i) |
| 3781 | *b1++ = *buf++; |
| 3782 | for (i = 0; i < prec - numdigits; i++) |
| 3783 | *b1++ = '0'; |
| 3784 | for (i = 0; i < numdigits; i++) |
| 3785 | *b1++ = *buf++; |
| 3786 | *b1 = '\0'; |
| 3787 | Py_DECREF(result); |
| 3788 | result = r1; |
| 3789 | buf = PyString_AS_STRING(result); |
| 3790 | len = numnondigits + prec; |
| 3791 | } |
| 3792 | |
| 3793 | /* Fix up case for hex conversions. */ |
Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 3794 | if (type == 'X') { |
| 3795 | /* Need to convert all lower case letters to upper case. |
| 3796 | and need to convert 0x to 0X (and -0x to -0X). */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3797 | for (i = 0; i < len; i++) |
Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 3798 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 3799 | buf[i] -= 'a'-'A'; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3800 | } |
| 3801 | *pbuf = buf; |
| 3802 | *plen = len; |
| 3803 | return result; |
| 3804 | } |
| 3805 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3806 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3807 | formatint(char *buf, size_t buflen, int flags, |
| 3808 | int prec, int type, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3809 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3810 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3811 | worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 3812 | + 1 + 1 = 24 */ |
| 3813 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 3814 | char *sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3815 | long x; |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 3816 | |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 3817 | x = PyInt_AsLong(v); |
| 3818 | if (x == -1 && PyErr_Occurred()) { |
| 3819 | PyErr_SetString(PyExc_TypeError, "int argument required"); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3820 | return -1; |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 3821 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 3822 | if (x < 0 && type == 'u') { |
| 3823 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 3824 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 3825 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 3826 | sign = "-"; |
| 3827 | else |
| 3828 | sign = ""; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3829 | if (prec < 0) |
| 3830 | prec = 1; |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 3831 | |
| 3832 | if ((flags & F_ALT) && |
| 3833 | (type == 'x' || type == 'X')) { |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3834 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 3835 | * of issues that cause pain: |
| 3836 | * - when 0 is being converted, the C standard leaves off |
| 3837 | * the '0x' or '0X', which is inconsistent with other |
| 3838 | * %#x/%#X conversions and inconsistent with Python's |
| 3839 | * hex() function |
| 3840 | * - there are platforms that violate the standard and |
| 3841 | * convert 0 with the '0x' or '0X' |
| 3842 | * (Metrowerks, Compaq Tru64) |
| 3843 | * - there are platforms that give '0x' when converting |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3844 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 3845 | * standard (OS/2 EMX) |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3846 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 3847 | * We can achieve the desired consistency by inserting our |
| 3848 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 3849 | * of %#x/%#X. |
| 3850 | * |
| 3851 | * Note that this is the same approach as used in |
| 3852 | * formatint() in unicodeobject.c |
| 3853 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 3854 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 3855 | sign, type, prec, type); |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 3856 | } |
| 3857 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 3858 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 3859 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 3860 | prec, type); |
| 3861 | } |
| 3862 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 3863 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 3864 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 3865 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 3866 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3867 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3868 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3869 | return -1; |
| 3870 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 3871 | if (sign[0]) |
| 3872 | PyOS_snprintf(buf, buflen, fmt, -x); |
| 3873 | else |
| 3874 | PyOS_snprintf(buf, buflen, fmt, x); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3875 | return (int)strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3876 | } |
| 3877 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3878 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3879 | formatchar(char *buf, size_t buflen, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3880 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3881 | /* presume that the buffer is at least 2 characters long */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3882 | if (PyString_Check(v)) { |
| 3883 | 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] | 3884 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3885 | } |
| 3886 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3887 | 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] | 3888 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3889 | } |
| 3890 | buf[1] = '\0'; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 3891 | return 1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3892 | } |
| 3893 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3894 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 3895 | |
| 3896 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 3897 | chars are formatted. XXX This is a magic number. Each formatting |
| 3898 | routine does bounds checking to ensure no overflow, but a better |
| 3899 | solution may be to malloc a buffer of appropriate size for each |
| 3900 | format. For now, the current solution is sufficient. |
| 3901 | */ |
| 3902 | #define FORMATBUFLEN (size_t)120 |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3903 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3904 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3905 | PyString_Format(PyObject *format, PyObject *args) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3906 | { |
| 3907 | char *fmt, *res; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3908 | Py_ssize_t arglen, argidx; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3909 | Py_ssize_t reslen, rescnt, fmtcnt; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 3910 | int args_owned = 0; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3911 | PyObject *result, *orig_args; |
| 3912 | #ifdef Py_USING_UNICODE |
| 3913 | PyObject *v, *w; |
| 3914 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3915 | PyObject *dict = NULL; |
| 3916 | if (format == NULL || !PyString_Check(format) || args == NULL) { |
| 3917 | PyErr_BadInternalCall(); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3918 | return NULL; |
| 3919 | } |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3920 | orig_args = args; |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 3921 | fmt = PyString_AS_STRING(format); |
| 3922 | fmtcnt = PyString_GET_SIZE(format); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 3923 | reslen = rescnt = fmtcnt + 100; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3924 | result = PyString_FromStringAndSize((char *)NULL, reslen); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3925 | if (result == NULL) |
| 3926 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3927 | res = PyString_AsString(result); |
| 3928 | if (PyTuple_Check(args)) { |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 3929 | arglen = PyTuple_GET_SIZE(args); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3930 | argidx = 0; |
| 3931 | } |
| 3932 | else { |
| 3933 | arglen = -1; |
| 3934 | argidx = -2; |
| 3935 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 3936 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 3937 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3938 | dict = args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3939 | while (--fmtcnt >= 0) { |
| 3940 | if (*fmt != '%') { |
| 3941 | if (--rescnt < 0) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 3942 | rescnt = fmtcnt + 100; |
| 3943 | reslen += rescnt; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3944 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3945 | return NULL; |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 3946 | res = PyString_AS_STRING(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3947 | + reslen - rescnt; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3948 | --rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3949 | } |
| 3950 | *res++ = *fmt++; |
| 3951 | } |
| 3952 | else { |
| 3953 | /* Got a format specifier */ |
| 3954 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3955 | Py_ssize_t width = -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3956 | int prec = -1; |
Guido van Rossum | 6938a29 | 1993-11-11 14:51:57 +0000 | [diff] [blame] | 3957 | int c = '\0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3958 | int fill; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3959 | PyObject *v = NULL; |
| 3960 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 3961 | char *pbuf; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3962 | int sign; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3963 | Py_ssize_t len; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3964 | char formatbuf[FORMATBUFLEN]; |
| 3965 | /* For format{float,int,char}() */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3966 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 3967 | char *fmt_start = fmt; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3968 | Py_ssize_t argidx_start = argidx; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3969 | #endif |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3970 | |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 3971 | fmt++; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3972 | if (*fmt == '(') { |
| 3973 | char *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3974 | Py_ssize_t keylen; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3975 | PyObject *key; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 3976 | int pcount = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3977 | |
| 3978 | if (dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3979 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3980 | "format requires a mapping"); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3981 | goto error; |
| 3982 | } |
| 3983 | ++fmt; |
| 3984 | --fmtcnt; |
| 3985 | keystart = fmt; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 3986 | /* Skip over balanced parentheses */ |
| 3987 | while (pcount > 0 && --fmtcnt >= 0) { |
| 3988 | if (*fmt == ')') |
| 3989 | --pcount; |
| 3990 | else if (*fmt == '(') |
| 3991 | ++pcount; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3992 | fmt++; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 3993 | } |
| 3994 | keylen = fmt - keystart - 1; |
| 3995 | if (fmtcnt < 0 || pcount > 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3996 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3997 | "incomplete format key"); |
| 3998 | goto error; |
| 3999 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4000 | key = PyString_FromStringAndSize(keystart, |
| 4001 | keylen); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4002 | if (key == NULL) |
| 4003 | goto error; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4004 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4005 | Py_DECREF(args); |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4006 | args_owned = 0; |
| 4007 | } |
| 4008 | args = PyObject_GetItem(dict, key); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4009 | Py_DECREF(key); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4010 | if (args == NULL) { |
| 4011 | goto error; |
| 4012 | } |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4013 | args_owned = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4014 | arglen = -1; |
| 4015 | argidx = -2; |
| 4016 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4017 | while (--fmtcnt >= 0) { |
| 4018 | switch (c = *fmt++) { |
| 4019 | case '-': flags |= F_LJUST; continue; |
| 4020 | case '+': flags |= F_SIGN; continue; |
| 4021 | case ' ': flags |= F_BLANK; continue; |
| 4022 | case '#': flags |= F_ALT; continue; |
| 4023 | case '0': flags |= F_ZERO; continue; |
| 4024 | } |
| 4025 | break; |
| 4026 | } |
| 4027 | if (c == '*') { |
| 4028 | v = getnextarg(args, arglen, &argidx); |
| 4029 | if (v == NULL) |
| 4030 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4031 | if (!PyInt_Check(v)) { |
| 4032 | PyErr_SetString(PyExc_TypeError, |
| 4033 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4034 | goto error; |
| 4035 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4036 | width = PyInt_AsLong(v); |
Guido van Rossum | 98c9eba | 1999-06-07 15:12:32 +0000 | [diff] [blame] | 4037 | if (width < 0) { |
| 4038 | flags |= F_LJUST; |
| 4039 | width = -width; |
| 4040 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4041 | if (--fmtcnt >= 0) |
| 4042 | c = *fmt++; |
| 4043 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4044 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4045 | width = c - '0'; |
| 4046 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4047 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4048 | if (!isdigit(c)) |
| 4049 | break; |
| 4050 | if ((width*10) / 10 != width) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4051 | PyErr_SetString( |
| 4052 | PyExc_ValueError, |
| 4053 | "width too big"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4054 | goto error; |
| 4055 | } |
| 4056 | width = width*10 + (c - '0'); |
| 4057 | } |
| 4058 | } |
| 4059 | if (c == '.') { |
| 4060 | prec = 0; |
| 4061 | if (--fmtcnt >= 0) |
| 4062 | c = *fmt++; |
| 4063 | if (c == '*') { |
| 4064 | v = getnextarg(args, arglen, &argidx); |
| 4065 | if (v == NULL) |
| 4066 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4067 | if (!PyInt_Check(v)) { |
| 4068 | PyErr_SetString( |
| 4069 | PyExc_TypeError, |
| 4070 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4071 | goto error; |
| 4072 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4073 | prec = PyInt_AsLong(v); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4074 | if (prec < 0) |
| 4075 | prec = 0; |
| 4076 | if (--fmtcnt >= 0) |
| 4077 | c = *fmt++; |
| 4078 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4079 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4080 | prec = c - '0'; |
| 4081 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4082 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4083 | if (!isdigit(c)) |
| 4084 | break; |
| 4085 | if ((prec*10) / 10 != prec) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4086 | PyErr_SetString( |
| 4087 | PyExc_ValueError, |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4088 | "prec too big"); |
| 4089 | goto error; |
| 4090 | } |
| 4091 | prec = prec*10 + (c - '0'); |
| 4092 | } |
| 4093 | } |
| 4094 | } /* prec */ |
| 4095 | if (fmtcnt >= 0) { |
| 4096 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4097 | if (--fmtcnt >= 0) |
| 4098 | c = *fmt++; |
| 4099 | } |
| 4100 | } |
| 4101 | if (fmtcnt < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4102 | PyErr_SetString(PyExc_ValueError, |
| 4103 | "incomplete format"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4104 | goto error; |
| 4105 | } |
| 4106 | if (c != '%') { |
| 4107 | v = getnextarg(args, arglen, &argidx); |
| 4108 | if (v == NULL) |
| 4109 | goto error; |
| 4110 | } |
| 4111 | sign = 0; |
| 4112 | fill = ' '; |
| 4113 | switch (c) { |
| 4114 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4115 | pbuf = "%"; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4116 | len = 1; |
| 4117 | break; |
| 4118 | case 's': |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4119 | #ifdef Py_USING_UNICODE |
Neil Schemenauer | ab61923 | 2005-08-31 23:02:05 +0000 | [diff] [blame] | 4120 | if (PyUnicode_Check(v)) { |
| 4121 | fmt = fmt_start; |
| 4122 | argidx = argidx_start; |
| 4123 | goto unicode; |
| 4124 | } |
Georg Brandl | d45014b | 2005-10-01 17:06:00 +0000 | [diff] [blame] | 4125 | #endif |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 4126 | temp = _PyObject_Str(v); |
Georg Brandl | d45014b | 2005-10-01 17:06:00 +0000 | [diff] [blame] | 4127 | #ifdef Py_USING_UNICODE |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 4128 | if (temp != NULL && PyUnicode_Check(temp)) { |
| 4129 | Py_DECREF(temp); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4130 | fmt = fmt_start; |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 4131 | argidx = argidx_start; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4132 | goto unicode; |
| 4133 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4134 | #endif |
Guido van Rossum | b00c07f | 2002-10-09 19:07:53 +0000 | [diff] [blame] | 4135 | /* Fall through */ |
Walter Dörwald | 9ff3f03 | 2003-06-18 14:17:01 +0000 | [diff] [blame] | 4136 | case 'r': |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 4137 | if (c == 'r') |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 4138 | temp = PyObject_Repr(v); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4139 | if (temp == NULL) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4140 | goto error; |
Guido van Rossum | 4a0144c | 1998-06-09 15:08:41 +0000 | [diff] [blame] | 4141 | if (!PyString_Check(temp)) { |
| 4142 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 8052f89 | 2002-10-09 19:14:30 +0000 | [diff] [blame] | 4143 | "%s argument has non-string str()"); |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4144 | Py_DECREF(temp); |
Guido van Rossum | 4a0144c | 1998-06-09 15:08:41 +0000 | [diff] [blame] | 4145 | goto error; |
| 4146 | } |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4147 | pbuf = PyString_AS_STRING(temp); |
| 4148 | len = PyString_GET_SIZE(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4149 | if (prec >= 0 && len > prec) |
| 4150 | len = prec; |
| 4151 | break; |
| 4152 | case 'i': |
| 4153 | case 'd': |
| 4154 | case 'u': |
| 4155 | case 'o': |
| 4156 | case 'x': |
| 4157 | case 'X': |
| 4158 | if (c == 'i') |
| 4159 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 4160 | if (PyLong_Check(v)) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4161 | int ilen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4162 | temp = _PyString_FormatLong(v, flags, |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4163 | prec, c, &pbuf, &ilen); |
| 4164 | len = ilen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4165 | if (!temp) |
| 4166 | goto error; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4167 | sign = 1; |
Guido van Rossum | 4acdc23 | 1997-01-29 06:00:24 +0000 | [diff] [blame] | 4168 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4169 | else { |
| 4170 | pbuf = formatbuf; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4171 | len = formatint(pbuf, |
| 4172 | sizeof(formatbuf), |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4173 | flags, prec, c, v); |
| 4174 | if (len < 0) |
| 4175 | goto error; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4176 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4177 | } |
| 4178 | if (flags & F_ZERO) |
| 4179 | fill = '0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4180 | break; |
| 4181 | case 'e': |
| 4182 | case 'E': |
| 4183 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 4184 | case 'F': |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4185 | case 'g': |
| 4186 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 4187 | if (c == 'F') |
| 4188 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4189 | pbuf = formatbuf; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4190 | len = formatfloat(pbuf, sizeof(formatbuf), |
| 4191 | flags, prec, c, v); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4192 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4193 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4194 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4195 | if (flags & F_ZERO) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4196 | fill = '0'; |
| 4197 | break; |
| 4198 | case 'c': |
Walter Dörwald | 43440a6 | 2003-03-31 18:07:50 +0000 | [diff] [blame] | 4199 | #ifdef Py_USING_UNICODE |
| 4200 | if (PyUnicode_Check(v)) { |
| 4201 | fmt = fmt_start; |
| 4202 | argidx = argidx_start; |
| 4203 | goto unicode; |
| 4204 | } |
| 4205 | #endif |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4206 | pbuf = formatbuf; |
| 4207 | len = formatchar(pbuf, sizeof(formatbuf), v); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4208 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4209 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4210 | break; |
| 4211 | default: |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4212 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 4213 | "unsupported format character '%c' (0x%x) " |
| 4214 | "at index %i", |
Guido van Rossum | efc1188 | 2002-09-12 14:43:41 +0000 | [diff] [blame] | 4215 | c, c, |
| 4216 | (int)(fmt - 1 - PyString_AsString(format))); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4217 | goto error; |
| 4218 | } |
| 4219 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4220 | if (*pbuf == '-' || *pbuf == '+') { |
| 4221 | sign = *pbuf++; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4222 | len--; |
| 4223 | } |
| 4224 | else if (flags & F_SIGN) |
| 4225 | sign = '+'; |
| 4226 | else if (flags & F_BLANK) |
| 4227 | sign = ' '; |
| 4228 | else |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4229 | sign = 0; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4230 | } |
| 4231 | if (width < len) |
| 4232 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 4233 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 4234 | reslen -= rescnt; |
| 4235 | rescnt = width + fmtcnt + 100; |
| 4236 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 4237 | if (reslen < 0) { |
| 4238 | Py_DECREF(result); |
| 4239 | return PyErr_NoMemory(); |
| 4240 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4241 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4242 | return NULL; |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4243 | res = PyString_AS_STRING(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4244 | + reslen - rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4245 | } |
| 4246 | if (sign) { |
Guido van Rossum | 71e57d0 | 1993-11-11 15:03:51 +0000 | [diff] [blame] | 4247 | if (fill != ' ') |
| 4248 | *res++ = sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4249 | rescnt--; |
| 4250 | if (width > len) |
| 4251 | width--; |
| 4252 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4253 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 4254 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4255 | assert(pbuf[1] == c); |
| 4256 | if (fill != ' ') { |
| 4257 | *res++ = *pbuf++; |
| 4258 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4259 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4260 | rescnt -= 2; |
| 4261 | width -= 2; |
| 4262 | if (width < 0) |
| 4263 | width = 0; |
| 4264 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4265 | } |
| 4266 | if (width > len && !(flags & F_LJUST)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4267 | do { |
| 4268 | --rescnt; |
| 4269 | *res++ = fill; |
| 4270 | } while (--width > len); |
| 4271 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4272 | if (fill == ' ') { |
| 4273 | if (sign) |
| 4274 | *res++ = sign; |
| 4275 | if ((flags & F_ALT) && |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4276 | (c == 'x' || c == 'X')) { |
| 4277 | assert(pbuf[0] == '0'); |
| 4278 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4279 | *res++ = *pbuf++; |
| 4280 | *res++ = *pbuf++; |
| 4281 | } |
| 4282 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4283 | memcpy(res, pbuf, len); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4284 | res += len; |
| 4285 | rescnt -= len; |
| 4286 | while (--width >= len) { |
| 4287 | --rescnt; |
| 4288 | *res++ = ' '; |
| 4289 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4290 | if (dict && (argidx < arglen) && c != '%') { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4291 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 4292 | "not all arguments converted during string formatting"); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4293 | goto error; |
| 4294 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4295 | Py_XDECREF(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4296 | } /* '%' */ |
| 4297 | } /* until end */ |
Guido van Rossum | caeaafc | 1995-02-27 10:13:23 +0000 | [diff] [blame] | 4298 | if (argidx < arglen && !dict) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4299 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 4300 | "not all arguments converted during string formatting"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4301 | goto error; |
| 4302 | } |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4303 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4304 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4305 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4306 | _PyString_Resize(&result, reslen - rescnt); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4307 | return result; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4308 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4309 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4310 | unicode: |
| 4311 | if (args_owned) { |
| 4312 | Py_DECREF(args); |
| 4313 | args_owned = 0; |
| 4314 | } |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 4315 | /* Fiddle args right (remove the first argidx arguments) */ |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4316 | if (PyTuple_Check(orig_args) && argidx > 0) { |
| 4317 | PyObject *v; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4318 | Py_ssize_t n = PyTuple_GET_SIZE(orig_args) - argidx; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4319 | v = PyTuple_New(n); |
| 4320 | if (v == NULL) |
| 4321 | goto error; |
| 4322 | while (--n >= 0) { |
| 4323 | PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx); |
| 4324 | Py_INCREF(w); |
| 4325 | PyTuple_SET_ITEM(v, n, w); |
| 4326 | } |
| 4327 | args = v; |
| 4328 | } else { |
| 4329 | Py_INCREF(orig_args); |
| 4330 | args = orig_args; |
| 4331 | } |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4332 | args_owned = 1; |
| 4333 | /* Take what we have of the result and let the Unicode formatting |
| 4334 | function format the rest of the input. */ |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4335 | rescnt = res - PyString_AS_STRING(result); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4336 | if (_PyString_Resize(&result, rescnt)) |
| 4337 | goto error; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4338 | fmtcnt = PyString_GET_SIZE(format) - \ |
| 4339 | (fmt - PyString_AS_STRING(format)); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4340 | format = PyUnicode_Decode(fmt, fmtcnt, NULL, NULL); |
| 4341 | if (format == NULL) |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4342 | goto error; |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4343 | v = PyUnicode_Format(format, args); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4344 | Py_DECREF(format); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4345 | if (v == NULL) |
| 4346 | goto error; |
| 4347 | /* Paste what we have (result) to what the Unicode formatting |
| 4348 | function returned (v) and return the result (or error) */ |
| 4349 | w = PyUnicode_Concat(result, v); |
| 4350 | Py_DECREF(result); |
| 4351 | Py_DECREF(v); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4352 | Py_DECREF(args); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4353 | return w; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4354 | #endif /* Py_USING_UNICODE */ |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4355 | |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4356 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4357 | Py_DECREF(result); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4358 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4359 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4360 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4361 | return NULL; |
| 4362 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4363 | |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4364 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4365 | PyString_InternInPlace(PyObject **p) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4366 | { |
| 4367 | register PyStringObject *s = (PyStringObject *)(*p); |
| 4368 | PyObject *t; |
| 4369 | if (s == NULL || !PyString_Check(s)) |
| 4370 | Py_FatalError("PyString_InternInPlace: strings only please!"); |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4371 | /* If it's a string subclass, we don't really know what putting |
| 4372 | it in the interned dict might do. */ |
| 4373 | if (!PyString_CheckExact(s)) |
| 4374 | return; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4375 | if (PyString_CHECK_INTERNED(s)) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4376 | return; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4377 | if (interned == NULL) { |
| 4378 | interned = PyDict_New(); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4379 | if (interned == NULL) { |
| 4380 | PyErr_Clear(); /* Don't leave an exception */ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4381 | return; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4382 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4383 | } |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4384 | t = PyDict_GetItem(interned, (PyObject *)s); |
| 4385 | if (t) { |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4386 | Py_INCREF(t); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4387 | Py_DECREF(*p); |
| 4388 | *p = t; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4389 | return; |
| 4390 | } |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4391 | |
Armin Rigo | 79f7ad2 | 2004-08-07 19:27:39 +0000 | [diff] [blame] | 4392 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4393 | PyErr_Clear(); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4394 | return; |
| 4395 | } |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4396 | /* The two references in interned are not counted by refcnt. |
| 4397 | The string deallocator will take care of this */ |
Armin Rigo | 79f7ad2 | 2004-08-07 19:27:39 +0000 | [diff] [blame] | 4398 | s->ob_refcnt -= 2; |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 4399 | PyString_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4400 | } |
| 4401 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4402 | void |
| 4403 | PyString_InternImmortal(PyObject **p) |
| 4404 | { |
| 4405 | PyString_InternInPlace(p); |
| 4406 | if (PyString_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 4407 | PyString_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 4408 | Py_INCREF(*p); |
| 4409 | } |
| 4410 | } |
| 4411 | |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4412 | |
| 4413 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4414 | PyString_InternFromString(const char *cp) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 4415 | { |
| 4416 | PyObject *s = PyString_FromString(cp); |
| 4417 | if (s == NULL) |
| 4418 | return NULL; |
| 4419 | PyString_InternInPlace(&s); |
| 4420 | return s; |
| 4421 | } |
| 4422 | |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 4423 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4424 | PyString_Fini(void) |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 4425 | { |
| 4426 | int i; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 4427 | for (i = 0; i < UCHAR_MAX + 1; i++) { |
| 4428 | Py_XDECREF(characters[i]); |
| 4429 | characters[i] = NULL; |
| 4430 | } |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 4431 | Py_XDECREF(nullstring); |
| 4432 | nullstring = NULL; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 4433 | } |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 4434 | |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 4435 | void _Py_ReleaseInternedStrings(void) |
| 4436 | { |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4437 | PyObject *keys; |
| 4438 | PyStringObject *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4439 | Py_ssize_t i, n; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4440 | |
| 4441 | if (interned == NULL || !PyDict_Check(interned)) |
| 4442 | return; |
| 4443 | keys = PyDict_Keys(interned); |
| 4444 | if (keys == NULL || !PyList_Check(keys)) { |
| 4445 | PyErr_Clear(); |
| 4446 | return; |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 4447 | } |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4448 | |
| 4449 | /* Since _Py_ReleaseInternedStrings() is intended to help a leak |
| 4450 | detector, interned strings are not forcibly deallocated; rather, we |
| 4451 | give them their stolen references back, and then clear and DECREF |
| 4452 | the interned dict. */ |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 4453 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4454 | fprintf(stderr, "releasing interned strings\n"); |
| 4455 | n = PyList_GET_SIZE(keys); |
| 4456 | for (i = 0; i < n; i++) { |
| 4457 | s = (PyStringObject *) PyList_GET_ITEM(keys, i); |
| 4458 | switch (s->ob_sstate) { |
| 4459 | case SSTATE_NOT_INTERNED: |
| 4460 | /* XXX Shouldn't happen */ |
| 4461 | break; |
| 4462 | case SSTATE_INTERNED_IMMORTAL: |
| 4463 | s->ob_refcnt += 1; |
| 4464 | break; |
| 4465 | case SSTATE_INTERNED_MORTAL: |
| 4466 | s->ob_refcnt += 2; |
| 4467 | break; |
| 4468 | default: |
| 4469 | Py_FatalError("Inconsistent interned string state."); |
| 4470 | } |
| 4471 | s->ob_sstate = SSTATE_NOT_INTERNED; |
| 4472 | } |
| 4473 | Py_DECREF(keys); |
| 4474 | PyDict_Clear(interned); |
| 4475 | Py_DECREF(interned); |
| 4476 | interned = NULL; |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 4477 | } |