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