| 1 | n/a | /* String (str/bytes) object implementation */ |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | #define PY_SSIZE_T_CLEAN |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | #include "Python.h" |
|---|
| 6 | n/a | #include <ctype.h> |
|---|
| 7 | n/a | #include <stddef.h> |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | #ifdef COUNT_ALLOCS |
|---|
| 10 | n/a | Py_ssize_t null_strings, one_strings; |
|---|
| 11 | n/a | #endif |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | static PyStringObject *characters[UCHAR_MAX + 1]; |
|---|
| 14 | n/a | static PyStringObject *nullstring; |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | /* This dictionary holds all interned strings. Note that references to |
|---|
| 17 | n/a | strings in this dictionary are *not* counted in the string's ob_refcnt. |
|---|
| 18 | n/a | When the interned string reaches a refcnt of 0 the string deallocation |
|---|
| 19 | n/a | function will delete the reference from this dictionary. |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | Another way to look at this is that to say that the actual reference |
|---|
| 22 | n/a | count of a string is: s->ob_refcnt + (s->ob_sstate?2:0) |
|---|
| 23 | n/a | */ |
|---|
| 24 | n/a | static PyObject *interned; |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | /* PyStringObject_SIZE gives the basic size of a string; any memory allocation |
|---|
| 27 | n/a | for a string of length n should request PyStringObject_SIZE + n bytes. |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | Using PyStringObject_SIZE instead of sizeof(PyStringObject) saves |
|---|
| 30 | n/a | 3 bytes per string allocation on a typical system. |
|---|
| 31 | n/a | */ |
|---|
| 32 | n/a | #define PyStringObject_SIZE (offsetof(PyStringObject, ob_sval) + 1) |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | /* |
|---|
| 35 | n/a | For both PyString_FromString() and PyString_FromStringAndSize(), the |
|---|
| 36 | n/a | parameter `size' denotes number of characters to allocate, not counting any |
|---|
| 37 | n/a | null terminating character. |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | For PyString_FromString(), the parameter `str' points to a null-terminated |
|---|
| 40 | n/a | string containing exactly `size' bytes. |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | For PyString_FromStringAndSize(), the parameter the parameter `str' is |
|---|
| 43 | n/a | either NULL or else points to a string containing at least `size' bytes. |
|---|
| 44 | n/a | For PyString_FromStringAndSize(), the string in the `str' parameter does |
|---|
| 45 | n/a | not have to be null-terminated. (Therefore it is safe to construct a |
|---|
| 46 | n/a | substring by calling `PyString_FromStringAndSize(origstring, substrlen)'.) |
|---|
| 47 | n/a | If `str' is NULL then PyString_FromStringAndSize() will allocate `size+1' |
|---|
| 48 | n/a | bytes (setting the last byte to the null terminating character) and you can |
|---|
| 49 | n/a | fill in the data yourself. If `str' is non-NULL then the resulting |
|---|
| 50 | n/a | PyString object must be treated as immutable and you must not fill in nor |
|---|
| 51 | n/a | alter the data yourself, since the strings may be shared. |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | The PyObject member `op->ob_size', which denotes the number of "extra |
|---|
| 54 | n/a | items" in a variable-size object, will contain the number of bytes |
|---|
| 55 | n/a | allocated for string data, not counting the null terminating character. It |
|---|
| 56 | n/a | is therefore equal to the equal to the `size' parameter (for |
|---|
| 57 | n/a | PyString_FromStringAndSize()) or the length of the string in the `str' |
|---|
| 58 | n/a | parameter (for PyString_FromString()). |
|---|
| 59 | n/a | */ |
|---|
| 60 | n/a | PyObject * |
|---|
| 61 | n/a | PyString_FromStringAndSize(const char *str, Py_ssize_t size) |
|---|
| 62 | 216587472 | { |
|---|
| 63 | n/a | register PyStringObject *op; |
|---|
| 64 | 216587472 | if (size < 0) { |
|---|
| 65 | 0 | PyErr_SetString(PyExc_SystemError, |
|---|
| 66 | n/a | "Negative size passed to PyString_FromStringAndSize"); |
|---|
| 67 | 0 | return NULL; |
|---|
| 68 | n/a | } |
|---|
| 69 | 216587472 | if (size == 0 && (op = nullstring) != NULL) { |
|---|
| 70 | n/a | #ifdef COUNT_ALLOCS |
|---|
| 71 | n/a | null_strings++; |
|---|
| 72 | n/a | #endif |
|---|
| 73 | 672721 | Py_INCREF(op); |
|---|
| 74 | 672721 | return (PyObject *)op; |
|---|
| 75 | n/a | } |
|---|
| 76 | 215914751 | if (size == 1 && str != NULL && |
|---|
| 77 | n/a | (op = characters[*str & UCHAR_MAX]) != NULL) |
|---|
| 78 | n/a | { |
|---|
| 79 | n/a | #ifdef COUNT_ALLOCS |
|---|
| 80 | n/a | one_strings++; |
|---|
| 81 | n/a | #endif |
|---|
| 82 | 9142408 | Py_INCREF(op); |
|---|
| 83 | 9142408 | return (PyObject *)op; |
|---|
| 84 | n/a | } |
|---|
| 85 | n/a | |
|---|
| 86 | 206772343 | if (size > PY_SSIZE_T_MAX - PyStringObject_SIZE) { |
|---|
| 87 | 0 | PyErr_SetString(PyExc_OverflowError, "string is too large"); |
|---|
| 88 | 0 | return NULL; |
|---|
| 89 | n/a | } |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | /* Inline PyObject_NewVar */ |
|---|
| 92 | 206772343 | op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + size); |
|---|
| 93 | 206772343 | if (op == NULL) |
|---|
| 94 | 0 | return PyErr_NoMemory(); |
|---|
| 95 | 206772343 | PyObject_INIT_VAR(op, &PyString_Type, size); |
|---|
| 96 | 206772343 | op->ob_shash = -1; |
|---|
| 97 | 206772343 | op->ob_sstate = SSTATE_NOT_INTERNED; |
|---|
| 98 | 206772343 | if (str != NULL) |
|---|
| 99 | 190861107 | Py_MEMCPY(op->ob_sval, str, size); |
|---|
| 100 | 206772343 | op->ob_sval[size] = '\0'; |
|---|
| 101 | n/a | /* share short strings */ |
|---|
| 102 | 206772343 | if (size == 0) { |
|---|
| 103 | 293 | PyObject *t = (PyObject *)op; |
|---|
| 104 | 293 | PyString_InternInPlace(&t); |
|---|
| 105 | 293 | op = (PyStringObject *)t; |
|---|
| 106 | 293 | nullstring = op; |
|---|
| 107 | 293 | Py_INCREF(op); |
|---|
| 108 | 206772050 | } else if (size == 1 && str != NULL) { |
|---|
| 109 | 27080 | PyObject *t = (PyObject *)op; |
|---|
| 110 | 27080 | PyString_InternInPlace(&t); |
|---|
| 111 | 27080 | op = (PyStringObject *)t; |
|---|
| 112 | 27080 | characters[*str & UCHAR_MAX] = op; |
|---|
| 113 | 27080 | Py_INCREF(op); |
|---|
| 114 | n/a | } |
|---|
| 115 | 206772343 | return (PyObject *) op; |
|---|
| 116 | n/a | } |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | PyObject * |
|---|
| 119 | n/a | PyString_FromString(const char *str) |
|---|
| 120 | 801461219 | { |
|---|
| 121 | n/a | register size_t size; |
|---|
| 122 | n/a | register PyStringObject *op; |
|---|
| 123 | n/a | |
|---|
| 124 | 801461219 | assert(str != NULL); |
|---|
| 125 | 801461219 | size = strlen(str); |
|---|
| 126 | 801461219 | if (size > PY_SSIZE_T_MAX - PyStringObject_SIZE) { |
|---|
| 127 | 0 | PyErr_SetString(PyExc_OverflowError, |
|---|
| 128 | n/a | "string is too long for a Python string"); |
|---|
| 129 | 0 | return NULL; |
|---|
| 130 | n/a | } |
|---|
| 131 | 801461219 | if (size == 0 && (op = nullstring) != NULL) { |
|---|
| 132 | n/a | #ifdef COUNT_ALLOCS |
|---|
| 133 | n/a | null_strings++; |
|---|
| 134 | n/a | #endif |
|---|
| 135 | 1049629 | Py_INCREF(op); |
|---|
| 136 | 1049629 | return (PyObject *)op; |
|---|
| 137 | n/a | } |
|---|
| 138 | 800411590 | if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
|---|
| 139 | n/a | #ifdef COUNT_ALLOCS |
|---|
| 140 | n/a | one_strings++; |
|---|
| 141 | n/a | #endif |
|---|
| 142 | 379260 | Py_INCREF(op); |
|---|
| 143 | 379260 | return (PyObject *)op; |
|---|
| 144 | n/a | } |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | /* Inline PyObject_NewVar */ |
|---|
| 147 | 800032330 | op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + size); |
|---|
| 148 | 800032330 | if (op == NULL) |
|---|
| 149 | 0 | return PyErr_NoMemory(); |
|---|
| 150 | 800032330 | PyObject_INIT_VAR(op, &PyString_Type, size); |
|---|
| 151 | 800032330 | op->ob_shash = -1; |
|---|
| 152 | 800032330 | op->ob_sstate = SSTATE_NOT_INTERNED; |
|---|
| 153 | 800032330 | Py_MEMCPY(op->ob_sval, str, size+1); |
|---|
| 154 | n/a | /* share short strings */ |
|---|
| 155 | 800032330 | if (size == 0) { |
|---|
| 156 | 0 | PyObject *t = (PyObject *)op; |
|---|
| 157 | 0 | PyString_InternInPlace(&t); |
|---|
| 158 | 0 | op = (PyStringObject *)t; |
|---|
| 159 | 0 | nullstring = op; |
|---|
| 160 | 0 | Py_INCREF(op); |
|---|
| 161 | 800032330 | } else if (size == 1) { |
|---|
| 162 | 1215 | PyObject *t = (PyObject *)op; |
|---|
| 163 | 1215 | PyString_InternInPlace(&t); |
|---|
| 164 | 1215 | op = (PyStringObject *)t; |
|---|
| 165 | 1215 | characters[*str & UCHAR_MAX] = op; |
|---|
| 166 | 1215 | Py_INCREF(op); |
|---|
| 167 | n/a | } |
|---|
| 168 | 800032330 | return (PyObject *) op; |
|---|
| 169 | n/a | } |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | PyObject * |
|---|
| 172 | n/a | PyString_FromFormatV(const char *format, va_list vargs) |
|---|
| 173 | 2611452 | { |
|---|
| 174 | n/a | va_list count; |
|---|
| 175 | 2611452 | Py_ssize_t n = 0; |
|---|
| 176 | n/a | const char* f; |
|---|
| 177 | n/a | char *s; |
|---|
| 178 | n/a | PyObject* string; |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | #ifdef VA_LIST_IS_ARRAY |
|---|
| 181 | 2611452 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
|---|
| 182 | n/a | #else |
|---|
| 183 | n/a | #ifdef __va_copy |
|---|
| 184 | n/a | __va_copy(count, vargs); |
|---|
| 185 | n/a | #else |
|---|
| 186 | n/a | count = vargs; |
|---|
| 187 | n/a | #endif |
|---|
| 188 | n/a | #endif |
|---|
| 189 | n/a | /* step 1: figure out how large a buffer we need */ |
|---|
| 190 | 82079217 | for (f = format; *f; f++) { |
|---|
| 191 | 79467766 | if (*f == '%') { |
|---|
| 192 | n/a | #ifdef HAVE_LONG_LONG |
|---|
| 193 | 4643160 | int longlongflag = 0; |
|---|
| 194 | n/a | #endif |
|---|
| 195 | 4643160 | const char* p = f; |
|---|
| 196 | 19784586 | while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
|---|
| 197 | n/a | ; |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
|---|
| 200 | n/a | * they don't affect the amount of space we reserve. |
|---|
| 201 | n/a | */ |
|---|
| 202 | 4643160 | if (*f == 'l') { |
|---|
| 203 | 99 | if (f[1] == 'd' || f[1] == 'u') { |
|---|
| 204 | 48 | ++f; |
|---|
| 205 | n/a | } |
|---|
| 206 | n/a | #ifdef HAVE_LONG_LONG |
|---|
| 207 | 3 | else if (f[1] == 'l' && |
|---|
| 208 | n/a | (f[2] == 'd' || f[2] == 'u')) { |
|---|
| 209 | 2 | longlongflag = 1; |
|---|
| 210 | 2 | f += 2; |
|---|
| 211 | n/a | } |
|---|
| 212 | n/a | #endif |
|---|
| 213 | n/a | } |
|---|
| 214 | 4643109 | else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
|---|
| 215 | 36458 | ++f; |
|---|
| 216 | n/a | } |
|---|
| 217 | n/a | |
|---|
| 218 | 4643160 | switch (*f) { |
|---|
| 219 | n/a | case 'c': |
|---|
| 220 | 46077 | (void)va_arg(count, int); |
|---|
| 221 | n/a | /* fall through... */ |
|---|
| 222 | n/a | case '%': |
|---|
| 223 | 46186 | n++; |
|---|
| 224 | 46186 | break; |
|---|
| 225 | n/a | case 'd': case 'u': case 'i': case 'x': |
|---|
| 226 | 112593 | (void) va_arg(count, int); |
|---|
| 227 | n/a | #ifdef HAVE_LONG_LONG |
|---|
| 228 | n/a | /* Need at most |
|---|
| 229 | n/a | ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
|---|
| 230 | n/a | plus 1 for the sign. 53/22 is an upper |
|---|
| 231 | n/a | bound for log10(256). */ |
|---|
| 232 | 112593 | if (longlongflag) |
|---|
| 233 | 2 | n += 2 + (SIZEOF_LONG_LONG*53-1) / 22; |
|---|
| 234 | n/a | else |
|---|
| 235 | n/a | #endif |
|---|
| 236 | n/a | /* 20 bytes is enough to hold a 64-bit |
|---|
| 237 | n/a | integer. Decimal takes the most |
|---|
| 238 | n/a | space. This isn't enough for |
|---|
| 239 | n/a | octal. */ |
|---|
| 240 | 112591 | n += 20; |
|---|
| 241 | n/a | |
|---|
| 242 | 112593 | break; |
|---|
| 243 | n/a | case 's': |
|---|
| 244 | 4484073 | s = va_arg(count, char*); |
|---|
| 245 | 4484073 | n += strlen(s); |
|---|
| 246 | 4484073 | break; |
|---|
| 247 | n/a | case 'p': |
|---|
| 248 | 307 | (void) va_arg(count, int); |
|---|
| 249 | n/a | /* maximum 64-bit pointer representation: |
|---|
| 250 | n/a | * 0xffffffffffffffff |
|---|
| 251 | n/a | * so 19 characters is enough. |
|---|
| 252 | n/a | * XXX I count 18 -- what's the extra for? |
|---|
| 253 | n/a | */ |
|---|
| 254 | 307 | n += 19; |
|---|
| 255 | 307 | break; |
|---|
| 256 | n/a | default: |
|---|
| 257 | n/a | /* if we stumble upon an unknown |
|---|
| 258 | n/a | formatting code, copy the rest of |
|---|
| 259 | n/a | the format string to the output |
|---|
| 260 | n/a | string. (we cannot just skip the |
|---|
| 261 | n/a | code, since there's no way to know |
|---|
| 262 | n/a | what's in the argument list) */ |
|---|
| 263 | 1 | n += strlen(p); |
|---|
| 264 | 1 | goto expand; |
|---|
| 265 | n/a | } |
|---|
| 266 | n/a | } else |
|---|
| 267 | 74824606 | n++; |
|---|
| 268 | n/a | } |
|---|
| 269 | 2611452 | expand: |
|---|
| 270 | n/a | /* step 2: fill the buffer */ |
|---|
| 271 | n/a | /* Since we've analyzed how much space we need for the worst case, |
|---|
| 272 | n/a | use sprintf directly instead of the slower PyOS_snprintf. */ |
|---|
| 273 | 2611452 | string = PyString_FromStringAndSize(NULL, n); |
|---|
| 274 | 2611452 | if (!string) |
|---|
| 275 | 0 | return NULL; |
|---|
| 276 | n/a | |
|---|
| 277 | 2611452 | s = PyString_AsString(string); |
|---|
| 278 | n/a | |
|---|
| 279 | 82079217 | for (f = format; *f; f++) { |
|---|
| 280 | 79467766 | if (*f == '%') { |
|---|
| 281 | 4643160 | const char* p = f++; |
|---|
| 282 | n/a | Py_ssize_t i; |
|---|
| 283 | 4643160 | int longflag = 0; |
|---|
| 284 | n/a | #ifdef HAVE_LONG_LONG |
|---|
| 285 | 4643160 | int longlongflag = 0; |
|---|
| 286 | n/a | #endif |
|---|
| 287 | 4643160 | int size_tflag = 0; |
|---|
| 288 | n/a | /* parse the width.precision part (we're only |
|---|
| 289 | n/a | interested in the precision value, if any) */ |
|---|
| 290 | 4643160 | n = 0; |
|---|
| 291 | 9286461 | while (isdigit(Py_CHARMASK(*f))) |
|---|
| 292 | 141 | n = (n*10) + *f++ - '0'; |
|---|
| 293 | 4643160 | if (*f == '.') { |
|---|
| 294 | 4237658 | f++; |
|---|
| 295 | 4237658 | n = 0; |
|---|
| 296 | 19378943 | while (isdigit(Py_CHARMASK(*f))) |
|---|
| 297 | 10903627 | n = (n*10) + *f++ - '0'; |
|---|
| 298 | n/a | } |
|---|
| 299 | 9286320 | while (*f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
|---|
| 300 | 0 | f++; |
|---|
| 301 | n/a | /* Handle %ld, %lu, %lld and %llu. */ |
|---|
| 302 | 4643160 | if (*f == 'l') { |
|---|
| 303 | 99 | if (f[1] == 'd' || f[1] == 'u') { |
|---|
| 304 | 48 | longflag = 1; |
|---|
| 305 | 48 | ++f; |
|---|
| 306 | n/a | } |
|---|
| 307 | n/a | #ifdef HAVE_LONG_LONG |
|---|
| 308 | 3 | else if (f[1] == 'l' && |
|---|
| 309 | n/a | (f[2] == 'd' || f[2] == 'u')) { |
|---|
| 310 | 2 | longlongflag = 1; |
|---|
| 311 | 2 | f += 2; |
|---|
| 312 | n/a | } |
|---|
| 313 | n/a | #endif |
|---|
| 314 | n/a | } |
|---|
| 315 | n/a | /* handle the size_t flag. */ |
|---|
| 316 | 4643109 | else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
|---|
| 317 | 36458 | size_tflag = 1; |
|---|
| 318 | 36458 | ++f; |
|---|
| 319 | n/a | } |
|---|
| 320 | n/a | |
|---|
| 321 | 4643160 | switch (*f) { |
|---|
| 322 | n/a | case 'c': |
|---|
| 323 | 46077 | *s++ = va_arg(vargs, int); |
|---|
| 324 | 46077 | break; |
|---|
| 325 | n/a | case 'd': |
|---|
| 326 | 112377 | if (longflag) |
|---|
| 327 | 47 | sprintf(s, "%ld", va_arg(vargs, long)); |
|---|
| 328 | n/a | #ifdef HAVE_LONG_LONG |
|---|
| 329 | 112330 | else if (longlongflag) |
|---|
| 330 | 1 | sprintf(s, "%" PY_FORMAT_LONG_LONG "d", |
|---|
| 331 | n/a | va_arg(vargs, PY_LONG_LONG)); |
|---|
| 332 | n/a | #endif |
|---|
| 333 | 112329 | else if (size_tflag) |
|---|
| 334 | 36253 | sprintf(s, "%" PY_FORMAT_SIZE_T "d", |
|---|
| 335 | n/a | va_arg(vargs, Py_ssize_t)); |
|---|
| 336 | n/a | else |
|---|
| 337 | 76076 | sprintf(s, "%d", va_arg(vargs, int)); |
|---|
| 338 | 112377 | s += strlen(s); |
|---|
| 339 | 112377 | break; |
|---|
| 340 | n/a | case 'u': |
|---|
| 341 | 208 | if (longflag) |
|---|
| 342 | 1 | sprintf(s, "%lu", |
|---|
| 343 | n/a | va_arg(vargs, unsigned long)); |
|---|
| 344 | n/a | #ifdef HAVE_LONG_LONG |
|---|
| 345 | 207 | else if (longlongflag) |
|---|
| 346 | 1 | sprintf(s, "%" PY_FORMAT_LONG_LONG "u", |
|---|
| 347 | n/a | va_arg(vargs, PY_LONG_LONG)); |
|---|
| 348 | n/a | #endif |
|---|
| 349 | 206 | else if (size_tflag) |
|---|
| 350 | 205 | sprintf(s, "%" PY_FORMAT_SIZE_T "u", |
|---|
| 351 | n/a | va_arg(vargs, size_t)); |
|---|
| 352 | n/a | else |
|---|
| 353 | 1 | sprintf(s, "%u", |
|---|
| 354 | n/a | va_arg(vargs, unsigned int)); |
|---|
| 355 | 208 | s += strlen(s); |
|---|
| 356 | 208 | break; |
|---|
| 357 | n/a | case 'i': |
|---|
| 358 | 6 | sprintf(s, "%i", va_arg(vargs, int)); |
|---|
| 359 | 6 | s += strlen(s); |
|---|
| 360 | 6 | break; |
|---|
| 361 | n/a | case 'x': |
|---|
| 362 | 2 | sprintf(s, "%x", va_arg(vargs, int)); |
|---|
| 363 | 2 | s += strlen(s); |
|---|
| 364 | 2 | break; |
|---|
| 365 | n/a | case 's': |
|---|
| 366 | 4484073 | p = va_arg(vargs, char*); |
|---|
| 367 | 4484073 | i = strlen(p); |
|---|
| 368 | 4484073 | if (n > 0 && i > n) |
|---|
| 369 | 19 | i = n; |
|---|
| 370 | 4484073 | Py_MEMCPY(s, p, i); |
|---|
| 371 | 4484073 | s += i; |
|---|
| 372 | 4484073 | break; |
|---|
| 373 | n/a | case 'p': |
|---|
| 374 | 307 | sprintf(s, "%p", va_arg(vargs, void*)); |
|---|
| 375 | n/a | /* %p is ill-defined: ensure leading 0x. */ |
|---|
| 376 | 307 | if (s[1] == 'X') |
|---|
| 377 | 0 | s[1] = 'x'; |
|---|
| 378 | 307 | else if (s[1] != 'x') { |
|---|
| 379 | 0 | memmove(s+2, s, strlen(s)+1); |
|---|
| 380 | 0 | s[0] = '0'; |
|---|
| 381 | 0 | s[1] = 'x'; |
|---|
| 382 | n/a | } |
|---|
| 383 | 307 | s += strlen(s); |
|---|
| 384 | 307 | break; |
|---|
| 385 | n/a | case '%': |
|---|
| 386 | 109 | *s++ = '%'; |
|---|
| 387 | 109 | break; |
|---|
| 388 | n/a | default: |
|---|
| 389 | 1 | strcpy(s, p); |
|---|
| 390 | 1 | s += strlen(s); |
|---|
| 391 | 1 | goto end; |
|---|
| 392 | n/a | } |
|---|
| 393 | n/a | } else |
|---|
| 394 | 74824606 | *s++ = *f; |
|---|
| 395 | n/a | } |
|---|
| 396 | n/a | |
|---|
| 397 | 2611452 | end: |
|---|
| 398 | 2611452 | if (_PyString_Resize(&string, s - PyString_AS_STRING(string))) |
|---|
| 399 | 0 | return NULL; |
|---|
| 400 | 2611452 | return string; |
|---|
| 401 | n/a | } |
|---|
| 402 | n/a | |
|---|
| 403 | n/a | PyObject * |
|---|
| 404 | n/a | PyString_FromFormat(const char *format, ...) |
|---|
| 405 | 85012 | { |
|---|
| 406 | n/a | PyObject* ret; |
|---|
| 407 | n/a | va_list vargs; |
|---|
| 408 | n/a | |
|---|
| 409 | n/a | #ifdef HAVE_STDARG_PROTOTYPES |
|---|
| 410 | 85012 | va_start(vargs, format); |
|---|
| 411 | n/a | #else |
|---|
| 412 | n/a | va_start(vargs); |
|---|
| 413 | n/a | #endif |
|---|
| 414 | 85012 | ret = PyString_FromFormatV(format, vargs); |
|---|
| 415 | 85012 | va_end(vargs); |
|---|
| 416 | 85012 | return ret; |
|---|
| 417 | n/a | } |
|---|
| 418 | n/a | |
|---|
| 419 | n/a | |
|---|
| 420 | n/a | PyObject *PyString_Decode(const char *s, |
|---|
| 421 | n/a | Py_ssize_t size, |
|---|
| 422 | n/a | const char *encoding, |
|---|
| 423 | n/a | const char *errors) |
|---|
| 424 | 0 | { |
|---|
| 425 | n/a | PyObject *v, *str; |
|---|
| 426 | n/a | |
|---|
| 427 | 0 | str = PyString_FromStringAndSize(s, size); |
|---|
| 428 | 0 | if (str == NULL) |
|---|
| 429 | 0 | return NULL; |
|---|
| 430 | 0 | v = PyString_AsDecodedString(str, encoding, errors); |
|---|
| 431 | 0 | Py_DECREF(str); |
|---|
| 432 | 0 | return v; |
|---|
| 433 | n/a | } |
|---|
| 434 | n/a | |
|---|
| 435 | n/a | PyObject *PyString_AsDecodedObject(PyObject *str, |
|---|
| 436 | n/a | const char *encoding, |
|---|
| 437 | n/a | const char *errors) |
|---|
| 438 | 1313281 | { |
|---|
| 439 | n/a | PyObject *v; |
|---|
| 440 | n/a | |
|---|
| 441 | 1313281 | if (!PyString_Check(str)) { |
|---|
| 442 | 0 | PyErr_BadArgument(); |
|---|
| 443 | 0 | goto onError; |
|---|
| 444 | n/a | } |
|---|
| 445 | n/a | |
|---|
| 446 | 1313281 | if (encoding == NULL) { |
|---|
| 447 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 448 | 0 | encoding = PyUnicode_GetDefaultEncoding(); |
|---|
| 449 | n/a | #else |
|---|
| 450 | n/a | PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
|---|
| 451 | n/a | goto onError; |
|---|
| 452 | n/a | #endif |
|---|
| 453 | n/a | } |
|---|
| 454 | n/a | |
|---|
| 455 | n/a | /* Decode via the codec registry */ |
|---|
| 456 | 1313281 | v = PyCodec_Decode(str, encoding, errors); |
|---|
| 457 | 1313281 | if (v == NULL) |
|---|
| 458 | 550 | goto onError; |
|---|
| 459 | n/a | |
|---|
| 460 | 1312731 | return v; |
|---|
| 461 | n/a | |
|---|
| 462 | 550 | onError: |
|---|
| 463 | 550 | return NULL; |
|---|
| 464 | n/a | } |
|---|
| 465 | n/a | |
|---|
| 466 | n/a | PyObject *PyString_AsDecodedString(PyObject *str, |
|---|
| 467 | n/a | const char *encoding, |
|---|
| 468 | n/a | const char *errors) |
|---|
| 469 | 0 | { |
|---|
| 470 | n/a | PyObject *v; |
|---|
| 471 | n/a | |
|---|
| 472 | 0 | v = PyString_AsDecodedObject(str, encoding, errors); |
|---|
| 473 | 0 | if (v == NULL) |
|---|
| 474 | 0 | goto onError; |
|---|
| 475 | n/a | |
|---|
| 476 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 477 | n/a | /* Convert Unicode to a string using the default encoding */ |
|---|
| 478 | 0 | if (PyUnicode_Check(v)) { |
|---|
| 479 | 0 | PyObject *temp = v; |
|---|
| 480 | 0 | v = PyUnicode_AsEncodedString(v, NULL, NULL); |
|---|
| 481 | 0 | Py_DECREF(temp); |
|---|
| 482 | 0 | if (v == NULL) |
|---|
| 483 | 0 | goto onError; |
|---|
| 484 | n/a | } |
|---|
| 485 | n/a | #endif |
|---|
| 486 | 0 | if (!PyString_Check(v)) { |
|---|
| 487 | 0 | PyErr_Format(PyExc_TypeError, |
|---|
| 488 | n/a | "decoder did not return a string object (type=%.400s)", |
|---|
| 489 | n/a | Py_TYPE(v)->tp_name); |
|---|
| 490 | 0 | Py_DECREF(v); |
|---|
| 491 | 0 | goto onError; |
|---|
| 492 | n/a | } |
|---|
| 493 | n/a | |
|---|
| 494 | 0 | return v; |
|---|
| 495 | n/a | |
|---|
| 496 | 0 | onError: |
|---|
| 497 | 0 | return NULL; |
|---|
| 498 | n/a | } |
|---|
| 499 | n/a | |
|---|
| 500 | n/a | PyObject *PyString_Encode(const char *s, |
|---|
| 501 | n/a | Py_ssize_t size, |
|---|
| 502 | n/a | const char *encoding, |
|---|
| 503 | n/a | const char *errors) |
|---|
| 504 | 0 | { |
|---|
| 505 | n/a | PyObject *v, *str; |
|---|
| 506 | n/a | |
|---|
| 507 | 0 | str = PyString_FromStringAndSize(s, size); |
|---|
| 508 | 0 | if (str == NULL) |
|---|
| 509 | 0 | return NULL; |
|---|
| 510 | 0 | v = PyString_AsEncodedString(str, encoding, errors); |
|---|
| 511 | 0 | Py_DECREF(str); |
|---|
| 512 | 0 | return v; |
|---|
| 513 | n/a | } |
|---|
| 514 | n/a | |
|---|
| 515 | n/a | PyObject *PyString_AsEncodedObject(PyObject *str, |
|---|
| 516 | n/a | const char *encoding, |
|---|
| 517 | n/a | const char *errors) |
|---|
| 518 | 4746 | { |
|---|
| 519 | n/a | PyObject *v; |
|---|
| 520 | n/a | |
|---|
| 521 | 4746 | if (!PyString_Check(str)) { |
|---|
| 522 | 0 | PyErr_BadArgument(); |
|---|
| 523 | 0 | goto onError; |
|---|
| 524 | n/a | } |
|---|
| 525 | n/a | |
|---|
| 526 | 4746 | if (encoding == NULL) { |
|---|
| 527 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 528 | 0 | encoding = PyUnicode_GetDefaultEncoding(); |
|---|
| 529 | n/a | #else |
|---|
| 530 | n/a | PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
|---|
| 531 | n/a | goto onError; |
|---|
| 532 | n/a | #endif |
|---|
| 533 | n/a | } |
|---|
| 534 | n/a | |
|---|
| 535 | n/a | /* Encode via the codec registry */ |
|---|
| 536 | 4746 | v = PyCodec_Encode(str, encoding, errors); |
|---|
| 537 | 4746 | if (v == NULL) |
|---|
| 538 | 2 | goto onError; |
|---|
| 539 | n/a | |
|---|
| 540 | 4744 | return v; |
|---|
| 541 | n/a | |
|---|
| 542 | 2 | onError: |
|---|
| 543 | 2 | return NULL; |
|---|
| 544 | n/a | } |
|---|
| 545 | n/a | |
|---|
| 546 | n/a | PyObject *PyString_AsEncodedString(PyObject *str, |
|---|
| 547 | n/a | const char *encoding, |
|---|
| 548 | n/a | const char *errors) |
|---|
| 549 | 0 | { |
|---|
| 550 | n/a | PyObject *v; |
|---|
| 551 | n/a | |
|---|
| 552 | 0 | v = PyString_AsEncodedObject(str, encoding, errors); |
|---|
| 553 | 0 | if (v == NULL) |
|---|
| 554 | 0 | goto onError; |
|---|
| 555 | n/a | |
|---|
| 556 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 557 | n/a | /* Convert Unicode to a string using the default encoding */ |
|---|
| 558 | 0 | if (PyUnicode_Check(v)) { |
|---|
| 559 | 0 | PyObject *temp = v; |
|---|
| 560 | 0 | v = PyUnicode_AsEncodedString(v, NULL, NULL); |
|---|
| 561 | 0 | Py_DECREF(temp); |
|---|
| 562 | 0 | if (v == NULL) |
|---|
| 563 | 0 | goto onError; |
|---|
| 564 | n/a | } |
|---|
| 565 | n/a | #endif |
|---|
| 566 | 0 | if (!PyString_Check(v)) { |
|---|
| 567 | 0 | PyErr_Format(PyExc_TypeError, |
|---|
| 568 | n/a | "encoder did not return a string object (type=%.400s)", |
|---|
| 569 | n/a | Py_TYPE(v)->tp_name); |
|---|
| 570 | 0 | Py_DECREF(v); |
|---|
| 571 | 0 | goto onError; |
|---|
| 572 | n/a | } |
|---|
| 573 | n/a | |
|---|
| 574 | 0 | return v; |
|---|
| 575 | n/a | |
|---|
| 576 | 0 | onError: |
|---|
| 577 | 0 | return NULL; |
|---|
| 578 | n/a | } |
|---|
| 579 | n/a | |
|---|
| 580 | n/a | static void |
|---|
| 581 | n/a | string_dealloc(PyObject *op) |
|---|
| 582 | 1008101876 | { |
|---|
| 583 | 1008101876 | switch (PyString_CHECK_INTERNED(op)) { |
|---|
| 584 | n/a | case SSTATE_NOT_INTERNED: |
|---|
| 585 | 1007373014 | break; |
|---|
| 586 | n/a | |
|---|
| 587 | n/a | case SSTATE_INTERNED_MORTAL: |
|---|
| 588 | n/a | /* revive dead object temporarily for DelItem */ |
|---|
| 589 | 728862 | Py_REFCNT(op) = 3; |
|---|
| 590 | 728862 | if (PyDict_DelItem(interned, op) != 0) |
|---|
| 591 | 0 | Py_FatalError( |
|---|
| 592 | n/a | "deletion of interned string failed"); |
|---|
| 593 | 728862 | break; |
|---|
| 594 | n/a | |
|---|
| 595 | n/a | case SSTATE_INTERNED_IMMORTAL: |
|---|
| 596 | 0 | Py_FatalError("Immortal interned string died."); |
|---|
| 597 | n/a | |
|---|
| 598 | n/a | default: |
|---|
| 599 | 0 | Py_FatalError("Inconsistent interned string state."); |
|---|
| 600 | n/a | } |
|---|
| 601 | 1008101876 | Py_TYPE(op)->tp_free(op); |
|---|
| 602 | 1008101876 | } |
|---|
| 603 | n/a | |
|---|
| 604 | n/a | /* Unescape a backslash-escaped string. If unicode is non-zero, |
|---|
| 605 | n/a | the string is a u-literal. If recode_encoding is non-zero, |
|---|
| 606 | n/a | the string is UTF-8 encoded and should be re-encoded in the |
|---|
| 607 | n/a | specified encoding. */ |
|---|
| 608 | n/a | |
|---|
| 609 | n/a | PyObject *PyString_DecodeEscape(const char *s, |
|---|
| 610 | n/a | Py_ssize_t len, |
|---|
| 611 | n/a | const char *errors, |
|---|
| 612 | n/a | Py_ssize_t unicode, |
|---|
| 613 | n/a | const char *recode_encoding) |
|---|
| 614 | 23935 | { |
|---|
| 615 | n/a | int c; |
|---|
| 616 | n/a | char *p, *buf; |
|---|
| 617 | n/a | const char *end; |
|---|
| 618 | n/a | PyObject *v; |
|---|
| 619 | 23935 | Py_ssize_t newlen = recode_encoding ? 4*len:len; |
|---|
| 620 | 23935 | v = PyString_FromStringAndSize((char *)NULL, newlen); |
|---|
| 621 | 23935 | if (v == NULL) |
|---|
| 622 | 0 | return NULL; |
|---|
| 623 | 23935 | p = buf = PyString_AsString(v); |
|---|
| 624 | 23935 | end = s + len; |
|---|
| 625 | 1520492 | while (s < end) { |
|---|
| 626 | 1472631 | if (*s != '\\') { |
|---|
| 627 | 1397212 | non_esc: |
|---|
| 628 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 629 | 1397212 | if (recode_encoding && (*s & 0x80)) { |
|---|
| 630 | n/a | PyObject *u, *w; |
|---|
| 631 | n/a | char *r; |
|---|
| 632 | n/a | const char* t; |
|---|
| 633 | n/a | Py_ssize_t rn; |
|---|
| 634 | 0 | t = s; |
|---|
| 635 | n/a | /* Decode non-ASCII bytes as UTF-8. */ |
|---|
| 636 | 0 | while (t < end && (*t & 0x80)) t++; |
|---|
| 637 | 0 | u = PyUnicode_DecodeUTF8(s, t - s, errors); |
|---|
| 638 | 0 | if(!u) goto failed; |
|---|
| 639 | n/a | |
|---|
| 640 | n/a | /* Recode them in target encoding. */ |
|---|
| 641 | 0 | w = PyUnicode_AsEncodedString( |
|---|
| 642 | n/a | u, recode_encoding, errors); |
|---|
| 643 | 0 | Py_DECREF(u); |
|---|
| 644 | 0 | if (!w) goto failed; |
|---|
| 645 | n/a | |
|---|
| 646 | n/a | /* Append bytes to output buffer. */ |
|---|
| 647 | 0 | assert(PyString_Check(w)); |
|---|
| 648 | 0 | r = PyString_AS_STRING(w); |
|---|
| 649 | 0 | rn = PyString_GET_SIZE(w); |
|---|
| 650 | 0 | Py_MEMCPY(p, r, rn); |
|---|
| 651 | 0 | p += rn; |
|---|
| 652 | 0 | Py_DECREF(w); |
|---|
| 653 | 0 | s = t; |
|---|
| 654 | n/a | } else { |
|---|
| 655 | 1397212 | *p++ = *s++; |
|---|
| 656 | n/a | } |
|---|
| 657 | n/a | #else |
|---|
| 658 | n/a | *p++ = *s++; |
|---|
| 659 | n/a | #endif |
|---|
| 660 | 1397212 | continue; |
|---|
| 661 | n/a | } |
|---|
| 662 | 76196 | s++; |
|---|
| 663 | 76196 | if (s==end) { |
|---|
| 664 | 9 | PyErr_SetString(PyExc_ValueError, |
|---|
| 665 | n/a | "Trailing \\ in string"); |
|---|
| 666 | 9 | goto failed; |
|---|
| 667 | n/a | } |
|---|
| 668 | 76187 | switch (*s++) { |
|---|
| 669 | n/a | /* XXX This assumes ASCII! */ |
|---|
| 670 | 998 | case '\n': break; |
|---|
| 671 | 2619 | case '\\': *p++ = '\\'; break; |
|---|
| 672 | 264 | case '\'': *p++ = '\''; break; |
|---|
| 673 | 452 | case '\"': *p++ = '\"'; break; |
|---|
| 674 | 48 | case 'b': *p++ = '\b'; break; |
|---|
| 675 | 71 | case 'f': *p++ = '\014'; break; /* FF */ |
|---|
| 676 | 1087 | case 't': *p++ = '\t'; break; |
|---|
| 677 | 24754 | case 'n': *p++ = '\n'; break; |
|---|
| 678 | 1412 | case 'r': *p++ = '\r'; break; |
|---|
| 679 | 69 | case 'v': *p++ = '\013'; break; /* VT */ |
|---|
| 680 | 39 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
|---|
| 681 | n/a | case '0': case '1': case '2': case '3': |
|---|
| 682 | n/a | case '4': case '5': case '6': case '7': |
|---|
| 683 | 2313 | c = s[-1] - '0'; |
|---|
| 684 | 2313 | if (s < end && '0' <= *s && *s <= '7') { |
|---|
| 685 | 1627 | c = (c<<3) + *s++ - '0'; |
|---|
| 686 | 1627 | if (s < end && '0' <= *s && *s <= '7') |
|---|
| 687 | 1601 | c = (c<<3) + *s++ - '0'; |
|---|
| 688 | n/a | } |
|---|
| 689 | 2313 | *p++ = c; |
|---|
| 690 | 2313 | break; |
|---|
| 691 | n/a | case 'x': |
|---|
| 692 | 41284 | if (s+1 < end && |
|---|
| 693 | n/a | isxdigit(Py_CHARMASK(s[0])) && |
|---|
| 694 | n/a | isxdigit(Py_CHARMASK(s[1]))) |
|---|
| 695 | n/a | { |
|---|
| 696 | 41284 | unsigned int x = 0; |
|---|
| 697 | 41284 | c = Py_CHARMASK(*s); |
|---|
| 698 | 41284 | s++; |
|---|
| 699 | 41284 | if (isdigit(c)) |
|---|
| 700 | 18937 | x = c - '0'; |
|---|
| 701 | 22347 | else if (islower(c)) |
|---|
| 702 | 22283 | x = 10 + c - 'a'; |
|---|
| 703 | n/a | else |
|---|
| 704 | 64 | x = 10 + c - 'A'; |
|---|
| 705 | 41284 | x = x << 4; |
|---|
| 706 | 41284 | c = Py_CHARMASK(*s); |
|---|
| 707 | 41284 | s++; |
|---|
| 708 | 41284 | if (isdigit(c)) |
|---|
| 709 | 29912 | x += c - '0'; |
|---|
| 710 | 11372 | else if (islower(c)) |
|---|
| 711 | 11294 | x += 10 + c - 'a'; |
|---|
| 712 | n/a | else |
|---|
| 713 | 78 | x += 10 + c - 'A'; |
|---|
| 714 | 41284 | *p++ = x; |
|---|
| 715 | 41284 | break; |
|---|
| 716 | n/a | } |
|---|
| 717 | 0 | if (!errors || strcmp(errors, "strict") == 0) { |
|---|
| 718 | 0 | PyErr_SetString(PyExc_ValueError, |
|---|
| 719 | n/a | "invalid \\x escape"); |
|---|
| 720 | 0 | goto failed; |
|---|
| 721 | n/a | } |
|---|
| 722 | 0 | if (strcmp(errors, "replace") == 0) { |
|---|
| 723 | 0 | *p++ = '?'; |
|---|
| 724 | 0 | } else if (strcmp(errors, "ignore") == 0) |
|---|
| 725 | n/a | /* do nothing */; |
|---|
| 726 | n/a | else { |
|---|
| 727 | 0 | PyErr_Format(PyExc_ValueError, |
|---|
| 728 | n/a | "decoding error; " |
|---|
| 729 | n/a | "unknown error handling code: %.400s", |
|---|
| 730 | n/a | errors); |
|---|
| 731 | 0 | goto failed; |
|---|
| 732 | n/a | } |
|---|
| 733 | n/a | #ifndef Py_USING_UNICODE |
|---|
| 734 | n/a | case 'u': |
|---|
| 735 | n/a | case 'U': |
|---|
| 736 | n/a | case 'N': |
|---|
| 737 | n/a | if (unicode) { |
|---|
| 738 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 739 | n/a | "Unicode escapes not legal " |
|---|
| 740 | n/a | "when Unicode disabled"); |
|---|
| 741 | n/a | goto failed; |
|---|
| 742 | n/a | } |
|---|
| 743 | n/a | #endif |
|---|
| 744 | n/a | default: |
|---|
| 745 | 777 | *p++ = '\\'; |
|---|
| 746 | 777 | s--; |
|---|
| 747 | 777 | goto non_esc; /* an arbitry number of unescaped |
|---|
| 748 | n/a | UTF-8 bytes may follow. */ |
|---|
| 749 | n/a | } |
|---|
| 750 | n/a | } |
|---|
| 751 | 23926 | if (p-buf < newlen && _PyString_Resize(&v, p - buf)) |
|---|
| 752 | 0 | goto failed; |
|---|
| 753 | 23926 | return v; |
|---|
| 754 | 9 | failed: |
|---|
| 755 | 9 | Py_DECREF(v); |
|---|
| 756 | 9 | return NULL; |
|---|
| 757 | n/a | } |
|---|
| 758 | n/a | |
|---|
| 759 | n/a | /* -------------------------------------------------------------------- */ |
|---|
| 760 | n/a | /* object api */ |
|---|
| 761 | n/a | |
|---|
| 762 | n/a | static Py_ssize_t |
|---|
| 763 | n/a | string_getsize(register PyObject *op) |
|---|
| 764 | 419940 | { |
|---|
| 765 | n/a | char *s; |
|---|
| 766 | n/a | Py_ssize_t len; |
|---|
| 767 | 419940 | if (PyString_AsStringAndSize(op, &s, &len)) |
|---|
| 768 | 8 | return -1; |
|---|
| 769 | 419932 | return len; |
|---|
| 770 | n/a | } |
|---|
| 771 | n/a | |
|---|
| 772 | n/a | static /*const*/ char * |
|---|
| 773 | n/a | string_getbuffer(register PyObject *op) |
|---|
| 774 | 4 | { |
|---|
| 775 | n/a | char *s; |
|---|
| 776 | n/a | Py_ssize_t len; |
|---|
| 777 | 4 | if (PyString_AsStringAndSize(op, &s, &len)) |
|---|
| 778 | 4 | return NULL; |
|---|
| 779 | 0 | return s; |
|---|
| 780 | n/a | } |
|---|
| 781 | n/a | |
|---|
| 782 | n/a | Py_ssize_t |
|---|
| 783 | n/a | PyString_Size(register PyObject *op) |
|---|
| 784 | 13588475 | { |
|---|
| 785 | 13588475 | if (!PyString_Check(op)) |
|---|
| 786 | 419940 | return string_getsize(op); |
|---|
| 787 | 13168535 | return Py_SIZE(op); |
|---|
| 788 | n/a | } |
|---|
| 789 | n/a | |
|---|
| 790 | n/a | /*const*/ char * |
|---|
| 791 | n/a | PyString_AsString(register PyObject *op) |
|---|
| 792 | 2145348248 | { |
|---|
| 793 | 2145348248 | if (!PyString_Check(op)) |
|---|
| 794 | 4 | return string_getbuffer(op); |
|---|
| 795 | 2145348244 | return ((PyStringObject *)op) -> ob_sval; |
|---|
| 796 | n/a | } |
|---|
| 797 | n/a | |
|---|
| 798 | n/a | int |
|---|
| 799 | n/a | PyString_AsStringAndSize(register PyObject *obj, |
|---|
| 800 | n/a | register char **s, |
|---|
| 801 | n/a | register Py_ssize_t *len) |
|---|
| 802 | 762660 | { |
|---|
| 803 | 762660 | if (s == NULL) { |
|---|
| 804 | 0 | PyErr_BadInternalCall(); |
|---|
| 805 | 0 | return -1; |
|---|
| 806 | n/a | } |
|---|
| 807 | n/a | |
|---|
| 808 | 762660 | if (!PyString_Check(obj)) { |
|---|
| 809 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 810 | 419949 | if (PyUnicode_Check(obj)) { |
|---|
| 811 | 419938 | obj = _PyUnicode_AsDefaultEncodedString(obj, NULL); |
|---|
| 812 | 419938 | if (obj == NULL) |
|---|
| 813 | 5 | return -1; |
|---|
| 814 | n/a | } |
|---|
| 815 | n/a | else |
|---|
| 816 | n/a | #endif |
|---|
| 817 | n/a | { |
|---|
| 818 | 11 | PyErr_Format(PyExc_TypeError, |
|---|
| 819 | n/a | "expected string or Unicode object, " |
|---|
| 820 | n/a | "%.200s found", Py_TYPE(obj)->tp_name); |
|---|
| 821 | 11 | return -1; |
|---|
| 822 | n/a | } |
|---|
| 823 | n/a | } |
|---|
| 824 | n/a | |
|---|
| 825 | 762644 | *s = PyString_AS_STRING(obj); |
|---|
| 826 | 762644 | if (len != NULL) |
|---|
| 827 | 434710 | *len = PyString_GET_SIZE(obj); |
|---|
| 828 | 327934 | else if (strlen(*s) != (size_t)PyString_GET_SIZE(obj)) { |
|---|
| 829 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 830 | n/a | "expected string without null bytes"); |
|---|
| 831 | 0 | return -1; |
|---|
| 832 | n/a | } |
|---|
| 833 | 762644 | return 0; |
|---|
| 834 | n/a | } |
|---|
| 835 | n/a | |
|---|
| 836 | n/a | /* -------------------------------------------------------------------- */ |
|---|
| 837 | n/a | /* Methods */ |
|---|
| 838 | n/a | |
|---|
| 839 | n/a | #include "stringlib/stringdefs.h" |
|---|
| 840 | n/a | #include "stringlib/fastsearch.h" |
|---|
| 841 | n/a | |
|---|
| 842 | n/a | #include "stringlib/count.h" |
|---|
| 843 | n/a | #include "stringlib/find.h" |
|---|
| 844 | n/a | #include "stringlib/partition.h" |
|---|
| 845 | n/a | #include "stringlib/split.h" |
|---|
| 846 | n/a | |
|---|
| 847 | n/a | #define _Py_InsertThousandsGrouping _PyString_InsertThousandsGrouping |
|---|
| 848 | n/a | #include "stringlib/localeutil.h" |
|---|
| 849 | n/a | |
|---|
| 850 | n/a | |
|---|
| 851 | n/a | |
|---|
| 852 | n/a | static int |
|---|
| 853 | n/a | string_print(PyStringObject *op, FILE *fp, int flags) |
|---|
| 854 | 2356 | { |
|---|
| 855 | n/a | Py_ssize_t i, str_len; |
|---|
| 856 | n/a | char c; |
|---|
| 857 | n/a | int quote; |
|---|
| 858 | n/a | |
|---|
| 859 | n/a | /* XXX Ought to check for interrupts when writing long strings */ |
|---|
| 860 | 2356 | if (! PyString_CheckExact(op)) { |
|---|
| 861 | n/a | int ret; |
|---|
| 862 | n/a | /* A str subclass may have its own __str__ method. */ |
|---|
| 863 | 0 | op = (PyStringObject *) PyObject_Str((PyObject *)op); |
|---|
| 864 | 0 | if (op == NULL) |
|---|
| 865 | 0 | return -1; |
|---|
| 866 | 0 | ret = string_print(op, fp, flags); |
|---|
| 867 | 0 | Py_DECREF(op); |
|---|
| 868 | 0 | return ret; |
|---|
| 869 | n/a | } |
|---|
| 870 | 2356 | if (flags & Py_PRINT_RAW) { |
|---|
| 871 | 2325 | char *data = op->ob_sval; |
|---|
| 872 | 2325 | Py_ssize_t size = Py_SIZE(op); |
|---|
| 873 | 2325 | Py_BEGIN_ALLOW_THREADS |
|---|
| 874 | 4650 | while (size > INT_MAX) { |
|---|
| 875 | n/a | /* Very long strings cannot be written atomically. |
|---|
| 876 | n/a | * But don't write exactly INT_MAX bytes at a time |
|---|
| 877 | n/a | * to avoid memory aligment issues. |
|---|
| 878 | n/a | */ |
|---|
| 879 | 0 | const int chunk_size = INT_MAX & ~0x3FFF; |
|---|
| 880 | 0 | fwrite(data, 1, chunk_size, fp); |
|---|
| 881 | 0 | data += chunk_size; |
|---|
| 882 | 0 | size -= chunk_size; |
|---|
| 883 | n/a | } |
|---|
| 884 | n/a | #ifdef __VMS |
|---|
| 885 | n/a | if (size) fwrite(data, (int)size, 1, fp); |
|---|
| 886 | n/a | #else |
|---|
| 887 | 2325 | fwrite(data, 1, (int)size, fp); |
|---|
| 888 | n/a | #endif |
|---|
| 889 | 2325 | Py_END_ALLOW_THREADS |
|---|
| 890 | 2325 | return 0; |
|---|
| 891 | n/a | } |
|---|
| 892 | n/a | |
|---|
| 893 | n/a | /* figure out which quote to use; single is preferred */ |
|---|
| 894 | 31 | quote = '\''; |
|---|
| 895 | 31 | if (memchr(op->ob_sval, '\'', Py_SIZE(op)) && |
|---|
| 896 | n/a | !memchr(op->ob_sval, '"', Py_SIZE(op))) |
|---|
| 897 | 1 | quote = '"'; |
|---|
| 898 | n/a | |
|---|
| 899 | 31 | str_len = Py_SIZE(op); |
|---|
| 900 | 31 | Py_BEGIN_ALLOW_THREADS |
|---|
| 901 | 31 | fputc(quote, fp); |
|---|
| 902 | 209 | for (i = 0; i < str_len; i++) { |
|---|
| 903 | n/a | /* Since strings are immutable and the caller should have a |
|---|
| 904 | n/a | reference, accessing the interal buffer should not be an issue |
|---|
| 905 | n/a | with the GIL released. */ |
|---|
| 906 | 178 | c = op->ob_sval[i]; |
|---|
| 907 | 178 | if (c == quote || c == '\\') |
|---|
| 908 | 0 | fprintf(fp, "\\%c", c); |
|---|
| 909 | 178 | else if (c == '\t') |
|---|
| 910 | 0 | fprintf(fp, "\\t"); |
|---|
| 911 | 178 | else if (c == '\n') |
|---|
| 912 | 0 | fprintf(fp, "\\n"); |
|---|
| 913 | 178 | else if (c == '\r') |
|---|
| 914 | 0 | fprintf(fp, "\\r"); |
|---|
| 915 | 178 | else if (c < ' ' || c >= 0x7f) |
|---|
| 916 | 0 | fprintf(fp, "\\x%02x", c & 0xff); |
|---|
| 917 | n/a | else |
|---|
| 918 | 178 | fputc(c, fp); |
|---|
| 919 | n/a | } |
|---|
| 920 | 31 | fputc(quote, fp); |
|---|
| 921 | 31 | Py_END_ALLOW_THREADS |
|---|
| 922 | 31 | return 0; |
|---|
| 923 | n/a | } |
|---|
| 924 | n/a | |
|---|
| 925 | n/a | PyObject * |
|---|
| 926 | n/a | PyString_Repr(PyObject *obj, int smartquotes) |
|---|
| 927 | 113711 | { |
|---|
| 928 | 113711 | register PyStringObject* op = (PyStringObject*) obj; |
|---|
| 929 | 113711 | size_t newsize = 2 + 4 * Py_SIZE(op); |
|---|
| 930 | n/a | PyObject *v; |
|---|
| 931 | 113711 | if (newsize > PY_SSIZE_T_MAX || newsize / 4 != Py_SIZE(op)) { |
|---|
| 932 | 0 | PyErr_SetString(PyExc_OverflowError, |
|---|
| 933 | n/a | "string is too large to make repr"); |
|---|
| 934 | 0 | return NULL; |
|---|
| 935 | n/a | } |
|---|
| 936 | 113711 | v = PyString_FromStringAndSize((char *)NULL, newsize); |
|---|
| 937 | 113711 | if (v == NULL) { |
|---|
| 938 | 0 | return NULL; |
|---|
| 939 | n/a | } |
|---|
| 940 | n/a | else { |
|---|
| 941 | n/a | register Py_ssize_t i; |
|---|
| 942 | n/a | register char c; |
|---|
| 943 | n/a | register char *p; |
|---|
| 944 | n/a | int quote; |
|---|
| 945 | n/a | |
|---|
| 946 | n/a | /* figure out which quote to use; single is preferred */ |
|---|
| 947 | 113711 | quote = '\''; |
|---|
| 948 | 113711 | if (smartquotes && |
|---|
| 949 | n/a | memchr(op->ob_sval, '\'', Py_SIZE(op)) && |
|---|
| 950 | n/a | !memchr(op->ob_sval, '"', Py_SIZE(op))) |
|---|
| 951 | 746 | quote = '"'; |
|---|
| 952 | n/a | |
|---|
| 953 | 113711 | p = PyString_AS_STRING(v); |
|---|
| 954 | 113711 | *p++ = quote; |
|---|
| 955 | 1466837 | for (i = 0; i < Py_SIZE(op); i++) { |
|---|
| 956 | n/a | /* There's at least enough room for a hex escape |
|---|
| 957 | n/a | and a closing quote. */ |
|---|
| 958 | 1353126 | assert(newsize - (p - PyString_AS_STRING(v)) >= 5); |
|---|
| 959 | 1353126 | c = op->ob_sval[i]; |
|---|
| 960 | 1358902 | if (c == quote || c == '\\') |
|---|
| 961 | 5776 | *p++ = '\\', *p++ = c; |
|---|
| 962 | 1347350 | else if (c == '\t') |
|---|
| 963 | 471 | *p++ = '\\', *p++ = 't'; |
|---|
| 964 | 1346879 | else if (c == '\n') |
|---|
| 965 | 573 | *p++ = '\\', *p++ = 'n'; |
|---|
| 966 | 1346306 | else if (c == '\r') |
|---|
| 967 | 86 | *p++ = '\\', *p++ = 'r'; |
|---|
| 968 | 1354543 | else if (c < ' ' || c >= 0x7f) { |
|---|
| 969 | n/a | /* For performance, we don't want to call |
|---|
| 970 | n/a | PyOS_snprintf here (extra layers of |
|---|
| 971 | n/a | function call). */ |
|---|
| 972 | 8323 | sprintf(p, "\\x%02x", c & 0xff); |
|---|
| 973 | 8323 | p += 4; |
|---|
| 974 | n/a | } |
|---|
| 975 | n/a | else |
|---|
| 976 | 1337897 | *p++ = c; |
|---|
| 977 | n/a | } |
|---|
| 978 | 113711 | assert(newsize - (p - PyString_AS_STRING(v)) >= 1); |
|---|
| 979 | 113711 | *p++ = quote; |
|---|
| 980 | 113711 | *p = '\0'; |
|---|
| 981 | 113711 | if (_PyString_Resize(&v, (p - PyString_AS_STRING(v)))) |
|---|
| 982 | 0 | return NULL; |
|---|
| 983 | 113711 | return v; |
|---|
| 984 | n/a | } |
|---|
| 985 | n/a | } |
|---|
| 986 | n/a | |
|---|
| 987 | n/a | static PyObject * |
|---|
| 988 | n/a | string_repr(PyObject *op) |
|---|
| 989 | 113709 | { |
|---|
| 990 | 113709 | return PyString_Repr(op, 1); |
|---|
| 991 | n/a | } |
|---|
| 992 | n/a | |
|---|
| 993 | n/a | static PyObject * |
|---|
| 994 | n/a | string_str(PyObject *s) |
|---|
| 995 | 134 | { |
|---|
| 996 | 134 | assert(PyString_Check(s)); |
|---|
| 997 | 134 | if (PyString_CheckExact(s)) { |
|---|
| 998 | 0 | Py_INCREF(s); |
|---|
| 999 | 0 | return s; |
|---|
| 1000 | n/a | } |
|---|
| 1001 | n/a | else { |
|---|
| 1002 | n/a | /* Subtype -- return genuine string with the same value. */ |
|---|
| 1003 | 134 | PyStringObject *t = (PyStringObject *) s; |
|---|
| 1004 | 134 | return PyString_FromStringAndSize(t->ob_sval, Py_SIZE(t)); |
|---|
| 1005 | n/a | } |
|---|
| 1006 | n/a | } |
|---|
| 1007 | n/a | |
|---|
| 1008 | n/a | static Py_ssize_t |
|---|
| 1009 | n/a | string_length(PyStringObject *a) |
|---|
| 1010 | 73116496 | { |
|---|
| 1011 | 73116496 | return Py_SIZE(a); |
|---|
| 1012 | n/a | } |
|---|
| 1013 | n/a | |
|---|
| 1014 | n/a | static PyObject * |
|---|
| 1015 | n/a | string_concat(register PyStringObject *a, register PyObject *bb) |
|---|
| 1016 | 2683380 | { |
|---|
| 1017 | n/a | register Py_ssize_t size; |
|---|
| 1018 | n/a | register PyStringObject *op; |
|---|
| 1019 | 2683380 | if (!PyString_Check(bb)) { |
|---|
| 1020 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 1021 | 201163 | if (PyUnicode_Check(bb)) |
|---|
| 1022 | 201086 | return PyUnicode_Concat((PyObject *)a, bb); |
|---|
| 1023 | n/a | #endif |
|---|
| 1024 | 77 | if (PyByteArray_Check(bb)) |
|---|
| 1025 | 2 | return PyByteArray_Concat((PyObject *)a, bb); |
|---|
| 1026 | 75 | PyErr_Format(PyExc_TypeError, |
|---|
| 1027 | n/a | "cannot concatenate 'str' and '%.200s' objects", |
|---|
| 1028 | n/a | Py_TYPE(bb)->tp_name); |
|---|
| 1029 | 75 | return NULL; |
|---|
| 1030 | n/a | } |
|---|
| 1031 | n/a | #define b ((PyStringObject *)bb) |
|---|
| 1032 | n/a | /* Optimize cases with empty left or right operand */ |
|---|
| 1033 | 2482217 | if ((Py_SIZE(a) == 0 || Py_SIZE(b) == 0) && |
|---|
| 1034 | n/a | PyString_CheckExact(a) && PyString_CheckExact(b)) { |
|---|
| 1035 | 704803 | if (Py_SIZE(a) == 0) { |
|---|
| 1036 | 529445 | Py_INCREF(bb); |
|---|
| 1037 | 529445 | return bb; |
|---|
| 1038 | n/a | } |
|---|
| 1039 | 175358 | Py_INCREF(a); |
|---|
| 1040 | 175358 | return (PyObject *)a; |
|---|
| 1041 | n/a | } |
|---|
| 1042 | 1777414 | size = Py_SIZE(a) + Py_SIZE(b); |
|---|
| 1043 | n/a | /* Check that string sizes are not negative, to prevent an |
|---|
| 1044 | n/a | overflow in cases where we are passed incorrectly-created |
|---|
| 1045 | n/a | strings with negative lengths (due to a bug in other code). |
|---|
| 1046 | n/a | */ |
|---|
| 1047 | 1777414 | if (Py_SIZE(a) < 0 || Py_SIZE(b) < 0 || |
|---|
| 1048 | n/a | Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) { |
|---|
| 1049 | 0 | PyErr_SetString(PyExc_OverflowError, |
|---|
| 1050 | n/a | "strings are too large to concat"); |
|---|
| 1051 | 0 | return NULL; |
|---|
| 1052 | n/a | } |
|---|
| 1053 | n/a | |
|---|
| 1054 | n/a | /* Inline PyObject_NewVar */ |
|---|
| 1055 | 1777414 | if (size > PY_SSIZE_T_MAX - PyStringObject_SIZE) { |
|---|
| 1056 | 0 | PyErr_SetString(PyExc_OverflowError, |
|---|
| 1057 | n/a | "strings are too large to concat"); |
|---|
| 1058 | 0 | return NULL; |
|---|
| 1059 | n/a | } |
|---|
| 1060 | 1777414 | op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + size); |
|---|
| 1061 | 1777414 | if (op == NULL) |
|---|
| 1062 | 0 | return PyErr_NoMemory(); |
|---|
| 1063 | 1777414 | PyObject_INIT_VAR(op, &PyString_Type, size); |
|---|
| 1064 | 1777414 | op->ob_shash = -1; |
|---|
| 1065 | 1777414 | op->ob_sstate = SSTATE_NOT_INTERNED; |
|---|
| 1066 | 1777414 | Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a)); |
|---|
| 1067 | 1777414 | Py_MEMCPY(op->ob_sval + Py_SIZE(a), b->ob_sval, Py_SIZE(b)); |
|---|
| 1068 | 1777414 | op->ob_sval[size] = '\0'; |
|---|
| 1069 | 1777414 | return (PyObject *) op; |
|---|
| 1070 | n/a | #undef b |
|---|
| 1071 | n/a | } |
|---|
| 1072 | n/a | |
|---|
| 1073 | n/a | static PyObject * |
|---|
| 1074 | n/a | string_repeat(register PyStringObject *a, register Py_ssize_t n) |
|---|
| 1075 | 226049 | { |
|---|
| 1076 | n/a | register Py_ssize_t i; |
|---|
| 1077 | n/a | register Py_ssize_t j; |
|---|
| 1078 | n/a | register Py_ssize_t size; |
|---|
| 1079 | n/a | register PyStringObject *op; |
|---|
| 1080 | n/a | size_t nbytes; |
|---|
| 1081 | 226049 | if (n < 0) |
|---|
| 1082 | 4191 | n = 0; |
|---|
| 1083 | n/a | /* watch out for overflows: the size can overflow int, |
|---|
| 1084 | n/a | * and the # of bytes needed can overflow size_t |
|---|
| 1085 | n/a | */ |
|---|
| 1086 | 226049 | size = Py_SIZE(a) * n; |
|---|
| 1087 | 226049 | if (n && size / n != Py_SIZE(a)) { |
|---|
| 1088 | 1 | PyErr_SetString(PyExc_OverflowError, |
|---|
| 1089 | n/a | "repeated string is too long"); |
|---|
| 1090 | 1 | return NULL; |
|---|
| 1091 | n/a | } |
|---|
| 1092 | 226048 | if (size == Py_SIZE(a) && PyString_CheckExact(a)) { |
|---|
| 1093 | 87261 | Py_INCREF(a); |
|---|
| 1094 | 87261 | return (PyObject *)a; |
|---|
| 1095 | n/a | } |
|---|
| 1096 | 138787 | nbytes = (size_t)size; |
|---|
| 1097 | 138787 | if (nbytes + PyStringObject_SIZE <= nbytes) { |
|---|
| 1098 | 0 | PyErr_SetString(PyExc_OverflowError, |
|---|
| 1099 | n/a | "repeated string is too long"); |
|---|
| 1100 | 0 | return NULL; |
|---|
| 1101 | n/a | } |
|---|
| 1102 | 138787 | op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + nbytes); |
|---|
| 1103 | 138787 | if (op == NULL) |
|---|
| 1104 | 0 | return PyErr_NoMemory(); |
|---|
| 1105 | 138787 | PyObject_INIT_VAR(op, &PyString_Type, size); |
|---|
| 1106 | 138787 | op->ob_shash = -1; |
|---|
| 1107 | 138787 | op->ob_sstate = SSTATE_NOT_INTERNED; |
|---|
| 1108 | 138787 | op->ob_sval[size] = '\0'; |
|---|
| 1109 | 138787 | if (Py_SIZE(a) == 1 && n > 0) { |
|---|
| 1110 | 52320 | memset(op->ob_sval, a->ob_sval[0] , n); |
|---|
| 1111 | 52320 | return (PyObject *) op; |
|---|
| 1112 | n/a | } |
|---|
| 1113 | 86467 | i = 0; |
|---|
| 1114 | 86467 | if (i < size) { |
|---|
| 1115 | 4279 | Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a)); |
|---|
| 1116 | 4279 | i = Py_SIZE(a); |
|---|
| 1117 | n/a | } |
|---|
| 1118 | 191691 | while (i < size) { |
|---|
| 1119 | 18757 | j = (i <= size-i) ? i : size-i; |
|---|
| 1120 | 18757 | Py_MEMCPY(op->ob_sval+i, op->ob_sval, j); |
|---|
| 1121 | 18757 | i += j; |
|---|
| 1122 | n/a | } |
|---|
| 1123 | 86467 | return (PyObject *) op; |
|---|
| 1124 | n/a | } |
|---|
| 1125 | n/a | |
|---|
| 1126 | n/a | /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ |
|---|
| 1127 | n/a | |
|---|
| 1128 | n/a | static PyObject * |
|---|
| 1129 | n/a | string_slice(register PyStringObject *a, register Py_ssize_t i, |
|---|
| 1130 | n/a | register Py_ssize_t j) |
|---|
| 1131 | n/a | /* j -- may be negative! */ |
|---|
| 1132 | 188731764 | { |
|---|
| 1133 | 188731764 | if (i < 0) |
|---|
| 1134 | 373 | i = 0; |
|---|
| 1135 | 188731764 | if (j < 0) |
|---|
| 1136 | 206 | j = 0; /* Avoid signed/unsigned bug in next line */ |
|---|
| 1137 | 188731764 | if (j > Py_SIZE(a)) |
|---|
| 1138 | 122478462 | j = Py_SIZE(a); |
|---|
| 1139 | 188731764 | if (i == 0 && j == Py_SIZE(a) && PyString_CheckExact(a)) { |
|---|
| 1140 | n/a | /* It's the same as a */ |
|---|
| 1141 | 2624154 | Py_INCREF(a); |
|---|
| 1142 | 2624154 | return (PyObject *)a; |
|---|
| 1143 | n/a | } |
|---|
| 1144 | 186107610 | if (j < i) |
|---|
| 1145 | 4847 | j = i; |
|---|
| 1146 | 186107610 | return PyString_FromStringAndSize(a->ob_sval + i, j-i); |
|---|
| 1147 | n/a | } |
|---|
| 1148 | n/a | |
|---|
| 1149 | n/a | static int |
|---|
| 1150 | n/a | string_contains(PyObject *str_obj, PyObject *sub_obj) |
|---|
| 1151 | 10826625 | { |
|---|
| 1152 | 10826625 | if (!PyString_CheckExact(sub_obj)) { |
|---|
| 1153 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 1154 | 448897 | if (PyUnicode_Check(sub_obj)) |
|---|
| 1155 | 448896 | return PyUnicode_Contains(str_obj, sub_obj); |
|---|
| 1156 | n/a | #endif |
|---|
| 1157 | 1 | if (!PyString_Check(sub_obj)) { |
|---|
| 1158 | 1 | PyErr_Format(PyExc_TypeError, |
|---|
| 1159 | n/a | "'in <string>' requires string as left operand, " |
|---|
| 1160 | n/a | "not %.200s", Py_TYPE(sub_obj)->tp_name); |
|---|
| 1161 | 1 | return -1; |
|---|
| 1162 | n/a | } |
|---|
| 1163 | n/a | } |
|---|
| 1164 | n/a | |
|---|
| 1165 | 10377728 | return stringlib_contains_obj(str_obj, sub_obj); |
|---|
| 1166 | n/a | } |
|---|
| 1167 | n/a | |
|---|
| 1168 | n/a | static PyObject * |
|---|
| 1169 | n/a | string_item(PyStringObject *a, register Py_ssize_t i) |
|---|
| 1170 | 69734218 | { |
|---|
| 1171 | n/a | char pchar; |
|---|
| 1172 | n/a | PyObject *v; |
|---|
| 1173 | 69734218 | if (i < 0 || i >= Py_SIZE(a)) { |
|---|
| 1174 | 342910 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
|---|
| 1175 | 342910 | return NULL; |
|---|
| 1176 | n/a | } |
|---|
| 1177 | 69391308 | pchar = a->ob_sval[i]; |
|---|
| 1178 | 69391308 | v = (PyObject *)characters[pchar & UCHAR_MAX]; |
|---|
| 1179 | 69391308 | if (v == NULL) |
|---|
| 1180 | 23600 | v = PyString_FromStringAndSize(&pchar, 1); |
|---|
| 1181 | n/a | else { |
|---|
| 1182 | n/a | #ifdef COUNT_ALLOCS |
|---|
| 1183 | n/a | one_strings++; |
|---|
| 1184 | n/a | #endif |
|---|
| 1185 | 69367708 | Py_INCREF(v); |
|---|
| 1186 | n/a | } |
|---|
| 1187 | 69391308 | return v; |
|---|
| 1188 | n/a | } |
|---|
| 1189 | n/a | |
|---|
| 1190 | n/a | static PyObject* |
|---|
| 1191 | n/a | string_richcompare(PyStringObject *a, PyStringObject *b, int op) |
|---|
| 1192 | 588174882 | { |
|---|
| 1193 | n/a | int c; |
|---|
| 1194 | n/a | Py_ssize_t len_a, len_b; |
|---|
| 1195 | n/a | Py_ssize_t min_len; |
|---|
| 1196 | n/a | PyObject *result; |
|---|
| 1197 | n/a | |
|---|
| 1198 | n/a | /* Make sure both arguments are strings. */ |
|---|
| 1199 | 588174882 | if (!(PyString_Check(a) && PyString_Check(b))) { |
|---|
| 1200 | 627153 | result = Py_NotImplemented; |
|---|
| 1201 | 627153 | goto out; |
|---|
| 1202 | n/a | } |
|---|
| 1203 | 587547729 | if (a == b) { |
|---|
| 1204 | 367246528 | switch (op) { |
|---|
| 1205 | n/a | case Py_EQ:case Py_LE:case Py_GE: |
|---|
| 1206 | 366540464 | result = Py_True; |
|---|
| 1207 | 366540464 | goto out; |
|---|
| 1208 | n/a | case Py_NE:case Py_LT:case Py_GT: |
|---|
| 1209 | 706064 | result = Py_False; |
|---|
| 1210 | 706064 | goto out; |
|---|
| 1211 | n/a | } |
|---|
| 1212 | n/a | } |
|---|
| 1213 | 220301201 | if (op == Py_EQ) { |
|---|
| 1214 | n/a | /* Supporting Py_NE here as well does not save |
|---|
| 1215 | n/a | much time, since Py_NE is rarely used. */ |
|---|
| 1216 | 172650780 | if (Py_SIZE(a) == Py_SIZE(b) |
|---|
| 1217 | n/a | && (a->ob_sval[0] == b->ob_sval[0] |
|---|
| 1218 | n/a | && memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0)) { |
|---|
| 1219 | 16976266 | result = Py_True; |
|---|
| 1220 | n/a | } else { |
|---|
| 1221 | 138698248 | result = Py_False; |
|---|
| 1222 | n/a | } |
|---|
| 1223 | 155674514 | goto out; |
|---|
| 1224 | n/a | } |
|---|
| 1225 | 64626687 | len_a = Py_SIZE(a); len_b = Py_SIZE(b); |
|---|
| 1226 | 64626687 | min_len = (len_a < len_b) ? len_a : len_b; |
|---|
| 1227 | 64626687 | if (min_len > 0) { |
|---|
| 1228 | 64622835 | c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
|---|
| 1229 | 64622835 | if (c==0) |
|---|
| 1230 | 762654 | c = memcmp(a->ob_sval, b->ob_sval, min_len); |
|---|
| 1231 | n/a | } else |
|---|
| 1232 | 3852 | c = 0; |
|---|
| 1233 | 64626687 | if (c == 0) |
|---|
| 1234 | 56285 | c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
|---|
| 1235 | 64626687 | switch (op) { |
|---|
| 1236 | 4112427 | case Py_LT: c = c < 0; break; |
|---|
| 1237 | 15323 | case Py_LE: c = c <= 0; break; |
|---|
| 1238 | 0 | case Py_EQ: assert(0); break; /* unreachable */ |
|---|
| 1239 | 60479494 | case Py_NE: c = c != 0; break; |
|---|
| 1240 | 18348 | case Py_GT: c = c > 0; break; |
|---|
| 1241 | 1095 | case Py_GE: c = c >= 0; break; |
|---|
| 1242 | n/a | default: |
|---|
| 1243 | 0 | result = Py_NotImplemented; |
|---|
| 1244 | 0 | goto out; |
|---|
| 1245 | n/a | } |
|---|
| 1246 | 64626687 | result = c ? Py_True : Py_False; |
|---|
| 1247 | 588174882 | out: |
|---|
| 1248 | 588174882 | Py_INCREF(result); |
|---|
| 1249 | 588174882 | return result; |
|---|
| 1250 | n/a | } |
|---|
| 1251 | n/a | |
|---|
| 1252 | n/a | int |
|---|
| 1253 | n/a | _PyString_Eq(PyObject *o1, PyObject *o2) |
|---|
| 1254 | 137216508 | { |
|---|
| 1255 | 137216508 | PyStringObject *a = (PyStringObject*) o1; |
|---|
| 1256 | 137216508 | PyStringObject *b = (PyStringObject*) o2; |
|---|
| 1257 | 137216508 | return Py_SIZE(a) == Py_SIZE(b) |
|---|
| 1258 | n/a | && *a->ob_sval == *b->ob_sval |
|---|
| 1259 | n/a | && memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0; |
|---|
| 1260 | n/a | } |
|---|
| 1261 | n/a | |
|---|
| 1262 | n/a | static long |
|---|
| 1263 | n/a | string_hash(PyStringObject *a) |
|---|
| 1264 | 1480466368 | { |
|---|
| 1265 | n/a | register Py_ssize_t len; |
|---|
| 1266 | n/a | register unsigned char *p; |
|---|
| 1267 | n/a | register long x; |
|---|
| 1268 | n/a | |
|---|
| 1269 | 1480466368 | if (a->ob_shash != -1) |
|---|
| 1270 | 614001477 | return a->ob_shash; |
|---|
| 1271 | 866464891 | len = Py_SIZE(a); |
|---|
| 1272 | 866464891 | p = (unsigned char *) a->ob_sval; |
|---|
| 1273 | 866464891 | x = *p << 7; |
|---|
| 1274 | 11031594862 | while (--len >= 0) |
|---|
| 1275 | 9298665080 | x = (1000003*x) ^ *p++; |
|---|
| 1276 | 866464891 | x ^= Py_SIZE(a); |
|---|
| 1277 | 866464891 | if (x == -1) |
|---|
| 1278 | 0 | x = -2; |
|---|
| 1279 | 866464891 | a->ob_shash = x; |
|---|
| 1280 | 866464891 | return x; |
|---|
| 1281 | n/a | } |
|---|
| 1282 | n/a | |
|---|
| 1283 | n/a | static PyObject* |
|---|
| 1284 | n/a | string_subscript(PyStringObject* self, PyObject* item) |
|---|
| 1285 | 67161707 | { |
|---|
| 1286 | 67161707 | if (PyIndex_Check(item)) { |
|---|
| 1287 | 67122301 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
|---|
| 1288 | 67122301 | if (i == -1 && PyErr_Occurred()) |
|---|
| 1289 | 2 | return NULL; |
|---|
| 1290 | 67122299 | if (i < 0) |
|---|
| 1291 | 135574 | i += PyString_GET_SIZE(self); |
|---|
| 1292 | 67122299 | return string_item(self, i); |
|---|
| 1293 | n/a | } |
|---|
| 1294 | 39406 | else if (PySlice_Check(item)) { |
|---|
| 1295 | n/a | Py_ssize_t start, stop, step, slicelength, cur, i; |
|---|
| 1296 | n/a | char* source_buf; |
|---|
| 1297 | n/a | char* result_buf; |
|---|
| 1298 | n/a | PyObject* result; |
|---|
| 1299 | n/a | |
|---|
| 1300 | 39401 | if (PySlice_GetIndicesEx((PySliceObject*)item, |
|---|
| 1301 | n/a | PyString_GET_SIZE(self), |
|---|
| 1302 | n/a | &start, &stop, &step, &slicelength) < 0) { |
|---|
| 1303 | 0 | return NULL; |
|---|
| 1304 | n/a | } |
|---|
| 1305 | n/a | |
|---|
| 1306 | 39401 | if (slicelength <= 0) { |
|---|
| 1307 | 3075 | return PyString_FromStringAndSize("", 0); |
|---|
| 1308 | n/a | } |
|---|
| 1309 | 36326 | else if (start == 0 && step == 1 && |
|---|
| 1310 | n/a | slicelength == PyString_GET_SIZE(self) && |
|---|
| 1311 | n/a | PyString_CheckExact(self)) { |
|---|
| 1312 | 44 | Py_INCREF(self); |
|---|
| 1313 | 44 | return (PyObject *)self; |
|---|
| 1314 | n/a | } |
|---|
| 1315 | 36282 | else if (step == 1) { |
|---|
| 1316 | 404 | return PyString_FromStringAndSize( |
|---|
| 1317 | n/a | PyString_AS_STRING(self) + start, |
|---|
| 1318 | n/a | slicelength); |
|---|
| 1319 | n/a | } |
|---|
| 1320 | n/a | else { |
|---|
| 1321 | 35878 | source_buf = PyString_AsString((PyObject*)self); |
|---|
| 1322 | 35878 | result_buf = (char *)PyMem_Malloc(slicelength); |
|---|
| 1323 | 35878 | if (result_buf == NULL) |
|---|
| 1324 | 0 | return PyErr_NoMemory(); |
|---|
| 1325 | n/a | |
|---|
| 1326 | 2666352 | for (cur = start, i = 0; i < slicelength; |
|---|
| 1327 | 2594596 | cur += step, i++) { |
|---|
| 1328 | 2594596 | result_buf[i] = source_buf[cur]; |
|---|
| 1329 | n/a | } |
|---|
| 1330 | n/a | |
|---|
| 1331 | 35878 | result = PyString_FromStringAndSize(result_buf, |
|---|
| 1332 | n/a | slicelength); |
|---|
| 1333 | 35878 | PyMem_Free(result_buf); |
|---|
| 1334 | 35878 | return result; |
|---|
| 1335 | n/a | } |
|---|
| 1336 | n/a | } |
|---|
| 1337 | n/a | else { |
|---|
| 1338 | 5 | PyErr_Format(PyExc_TypeError, |
|---|
| 1339 | n/a | "string indices must be integers, not %.200s", |
|---|
| 1340 | n/a | Py_TYPE(item)->tp_name); |
|---|
| 1341 | 5 | return NULL; |
|---|
| 1342 | n/a | } |
|---|
| 1343 | n/a | } |
|---|
| 1344 | n/a | |
|---|
| 1345 | n/a | static Py_ssize_t |
|---|
| 1346 | n/a | string_buffer_getreadbuf(PyStringObject *self, Py_ssize_t index, const void **ptr) |
|---|
| 1347 | 4580286 | { |
|---|
| 1348 | 4580286 | if ( index != 0 ) { |
|---|
| 1349 | 0 | PyErr_SetString(PyExc_SystemError, |
|---|
| 1350 | n/a | "accessing non-existent string segment"); |
|---|
| 1351 | 0 | return -1; |
|---|
| 1352 | n/a | } |
|---|
| 1353 | 4580286 | *ptr = (void *)self->ob_sval; |
|---|
| 1354 | 4580286 | return Py_SIZE(self); |
|---|
| 1355 | n/a | } |
|---|
| 1356 | n/a | |
|---|
| 1357 | n/a | static Py_ssize_t |
|---|
| 1358 | n/a | string_buffer_getwritebuf(PyStringObject *self, Py_ssize_t index, const void **ptr) |
|---|
| 1359 | 2 | { |
|---|
| 1360 | 2 | PyErr_SetString(PyExc_TypeError, |
|---|
| 1361 | n/a | "Cannot use string as modifiable buffer"); |
|---|
| 1362 | 2 | return -1; |
|---|
| 1363 | n/a | } |
|---|
| 1364 | n/a | |
|---|
| 1365 | n/a | static Py_ssize_t |
|---|
| 1366 | n/a | string_buffer_getsegcount(PyStringObject *self, Py_ssize_t *lenp) |
|---|
| 1367 | 8006689 | { |
|---|
| 1368 | 8006689 | if ( lenp ) |
|---|
| 1369 | 0 | *lenp = Py_SIZE(self); |
|---|
| 1370 | 8006689 | return 1; |
|---|
| 1371 | n/a | } |
|---|
| 1372 | n/a | |
|---|
| 1373 | n/a | static Py_ssize_t |
|---|
| 1374 | n/a | string_buffer_getcharbuf(PyStringObject *self, Py_ssize_t index, const char **ptr) |
|---|
| 1375 | 2849801 | { |
|---|
| 1376 | 2849801 | if ( index != 0 ) { |
|---|
| 1377 | 0 | PyErr_SetString(PyExc_SystemError, |
|---|
| 1378 | n/a | "accessing non-existent string segment"); |
|---|
| 1379 | 0 | return -1; |
|---|
| 1380 | n/a | } |
|---|
| 1381 | 2849801 | *ptr = self->ob_sval; |
|---|
| 1382 | 2849801 | return Py_SIZE(self); |
|---|
| 1383 | n/a | } |
|---|
| 1384 | n/a | |
|---|
| 1385 | n/a | static int |
|---|
| 1386 | n/a | string_buffer_getbuffer(PyStringObject *self, Py_buffer *view, int flags) |
|---|
| 1387 | 140714 | { |
|---|
| 1388 | 140714 | return PyBuffer_FillInfo(view, (PyObject*)self, |
|---|
| 1389 | n/a | (void *)self->ob_sval, Py_SIZE(self), |
|---|
| 1390 | n/a | 1, flags); |
|---|
| 1391 | n/a | } |
|---|
| 1392 | n/a | |
|---|
| 1393 | n/a | static PySequenceMethods string_as_sequence = { |
|---|
| 1394 | n/a | (lenfunc)string_length, /*sq_length*/ |
|---|
| 1395 | n/a | (binaryfunc)string_concat, /*sq_concat*/ |
|---|
| 1396 | n/a | (ssizeargfunc)string_repeat, /*sq_repeat*/ |
|---|
| 1397 | n/a | (ssizeargfunc)string_item, /*sq_item*/ |
|---|
| 1398 | n/a | (ssizessizeargfunc)string_slice, /*sq_slice*/ |
|---|
| 1399 | n/a | 0, /*sq_ass_item*/ |
|---|
| 1400 | n/a | 0, /*sq_ass_slice*/ |
|---|
| 1401 | n/a | (objobjproc)string_contains /*sq_contains*/ |
|---|
| 1402 | n/a | }; |
|---|
| 1403 | n/a | |
|---|
| 1404 | n/a | static PyMappingMethods string_as_mapping = { |
|---|
| 1405 | n/a | (lenfunc)string_length, |
|---|
| 1406 | n/a | (binaryfunc)string_subscript, |
|---|
| 1407 | n/a | 0, |
|---|
| 1408 | n/a | }; |
|---|
| 1409 | n/a | |
|---|
| 1410 | n/a | static PyBufferProcs string_as_buffer = { |
|---|
| 1411 | n/a | (readbufferproc)string_buffer_getreadbuf, |
|---|
| 1412 | n/a | (writebufferproc)string_buffer_getwritebuf, |
|---|
| 1413 | n/a | (segcountproc)string_buffer_getsegcount, |
|---|
| 1414 | n/a | (charbufferproc)string_buffer_getcharbuf, |
|---|
| 1415 | n/a | (getbufferproc)string_buffer_getbuffer, |
|---|
| 1416 | n/a | 0, /* XXX */ |
|---|
| 1417 | n/a | }; |
|---|
| 1418 | n/a | |
|---|
| 1419 | n/a | |
|---|
| 1420 | n/a | |
|---|
| 1421 | n/a | #define LEFTSTRIP 0 |
|---|
| 1422 | n/a | #define RIGHTSTRIP 1 |
|---|
| 1423 | n/a | #define BOTHSTRIP 2 |
|---|
| 1424 | n/a | |
|---|
| 1425 | n/a | /* Arrays indexed by above */ |
|---|
| 1426 | n/a | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
|---|
| 1427 | n/a | |
|---|
| 1428 | n/a | #define STRIPNAME(i) (stripformat[i]+3) |
|---|
| 1429 | n/a | |
|---|
| 1430 | n/a | PyDoc_STRVAR(split__doc__, |
|---|
| 1431 | n/a | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
|---|
| 1432 | n/a | \n\ |
|---|
| 1433 | n/a | Return a list of the words in the string S, using sep as the\n\ |
|---|
| 1434 | n/a | delimiter string. If maxsplit is given, at most maxsplit\n\ |
|---|
| 1435 | n/a | splits are done. If sep is not specified or is None, any\n\ |
|---|
| 1436 | n/a | whitespace string is a separator and empty strings are removed\n\ |
|---|
| 1437 | n/a | from the result."); |
|---|
| 1438 | n/a | |
|---|
| 1439 | n/a | static PyObject * |
|---|
| 1440 | n/a | string_split(PyStringObject *self, PyObject *args) |
|---|
| 1441 | 1284685 | { |
|---|
| 1442 | 1284685 | Py_ssize_t len = PyString_GET_SIZE(self), n; |
|---|
| 1443 | 1284685 | Py_ssize_t maxsplit = -1; |
|---|
| 1444 | 1284685 | const char *s = PyString_AS_STRING(self), *sub; |
|---|
| 1445 | 1284685 | PyObject *subobj = Py_None; |
|---|
| 1446 | n/a | |
|---|
| 1447 | 1284685 | if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit)) |
|---|
| 1448 | 2 | return NULL; |
|---|
| 1449 | 1284683 | if (maxsplit < 0) |
|---|
| 1450 | 1280216 | maxsplit = PY_SSIZE_T_MAX; |
|---|
| 1451 | 1284683 | if (subobj == Py_None) |
|---|
| 1452 | 333985 | return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); |
|---|
| 1453 | 950698 | if (PyString_Check(subobj)) { |
|---|
| 1454 | 950692 | sub = PyString_AS_STRING(subobj); |
|---|
| 1455 | 950692 | n = PyString_GET_SIZE(subobj); |
|---|
| 1456 | n/a | } |
|---|
| 1457 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 1458 | 6 | else if (PyUnicode_Check(subobj)) |
|---|
| 1459 | 6 | return PyUnicode_Split((PyObject *)self, subobj, maxsplit); |
|---|
| 1460 | n/a | #endif |
|---|
| 1461 | 0 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
|---|
| 1462 | 0 | return NULL; |
|---|
| 1463 | n/a | |
|---|
| 1464 | 950692 | return stringlib_split((PyObject*) self, s, len, sub, n, maxsplit); |
|---|
| 1465 | n/a | } |
|---|
| 1466 | n/a | |
|---|
| 1467 | n/a | PyDoc_STRVAR(partition__doc__, |
|---|
| 1468 | n/a | "S.partition(sep) -> (head, sep, tail)\n\ |
|---|
| 1469 | n/a | \n\ |
|---|
| 1470 | n/a | Search for the separator sep in S, and return the part before it,\n\ |
|---|
| 1471 | n/a | the separator itself, and the part after it. If the separator is not\n\ |
|---|
| 1472 | n/a | found, return S and two empty strings."); |
|---|
| 1473 | n/a | |
|---|
| 1474 | n/a | static PyObject * |
|---|
| 1475 | n/a | string_partition(PyStringObject *self, PyObject *sep_obj) |
|---|
| 1476 | 5982 | { |
|---|
| 1477 | n/a | const char *sep; |
|---|
| 1478 | n/a | Py_ssize_t sep_len; |
|---|
| 1479 | n/a | |
|---|
| 1480 | 5982 | if (PyString_Check(sep_obj)) { |
|---|
| 1481 | 5975 | sep = PyString_AS_STRING(sep_obj); |
|---|
| 1482 | 5975 | sep_len = PyString_GET_SIZE(sep_obj); |
|---|
| 1483 | n/a | } |
|---|
| 1484 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 1485 | 7 | else if (PyUnicode_Check(sep_obj)) |
|---|
| 1486 | 4 | return PyUnicode_Partition((PyObject *) self, sep_obj); |
|---|
| 1487 | n/a | #endif |
|---|
| 1488 | 3 | else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
|---|
| 1489 | 3 | return NULL; |
|---|
| 1490 | n/a | |
|---|
| 1491 | 5975 | return stringlib_partition( |
|---|
| 1492 | n/a | (PyObject*) self, |
|---|
| 1493 | n/a | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
|---|
| 1494 | n/a | sep_obj, sep, sep_len |
|---|
| 1495 | n/a | ); |
|---|
| 1496 | n/a | } |
|---|
| 1497 | n/a | |
|---|
| 1498 | n/a | PyDoc_STRVAR(rpartition__doc__, |
|---|
| 1499 | n/a | "S.rpartition(sep) -> (head, sep, tail)\n\ |
|---|
| 1500 | n/a | \n\ |
|---|
| 1501 | n/a | Search for the separator sep in S, starting at the end of S, and return\n\ |
|---|
| 1502 | n/a | the part before it, the separator itself, and the part after it. If the\n\ |
|---|
| 1503 | n/a | separator is not found, return two empty strings and S."); |
|---|
| 1504 | n/a | |
|---|
| 1505 | n/a | static PyObject * |
|---|
| 1506 | n/a | string_rpartition(PyStringObject *self, PyObject *sep_obj) |
|---|
| 1507 | 2640 | { |
|---|
| 1508 | n/a | const char *sep; |
|---|
| 1509 | n/a | Py_ssize_t sep_len; |
|---|
| 1510 | n/a | |
|---|
| 1511 | 2640 | if (PyString_Check(sep_obj)) { |
|---|
| 1512 | 2633 | sep = PyString_AS_STRING(sep_obj); |
|---|
| 1513 | 2633 | sep_len = PyString_GET_SIZE(sep_obj); |
|---|
| 1514 | n/a | } |
|---|
| 1515 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 1516 | 7 | else if (PyUnicode_Check(sep_obj)) |
|---|
| 1517 | 4 | return PyUnicode_RPartition((PyObject *) self, sep_obj); |
|---|
| 1518 | n/a | #endif |
|---|
| 1519 | 3 | else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
|---|
| 1520 | 3 | return NULL; |
|---|
| 1521 | n/a | |
|---|
| 1522 | 2633 | return stringlib_rpartition( |
|---|
| 1523 | n/a | (PyObject*) self, |
|---|
| 1524 | n/a | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
|---|
| 1525 | n/a | sep_obj, sep, sep_len |
|---|
| 1526 | n/a | ); |
|---|
| 1527 | n/a | } |
|---|
| 1528 | n/a | |
|---|
| 1529 | n/a | PyDoc_STRVAR(rsplit__doc__, |
|---|
| 1530 | n/a | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
|---|
| 1531 | n/a | \n\ |
|---|
| 1532 | n/a | Return a list of the words in the string S, using sep as the\n\ |
|---|
| 1533 | n/a | delimiter string, starting at the end of the string and working\n\ |
|---|
| 1534 | n/a | to the front. If maxsplit is given, at most maxsplit splits are\n\ |
|---|
| 1535 | n/a | done. If sep is not specified or is None, any whitespace string\n\ |
|---|
| 1536 | n/a | is a separator."); |
|---|
| 1537 | n/a | |
|---|
| 1538 | n/a | static PyObject * |
|---|
| 1539 | n/a | string_rsplit(PyStringObject *self, PyObject *args) |
|---|
| 1540 | 1109 | { |
|---|
| 1541 | 1109 | Py_ssize_t len = PyString_GET_SIZE(self), n; |
|---|
| 1542 | 1109 | Py_ssize_t maxsplit = -1; |
|---|
| 1543 | 1109 | const char *s = PyString_AS_STRING(self), *sub; |
|---|
| 1544 | 1109 | PyObject *subobj = Py_None; |
|---|
| 1545 | n/a | |
|---|
| 1546 | 1109 | if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit)) |
|---|
| 1547 | 2 | return NULL; |
|---|
| 1548 | 1107 | if (maxsplit < 0) |
|---|
| 1549 | 125 | maxsplit = PY_SSIZE_T_MAX; |
|---|
| 1550 | 1107 | if (subobj == Py_None) |
|---|
| 1551 | 100 | return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); |
|---|
| 1552 | 1007 | if (PyString_Check(subobj)) { |
|---|
| 1553 | 1002 | sub = PyString_AS_STRING(subobj); |
|---|
| 1554 | 1002 | n = PyString_GET_SIZE(subobj); |
|---|
| 1555 | n/a | } |
|---|
| 1556 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 1557 | 5 | else if (PyUnicode_Check(subobj)) |
|---|
| 1558 | 5 | return PyUnicode_RSplit((PyObject *)self, subobj, maxsplit); |
|---|
| 1559 | n/a | #endif |
|---|
| 1560 | 0 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
|---|
| 1561 | 0 | return NULL; |
|---|
| 1562 | n/a | |
|---|
| 1563 | 1002 | return stringlib_rsplit((PyObject*) self, s, len, sub, n, maxsplit); |
|---|
| 1564 | n/a | } |
|---|
| 1565 | n/a | |
|---|
| 1566 | n/a | |
|---|
| 1567 | n/a | PyDoc_STRVAR(join__doc__, |
|---|
| 1568 | n/a | "S.join(iterable) -> string\n\ |
|---|
| 1569 | n/a | \n\ |
|---|
| 1570 | n/a | Return a string which is the concatenation of the strings in the\n\ |
|---|
| 1571 | n/a | iterable. The separator between elements is S."); |
|---|
| 1572 | n/a | |
|---|
| 1573 | n/a | static PyObject * |
|---|
| 1574 | n/a | string_join(PyStringObject *self, PyObject *orig) |
|---|
| 1575 | 866867 | { |
|---|
| 1576 | 866867 | char *sep = PyString_AS_STRING(self); |
|---|
| 1577 | 866867 | const Py_ssize_t seplen = PyString_GET_SIZE(self); |
|---|
| 1578 | 866867 | PyObject *res = NULL; |
|---|
| 1579 | n/a | char *p; |
|---|
| 1580 | 866867 | Py_ssize_t seqlen = 0; |
|---|
| 1581 | 866867 | size_t sz = 0; |
|---|
| 1582 | n/a | Py_ssize_t i; |
|---|
| 1583 | n/a | PyObject *seq, *item; |
|---|
| 1584 | n/a | |
|---|
| 1585 | 866867 | seq = PySequence_Fast(orig, ""); |
|---|
| 1586 | 866867 | if (seq == NULL) { |
|---|
| 1587 | 27 | return NULL; |
|---|
| 1588 | n/a | } |
|---|
| 1589 | n/a | |
|---|
| 1590 | 866840 | seqlen = PySequence_Size(seq); |
|---|
| 1591 | 866840 | if (seqlen == 0) { |
|---|
| 1592 | 183714 | Py_DECREF(seq); |
|---|
| 1593 | 183714 | return PyString_FromString(""); |
|---|
| 1594 | n/a | } |
|---|
| 1595 | 683126 | if (seqlen == 1) { |
|---|
| 1596 | 214037 | item = PySequence_Fast_GET_ITEM(seq, 0); |
|---|
| 1597 | 214037 | if (PyString_CheckExact(item) || PyUnicode_CheckExact(item)) { |
|---|
| 1598 | 214034 | Py_INCREF(item); |
|---|
| 1599 | 214034 | Py_DECREF(seq); |
|---|
| 1600 | 214034 | return item; |
|---|
| 1601 | n/a | } |
|---|
| 1602 | n/a | } |
|---|
| 1603 | n/a | |
|---|
| 1604 | n/a | /* There are at least two things to join, or else we have a subclass |
|---|
| 1605 | n/a | * of the builtin types in the sequence. |
|---|
| 1606 | n/a | * Do a pre-pass to figure out the total amount of space we'll |
|---|
| 1607 | n/a | * need (sz), see whether any argument is absurd, and defer to |
|---|
| 1608 | n/a | * the Unicode join if appropriate. |
|---|
| 1609 | n/a | */ |
|---|
| 1610 | 9996789 | for (i = 0; i < seqlen; i++) { |
|---|
| 1611 | 9530763 | const size_t old_sz = sz; |
|---|
| 1612 | 9530763 | item = PySequence_Fast_GET_ITEM(seq, i); |
|---|
| 1613 | 9530763 | if (!PyString_Check(item)){ |
|---|
| 1614 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 1615 | 3066 | if (PyUnicode_Check(item)) { |
|---|
| 1616 | n/a | /* Defer to Unicode join. |
|---|
| 1617 | n/a | * CAUTION: There's no gurantee that the |
|---|
| 1618 | n/a | * original sequence can be iterated over |
|---|
| 1619 | n/a | * again, so we must pass seq here. |
|---|
| 1620 | n/a | */ |
|---|
| 1621 | n/a | PyObject *result; |
|---|
| 1622 | 3058 | result = PyUnicode_Join((PyObject *)self, seq); |
|---|
| 1623 | 3058 | Py_DECREF(seq); |
|---|
| 1624 | 3058 | return result; |
|---|
| 1625 | n/a | } |
|---|
| 1626 | n/a | #endif |
|---|
| 1627 | 8 | PyErr_Format(PyExc_TypeError, |
|---|
| 1628 | n/a | "sequence item %zd: expected string," |
|---|
| 1629 | n/a | " %.80s found", |
|---|
| 1630 | n/a | i, Py_TYPE(item)->tp_name); |
|---|
| 1631 | 8 | Py_DECREF(seq); |
|---|
| 1632 | 8 | return NULL; |
|---|
| 1633 | n/a | } |
|---|
| 1634 | 9527697 | sz += PyString_GET_SIZE(item); |
|---|
| 1635 | 9527697 | if (i != 0) |
|---|
| 1636 | 9061530 | sz += seplen; |
|---|
| 1637 | 9527697 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
|---|
| 1638 | 0 | PyErr_SetString(PyExc_OverflowError, |
|---|
| 1639 | n/a | "join() result is too long for a Python string"); |
|---|
| 1640 | 0 | Py_DECREF(seq); |
|---|
| 1641 | 0 | return NULL; |
|---|
| 1642 | n/a | } |
|---|
| 1643 | n/a | } |
|---|
| 1644 | n/a | |
|---|
| 1645 | n/a | /* Allocate result space. */ |
|---|
| 1646 | 466026 | res = PyString_FromStringAndSize((char*)NULL, sz); |
|---|
| 1647 | 466026 | if (res == NULL) { |
|---|
| 1648 | 0 | Py_DECREF(seq); |
|---|
| 1649 | 0 | return NULL; |
|---|
| 1650 | n/a | } |
|---|
| 1651 | n/a | |
|---|
| 1652 | n/a | /* Catenate everything. */ |
|---|
| 1653 | 466026 | p = PyString_AS_STRING(res); |
|---|
| 1654 | 9993572 | for (i = 0; i < seqlen; ++i) { |
|---|
| 1655 | n/a | size_t n; |
|---|
| 1656 | 9527546 | item = PySequence_Fast_GET_ITEM(seq, i); |
|---|
| 1657 | 9527546 | n = PyString_GET_SIZE(item); |
|---|
| 1658 | 9527546 | Py_MEMCPY(p, PyString_AS_STRING(item), n); |
|---|
| 1659 | 9527546 | p += n; |
|---|
| 1660 | 9527546 | if (i < seqlen - 1) { |
|---|
| 1661 | 9061520 | Py_MEMCPY(p, sep, seplen); |
|---|
| 1662 | 9061520 | p += seplen; |
|---|
| 1663 | n/a | } |
|---|
| 1664 | n/a | } |
|---|
| 1665 | n/a | |
|---|
| 1666 | 466026 | Py_DECREF(seq); |
|---|
| 1667 | 466026 | return res; |
|---|
| 1668 | n/a | } |
|---|
| 1669 | n/a | |
|---|
| 1670 | n/a | PyObject * |
|---|
| 1671 | n/a | _PyString_Join(PyObject *sep, PyObject *x) |
|---|
| 1672 | 53626 | { |
|---|
| 1673 | 53626 | assert(sep != NULL && PyString_Check(sep)); |
|---|
| 1674 | 53626 | assert(x != NULL); |
|---|
| 1675 | 53626 | return string_join((PyStringObject *)sep, x); |
|---|
| 1676 | n/a | } |
|---|
| 1677 | n/a | |
|---|
| 1678 | n/a | /* helper macro to fixup start/end slice values */ |
|---|
| 1679 | n/a | #define ADJUST_INDICES(start, end, len) \ |
|---|
| 1680 | n/a | if (end > len) \ |
|---|
| 1681 | n/a | end = len; \ |
|---|
| 1682 | n/a | else if (end < 0) { \ |
|---|
| 1683 | n/a | end += len; \ |
|---|
| 1684 | n/a | if (end < 0) \ |
|---|
| 1685 | n/a | end = 0; \ |
|---|
| 1686 | n/a | } \ |
|---|
| 1687 | n/a | if (start < 0) { \ |
|---|
| 1688 | n/a | start += len; \ |
|---|
| 1689 | n/a | if (start < 0) \ |
|---|
| 1690 | n/a | start = 0; \ |
|---|
| 1691 | n/a | } |
|---|
| 1692 | n/a | |
|---|
| 1693 | n/a | Py_LOCAL_INLINE(Py_ssize_t) |
|---|
| 1694 | n/a | string_find_internal(PyStringObject *self, PyObject *args, int dir) |
|---|
| 1695 | 181131402 | { |
|---|
| 1696 | n/a | PyObject *subobj; |
|---|
| 1697 | n/a | const char *sub; |
|---|
| 1698 | n/a | Py_ssize_t sub_len; |
|---|
| 1699 | 181131402 | Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
|---|
| 1700 | 181131402 | PyObject *obj_start=Py_None, *obj_end=Py_None; |
|---|
| 1701 | n/a | |
|---|
| 1702 | 181131402 | if (!PyArg_ParseTuple(args, "O|OO:find/rfind/index/rindex", &subobj, |
|---|
| 1703 | n/a | &obj_start, &obj_end)) |
|---|
| 1704 | 12 | return -2; |
|---|
| 1705 | n/a | /* To support None in "start" and "end" arguments, meaning |
|---|
| 1706 | n/a | the same as if they were not passed. |
|---|
| 1707 | n/a | */ |
|---|
| 1708 | 181131390 | if (obj_start != Py_None) |
|---|
| 1709 | 592268 | if (!_PyEval_SliceIndex(obj_start, &start)) |
|---|
| 1710 | 0 | return -2; |
|---|
| 1711 | 181131390 | if (obj_end != Py_None) |
|---|
| 1712 | 530298 | if (!_PyEval_SliceIndex(obj_end, &end)) |
|---|
| 1713 | 0 | return -2; |
|---|
| 1714 | n/a | |
|---|
| 1715 | 181131390 | if (PyString_Check(subobj)) { |
|---|
| 1716 | 181131351 | sub = PyString_AS_STRING(subobj); |
|---|
| 1717 | 181131351 | sub_len = PyString_GET_SIZE(subobj); |
|---|
| 1718 | n/a | } |
|---|
| 1719 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 1720 | 39 | else if (PyUnicode_Check(subobj)) |
|---|
| 1721 | 19 | return PyUnicode_Find( |
|---|
| 1722 | n/a | (PyObject *)self, subobj, start, end, dir); |
|---|
| 1723 | n/a | #endif |
|---|
| 1724 | 20 | else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len)) |
|---|
| 1725 | n/a | /* XXX - the "expected a character buffer object" is pretty |
|---|
| 1726 | n/a | confusing for a non-expert. remap to something else ? */ |
|---|
| 1727 | 20 | return -2; |
|---|
| 1728 | n/a | |
|---|
| 1729 | 181131351 | if (dir > 0) |
|---|
| 1730 | 1024015 | return stringlib_find_slice( |
|---|
| 1731 | n/a | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
|---|
| 1732 | n/a | sub, sub_len, start, end); |
|---|
| 1733 | n/a | else |
|---|
| 1734 | 180107336 | return stringlib_rfind_slice( |
|---|
| 1735 | n/a | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
|---|
| 1736 | n/a | sub, sub_len, start, end); |
|---|
| 1737 | n/a | } |
|---|
| 1738 | n/a | |
|---|
| 1739 | n/a | |
|---|
| 1740 | n/a | PyDoc_STRVAR(find__doc__, |
|---|
| 1741 | n/a | "S.find(sub [,start [,end]]) -> int\n\ |
|---|
| 1742 | n/a | \n\ |
|---|
| 1743 | n/a | Return the lowest index in S where substring sub is found,\n\ |
|---|
| 1744 | n/a | such that sub is contained within s[start:end]. Optional\n\ |
|---|
| 1745 | n/a | arguments start and end are interpreted as in slice notation.\n\ |
|---|
| 1746 | n/a | \n\ |
|---|
| 1747 | n/a | Return -1 on failure."); |
|---|
| 1748 | n/a | |
|---|
| 1749 | n/a | static PyObject * |
|---|
| 1750 | n/a | string_find(PyStringObject *self, PyObject *args) |
|---|
| 1751 | 1022259 | { |
|---|
| 1752 | 1022259 | Py_ssize_t result = string_find_internal(self, args, +1); |
|---|
| 1753 | 1022259 | if (result == -2) |
|---|
| 1754 | 8 | return NULL; |
|---|
| 1755 | 1022251 | return PyInt_FromSsize_t(result); |
|---|
| 1756 | n/a | } |
|---|
| 1757 | n/a | |
|---|
| 1758 | n/a | |
|---|
| 1759 | n/a | PyDoc_STRVAR(index__doc__, |
|---|
| 1760 | n/a | "S.index(sub [,start [,end]]) -> int\n\ |
|---|
| 1761 | n/a | \n\ |
|---|
| 1762 | n/a | Like S.find() but raise ValueError when the substring is not found."); |
|---|
| 1763 | n/a | |
|---|
| 1764 | n/a | static PyObject * |
|---|
| 1765 | n/a | string_index(PyStringObject *self, PyObject *args) |
|---|
| 1766 | 1780 | { |
|---|
| 1767 | 1780 | Py_ssize_t result = string_find_internal(self, args, +1); |
|---|
| 1768 | 1780 | if (result == -2) |
|---|
| 1769 | 8 | return NULL; |
|---|
| 1770 | 1772 | if (result == -1) { |
|---|
| 1771 | 60 | PyErr_SetString(PyExc_ValueError, |
|---|
| 1772 | n/a | "substring not found"); |
|---|
| 1773 | 60 | return NULL; |
|---|
| 1774 | n/a | } |
|---|
| 1775 | 1712 | return PyInt_FromSsize_t(result); |
|---|
| 1776 | n/a | } |
|---|
| 1777 | n/a | |
|---|
| 1778 | n/a | |
|---|
| 1779 | n/a | PyDoc_STRVAR(rfind__doc__, |
|---|
| 1780 | n/a | "S.rfind(sub [,start [,end]]) -> int\n\ |
|---|
| 1781 | n/a | \n\ |
|---|
| 1782 | n/a | Return the highest index in S where substring sub is found,\n\ |
|---|
| 1783 | n/a | such that sub is contained within s[start:end]. Optional\n\ |
|---|
| 1784 | n/a | arguments start and end are interpreted as in slice notation.\n\ |
|---|
| 1785 | n/a | \n\ |
|---|
| 1786 | n/a | Return -1 on failure."); |
|---|
| 1787 | n/a | |
|---|
| 1788 | n/a | static PyObject * |
|---|
| 1789 | n/a | string_rfind(PyStringObject *self, PyObject *args) |
|---|
| 1790 | 180107198 | { |
|---|
| 1791 | 180107198 | Py_ssize_t result = string_find_internal(self, args, -1); |
|---|
| 1792 | 180107198 | if (result == -2) |
|---|
| 1793 | 8 | return NULL; |
|---|
| 1794 | 180107190 | return PyInt_FromSsize_t(result); |
|---|
| 1795 | n/a | } |
|---|
| 1796 | n/a | |
|---|
| 1797 | n/a | |
|---|
| 1798 | n/a | PyDoc_STRVAR(rindex__doc__, |
|---|
| 1799 | n/a | "S.rindex(sub [,start [,end]]) -> int\n\ |
|---|
| 1800 | n/a | \n\ |
|---|
| 1801 | n/a | Like S.rfind() but raise ValueError when the substring is not found."); |
|---|
| 1802 | n/a | |
|---|
| 1803 | n/a | static PyObject * |
|---|
| 1804 | n/a | string_rindex(PyStringObject *self, PyObject *args) |
|---|
| 1805 | 165 | { |
|---|
| 1806 | 165 | Py_ssize_t result = string_find_internal(self, args, -1); |
|---|
| 1807 | 165 | if (result == -2) |
|---|
| 1808 | 8 | return NULL; |
|---|
| 1809 | 157 | if (result == -1) { |
|---|
| 1810 | 37 | PyErr_SetString(PyExc_ValueError, |
|---|
| 1811 | n/a | "substring not found"); |
|---|
| 1812 | 37 | return NULL; |
|---|
| 1813 | n/a | } |
|---|
| 1814 | 120 | return PyInt_FromSsize_t(result); |
|---|
| 1815 | n/a | } |
|---|
| 1816 | n/a | |
|---|
| 1817 | n/a | |
|---|
| 1818 | n/a | Py_LOCAL_INLINE(PyObject *) |
|---|
| 1819 | n/a | do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj) |
|---|
| 1820 | 323653 | { |
|---|
| 1821 | 323653 | char *s = PyString_AS_STRING(self); |
|---|
| 1822 | 323653 | Py_ssize_t len = PyString_GET_SIZE(self); |
|---|
| 1823 | 323653 | char *sep = PyString_AS_STRING(sepobj); |
|---|
| 1824 | 323653 | Py_ssize_t seplen = PyString_GET_SIZE(sepobj); |
|---|
| 1825 | n/a | Py_ssize_t i, j; |
|---|
| 1826 | n/a | |
|---|
| 1827 | 323653 | i = 0; |
|---|
| 1828 | 323653 | if (striptype != RIGHTSTRIP) { |
|---|
| 1829 | 1250735 | while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) { |
|---|
| 1830 | 700199 | i++; |
|---|
| 1831 | n/a | } |
|---|
| 1832 | n/a | } |
|---|
| 1833 | n/a | |
|---|
| 1834 | 323653 | j = len; |
|---|
| 1835 | 323653 | if (striptype != LEFTSTRIP) { |
|---|
| 1836 | n/a | do { |
|---|
| 1837 | 98789 | j--; |
|---|
| 1838 | 98789 | } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen)); |
|---|
| 1839 | 50617 | j++; |
|---|
| 1840 | n/a | } |
|---|
| 1841 | n/a | |
|---|
| 1842 | 323653 | if (i == 0 && j == len && PyString_CheckExact(self)) { |
|---|
| 1843 | 11400 | Py_INCREF(self); |
|---|
| 1844 | 11400 | return (PyObject*)self; |
|---|
| 1845 | n/a | } |
|---|
| 1846 | n/a | else |
|---|
| 1847 | 312253 | return PyString_FromStringAndSize(s+i, j-i); |
|---|
| 1848 | n/a | } |
|---|
| 1849 | n/a | |
|---|
| 1850 | n/a | |
|---|
| 1851 | n/a | Py_LOCAL_INLINE(PyObject *) |
|---|
| 1852 | n/a | do_strip(PyStringObject *self, int striptype) |
|---|
| 1853 | 987339 | { |
|---|
| 1854 | 987339 | char *s = PyString_AS_STRING(self); |
|---|
| 1855 | 987339 | Py_ssize_t len = PyString_GET_SIZE(self), i, j; |
|---|
| 1856 | n/a | |
|---|
| 1857 | 987339 | i = 0; |
|---|
| 1858 | 987339 | if (striptype != RIGHTSTRIP) { |
|---|
| 1859 | 2562544 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
|---|
| 1860 | 606510 | i++; |
|---|
| 1861 | n/a | } |
|---|
| 1862 | n/a | } |
|---|
| 1863 | n/a | |
|---|
| 1864 | 987339 | j = len; |
|---|
| 1865 | 987339 | if (striptype != LEFTSTRIP) { |
|---|
| 1866 | n/a | do { |
|---|
| 1867 | 1782478 | j--; |
|---|
| 1868 | 1782478 | } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
|---|
| 1869 | 977808 | j++; |
|---|
| 1870 | n/a | } |
|---|
| 1871 | n/a | |
|---|
| 1872 | 987339 | if (i == 0 && j == len && PyString_CheckExact(self)) { |
|---|
| 1873 | 308443 | Py_INCREF(self); |
|---|
| 1874 | 308443 | return (PyObject*)self; |
|---|
| 1875 | n/a | } |
|---|
| 1876 | n/a | else |
|---|
| 1877 | 678896 | return PyString_FromStringAndSize(s+i, j-i); |
|---|
| 1878 | n/a | } |
|---|
| 1879 | n/a | |
|---|
| 1880 | n/a | |
|---|
| 1881 | n/a | Py_LOCAL_INLINE(PyObject *) |
|---|
| 1882 | n/a | do_argstrip(PyStringObject *self, int striptype, PyObject *args) |
|---|
| 1883 | 324603 | { |
|---|
| 1884 | 324603 | PyObject *sep = NULL; |
|---|
| 1885 | n/a | |
|---|
| 1886 | 324603 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
|---|
| 1887 | 6 | return NULL; |
|---|
| 1888 | n/a | |
|---|
| 1889 | 324597 | if (sep != NULL && sep != Py_None) { |
|---|
| 1890 | 323668 | if (PyString_Check(sep)) |
|---|
| 1891 | 323653 | return do_xstrip(self, striptype, sep); |
|---|
| 1892 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 1893 | 15 | else if (PyUnicode_Check(sep)) { |
|---|
| 1894 | 15 | PyObject *uniself = PyUnicode_FromObject((PyObject *)self); |
|---|
| 1895 | n/a | PyObject *res; |
|---|
| 1896 | 15 | if (uniself==NULL) |
|---|
| 1897 | 0 | return NULL; |
|---|
| 1898 | 15 | res = _PyUnicode_XStrip((PyUnicodeObject *)uniself, |
|---|
| 1899 | n/a | striptype, sep); |
|---|
| 1900 | 15 | Py_DECREF(uniself); |
|---|
| 1901 | 15 | return res; |
|---|
| 1902 | n/a | } |
|---|
| 1903 | n/a | #endif |
|---|
| 1904 | 0 | PyErr_Format(PyExc_TypeError, |
|---|
| 1905 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 1906 | n/a | "%s arg must be None, str or unicode", |
|---|
| 1907 | n/a | #else |
|---|
| 1908 | n/a | "%s arg must be None or str", |
|---|
| 1909 | n/a | #endif |
|---|
| 1910 | n/a | STRIPNAME(striptype)); |
|---|
| 1911 | 0 | return NULL; |
|---|
| 1912 | n/a | } |
|---|
| 1913 | n/a | |
|---|
| 1914 | 929 | return do_strip(self, striptype); |
|---|
| 1915 | n/a | } |
|---|
| 1916 | n/a | |
|---|
| 1917 | n/a | |
|---|
| 1918 | n/a | PyDoc_STRVAR(strip__doc__, |
|---|
| 1919 | n/a | "S.strip([chars]) -> string or unicode\n\ |
|---|
| 1920 | n/a | \n\ |
|---|
| 1921 | n/a | Return a copy of the string S with leading and trailing\n\ |
|---|
| 1922 | n/a | whitespace removed.\n\ |
|---|
| 1923 | n/a | If chars is given and not None, remove characters in chars instead.\n\ |
|---|
| 1924 | n/a | If chars is unicode, S will be converted to unicode before stripping"); |
|---|
| 1925 | n/a | |
|---|
| 1926 | n/a | static PyObject * |
|---|
| 1927 | n/a | string_strip(PyStringObject *self, PyObject *args) |
|---|
| 1928 | 970725 | { |
|---|
| 1929 | 970725 | if (PyTuple_GET_SIZE(args) == 0) |
|---|
| 1930 | 968377 | return do_strip(self, BOTHSTRIP); /* Common case */ |
|---|
| 1931 | n/a | else |
|---|
| 1932 | 2348 | return do_argstrip(self, BOTHSTRIP, args); |
|---|
| 1933 | n/a | } |
|---|
| 1934 | n/a | |
|---|
| 1935 | n/a | |
|---|
| 1936 | n/a | PyDoc_STRVAR(lstrip__doc__, |
|---|
| 1937 | n/a | "S.lstrip([chars]) -> string or unicode\n\ |
|---|
| 1938 | n/a | \n\ |
|---|
| 1939 | n/a | Return a copy of the string S with leading whitespace removed.\n\ |
|---|
| 1940 | n/a | If chars is given and not None, remove characters in chars instead.\n\ |
|---|
| 1941 | n/a | If chars is unicode, S will be converted to unicode before stripping"); |
|---|
| 1942 | n/a | |
|---|
| 1943 | n/a | static PyObject * |
|---|
| 1944 | n/a | string_lstrip(PyStringObject *self, PyObject *args) |
|---|
| 1945 | 282574 | { |
|---|
| 1946 | 282574 | if (PyTuple_GET_SIZE(args) == 0) |
|---|
| 1947 | 9183 | return do_strip(self, LEFTSTRIP); /* Common case */ |
|---|
| 1948 | n/a | else |
|---|
| 1949 | 273391 | return do_argstrip(self, LEFTSTRIP, args); |
|---|
| 1950 | n/a | } |
|---|
| 1951 | n/a | |
|---|
| 1952 | n/a | |
|---|
| 1953 | n/a | PyDoc_STRVAR(rstrip__doc__, |
|---|
| 1954 | n/a | "S.rstrip([chars]) -> string or unicode\n\ |
|---|
| 1955 | n/a | \n\ |
|---|
| 1956 | n/a | Return a copy of the string S with trailing whitespace removed.\n\ |
|---|
| 1957 | n/a | If chars is given and not None, remove characters in chars instead.\n\ |
|---|
| 1958 | n/a | If chars is unicode, S will be converted to unicode before stripping"); |
|---|
| 1959 | n/a | |
|---|
| 1960 | n/a | static PyObject * |
|---|
| 1961 | n/a | string_rstrip(PyStringObject *self, PyObject *args) |
|---|
| 1962 | 57714 | { |
|---|
| 1963 | 57714 | if (PyTuple_GET_SIZE(args) == 0) |
|---|
| 1964 | 8850 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
|---|
| 1965 | n/a | else |
|---|
| 1966 | 48864 | return do_argstrip(self, RIGHTSTRIP, args); |
|---|
| 1967 | n/a | } |
|---|
| 1968 | n/a | |
|---|
| 1969 | n/a | |
|---|
| 1970 | n/a | PyDoc_STRVAR(lower__doc__, |
|---|
| 1971 | n/a | "S.lower() -> string\n\ |
|---|
| 1972 | n/a | \n\ |
|---|
| 1973 | n/a | Return a copy of the string S converted to lowercase."); |
|---|
| 1974 | n/a | |
|---|
| 1975 | n/a | /* _tolower and _toupper are defined by SUSv2, but they're not ISO C */ |
|---|
| 1976 | n/a | #ifndef _tolower |
|---|
| 1977 | n/a | #define _tolower tolower |
|---|
| 1978 | n/a | #endif |
|---|
| 1979 | n/a | |
|---|
| 1980 | n/a | static PyObject * |
|---|
| 1981 | n/a | string_lower(PyStringObject *self) |
|---|
| 1982 | 269806 | { |
|---|
| 1983 | n/a | char *s; |
|---|
| 1984 | 269806 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
|---|
| 1985 | n/a | PyObject *newobj; |
|---|
| 1986 | n/a | |
|---|
| 1987 | 269806 | newobj = PyString_FromStringAndSize(NULL, n); |
|---|
| 1988 | 269806 | if (!newobj) |
|---|
| 1989 | 0 | return NULL; |
|---|
| 1990 | n/a | |
|---|
| 1991 | 269806 | s = PyString_AS_STRING(newobj); |
|---|
| 1992 | n/a | |
|---|
| 1993 | 269806 | Py_MEMCPY(s, PyString_AS_STRING(self), n); |
|---|
| 1994 | n/a | |
|---|
| 1995 | 2411390 | for (i = 0; i < n; i++) { |
|---|
| 1996 | 2141584 | int c = Py_CHARMASK(s[i]); |
|---|
| 1997 | 2141584 | if (isupper(c)) |
|---|
| 1998 | 255463 | s[i] = _tolower(c); |
|---|
| 1999 | n/a | } |
|---|
| 2000 | n/a | |
|---|
| 2001 | 269806 | return newobj; |
|---|
| 2002 | n/a | } |
|---|
| 2003 | n/a | |
|---|
| 2004 | n/a | PyDoc_STRVAR(upper__doc__, |
|---|
| 2005 | n/a | "S.upper() -> string\n\ |
|---|
| 2006 | n/a | \n\ |
|---|
| 2007 | n/a | Return a copy of the string S converted to uppercase."); |
|---|
| 2008 | n/a | |
|---|
| 2009 | n/a | #ifndef _toupper |
|---|
| 2010 | n/a | #define _toupper toupper |
|---|
| 2011 | n/a | #endif |
|---|
| 2012 | n/a | |
|---|
| 2013 | n/a | static PyObject * |
|---|
| 2014 | n/a | string_upper(PyStringObject *self) |
|---|
| 2015 | 5793 | { |
|---|
| 2016 | n/a | char *s; |
|---|
| 2017 | 5793 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
|---|
| 2018 | n/a | PyObject *newobj; |
|---|
| 2019 | n/a | |
|---|
| 2020 | 5793 | newobj = PyString_FromStringAndSize(NULL, n); |
|---|
| 2021 | 5793 | if (!newobj) |
|---|
| 2022 | 0 | return NULL; |
|---|
| 2023 | n/a | |
|---|
| 2024 | 5793 | s = PyString_AS_STRING(newobj); |
|---|
| 2025 | n/a | |
|---|
| 2026 | 5793 | Py_MEMCPY(s, PyString_AS_STRING(self), n); |
|---|
| 2027 | n/a | |
|---|
| 2028 | 27100 | for (i = 0; i < n; i++) { |
|---|
| 2029 | 21307 | int c = Py_CHARMASK(s[i]); |
|---|
| 2030 | 21307 | if (islower(c)) |
|---|
| 2031 | 17983 | s[i] = _toupper(c); |
|---|
| 2032 | n/a | } |
|---|
| 2033 | n/a | |
|---|
| 2034 | 5793 | return newobj; |
|---|
| 2035 | n/a | } |
|---|
| 2036 | n/a | |
|---|
| 2037 | n/a | PyDoc_STRVAR(title__doc__, |
|---|
| 2038 | n/a | "S.title() -> string\n\ |
|---|
| 2039 | n/a | \n\ |
|---|
| 2040 | n/a | Return a titlecased version of S, i.e. words start with uppercase\n\ |
|---|
| 2041 | n/a | characters, all remaining cased characters have lowercase."); |
|---|
| 2042 | n/a | |
|---|
| 2043 | n/a | static PyObject* |
|---|
| 2044 | n/a | string_title(PyStringObject *self) |
|---|
| 2045 | 1000 | { |
|---|
| 2046 | 1000 | char *s = PyString_AS_STRING(self), *s_new; |
|---|
| 2047 | 1000 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
|---|
| 2048 | 1000 | int previous_is_cased = 0; |
|---|
| 2049 | n/a | PyObject *newobj; |
|---|
| 2050 | n/a | |
|---|
| 2051 | 1000 | newobj = PyString_FromStringAndSize(NULL, n); |
|---|
| 2052 | 1000 | if (newobj == NULL) |
|---|
| 2053 | 0 | return NULL; |
|---|
| 2054 | 1000 | s_new = PyString_AsString(newobj); |
|---|
| 2055 | 12509 | for (i = 0; i < n; i++) { |
|---|
| 2056 | 11509 | int c = Py_CHARMASK(*s++); |
|---|
| 2057 | 11509 | if (islower(c)) { |
|---|
| 2058 | 9581 | if (!previous_is_cased) |
|---|
| 2059 | 904 | c = toupper(c); |
|---|
| 2060 | 9581 | previous_is_cased = 1; |
|---|
| 2061 | 1928 | } else if (isupper(c)) { |
|---|
| 2062 | 1815 | if (previous_is_cased) |
|---|
| 2063 | 1638 | c = tolower(c); |
|---|
| 2064 | 1815 | previous_is_cased = 1; |
|---|
| 2065 | n/a | } else |
|---|
| 2066 | 113 | previous_is_cased = 0; |
|---|
| 2067 | 11509 | *s_new++ = c; |
|---|
| 2068 | n/a | } |
|---|
| 2069 | 1000 | return newobj; |
|---|
| 2070 | n/a | } |
|---|
| 2071 | n/a | |
|---|
| 2072 | n/a | PyDoc_STRVAR(capitalize__doc__, |
|---|
| 2073 | n/a | "S.capitalize() -> string\n\ |
|---|
| 2074 | n/a | \n\ |
|---|
| 2075 | n/a | Return a copy of the string S with only its first character\n\ |
|---|
| 2076 | n/a | capitalized."); |
|---|
| 2077 | n/a | |
|---|
| 2078 | n/a | static PyObject * |
|---|
| 2079 | n/a | string_capitalize(PyStringObject *self) |
|---|
| 2080 | 397 | { |
|---|
| 2081 | 397 | char *s = PyString_AS_STRING(self), *s_new; |
|---|
| 2082 | 397 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
|---|
| 2083 | n/a | PyObject *newobj; |
|---|
| 2084 | n/a | |
|---|
| 2085 | 397 | newobj = PyString_FromStringAndSize(NULL, n); |
|---|
| 2086 | 397 | if (newobj == NULL) |
|---|
| 2087 | 0 | return NULL; |
|---|
| 2088 | 397 | s_new = PyString_AsString(newobj); |
|---|
| 2089 | 397 | if (0 < n) { |
|---|
| 2090 | 395 | int c = Py_CHARMASK(*s++); |
|---|
| 2091 | 395 | if (islower(c)) |
|---|
| 2092 | 58 | *s_new = toupper(c); |
|---|
| 2093 | n/a | else |
|---|
| 2094 | 337 | *s_new = c; |
|---|
| 2095 | 395 | s_new++; |
|---|
| 2096 | n/a | } |
|---|
| 2097 | 8077 | for (i = 1; i < n; i++) { |
|---|
| 2098 | 7680 | int c = Py_CHARMASK(*s++); |
|---|
| 2099 | 7680 | if (isupper(c)) |
|---|
| 2100 | 53 | *s_new = tolower(c); |
|---|
| 2101 | n/a | else |
|---|
| 2102 | 7627 | *s_new = c; |
|---|
| 2103 | 7680 | s_new++; |
|---|
| 2104 | n/a | } |
|---|
| 2105 | 397 | return newobj; |
|---|
| 2106 | n/a | } |
|---|
| 2107 | n/a | |
|---|
| 2108 | n/a | |
|---|
| 2109 | n/a | PyDoc_STRVAR(count__doc__, |
|---|
| 2110 | n/a | "S.count(sub[, start[, end]]) -> int\n\ |
|---|
| 2111 | n/a | \n\ |
|---|
| 2112 | n/a | Return the number of non-overlapping occurrences of substring sub in\n\ |
|---|
| 2113 | n/a | string S[start:end]. Optional arguments start and end are interpreted\n\ |
|---|
| 2114 | n/a | as in slice notation."); |
|---|
| 2115 | n/a | |
|---|
| 2116 | n/a | static PyObject * |
|---|
| 2117 | n/a | string_count(PyStringObject *self, PyObject *args) |
|---|
| 2118 | 479147 | { |
|---|
| 2119 | n/a | PyObject *sub_obj; |
|---|
| 2120 | 479147 | const char *str = PyString_AS_STRING(self), *sub; |
|---|
| 2121 | n/a | Py_ssize_t sub_len; |
|---|
| 2122 | 479147 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
|---|
| 2123 | n/a | |
|---|
| 2124 | 479147 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj, |
|---|
| 2125 | n/a | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
|---|
| 2126 | 3 | return NULL; |
|---|
| 2127 | n/a | |
|---|
| 2128 | 479144 | if (PyString_Check(sub_obj)) { |
|---|
| 2129 | 479137 | sub = PyString_AS_STRING(sub_obj); |
|---|
| 2130 | 479137 | sub_len = PyString_GET_SIZE(sub_obj); |
|---|
| 2131 | n/a | } |
|---|
| 2132 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 2133 | 7 | else if (PyUnicode_Check(sub_obj)) { |
|---|
| 2134 | n/a | Py_ssize_t count; |
|---|
| 2135 | 2 | count = PyUnicode_Count((PyObject *)self, sub_obj, start, end); |
|---|
| 2136 | 2 | if (count == -1) |
|---|
| 2137 | 0 | return NULL; |
|---|
| 2138 | n/a | else |
|---|
| 2139 | 2 | return PyInt_FromSsize_t(count); |
|---|
| 2140 | n/a | } |
|---|
| 2141 | n/a | #endif |
|---|
| 2142 | 5 | else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len)) |
|---|
| 2143 | 5 | return NULL; |
|---|
| 2144 | n/a | |
|---|
| 2145 | 479137 | ADJUST_INDICES(start, end, PyString_GET_SIZE(self)); |
|---|
| 2146 | n/a | |
|---|
| 2147 | 479137 | return PyInt_FromSsize_t( |
|---|
| 2148 | n/a | stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX) |
|---|
| 2149 | n/a | ); |
|---|
| 2150 | n/a | } |
|---|
| 2151 | n/a | |
|---|
| 2152 | n/a | PyDoc_STRVAR(swapcase__doc__, |
|---|
| 2153 | n/a | "S.swapcase() -> string\n\ |
|---|
| 2154 | n/a | \n\ |
|---|
| 2155 | n/a | Return a copy of the string S with uppercase characters\n\ |
|---|
| 2156 | n/a | converted to lowercase and vice versa."); |
|---|
| 2157 | n/a | |
|---|
| 2158 | n/a | static PyObject * |
|---|
| 2159 | n/a | string_swapcase(PyStringObject *self) |
|---|
| 2160 | 14 | { |
|---|
| 2161 | 14 | char *s = PyString_AS_STRING(self), *s_new; |
|---|
| 2162 | 14 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
|---|
| 2163 | n/a | PyObject *newobj; |
|---|
| 2164 | n/a | |
|---|
| 2165 | 14 | newobj = PyString_FromStringAndSize(NULL, n); |
|---|
| 2166 | 14 | if (newobj == NULL) |
|---|
| 2167 | 0 | return NULL; |
|---|
| 2168 | 14 | s_new = PyString_AsString(newobj); |
|---|
| 2169 | 7246 | for (i = 0; i < n; i++) { |
|---|
| 2170 | 7232 | int c = Py_CHARMASK(*s++); |
|---|
| 2171 | 7232 | if (islower(c)) { |
|---|
| 2172 | 2413 | *s_new = toupper(c); |
|---|
| 2173 | n/a | } |
|---|
| 2174 | 4819 | else if (isupper(c)) { |
|---|
| 2175 | 1759 | *s_new = tolower(c); |
|---|
| 2176 | n/a | } |
|---|
| 2177 | n/a | else |
|---|
| 2178 | 3060 | *s_new = c; |
|---|
| 2179 | 7232 | s_new++; |
|---|
| 2180 | n/a | } |
|---|
| 2181 | 14 | return newobj; |
|---|
| 2182 | n/a | } |
|---|
| 2183 | n/a | |
|---|
| 2184 | n/a | |
|---|
| 2185 | n/a | PyDoc_STRVAR(translate__doc__, |
|---|
| 2186 | n/a | "S.translate(table [,deletechars]) -> string\n\ |
|---|
| 2187 | n/a | \n\ |
|---|
| 2188 | n/a | Return a copy of the string S, where all characters occurring\n\ |
|---|
| 2189 | n/a | in the optional argument deletechars are removed, and the\n\ |
|---|
| 2190 | n/a | remaining characters have been mapped through the given\n\ |
|---|
| 2191 | n/a | translation table, which must be a string of length 256."); |
|---|
| 2192 | n/a | |
|---|
| 2193 | n/a | static PyObject * |
|---|
| 2194 | n/a | string_translate(PyStringObject *self, PyObject *args) |
|---|
| 2195 | 4466 | { |
|---|
| 2196 | n/a | register char *input, *output; |
|---|
| 2197 | n/a | const char *table; |
|---|
| 2198 | 4466 | register Py_ssize_t i, c, changed = 0; |
|---|
| 2199 | 4466 | PyObject *input_obj = (PyObject*)self; |
|---|
| 2200 | 4466 | const char *output_start, *del_table=NULL; |
|---|
| 2201 | 4466 | Py_ssize_t inlen, tablen, dellen = 0; |
|---|
| 2202 | n/a | PyObject *result; |
|---|
| 2203 | n/a | int trans_table[256]; |
|---|
| 2204 | 4466 | PyObject *tableobj, *delobj = NULL; |
|---|
| 2205 | n/a | |
|---|
| 2206 | 4466 | if (!PyArg_UnpackTuple(args, "translate", 1, 2, |
|---|
| 2207 | n/a | &tableobj, &delobj)) |
|---|
| 2208 | 0 | return NULL; |
|---|
| 2209 | n/a | |
|---|
| 2210 | 4466 | if (PyString_Check(tableobj)) { |
|---|
| 2211 | 4457 | table = PyString_AS_STRING(tableobj); |
|---|
| 2212 | 4457 | tablen = PyString_GET_SIZE(tableobj); |
|---|
| 2213 | n/a | } |
|---|
| 2214 | 9 | else if (tableobj == Py_None) { |
|---|
| 2215 | 9 | table = NULL; |
|---|
| 2216 | 9 | tablen = 256; |
|---|
| 2217 | n/a | } |
|---|
| 2218 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 2219 | 0 | else if (PyUnicode_Check(tableobj)) { |
|---|
| 2220 | n/a | /* Unicode .translate() does not support the deletechars |
|---|
| 2221 | n/a | parameter; instead a mapping to None will cause characters |
|---|
| 2222 | n/a | to be deleted. */ |
|---|
| 2223 | 0 | if (delobj != NULL) { |
|---|
| 2224 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 2225 | n/a | "deletions are implemented differently for unicode"); |
|---|
| 2226 | 0 | return NULL; |
|---|
| 2227 | n/a | } |
|---|
| 2228 | 0 | return PyUnicode_Translate((PyObject *)self, tableobj, NULL); |
|---|
| 2229 | n/a | } |
|---|
| 2230 | n/a | #endif |
|---|
| 2231 | 0 | else if (PyObject_AsCharBuffer(tableobj, &table, &tablen)) |
|---|
| 2232 | 0 | return NULL; |
|---|
| 2233 | n/a | |
|---|
| 2234 | 4466 | if (tablen != 256) { |
|---|
| 2235 | 6 | PyErr_SetString(PyExc_ValueError, |
|---|
| 2236 | n/a | "translation table must be 256 characters long"); |
|---|
| 2237 | 6 | return NULL; |
|---|
| 2238 | n/a | } |
|---|
| 2239 | n/a | |
|---|
| 2240 | 4460 | if (delobj != NULL) { |
|---|
| 2241 | 47 | if (PyString_Check(delobj)) { |
|---|
| 2242 | 47 | del_table = PyString_AS_STRING(delobj); |
|---|
| 2243 | 47 | dellen = PyString_GET_SIZE(delobj); |
|---|
| 2244 | n/a | } |
|---|
| 2245 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 2246 | 0 | else if (PyUnicode_Check(delobj)) { |
|---|
| 2247 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 2248 | n/a | "deletions are implemented differently for unicode"); |
|---|
| 2249 | 0 | return NULL; |
|---|
| 2250 | n/a | } |
|---|
| 2251 | n/a | #endif |
|---|
| 2252 | 0 | else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen)) |
|---|
| 2253 | 0 | return NULL; |
|---|
| 2254 | n/a | } |
|---|
| 2255 | n/a | else { |
|---|
| 2256 | 4413 | del_table = NULL; |
|---|
| 2257 | 4413 | dellen = 0; |
|---|
| 2258 | n/a | } |
|---|
| 2259 | n/a | |
|---|
| 2260 | 4460 | inlen = PyString_GET_SIZE(input_obj); |
|---|
| 2261 | 4460 | result = PyString_FromStringAndSize((char *)NULL, inlen); |
|---|
| 2262 | 4460 | if (result == NULL) |
|---|
| 2263 | 0 | return NULL; |
|---|
| 2264 | 4460 | output_start = output = PyString_AsString(result); |
|---|
| 2265 | 4460 | input = PyString_AS_STRING(input_obj); |
|---|
| 2266 | n/a | |
|---|
| 2267 | 4460 | if (dellen == 0 && table != NULL) { |
|---|
| 2268 | n/a | /* If no deletions are required, use faster code */ |
|---|
| 2269 | 146032 | for (i = inlen; --i >= 0; ) { |
|---|
| 2270 | 137210 | c = Py_CHARMASK(*input++); |
|---|
| 2271 | 137210 | if (Py_CHARMASK((*output++ = table[c])) != c) |
|---|
| 2272 | 66449 | changed = 1; |
|---|
| 2273 | n/a | } |
|---|
| 2274 | 4411 | if (changed || !PyString_CheckExact(input_obj)) |
|---|
| 2275 | 1592 | return result; |
|---|
| 2276 | 2819 | Py_DECREF(result); |
|---|
| 2277 | 2819 | Py_INCREF(input_obj); |
|---|
| 2278 | 2819 | return input_obj; |
|---|
| 2279 | n/a | } |
|---|
| 2280 | n/a | |
|---|
| 2281 | 49 | if (table == NULL) { |
|---|
| 2282 | 2313 | for (i = 0; i < 256; i++) |
|---|
| 2283 | 2304 | trans_table[i] = Py_CHARMASK(i); |
|---|
| 2284 | n/a | } else { |
|---|
| 2285 | 10280 | for (i = 0; i < 256; i++) |
|---|
| 2286 | 10240 | trans_table[i] = Py_CHARMASK(table[i]); |
|---|
| 2287 | n/a | } |
|---|
| 2288 | n/a | |
|---|
| 2289 | 2454 | for (i = 0; i < dellen; i++) |
|---|
| 2290 | 2405 | trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
|---|
| 2291 | n/a | |
|---|
| 2292 | 380 | for (i = inlen; --i >= 0; ) { |
|---|
| 2293 | 282 | c = Py_CHARMASK(*input++); |
|---|
| 2294 | 282 | if (trans_table[c] != -1) |
|---|
| 2295 | 76 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
|---|
| 2296 | 67 | continue; |
|---|
| 2297 | 215 | changed = 1; |
|---|
| 2298 | n/a | } |
|---|
| 2299 | 49 | if (!changed && PyString_CheckExact(input_obj)) { |
|---|
| 2300 | 6 | Py_DECREF(result); |
|---|
| 2301 | 6 | Py_INCREF(input_obj); |
|---|
| 2302 | 6 | return input_obj; |
|---|
| 2303 | n/a | } |
|---|
| 2304 | n/a | /* Fix the size of the resulting string */ |
|---|
| 2305 | 43 | if (inlen > 0 && _PyString_Resize(&result, output - output_start)) |
|---|
| 2306 | 0 | return NULL; |
|---|
| 2307 | 43 | return result; |
|---|
| 2308 | n/a | } |
|---|
| 2309 | n/a | |
|---|
| 2310 | n/a | |
|---|
| 2311 | n/a | /* find and count characters and substrings */ |
|---|
| 2312 | n/a | |
|---|
| 2313 | n/a | #define findchar(target, target_len, c) \ |
|---|
| 2314 | n/a | ((char *)memchr((const void *)(target), c, target_len)) |
|---|
| 2315 | n/a | |
|---|
| 2316 | n/a | /* String ops must return a string. */ |
|---|
| 2317 | n/a | /* If the object is subclass of string, create a copy */ |
|---|
| 2318 | n/a | Py_LOCAL(PyStringObject *) |
|---|
| 2319 | n/a | return_self(PyStringObject *self) |
|---|
| 2320 | 1638610 | { |
|---|
| 2321 | 1638610 | if (PyString_CheckExact(self)) { |
|---|
| 2322 | 1638571 | Py_INCREF(self); |
|---|
| 2323 | 1638571 | return self; |
|---|
| 2324 | n/a | } |
|---|
| 2325 | 39 | return (PyStringObject *)PyString_FromStringAndSize( |
|---|
| 2326 | n/a | PyString_AS_STRING(self), |
|---|
| 2327 | n/a | PyString_GET_SIZE(self)); |
|---|
| 2328 | n/a | } |
|---|
| 2329 | n/a | |
|---|
| 2330 | n/a | Py_LOCAL_INLINE(Py_ssize_t) |
|---|
| 2331 | n/a | countchar(const char *target, int target_len, char c, Py_ssize_t maxcount) |
|---|
| 2332 | 462253 | { |
|---|
| 2333 | 462253 | Py_ssize_t count=0; |
|---|
| 2334 | 462253 | const char *start=target; |
|---|
| 2335 | 462253 | const char *end=target+target_len; |
|---|
| 2336 | n/a | |
|---|
| 2337 | 1020085 | while ( (start=findchar(start, end-start, c)) != NULL ) { |
|---|
| 2338 | 95629 | count++; |
|---|
| 2339 | 95629 | if (count >= maxcount) |
|---|
| 2340 | 50 | break; |
|---|
| 2341 | 95579 | start += 1; |
|---|
| 2342 | n/a | } |
|---|
| 2343 | 462253 | return count; |
|---|
| 2344 | n/a | } |
|---|
| 2345 | n/a | |
|---|
| 2346 | n/a | |
|---|
| 2347 | n/a | /* Algorithms for different cases of string replacement */ |
|---|
| 2348 | n/a | |
|---|
| 2349 | n/a | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
|---|
| 2350 | n/a | Py_LOCAL(PyStringObject *) |
|---|
| 2351 | n/a | replace_interleave(PyStringObject *self, |
|---|
| 2352 | n/a | const char *to_s, Py_ssize_t to_len, |
|---|
| 2353 | n/a | Py_ssize_t maxcount) |
|---|
| 2354 | 65 | { |
|---|
| 2355 | n/a | char *self_s, *result_s; |
|---|
| 2356 | n/a | Py_ssize_t self_len, result_len; |
|---|
| 2357 | n/a | Py_ssize_t count, i, product; |
|---|
| 2358 | n/a | PyStringObject *result; |
|---|
| 2359 | n/a | |
|---|
| 2360 | 65 | self_len = PyString_GET_SIZE(self); |
|---|
| 2361 | n/a | |
|---|
| 2362 | n/a | /* 1 at the end plus 1 after every character */ |
|---|
| 2363 | 65 | count = self_len+1; |
|---|
| 2364 | 65 | if (maxcount < count) |
|---|
| 2365 | 15 | count = maxcount; |
|---|
| 2366 | n/a | |
|---|
| 2367 | n/a | /* Check for overflow */ |
|---|
| 2368 | n/a | /* result_len = count * to_len + self_len; */ |
|---|
| 2369 | 65 | product = count * to_len; |
|---|
| 2370 | 65 | if (product / to_len != count) { |
|---|
| 2371 | 0 | PyErr_SetString(PyExc_OverflowError, |
|---|
| 2372 | n/a | "replace string is too long"); |
|---|
| 2373 | 0 | return NULL; |
|---|
| 2374 | n/a | } |
|---|
| 2375 | 65 | result_len = product + self_len; |
|---|
| 2376 | 65 | if (result_len < 0) { |
|---|
| 2377 | 0 | PyErr_SetString(PyExc_OverflowError, |
|---|
| 2378 | n/a | "replace string is too long"); |
|---|
| 2379 | 0 | return NULL; |
|---|
| 2380 | n/a | } |
|---|
| 2381 | n/a | |
|---|
| 2382 | 65 | if (! (result = (PyStringObject *) |
|---|
| 2383 | n/a | PyString_FromStringAndSize(NULL, result_len)) ) |
|---|
| 2384 | 0 | return NULL; |
|---|
| 2385 | n/a | |
|---|
| 2386 | 65 | self_s = PyString_AS_STRING(self); |
|---|
| 2387 | 65 | result_s = PyString_AS_STRING(result); |
|---|
| 2388 | n/a | |
|---|
| 2389 | n/a | /* TODO: special case single character, which doesn't need memcpy */ |
|---|
| 2390 | n/a | |
|---|
| 2391 | n/a | /* Lay the first one down (guaranteed this will occur) */ |
|---|
| 2392 | 65 | Py_MEMCPY(result_s, to_s, to_len); |
|---|
| 2393 | 65 | result_s += to_len; |
|---|
| 2394 | 65 | count -= 1; |
|---|
| 2395 | n/a | |
|---|
| 2396 | 160 | for (i=0; i<count; i++) { |
|---|
| 2397 | 95 | *result_s++ = *self_s++; |
|---|
| 2398 | 95 | Py_MEMCPY(result_s, to_s, to_len); |
|---|
| 2399 | 95 | result_s += to_len; |
|---|
| 2400 | n/a | } |
|---|
| 2401 | n/a | |
|---|
| 2402 | n/a | /* Copy the rest of the original string */ |
|---|
| 2403 | 65 | Py_MEMCPY(result_s, self_s, self_len-i); |
|---|
| 2404 | n/a | |
|---|
| 2405 | 65 | return result; |
|---|
| 2406 | n/a | } |
|---|
| 2407 | n/a | |
|---|
| 2408 | n/a | /* Special case for deleting a single character */ |
|---|
| 2409 | n/a | /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
|---|
| 2410 | n/a | Py_LOCAL(PyStringObject *) |
|---|
| 2411 | n/a | replace_delete_single_character(PyStringObject *self, |
|---|
| 2412 | n/a | char from_c, Py_ssize_t maxcount) |
|---|
| 2413 | 454497 | { |
|---|
| 2414 | n/a | char *self_s, *result_s; |
|---|
| 2415 | n/a | char *start, *next, *end; |
|---|
| 2416 | n/a | Py_ssize_t self_len, result_len; |
|---|
| 2417 | n/a | Py_ssize_t count; |
|---|
| 2418 | n/a | PyStringObject *result; |
|---|
| 2419 | n/a | |
|---|
| 2420 | 454497 | self_len = PyString_GET_SIZE(self); |
|---|
| 2421 | 454497 | self_s = PyString_AS_STRING(self); |
|---|
| 2422 | n/a | |
|---|
| 2423 | 454497 | count = countchar(self_s, self_len, from_c, maxcount); |
|---|
| 2424 | 454497 | if (count == 0) { |
|---|
| 2425 | 410242 | return return_self(self); |
|---|
| 2426 | n/a | } |
|---|
| 2427 | n/a | |
|---|
| 2428 | 44255 | result_len = self_len - count; /* from_len == 1 */ |
|---|
| 2429 | 44255 | assert(result_len>=0); |
|---|
| 2430 | n/a | |
|---|
| 2431 | 44255 | if ( (result = (PyStringObject *) |
|---|
| 2432 | n/a | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
|---|
| 2433 | 0 | return NULL; |
|---|
| 2434 | 44255 | result_s = PyString_AS_STRING(result); |
|---|
| 2435 | n/a | |
|---|
| 2436 | 44255 | start = self_s; |
|---|
| 2437 | 44255 | end = self_s + self_len; |
|---|
| 2438 | 165557 | while (count-- > 0) { |
|---|
| 2439 | 77047 | next = findchar(start, end-start, from_c); |
|---|
| 2440 | 77047 | if (next == NULL) |
|---|
| 2441 | 0 | break; |
|---|
| 2442 | 77047 | Py_MEMCPY(result_s, start, next-start); |
|---|
| 2443 | 77047 | result_s += (next-start); |
|---|
| 2444 | 77047 | start = next+1; |
|---|
| 2445 | n/a | } |
|---|
| 2446 | 44255 | Py_MEMCPY(result_s, start, end-start); |
|---|
| 2447 | n/a | |
|---|
| 2448 | 44255 | return result; |
|---|
| 2449 | n/a | } |
|---|
| 2450 | n/a | |
|---|
| 2451 | n/a | /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
|---|
| 2452 | n/a | |
|---|
| 2453 | n/a | Py_LOCAL(PyStringObject *) |
|---|
| 2454 | n/a | replace_delete_substring(PyStringObject *self, |
|---|
| 2455 | n/a | const char *from_s, Py_ssize_t from_len, |
|---|
| 2456 | 440026 | Py_ssize_t maxcount) { |
|---|
| 2457 | n/a | char *self_s, *result_s; |
|---|
| 2458 | n/a | char *start, *next, *end; |
|---|
| 2459 | n/a | Py_ssize_t self_len, result_len; |
|---|
| 2460 | n/a | Py_ssize_t count, offset; |
|---|
| 2461 | n/a | PyStringObject *result; |
|---|
| 2462 | n/a | |
|---|
| 2463 | 440026 | self_len = PyString_GET_SIZE(self); |
|---|
| 2464 | 440026 | self_s = PyString_AS_STRING(self); |
|---|
| 2465 | n/a | |
|---|
| 2466 | 440026 | count = stringlib_count(self_s, self_len, |
|---|
| 2467 | n/a | from_s, from_len, |
|---|
| 2468 | n/a | maxcount); |
|---|
| 2469 | n/a | |
|---|
| 2470 | 440026 | if (count == 0) { |
|---|
| 2471 | n/a | /* no matches */ |
|---|
| 2472 | 369117 | return return_self(self); |
|---|
| 2473 | n/a | } |
|---|
| 2474 | n/a | |
|---|
| 2475 | 70909 | result_len = self_len - (count * from_len); |
|---|
| 2476 | 70909 | assert (result_len>=0); |
|---|
| 2477 | n/a | |
|---|
| 2478 | 70909 | if ( (result = (PyStringObject *) |
|---|
| 2479 | n/a | PyString_FromStringAndSize(NULL, result_len)) == NULL ) |
|---|
| 2480 | 0 | return NULL; |
|---|
| 2481 | n/a | |
|---|
| 2482 | 70909 | result_s = PyString_AS_STRING(result); |
|---|
| 2483 | n/a | |
|---|
| 2484 | 70909 | start = self_s; |
|---|
| 2485 | 70909 | end = self_s + self_len; |
|---|
| 2486 | 218774 | while (count-- > 0) { |
|---|
| 2487 | 76956 | offset = stringlib_find(start, end-start, |
|---|
| 2488 | n/a | from_s, from_len, |
|---|
| 2489 | n/a | 0); |
|---|
| 2490 | 76956 | if (offset == -1) |
|---|
| 2491 | 0 | break; |
|---|
| 2492 | 76956 | next = start + offset; |
|---|
| 2493 | n/a | |
|---|
| 2494 | 76956 | Py_MEMCPY(result_s, start, next-start); |
|---|
| 2495 | n/a | |
|---|
| 2496 | 76956 | result_s += (next-start); |
|---|
| 2497 | 76956 | start = next+from_len; |
|---|
| 2498 | n/a | } |
|---|
| 2499 | 70909 | Py_MEMCPY(result_s, start, end-start); |
|---|
| 2500 | 70909 | return result; |
|---|
| 2501 | n/a | } |
|---|
| 2502 | n/a | |
|---|
| 2503 | n/a | /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
|---|
| 2504 | n/a | Py_LOCAL(PyStringObject *) |
|---|
| 2505 | n/a | replace_single_character_in_place(PyStringObject *self, |
|---|
| 2506 | n/a | char from_c, char to_c, |
|---|
| 2507 | n/a | Py_ssize_t maxcount) |
|---|
| 2508 | 18117 | { |
|---|
| 2509 | n/a | char *self_s, *result_s, *start, *end, *next; |
|---|
| 2510 | n/a | Py_ssize_t self_len; |
|---|
| 2511 | n/a | PyStringObject *result; |
|---|
| 2512 | n/a | |
|---|
| 2513 | n/a | /* The result string will be the same size */ |
|---|
| 2514 | 18117 | self_s = PyString_AS_STRING(self); |
|---|
| 2515 | 18117 | self_len = PyString_GET_SIZE(self); |
|---|
| 2516 | n/a | |
|---|
| 2517 | 18117 | next = findchar(self_s, self_len, from_c); |
|---|
| 2518 | n/a | |
|---|
| 2519 | 18117 | if (next == NULL) { |
|---|
| 2520 | n/a | /* No matches; return the original string */ |
|---|
| 2521 | 9638 | return return_self(self); |
|---|
| 2522 | n/a | } |
|---|
| 2523 | n/a | |
|---|
| 2524 | n/a | /* Need to make a new string */ |
|---|
| 2525 | 8479 | result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len); |
|---|
| 2526 | 8479 | if (result == NULL) |
|---|
| 2527 | 0 | return NULL; |
|---|
| 2528 | 8479 | result_s = PyString_AS_STRING(result); |
|---|
| 2529 | 8479 | Py_MEMCPY(result_s, self_s, self_len); |
|---|
| 2530 | n/a | |
|---|
| 2531 | n/a | /* change everything in-place, starting with this one */ |
|---|
| 2532 | 8479 | start = result_s + (next-self_s); |
|---|
| 2533 | 8479 | *start = to_c; |
|---|
| 2534 | 8479 | start++; |
|---|
| 2535 | 8479 | end = result_s + self_len; |
|---|
| 2536 | n/a | |
|---|
| 2537 | 37550 | while (--maxcount > 0) { |
|---|
| 2538 | 29045 | next = findchar(start, end-start, from_c); |
|---|
| 2539 | 29045 | if (next == NULL) |
|---|
| 2540 | 8453 | break; |
|---|
| 2541 | 20592 | *next = to_c; |
|---|
| 2542 | 20592 | start = next+1; |
|---|
| 2543 | n/a | } |
|---|
| 2544 | n/a | |
|---|
| 2545 | 8479 | return result; |
|---|
| 2546 | n/a | } |
|---|
| 2547 | n/a | |
|---|
| 2548 | n/a | /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
|---|
| 2549 | n/a | Py_LOCAL(PyStringObject *) |
|---|
| 2550 | n/a | replace_substring_in_place(PyStringObject *self, |
|---|
| 2551 | n/a | const char *from_s, Py_ssize_t from_len, |
|---|
| 2552 | n/a | const char *to_s, Py_ssize_t to_len, |
|---|
| 2553 | n/a | Py_ssize_t maxcount) |
|---|
| 2554 | 932 | { |
|---|
| 2555 | n/a | char *result_s, *start, *end; |
|---|
| 2556 | n/a | char *self_s; |
|---|
| 2557 | n/a | Py_ssize_t self_len, offset; |
|---|
| 2558 | n/a | PyStringObject *result; |
|---|
| 2559 | n/a | |
|---|
| 2560 | n/a | /* The result string will be the same size */ |
|---|
| 2561 | n/a | |
|---|
| 2562 | 932 | self_s = PyString_AS_STRING(self); |
|---|
| 2563 | 932 | self_len = PyString_GET_SIZE(self); |
|---|
| 2564 | n/a | |
|---|
| 2565 | 932 | offset = stringlib_find(self_s, self_len, |
|---|
| 2566 | n/a | from_s, from_len, |
|---|
| 2567 | n/a | 0); |
|---|
| 2568 | 932 | if (offset == -1) { |
|---|
| 2569 | n/a | /* No matches; return the original string */ |
|---|
| 2570 | 592 | return return_self(self); |
|---|
| 2571 | n/a | } |
|---|
| 2572 | n/a | |
|---|
| 2573 | n/a | /* Need to make a new string */ |
|---|
| 2574 | 340 | result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len); |
|---|
| 2575 | 340 | if (result == NULL) |
|---|
| 2576 | 0 | return NULL; |
|---|
| 2577 | 340 | result_s = PyString_AS_STRING(result); |
|---|
| 2578 | 340 | Py_MEMCPY(result_s, self_s, self_len); |
|---|
| 2579 | n/a | |
|---|
| 2580 | n/a | /* change everything in-place, starting with this one */ |
|---|
| 2581 | 340 | start = result_s + offset; |
|---|
| 2582 | 340 | Py_MEMCPY(start, to_s, from_len); |
|---|
| 2583 | 340 | start += from_len; |
|---|
| 2584 | 340 | end = result_s + self_len; |
|---|
| 2585 | n/a | |
|---|
| 2586 | 745 | while ( --maxcount > 0) { |
|---|
| 2587 | 390 | offset = stringlib_find(start, end-start, |
|---|
| 2588 | n/a | from_s, from_len, |
|---|
| 2589 | n/a | 0); |
|---|
| 2590 | 390 | if (offset==-1) |
|---|
| 2591 | 325 | break; |
|---|
| 2592 | 65 | Py_MEMCPY(start+offset, to_s, from_len); |
|---|
| 2593 | 65 | start += offset+from_len; |
|---|
| 2594 | n/a | } |
|---|
| 2595 | n/a | |
|---|
| 2596 | 340 | return result; |
|---|
| 2597 | n/a | } |
|---|
| 2598 | n/a | |
|---|
| 2599 | n/a | /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
|---|
| 2600 | n/a | Py_LOCAL(PyStringObject *) |
|---|
| 2601 | n/a | replace_single_character(PyStringObject *self, |
|---|
| 2602 | n/a | char from_c, |
|---|
| 2603 | n/a | const char *to_s, Py_ssize_t to_len, |
|---|
| 2604 | n/a | Py_ssize_t maxcount) |
|---|
| 2605 | 7756 | { |
|---|
| 2606 | n/a | char *self_s, *result_s; |
|---|
| 2607 | n/a | char *start, *next, *end; |
|---|
| 2608 | n/a | Py_ssize_t self_len, result_len; |
|---|
| 2609 | n/a | Py_ssize_t count, product; |
|---|
| 2610 | n/a | PyStringObject *result; |
|---|
| 2611 | n/a | |
|---|
| 2612 | 7756 | self_s = PyString_AS_STRING(self); |
|---|
| 2613 | 7756 | self_len = PyString_GET_SIZE(self); |
|---|
| 2614 | n/a | |
|---|
| 2615 | 7756 | count = countchar(self_s, self_len, from_c, maxcount); |
|---|
| 2616 | 7756 | if (count == 0) { |
|---|
| 2617 | n/a | /* no matches, return unchanged */ |
|---|
| 2618 | 7212 | return return_self(self); |
|---|
| 2619 | n/a | } |
|---|
| 2620 | n/a | |
|---|
| 2621 | n/a | /* use the difference between current and new, hence the "-1" */ |
|---|
| 2622 | n/a | /* result_len = self_len + count * (to_len-1) */ |
|---|
| 2623 | 544 | product = count * (to_len-1); |
|---|
| 2624 | 544 | if (product / (to_len-1) != count) { |
|---|
| 2625 | 0 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
|---|
| 2626 | 0 | return NULL; |
|---|
| 2627 | n/a | } |
|---|
| 2628 | 544 | result_len = self_len + product; |
|---|
| 2629 | 544 | if (result_len < 0) { |
|---|
| 2630 | 0 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
|---|
| 2631 | 0 | return NULL; |
|---|
| 2632 | n/a | } |
|---|
| 2633 | n/a | |
|---|
| 2634 | 544 | if ( (result = (PyStringObject *) |
|---|
| 2635 | n/a | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
|---|
| 2636 | 0 | return NULL; |
|---|
| 2637 | 544 | result_s = PyString_AS_STRING(result); |
|---|
| 2638 | n/a | |
|---|
| 2639 | 544 | start = self_s; |
|---|
| 2640 | 544 | end = self_s + self_len; |
|---|
| 2641 | 19670 | while (count-- > 0) { |
|---|
| 2642 | 18582 | next = findchar(start, end-start, from_c); |
|---|
| 2643 | 18582 | if (next == NULL) |
|---|
| 2644 | 0 | break; |
|---|
| 2645 | n/a | |
|---|
| 2646 | 18582 | if (next == start) { |
|---|
| 2647 | n/a | /* replace with the 'to' */ |
|---|
| 2648 | 624 | Py_MEMCPY(result_s, to_s, to_len); |
|---|
| 2649 | 624 | result_s += to_len; |
|---|
| 2650 | 624 | start += 1; |
|---|
| 2651 | n/a | } else { |
|---|
| 2652 | n/a | /* copy the unchanged old then the 'to' */ |
|---|
| 2653 | 17958 | Py_MEMCPY(result_s, start, next-start); |
|---|
| 2654 | 17958 | result_s += (next-start); |
|---|
| 2655 | 17958 | Py_MEMCPY(result_s, to_s, to_len); |
|---|
| 2656 | 17958 | result_s += to_len; |
|---|
| 2657 | 17958 | start = next+1; |
|---|
| 2658 | n/a | } |
|---|
| 2659 | n/a | } |
|---|
| 2660 | n/a | /* Copy the remainder of the remaining string */ |
|---|
| 2661 | 544 | Py_MEMCPY(result_s, start, end-start); |
|---|
| 2662 | n/a | |
|---|
| 2663 | 544 | return result; |
|---|
| 2664 | n/a | } |
|---|
| 2665 | n/a | |
|---|
| 2666 | n/a | /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
|---|
| 2667 | n/a | Py_LOCAL(PyStringObject *) |
|---|
| 2668 | n/a | replace_substring(PyStringObject *self, |
|---|
| 2669 | n/a | const char *from_s, Py_ssize_t from_len, |
|---|
| 2670 | n/a | const char *to_s, Py_ssize_t to_len, |
|---|
| 2671 | 793514 | Py_ssize_t maxcount) { |
|---|
| 2672 | n/a | char *self_s, *result_s; |
|---|
| 2673 | n/a | char *start, *next, *end; |
|---|
| 2674 | n/a | Py_ssize_t self_len, result_len; |
|---|
| 2675 | n/a | Py_ssize_t count, offset, product; |
|---|
| 2676 | n/a | PyStringObject *result; |
|---|
| 2677 | n/a | |
|---|
| 2678 | 793514 | self_s = PyString_AS_STRING(self); |
|---|
| 2679 | 793514 | self_len = PyString_GET_SIZE(self); |
|---|
| 2680 | n/a | |
|---|
| 2681 | 793514 | count = stringlib_count(self_s, self_len, |
|---|
| 2682 | n/a | from_s, from_len, |
|---|
| 2683 | n/a | maxcount); |
|---|
| 2684 | n/a | |
|---|
| 2685 | 793514 | if (count == 0) { |
|---|
| 2686 | n/a | /* no matches, return unchanged */ |
|---|
| 2687 | 790926 | return return_self(self); |
|---|
| 2688 | n/a | } |
|---|
| 2689 | n/a | |
|---|
| 2690 | n/a | /* Check for overflow */ |
|---|
| 2691 | n/a | /* result_len = self_len + count * (to_len-from_len) */ |
|---|
| 2692 | 2588 | product = count * (to_len-from_len); |
|---|
| 2693 | 2588 | if (product / (to_len-from_len) != count) { |
|---|
| 2694 | 0 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
|---|
| 2695 | 0 | return NULL; |
|---|
| 2696 | n/a | } |
|---|
| 2697 | 2588 | result_len = self_len + product; |
|---|
| 2698 | 2588 | if (result_len < 0) { |
|---|
| 2699 | 0 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
|---|
| 2700 | 0 | return NULL; |
|---|
| 2701 | n/a | } |
|---|
| 2702 | n/a | |
|---|
| 2703 | 2588 | if ( (result = (PyStringObject *) |
|---|
| 2704 | n/a | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
|---|
| 2705 | 0 | return NULL; |
|---|
| 2706 | 2588 | result_s = PyString_AS_STRING(result); |
|---|
| 2707 | n/a | |
|---|
| 2708 | 2588 | start = self_s; |
|---|
| 2709 | 2588 | end = self_s + self_len; |
|---|
| 2710 | 11208 | while (count-- > 0) { |
|---|
| 2711 | 6032 | offset = stringlib_find(start, end-start, |
|---|
| 2712 | n/a | from_s, from_len, |
|---|
| 2713 | n/a | 0); |
|---|
| 2714 | 6032 | if (offset == -1) |
|---|
| 2715 | 0 | break; |
|---|
| 2716 | 6032 | next = start+offset; |
|---|
| 2717 | 6032 | if (next == start) { |
|---|
| 2718 | n/a | /* replace with the 'to' */ |
|---|
| 2719 | 719 | Py_MEMCPY(result_s, to_s, to_len); |
|---|
| 2720 | 719 | result_s += to_len; |
|---|
| 2721 | 719 | start += from_len; |
|---|
| 2722 | n/a | } else { |
|---|
| 2723 | n/a | /* copy the unchanged old then the 'to' */ |
|---|
| 2724 | 5313 | Py_MEMCPY(result_s, start, next-start); |
|---|
| 2725 | 5313 | result_s += (next-start); |
|---|
| 2726 | 5313 | Py_MEMCPY(result_s, to_s, to_len); |
|---|
| 2727 | 5313 | result_s += to_len; |
|---|
| 2728 | 5313 | start = next+from_len; |
|---|
| 2729 | n/a | } |
|---|
| 2730 | n/a | } |
|---|
| 2731 | n/a | /* Copy the remainder of the remaining string */ |
|---|
| 2732 | 2588 | Py_MEMCPY(result_s, start, end-start); |
|---|
| 2733 | n/a | |
|---|
| 2734 | 2588 | return result; |
|---|
| 2735 | n/a | } |
|---|
| 2736 | n/a | |
|---|
| 2737 | n/a | |
|---|
| 2738 | n/a | Py_LOCAL(PyStringObject *) |
|---|
| 2739 | n/a | replace(PyStringObject *self, |
|---|
| 2740 | n/a | const char *from_s, Py_ssize_t from_len, |
|---|
| 2741 | n/a | const char *to_s, Py_ssize_t to_len, |
|---|
| 2742 | n/a | Py_ssize_t maxcount) |
|---|
| 2743 | 1765790 | { |
|---|
| 2744 | 1765790 | if (maxcount < 0) { |
|---|
| 2745 | 1765489 | maxcount = PY_SSIZE_T_MAX; |
|---|
| 2746 | 301 | } else if (maxcount == 0 || PyString_GET_SIZE(self) == 0) { |
|---|
| 2747 | n/a | /* nothing to do; return the original string */ |
|---|
| 2748 | 78 | return return_self(self); |
|---|
| 2749 | n/a | } |
|---|
| 2750 | n/a | |
|---|
| 2751 | 1765712 | if (maxcount == 0 || |
|---|
| 2752 | n/a | (from_len == 0 && to_len == 0)) { |
|---|
| 2753 | n/a | /* nothing to do; return the original string */ |
|---|
| 2754 | 18 | return return_self(self); |
|---|
| 2755 | n/a | } |
|---|
| 2756 | n/a | |
|---|
| 2757 | n/a | /* Handle zero-length special cases */ |
|---|
| 2758 | n/a | |
|---|
| 2759 | 1765694 | if (from_len == 0) { |
|---|
| 2760 | n/a | /* insert the 'to' string everywhere. */ |
|---|
| 2761 | n/a | /* >>> "Python".replace("", ".") */ |
|---|
| 2762 | n/a | /* '.P.y.t.h.o.n.' */ |
|---|
| 2763 | 65 | return replace_interleave(self, to_s, to_len, maxcount); |
|---|
| 2764 | n/a | } |
|---|
| 2765 | n/a | |
|---|
| 2766 | n/a | /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
|---|
| 2767 | n/a | /* point for an empty self string to generate a non-empty string */ |
|---|
| 2768 | n/a | /* Special case so the remaining code always gets a non-empty string */ |
|---|
| 2769 | 1765629 | if (PyString_GET_SIZE(self) == 0) { |
|---|
| 2770 | 50787 | return return_self(self); |
|---|
| 2771 | n/a | } |
|---|
| 2772 | n/a | |
|---|
| 2773 | 1714842 | if (to_len == 0) { |
|---|
| 2774 | n/a | /* delete all occurances of 'from' string */ |
|---|
| 2775 | 894523 | if (from_len == 1) { |
|---|
| 2776 | 454497 | return replace_delete_single_character( |
|---|
| 2777 | n/a | self, from_s[0], maxcount); |
|---|
| 2778 | n/a | } else { |
|---|
| 2779 | 440026 | return replace_delete_substring(self, from_s, from_len, maxcount); |
|---|
| 2780 | n/a | } |
|---|
| 2781 | n/a | } |
|---|
| 2782 | n/a | |
|---|
| 2783 | n/a | /* Handle special case where both strings have the same length */ |
|---|
| 2784 | n/a | |
|---|
| 2785 | 820319 | if (from_len == to_len) { |
|---|
| 2786 | 19049 | if (from_len == 1) { |
|---|
| 2787 | 18117 | return replace_single_character_in_place( |
|---|
| 2788 | n/a | self, |
|---|
| 2789 | n/a | from_s[0], |
|---|
| 2790 | n/a | to_s[0], |
|---|
| 2791 | n/a | maxcount); |
|---|
| 2792 | n/a | } else { |
|---|
| 2793 | 932 | return replace_substring_in_place( |
|---|
| 2794 | n/a | self, from_s, from_len, to_s, to_len, maxcount); |
|---|
| 2795 | n/a | } |
|---|
| 2796 | n/a | } |
|---|
| 2797 | n/a | |
|---|
| 2798 | n/a | /* Otherwise use the more generic algorithms */ |
|---|
| 2799 | 801270 | if (from_len == 1) { |
|---|
| 2800 | 7756 | return replace_single_character(self, from_s[0], |
|---|
| 2801 | n/a | to_s, to_len, maxcount); |
|---|
| 2802 | n/a | } else { |
|---|
| 2803 | n/a | /* len('from')>=2, len('to')>=1 */ |
|---|
| 2804 | 793514 | return replace_substring(self, from_s, from_len, to_s, to_len, maxcount); |
|---|
| 2805 | n/a | } |
|---|
| 2806 | n/a | } |
|---|
| 2807 | n/a | |
|---|
| 2808 | n/a | PyDoc_STRVAR(replace__doc__, |
|---|
| 2809 | n/a | "S.replace(old, new[, count]) -> string\n\ |
|---|
| 2810 | n/a | \n\ |
|---|
| 2811 | n/a | Return a copy of string S with all occurrences of substring\n\ |
|---|
| 2812 | n/a | old replaced by new. If the optional argument count is\n\ |
|---|
| 2813 | n/a | given, only the first count occurrences are replaced."); |
|---|
| 2814 | n/a | |
|---|
| 2815 | n/a | static PyObject * |
|---|
| 2816 | n/a | string_replace(PyStringObject *self, PyObject *args) |
|---|
| 2817 | 1765806 | { |
|---|
| 2818 | 1765806 | Py_ssize_t count = -1; |
|---|
| 2819 | n/a | PyObject *from, *to; |
|---|
| 2820 | n/a | const char *from_s, *to_s; |
|---|
| 2821 | n/a | Py_ssize_t from_len, to_len; |
|---|
| 2822 | n/a | |
|---|
| 2823 | 1765806 | if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count)) |
|---|
| 2824 | 4 | return NULL; |
|---|
| 2825 | n/a | |
|---|
| 2826 | 1765802 | if (PyString_Check(from)) { |
|---|
| 2827 | 1765785 | from_s = PyString_AS_STRING(from); |
|---|
| 2828 | 1765785 | from_len = PyString_GET_SIZE(from); |
|---|
| 2829 | n/a | } |
|---|
| 2830 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 2831 | 1765802 | if (PyUnicode_Check(from)) |
|---|
| 2832 | 2 | return PyUnicode_Replace((PyObject *)self, |
|---|
| 2833 | n/a | from, to, count); |
|---|
| 2834 | n/a | #endif |
|---|
| 2835 | 1765800 | else if (PyObject_AsCharBuffer(from, &from_s, &from_len)) |
|---|
| 2836 | 5 | return NULL; |
|---|
| 2837 | n/a | |
|---|
| 2838 | 1765795 | if (PyString_Check(to)) { |
|---|
| 2839 | 1765780 | to_s = PyString_AS_STRING(to); |
|---|
| 2840 | 1765780 | to_len = PyString_GET_SIZE(to); |
|---|
| 2841 | n/a | } |
|---|
| 2842 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 2843 | 15 | else if (PyUnicode_Check(to)) |
|---|
| 2844 | 0 | return PyUnicode_Replace((PyObject *)self, |
|---|
| 2845 | n/a | from, to, count); |
|---|
| 2846 | n/a | #endif |
|---|
| 2847 | 15 | else if (PyObject_AsCharBuffer(to, &to_s, &to_len)) |
|---|
| 2848 | 5 | return NULL; |
|---|
| 2849 | n/a | |
|---|
| 2850 | 1765790 | return (PyObject *)replace((PyStringObject *) self, |
|---|
| 2851 | n/a | from_s, from_len, |
|---|
| 2852 | n/a | to_s, to_len, count); |
|---|
| 2853 | n/a | } |
|---|
| 2854 | n/a | |
|---|
| 2855 | n/a | /** End DALKE **/ |
|---|
| 2856 | n/a | |
|---|
| 2857 | n/a | /* Matches the end (direction >= 0) or start (direction < 0) of self |
|---|
| 2858 | n/a | * against substr, using the start and end arguments. Returns |
|---|
| 2859 | n/a | * -1 on error, 0 if not found and 1 if found. |
|---|
| 2860 | n/a | */ |
|---|
| 2861 | n/a | Py_LOCAL(int) |
|---|
| 2862 | n/a | _string_tailmatch(PyStringObject *self, PyObject *substr, Py_ssize_t start, |
|---|
| 2863 | n/a | Py_ssize_t end, int direction) |
|---|
| 2864 | 2076527 | { |
|---|
| 2865 | 2076527 | Py_ssize_t len = PyString_GET_SIZE(self); |
|---|
| 2866 | n/a | Py_ssize_t slen; |
|---|
| 2867 | n/a | const char* sub; |
|---|
| 2868 | n/a | const char* str; |
|---|
| 2869 | n/a | |
|---|
| 2870 | 2076527 | if (PyString_Check(substr)) { |
|---|
| 2871 | 2076458 | sub = PyString_AS_STRING(substr); |
|---|
| 2872 | 2076458 | slen = PyString_GET_SIZE(substr); |
|---|
| 2873 | n/a | } |
|---|
| 2874 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 2875 | 69 | else if (PyUnicode_Check(substr)) |
|---|
| 2876 | 57 | return PyUnicode_Tailmatch((PyObject *)self, |
|---|
| 2877 | n/a | substr, start, end, direction); |
|---|
| 2878 | n/a | #endif |
|---|
| 2879 | 12 | else if (PyObject_AsCharBuffer(substr, &sub, &slen)) |
|---|
| 2880 | 12 | return -1; |
|---|
| 2881 | 2076458 | str = PyString_AS_STRING(self); |
|---|
| 2882 | n/a | |
|---|
| 2883 | 2076458 | ADJUST_INDICES(start, end, len); |
|---|
| 2884 | n/a | |
|---|
| 2885 | 2076458 | if (direction < 0) { |
|---|
| 2886 | n/a | /* startswith */ |
|---|
| 2887 | 1599797 | if (start+slen > len) |
|---|
| 2888 | 30518 | return 0; |
|---|
| 2889 | n/a | } else { |
|---|
| 2890 | n/a | /* endswith */ |
|---|
| 2891 | 476661 | if (end-start < slen || start > len) |
|---|
| 2892 | 3160 | return 0; |
|---|
| 2893 | n/a | |
|---|
| 2894 | 473501 | if (end-slen > start) |
|---|
| 2895 | 329457 | start = end - slen; |
|---|
| 2896 | n/a | } |
|---|
| 2897 | 2042780 | if (end-start >= slen) |
|---|
| 2898 | 2042768 | return ! memcmp(str+start, sub, slen); |
|---|
| 2899 | 12 | return 0; |
|---|
| 2900 | n/a | } |
|---|
| 2901 | n/a | |
|---|
| 2902 | n/a | |
|---|
| 2903 | n/a | PyDoc_STRVAR(startswith__doc__, |
|---|
| 2904 | n/a | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
|---|
| 2905 | n/a | \n\ |
|---|
| 2906 | n/a | Return True if S starts with the specified prefix, False otherwise.\n\ |
|---|
| 2907 | n/a | With optional start, test S beginning at that position.\n\ |
|---|
| 2908 | n/a | With optional end, stop comparing S at that position.\n\ |
|---|
| 2909 | n/a | prefix can also be a tuple of strings to try."); |
|---|
| 2910 | n/a | |
|---|
| 2911 | n/a | static PyObject * |
|---|
| 2912 | n/a | string_startswith(PyStringObject *self, PyObject *args) |
|---|
| 2913 | 1598751 | { |
|---|
| 2914 | 1598751 | Py_ssize_t start = 0; |
|---|
| 2915 | 1598751 | Py_ssize_t end = PY_SSIZE_T_MAX; |
|---|
| 2916 | n/a | PyObject *subobj; |
|---|
| 2917 | n/a | int result; |
|---|
| 2918 | n/a | |
|---|
| 2919 | 1598751 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
|---|
| 2920 | n/a | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
|---|
| 2921 | 1 | return NULL; |
|---|
| 2922 | 1598750 | if (PyTuple_Check(subobj)) { |
|---|
| 2923 | n/a | Py_ssize_t i; |
|---|
| 2924 | 2599 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
|---|
| 2925 | 1887 | result = _string_tailmatch(self, |
|---|
| 2926 | n/a | PyTuple_GET_ITEM(subobj, i), |
|---|
| 2927 | n/a | start, end, -1); |
|---|
| 2928 | 1887 | if (result == -1) |
|---|
| 2929 | 3 | return NULL; |
|---|
| 2930 | 1884 | else if (result) { |
|---|
| 2931 | 62 | Py_RETURN_TRUE; |
|---|
| 2932 | n/a | } |
|---|
| 2933 | n/a | } |
|---|
| 2934 | 712 | Py_RETURN_FALSE; |
|---|
| 2935 | n/a | } |
|---|
| 2936 | 1597973 | result = _string_tailmatch(self, subobj, start, end, -1); |
|---|
| 2937 | 1597973 | if (result == -1) |
|---|
| 2938 | 3 | return NULL; |
|---|
| 2939 | n/a | else |
|---|
| 2940 | 1597970 | return PyBool_FromLong(result); |
|---|
| 2941 | n/a | } |
|---|
| 2942 | n/a | |
|---|
| 2943 | n/a | |
|---|
| 2944 | n/a | PyDoc_STRVAR(endswith__doc__, |
|---|
| 2945 | n/a | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
|---|
| 2946 | n/a | \n\ |
|---|
| 2947 | n/a | Return True if S ends with the specified suffix, False otherwise.\n\ |
|---|
| 2948 | n/a | With optional start, test S beginning at that position.\n\ |
|---|
| 2949 | n/a | With optional end, stop comparing S at that position.\n\ |
|---|
| 2950 | n/a | suffix can also be a tuple of strings to try."); |
|---|
| 2951 | n/a | |
|---|
| 2952 | n/a | static PyObject * |
|---|
| 2953 | n/a | string_endswith(PyStringObject *self, PyObject *args) |
|---|
| 2954 | 475387 | { |
|---|
| 2955 | 475387 | Py_ssize_t start = 0; |
|---|
| 2956 | 475387 | Py_ssize_t end = PY_SSIZE_T_MAX; |
|---|
| 2957 | n/a | PyObject *subobj; |
|---|
| 2958 | n/a | int result; |
|---|
| 2959 | n/a | |
|---|
| 2960 | 475387 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
|---|
| 2961 | n/a | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
|---|
| 2962 | 1 | return NULL; |
|---|
| 2963 | 475386 | if (PyTuple_Check(subobj)) { |
|---|
| 2964 | n/a | Py_ssize_t i; |
|---|
| 2965 | 3849 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
|---|
| 2966 | 2574 | result = _string_tailmatch(self, |
|---|
| 2967 | n/a | PyTuple_GET_ITEM(subobj, i), |
|---|
| 2968 | n/a | start, end, +1); |
|---|
| 2969 | 2574 | if (result == -1) |
|---|
| 2970 | 3 | return NULL; |
|---|
| 2971 | 2571 | else if (result) { |
|---|
| 2972 | 15 | Py_RETURN_TRUE; |
|---|
| 2973 | n/a | } |
|---|
| 2974 | n/a | } |
|---|
| 2975 | 1275 | Py_RETURN_FALSE; |
|---|
| 2976 | n/a | } |
|---|
| 2977 | 474093 | result = _string_tailmatch(self, subobj, start, end, +1); |
|---|
| 2978 | 474093 | if (result == -1) |
|---|
| 2979 | 3 | return NULL; |
|---|
| 2980 | n/a | else |
|---|
| 2981 | 474090 | return PyBool_FromLong(result); |
|---|
| 2982 | n/a | } |
|---|
| 2983 | n/a | |
|---|
| 2984 | n/a | |
|---|
| 2985 | n/a | PyDoc_STRVAR(encode__doc__, |
|---|
| 2986 | n/a | "S.encode([encoding[,errors]]) -> object\n\ |
|---|
| 2987 | n/a | \n\ |
|---|
| 2988 | n/a | Encodes S using the codec registered for encoding. encoding defaults\n\ |
|---|
| 2989 | n/a | to the default encoding. errors may be given to set a different error\n\ |
|---|
| 2990 | n/a | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
|---|
| 2991 | n/a | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
|---|
| 2992 | n/a | 'xmlcharrefreplace' as well as any other name registered with\n\ |
|---|
| 2993 | n/a | codecs.register_error that is able to handle UnicodeEncodeErrors."); |
|---|
| 2994 | n/a | |
|---|
| 2995 | n/a | static PyObject * |
|---|
| 2996 | n/a | string_encode(PyStringObject *self, PyObject *args, PyObject *kwargs) |
|---|
| 2997 | 4749 | { |
|---|
| 2998 | n/a | static char *kwlist[] = {"encoding", "errors", 0}; |
|---|
| 2999 | 4749 | char *encoding = NULL; |
|---|
| 3000 | 4749 | char *errors = NULL; |
|---|
| 3001 | n/a | PyObject *v; |
|---|
| 3002 | n/a | |
|---|
| 3003 | 4749 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
|---|
| 3004 | n/a | kwlist, &encoding, &errors)) |
|---|
| 3005 | 3 | return NULL; |
|---|
| 3006 | 4746 | v = PyString_AsEncodedObject((PyObject *)self, encoding, errors); |
|---|
| 3007 | 4746 | if (v == NULL) |
|---|
| 3008 | 2 | goto onError; |
|---|
| 3009 | 4744 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
|---|
| 3010 | 0 | PyErr_Format(PyExc_TypeError, |
|---|
| 3011 | n/a | "encoder did not return a string/unicode object " |
|---|
| 3012 | n/a | "(type=%.400s)", |
|---|
| 3013 | n/a | Py_TYPE(v)->tp_name); |
|---|
| 3014 | 0 | Py_DECREF(v); |
|---|
| 3015 | 0 | return NULL; |
|---|
| 3016 | n/a | } |
|---|
| 3017 | 4744 | return v; |
|---|
| 3018 | n/a | |
|---|
| 3019 | 2 | onError: |
|---|
| 3020 | 2 | return NULL; |
|---|
| 3021 | n/a | } |
|---|
| 3022 | n/a | |
|---|
| 3023 | n/a | |
|---|
| 3024 | n/a | PyDoc_STRVAR(decode__doc__, |
|---|
| 3025 | n/a | "S.decode([encoding[,errors]]) -> object\n\ |
|---|
| 3026 | n/a | \n\ |
|---|
| 3027 | n/a | Decodes S using the codec registered for encoding. encoding defaults\n\ |
|---|
| 3028 | n/a | to the default encoding. errors may be given to set a different error\n\ |
|---|
| 3029 | n/a | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
|---|
| 3030 | n/a | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
|---|
| 3031 | n/a | as well as any other name registered with codecs.register_error that is\n\ |
|---|
| 3032 | n/a | able to handle UnicodeDecodeErrors."); |
|---|
| 3033 | n/a | |
|---|
| 3034 | n/a | static PyObject * |
|---|
| 3035 | n/a | string_decode(PyStringObject *self, PyObject *args, PyObject *kwargs) |
|---|
| 3036 | 1313284 | { |
|---|
| 3037 | n/a | static char *kwlist[] = {"encoding", "errors", 0}; |
|---|
| 3038 | 1313284 | char *encoding = NULL; |
|---|
| 3039 | 1313284 | char *errors = NULL; |
|---|
| 3040 | n/a | PyObject *v; |
|---|
| 3041 | n/a | |
|---|
| 3042 | 1313284 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", |
|---|
| 3043 | n/a | kwlist, &encoding, &errors)) |
|---|
| 3044 | 3 | return NULL; |
|---|
| 3045 | 1313281 | v = PyString_AsDecodedObject((PyObject *)self, encoding, errors); |
|---|
| 3046 | 1313281 | if (v == NULL) |
|---|
| 3047 | 550 | goto onError; |
|---|
| 3048 | 1312731 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
|---|
| 3049 | 0 | PyErr_Format(PyExc_TypeError, |
|---|
| 3050 | n/a | "decoder did not return a string/unicode object " |
|---|
| 3051 | n/a | "(type=%.400s)", |
|---|
| 3052 | n/a | Py_TYPE(v)->tp_name); |
|---|
| 3053 | 0 | Py_DECREF(v); |
|---|
| 3054 | 0 | return NULL; |
|---|
| 3055 | n/a | } |
|---|
| 3056 | 1312731 | return v; |
|---|
| 3057 | n/a | |
|---|
| 3058 | 550 | onError: |
|---|
| 3059 | 550 | return NULL; |
|---|
| 3060 | n/a | } |
|---|
| 3061 | n/a | |
|---|
| 3062 | n/a | |
|---|
| 3063 | n/a | PyDoc_STRVAR(expandtabs__doc__, |
|---|
| 3064 | n/a | "S.expandtabs([tabsize]) -> string\n\ |
|---|
| 3065 | n/a | \n\ |
|---|
| 3066 | n/a | Return a copy of S where all tab characters are expanded using spaces.\n\ |
|---|
| 3067 | n/a | If tabsize is not given, a tab size of 8 characters is assumed."); |
|---|
| 3068 | n/a | |
|---|
| 3069 | n/a | static PyObject* |
|---|
| 3070 | n/a | string_expandtabs(PyStringObject *self, PyObject *args) |
|---|
| 3071 | 391893 | { |
|---|
| 3072 | n/a | const char *e, *p, *qe; |
|---|
| 3073 | n/a | char *q; |
|---|
| 3074 | n/a | Py_ssize_t i, j, incr; |
|---|
| 3075 | n/a | PyObject *u; |
|---|
| 3076 | 391893 | int tabsize = 8; |
|---|
| 3077 | n/a | |
|---|
| 3078 | 391893 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
|---|
| 3079 | 1 | return NULL; |
|---|
| 3080 | n/a | |
|---|
| 3081 | n/a | /* First pass: determine size of output string */ |
|---|
| 3082 | 391892 | i = 0; /* chars up to and including most recent \n or \r */ |
|---|
| 3083 | 391892 | j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
|---|
| 3084 | 391892 | e = PyString_AS_STRING(self) + PyString_GET_SIZE(self); /* end of input */ |
|---|
| 3085 | 15822006 | for (p = PyString_AS_STRING(self); p < e; p++) |
|---|
| 3086 | 15430114 | if (*p == '\t') { |
|---|
| 3087 | 722 | if (tabsize > 0) { |
|---|
| 3088 | 722 | incr = tabsize - (j % tabsize); |
|---|
| 3089 | 722 | if (j > PY_SSIZE_T_MAX - incr) |
|---|
| 3090 | 0 | goto overflow1; |
|---|
| 3091 | 722 | j += incr; |
|---|
| 3092 | n/a | } |
|---|
| 3093 | n/a | } |
|---|
| 3094 | n/a | else { |
|---|
| 3095 | 15429392 | if (j > PY_SSIZE_T_MAX - 1) |
|---|
| 3096 | 0 | goto overflow1; |
|---|
| 3097 | 15429392 | j++; |
|---|
| 3098 | 15429392 | if (*p == '\n' || *p == '\r') { |
|---|
| 3099 | 420301 | if (i > PY_SSIZE_T_MAX - j) |
|---|
| 3100 | 0 | goto overflow1; |
|---|
| 3101 | 420301 | i += j; |
|---|
| 3102 | 420301 | j = 0; |
|---|
| 3103 | n/a | } |
|---|
| 3104 | n/a | } |
|---|
| 3105 | n/a | |
|---|
| 3106 | 391892 | if (i > PY_SSIZE_T_MAX - j) |
|---|
| 3107 | 0 | goto overflow1; |
|---|
| 3108 | n/a | |
|---|
| 3109 | n/a | /* Second pass: create output string and fill it */ |
|---|
| 3110 | 391892 | u = PyString_FromStringAndSize(NULL, i + j); |
|---|
| 3111 | 391892 | if (!u) |
|---|
| 3112 | 0 | return NULL; |
|---|
| 3113 | n/a | |
|---|
| 3114 | 391892 | j = 0; /* same as in first pass */ |
|---|
| 3115 | 391892 | q = PyString_AS_STRING(u); /* next output char */ |
|---|
| 3116 | 391892 | qe = PyString_AS_STRING(u) + PyString_GET_SIZE(u); /* end of output */ |
|---|
| 3117 | n/a | |
|---|
| 3118 | 15822006 | for (p = PyString_AS_STRING(self); p < e; p++) |
|---|
| 3119 | 15430114 | if (*p == '\t') { |
|---|
| 3120 | 722 | if (tabsize > 0) { |
|---|
| 3121 | 722 | i = tabsize - (j % tabsize); |
|---|
| 3122 | 722 | j += i; |
|---|
| 3123 | 2443 | while (i--) { |
|---|
| 3124 | 999 | if (q >= qe) |
|---|
| 3125 | 0 | goto overflow2; |
|---|
| 3126 | 999 | *q++ = ' '; |
|---|
| 3127 | n/a | } |
|---|
| 3128 | n/a | } |
|---|
| 3129 | n/a | } |
|---|
| 3130 | n/a | else { |
|---|
| 3131 | 15429392 | if (q >= qe) |
|---|
| 3132 | 0 | goto overflow2; |
|---|
| 3133 | 15429392 | *q++ = *p; |
|---|
| 3134 | 15429392 | j++; |
|---|
| 3135 | 15429392 | if (*p == '\n' || *p == '\r') |
|---|
| 3136 | 420301 | j = 0; |
|---|
| 3137 | n/a | } |
|---|
| 3138 | n/a | |
|---|
| 3139 | 391892 | return u; |
|---|
| 3140 | n/a | |
|---|
| 3141 | 0 | overflow2: |
|---|
| 3142 | 0 | Py_DECREF(u); |
|---|
| 3143 | 0 | overflow1: |
|---|
| 3144 | 0 | PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
|---|
| 3145 | 0 | return NULL; |
|---|
| 3146 | n/a | } |
|---|
| 3147 | n/a | |
|---|
| 3148 | n/a | Py_LOCAL_INLINE(PyObject *) |
|---|
| 3149 | n/a | pad(PyStringObject *self, Py_ssize_t left, Py_ssize_t right, char fill) |
|---|
| 3150 | 8842 | { |
|---|
| 3151 | n/a | PyObject *u; |
|---|
| 3152 | n/a | |
|---|
| 3153 | 8842 | if (left < 0) |
|---|
| 3154 | 1 | left = 0; |
|---|
| 3155 | 8842 | if (right < 0) |
|---|
| 3156 | 2 | right = 0; |
|---|
| 3157 | n/a | |
|---|
| 3158 | 8842 | if (left == 0 && right == 0 && PyString_CheckExact(self)) { |
|---|
| 3159 | 0 | Py_INCREF(self); |
|---|
| 3160 | 0 | return (PyObject *)self; |
|---|
| 3161 | n/a | } |
|---|
| 3162 | n/a | |
|---|
| 3163 | 8842 | u = PyString_FromStringAndSize(NULL, |
|---|
| 3164 | n/a | left + PyString_GET_SIZE(self) + right); |
|---|
| 3165 | 8842 | if (u) { |
|---|
| 3166 | 8842 | if (left) |
|---|
| 3167 | 5405 | memset(PyString_AS_STRING(u), fill, left); |
|---|
| 3168 | 8842 | Py_MEMCPY(PyString_AS_STRING(u) + left, |
|---|
| 3169 | n/a | PyString_AS_STRING(self), |
|---|
| 3170 | n/a | PyString_GET_SIZE(self)); |
|---|
| 3171 | 8842 | if (right) |
|---|
| 3172 | 3611 | memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self), |
|---|
| 3173 | n/a | fill, right); |
|---|
| 3174 | n/a | } |
|---|
| 3175 | n/a | |
|---|
| 3176 | 8842 | return u; |
|---|
| 3177 | n/a | } |
|---|
| 3178 | n/a | |
|---|
| 3179 | n/a | PyDoc_STRVAR(ljust__doc__, |
|---|
| 3180 | n/a | "S.ljust(width[, fillchar]) -> string\n" |
|---|
| 3181 | n/a | "\n" |
|---|
| 3182 | n/a | "Return S left-justified in a string of length width. Padding is\n" |
|---|
| 3183 | n/a | "done using the specified fill character (default is a space)."); |
|---|
| 3184 | n/a | |
|---|
| 3185 | n/a | static PyObject * |
|---|
| 3186 | n/a | string_ljust(PyStringObject *self, PyObject *args) |
|---|
| 3187 | 3486 | { |
|---|
| 3188 | n/a | Py_ssize_t width; |
|---|
| 3189 | 3486 | char fillchar = ' '; |
|---|
| 3190 | n/a | |
|---|
| 3191 | 3486 | if (!PyArg_ParseTuple(args, "n|c:ljust", &width, &fillchar)) |
|---|
| 3192 | 2 | return NULL; |
|---|
| 3193 | n/a | |
|---|
| 3194 | 3484 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
|---|
| 3195 | 55 | Py_INCREF(self); |
|---|
| 3196 | 55 | return (PyObject*) self; |
|---|
| 3197 | n/a | } |
|---|
| 3198 | n/a | |
|---|
| 3199 | 3429 | return pad(self, 0, width - PyString_GET_SIZE(self), fillchar); |
|---|
| 3200 | n/a | } |
|---|
| 3201 | n/a | |
|---|
| 3202 | n/a | |
|---|
| 3203 | n/a | PyDoc_STRVAR(rjust__doc__, |
|---|
| 3204 | n/a | "S.rjust(width[, fillchar]) -> string\n" |
|---|
| 3205 | n/a | "\n" |
|---|
| 3206 | n/a | "Return S right-justified in a string of length width. Padding is\n" |
|---|
| 3207 | n/a | "done using the specified fill character (default is a space)"); |
|---|
| 3208 | n/a | |
|---|
| 3209 | n/a | static PyObject * |
|---|
| 3210 | n/a | string_rjust(PyStringObject *self, PyObject *args) |
|---|
| 3211 | 5209 | { |
|---|
| 3212 | n/a | Py_ssize_t width; |
|---|
| 3213 | 5209 | char fillchar = ' '; |
|---|
| 3214 | n/a | |
|---|
| 3215 | 5209 | if (!PyArg_ParseTuple(args, "n|c:rjust", &width, &fillchar)) |
|---|
| 3216 | 2 | return NULL; |
|---|
| 3217 | n/a | |
|---|
| 3218 | 5207 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
|---|
| 3219 | 10 | Py_INCREF(self); |
|---|
| 3220 | 10 | return (PyObject*) self; |
|---|
| 3221 | n/a | } |
|---|
| 3222 | n/a | |
|---|
| 3223 | 5197 | return pad(self, width - PyString_GET_SIZE(self), 0, fillchar); |
|---|
| 3224 | n/a | } |
|---|
| 3225 | n/a | |
|---|
| 3226 | n/a | |
|---|
| 3227 | n/a | PyDoc_STRVAR(center__doc__, |
|---|
| 3228 | n/a | "S.center(width[, fillchar]) -> string\n" |
|---|
| 3229 | n/a | "\n" |
|---|
| 3230 | n/a | "Return S centered in a string of length width. Padding is\n" |
|---|
| 3231 | n/a | "done using the specified fill character (default is a space)"); |
|---|
| 3232 | n/a | |
|---|
| 3233 | n/a | static PyObject * |
|---|
| 3234 | n/a | string_center(PyStringObject *self, PyObject *args) |
|---|
| 3235 | 1120 | { |
|---|
| 3236 | n/a | Py_ssize_t marg, left; |
|---|
| 3237 | n/a | Py_ssize_t width; |
|---|
| 3238 | 1120 | char fillchar = ' '; |
|---|
| 3239 | n/a | |
|---|
| 3240 | 1120 | if (!PyArg_ParseTuple(args, "n|c:center", &width, &fillchar)) |
|---|
| 3241 | 2 | return NULL; |
|---|
| 3242 | n/a | |
|---|
| 3243 | 1118 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
|---|
| 3244 | 928 | Py_INCREF(self); |
|---|
| 3245 | 928 | return (PyObject*) self; |
|---|
| 3246 | n/a | } |
|---|
| 3247 | n/a | |
|---|
| 3248 | 190 | marg = width - PyString_GET_SIZE(self); |
|---|
| 3249 | 190 | left = marg / 2 + (marg & width & 1); |
|---|
| 3250 | n/a | |
|---|
| 3251 | 190 | return pad(self, left, marg - left, fillchar); |
|---|
| 3252 | n/a | } |
|---|
| 3253 | n/a | |
|---|
| 3254 | n/a | PyDoc_STRVAR(zfill__doc__, |
|---|
| 3255 | n/a | "S.zfill(width) -> string\n" |
|---|
| 3256 | n/a | "\n" |
|---|
| 3257 | n/a | "Pad a numeric string S with zeros on the left, to fill a field\n" |
|---|
| 3258 | n/a | "of the specified width. The string S is never truncated."); |
|---|
| 3259 | n/a | |
|---|
| 3260 | n/a | static PyObject * |
|---|
| 3261 | n/a | string_zfill(PyStringObject *self, PyObject *args) |
|---|
| 3262 | 70 | { |
|---|
| 3263 | n/a | Py_ssize_t fill; |
|---|
| 3264 | n/a | PyObject *s; |
|---|
| 3265 | n/a | char *p; |
|---|
| 3266 | n/a | Py_ssize_t width; |
|---|
| 3267 | n/a | |
|---|
| 3268 | 70 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
|---|
| 3269 | 2 | return NULL; |
|---|
| 3270 | n/a | |
|---|
| 3271 | 68 | if (PyString_GET_SIZE(self) >= width) { |
|---|
| 3272 | 42 | if (PyString_CheckExact(self)) { |
|---|
| 3273 | 35 | Py_INCREF(self); |
|---|
| 3274 | 35 | return (PyObject*) self; |
|---|
| 3275 | n/a | } |
|---|
| 3276 | n/a | else |
|---|
| 3277 | 7 | return PyString_FromStringAndSize( |
|---|
| 3278 | n/a | PyString_AS_STRING(self), |
|---|
| 3279 | n/a | PyString_GET_SIZE(self) |
|---|
| 3280 | n/a | ); |
|---|
| 3281 | n/a | } |
|---|
| 3282 | n/a | |
|---|
| 3283 | 26 | fill = width - PyString_GET_SIZE(self); |
|---|
| 3284 | n/a | |
|---|
| 3285 | 26 | s = pad(self, fill, 0, '0'); |
|---|
| 3286 | n/a | |
|---|
| 3287 | 26 | if (s == NULL) |
|---|
| 3288 | 0 | return NULL; |
|---|
| 3289 | n/a | |
|---|
| 3290 | 26 | p = PyString_AS_STRING(s); |
|---|
| 3291 | 26 | if (p[fill] == '+' || p[fill] == '-') { |
|---|
| 3292 | n/a | /* move sign to beginning of string */ |
|---|
| 3293 | 11 | p[0] = p[fill]; |
|---|
| 3294 | 11 | p[fill] = '0'; |
|---|
| 3295 | n/a | } |
|---|
| 3296 | n/a | |
|---|
| 3297 | 26 | return (PyObject*) s; |
|---|
| 3298 | n/a | } |
|---|
| 3299 | n/a | |
|---|
| 3300 | n/a | PyDoc_STRVAR(isspace__doc__, |
|---|
| 3301 | n/a | "S.isspace() -> bool\n\ |
|---|
| 3302 | n/a | \n\ |
|---|
| 3303 | n/a | Return True if all characters in S are whitespace\n\ |
|---|
| 3304 | n/a | and there is at least one character in S, False otherwise."); |
|---|
| 3305 | n/a | |
|---|
| 3306 | n/a | static PyObject* |
|---|
| 3307 | n/a | string_isspace(PyStringObject *self) |
|---|
| 3308 | 10153 | { |
|---|
| 3309 | n/a | register const unsigned char *p |
|---|
| 3310 | 10153 | = (unsigned char *) PyString_AS_STRING(self); |
|---|
| 3311 | n/a | register const unsigned char *e; |
|---|
| 3312 | n/a | |
|---|
| 3313 | n/a | /* Shortcut for single character strings */ |
|---|
| 3314 | 10153 | if (PyString_GET_SIZE(self) == 1 && |
|---|
| 3315 | n/a | isspace(*p)) |
|---|
| 3316 | 7097 | return PyBool_FromLong(1); |
|---|
| 3317 | n/a | |
|---|
| 3318 | n/a | /* Special case for empty strings */ |
|---|
| 3319 | 3056 | if (PyString_GET_SIZE(self) == 0) |
|---|
| 3320 | 5 | return PyBool_FromLong(0); |
|---|
| 3321 | n/a | |
|---|
| 3322 | 3051 | e = p + PyString_GET_SIZE(self); |
|---|
| 3323 | 13383 | for (; p < e; p++) { |
|---|
| 3324 | 13379 | if (!isspace(*p)) |
|---|
| 3325 | 3047 | return PyBool_FromLong(0); |
|---|
| 3326 | n/a | } |
|---|
| 3327 | 4 | return PyBool_FromLong(1); |
|---|
| 3328 | n/a | } |
|---|
| 3329 | n/a | |
|---|
| 3330 | n/a | |
|---|
| 3331 | n/a | PyDoc_STRVAR(isalpha__doc__, |
|---|
| 3332 | n/a | "S.isalpha() -> bool\n\ |
|---|
| 3333 | n/a | \n\ |
|---|
| 3334 | n/a | Return True if all characters in S are alphabetic\n\ |
|---|
| 3335 | n/a | and there is at least one character in S, False otherwise."); |
|---|
| 3336 | n/a | |
|---|
| 3337 | n/a | static PyObject* |
|---|
| 3338 | n/a | string_isalpha(PyStringObject *self) |
|---|
| 3339 | 56902 | { |
|---|
| 3340 | n/a | register const unsigned char *p |
|---|
| 3341 | 56902 | = (unsigned char *) PyString_AS_STRING(self); |
|---|
| 3342 | n/a | register const unsigned char *e; |
|---|
| 3343 | n/a | |
|---|
| 3344 | n/a | /* Shortcut for single character strings */ |
|---|
| 3345 | 56902 | if (PyString_GET_SIZE(self) == 1 && |
|---|
| 3346 | n/a | isalpha(*p)) |
|---|
| 3347 | 54573 | return PyBool_FromLong(1); |
|---|
| 3348 | n/a | |
|---|
| 3349 | n/a | /* Special case for empty strings */ |
|---|
| 3350 | 2329 | if (PyString_GET_SIZE(self) == 0) |
|---|
| 3351 | 3 | return PyBool_FromLong(0); |
|---|
| 3352 | n/a | |
|---|
| 3353 | 2326 | e = p + PyString_GET_SIZE(self); |
|---|
| 3354 | 12664 | for (; p < e; p++) { |
|---|
| 3355 | 12659 | if (!isalpha(*p)) |
|---|
| 3356 | 2321 | return PyBool_FromLong(0); |
|---|
| 3357 | n/a | } |
|---|
| 3358 | 5 | return PyBool_FromLong(1); |
|---|
| 3359 | n/a | } |
|---|
| 3360 | n/a | |
|---|
| 3361 | n/a | |
|---|
| 3362 | n/a | PyDoc_STRVAR(isalnum__doc__, |
|---|
| 3363 | n/a | "S.isalnum() -> bool\n\ |
|---|
| 3364 | n/a | \n\ |
|---|
| 3365 | n/a | Return True if all characters in S are alphanumeric\n\ |
|---|
| 3366 | n/a | and there is at least one character in S, False otherwise."); |
|---|
| 3367 | n/a | |
|---|
| 3368 | n/a | static PyObject* |
|---|
| 3369 | n/a | string_isalnum(PyStringObject *self) |
|---|
| 3370 | 53252 | { |
|---|
| 3371 | n/a | register const unsigned char *p |
|---|
| 3372 | 53252 | = (unsigned char *) PyString_AS_STRING(self); |
|---|
| 3373 | n/a | register const unsigned char *e; |
|---|
| 3374 | n/a | |
|---|
| 3375 | n/a | /* Shortcut for single character strings */ |
|---|
| 3376 | 53252 | if (PyString_GET_SIZE(self) == 1 && |
|---|
| 3377 | n/a | isalnum(*p)) |
|---|
| 3378 | 53186 | return PyBool_FromLong(1); |
|---|
| 3379 | n/a | |
|---|
| 3380 | n/a | /* Special case for empty strings */ |
|---|
| 3381 | 66 | if (PyString_GET_SIZE(self) == 0) |
|---|
| 3382 | 3 | return PyBool_FromLong(0); |
|---|
| 3383 | n/a | |
|---|
| 3384 | 63 | e = p + PyString_GET_SIZE(self); |
|---|
| 3385 | 10445 | for (; p < e; p++) { |
|---|
| 3386 | 10437 | if (!isalnum(*p)) |
|---|
| 3387 | 55 | return PyBool_FromLong(0); |
|---|
| 3388 | n/a | } |
|---|
| 3389 | 8 | return PyBool_FromLong(1); |
|---|
| 3390 | n/a | } |
|---|
| 3391 | n/a | |
|---|
| 3392 | n/a | |
|---|
| 3393 | n/a | PyDoc_STRVAR(isdigit__doc__, |
|---|
| 3394 | n/a | "S.isdigit() -> bool\n\ |
|---|
| 3395 | n/a | \n\ |
|---|
| 3396 | n/a | Return True if all characters in S are digits\n\ |
|---|
| 3397 | n/a | and there is at least one character in S, False otherwise."); |
|---|
| 3398 | n/a | |
|---|
| 3399 | n/a | static PyObject* |
|---|
| 3400 | n/a | string_isdigit(PyStringObject *self) |
|---|
| 3401 | 9315 | { |
|---|
| 3402 | n/a | register const unsigned char *p |
|---|
| 3403 | 9315 | = (unsigned char *) PyString_AS_STRING(self); |
|---|
| 3404 | n/a | register const unsigned char *e; |
|---|
| 3405 | n/a | |
|---|
| 3406 | n/a | /* Shortcut for single character strings */ |
|---|
| 3407 | 9315 | if (PyString_GET_SIZE(self) == 1 && |
|---|
| 3408 | n/a | isdigit(*p)) |
|---|
| 3409 | 2816 | return PyBool_FromLong(1); |
|---|
| 3410 | n/a | |
|---|
| 3411 | n/a | /* Special case for empty strings */ |
|---|
| 3412 | 6499 | if (PyString_GET_SIZE(self) == 0) |
|---|
| 3413 | 3 | return PyBool_FromLong(0); |
|---|
| 3414 | n/a | |
|---|
| 3415 | 6496 | e = p + PyString_GET_SIZE(self); |
|---|
| 3416 | 17088 | for (; p < e; p++) { |
|---|
| 3417 | 16984 | if (!isdigit(*p)) |
|---|
| 3418 | 6392 | return PyBool_FromLong(0); |
|---|
| 3419 | n/a | } |
|---|
| 3420 | 104 | return PyBool_FromLong(1); |
|---|
| 3421 | n/a | } |
|---|
| 3422 | n/a | |
|---|
| 3423 | n/a | |
|---|
| 3424 | n/a | PyDoc_STRVAR(islower__doc__, |
|---|
| 3425 | n/a | "S.islower() -> bool\n\ |
|---|
| 3426 | n/a | \n\ |
|---|
| 3427 | n/a | Return True if all cased characters in S are lowercase and there is\n\ |
|---|
| 3428 | n/a | at least one cased character in S, False otherwise."); |
|---|
| 3429 | n/a | |
|---|
| 3430 | n/a | static PyObject* |
|---|
| 3431 | n/a | string_islower(PyStringObject *self) |
|---|
| 3432 | 521 | { |
|---|
| 3433 | n/a | register const unsigned char *p |
|---|
| 3434 | 521 | = (unsigned char *) PyString_AS_STRING(self); |
|---|
| 3435 | n/a | register const unsigned char *e; |
|---|
| 3436 | n/a | int cased; |
|---|
| 3437 | n/a | |
|---|
| 3438 | n/a | /* Shortcut for single character strings */ |
|---|
| 3439 | 521 | if (PyString_GET_SIZE(self) == 1) |
|---|
| 3440 | 373 | return PyBool_FromLong(islower(*p) != 0); |
|---|
| 3441 | n/a | |
|---|
| 3442 | n/a | /* Special case for empty strings */ |
|---|
| 3443 | 148 | if (PyString_GET_SIZE(self) == 0) |
|---|
| 3444 | 3 | return PyBool_FromLong(0); |
|---|
| 3445 | n/a | |
|---|
| 3446 | 145 | e = p + PyString_GET_SIZE(self); |
|---|
| 3447 | 145 | cased = 0; |
|---|
| 3448 | 11547 | for (; p < e; p++) { |
|---|
| 3449 | 11413 | if (isupper(*p)) |
|---|
| 3450 | 11 | return PyBool_FromLong(0); |
|---|
| 3451 | 11402 | else if (!cased && islower(*p)) |
|---|
| 3452 | 82 | cased = 1; |
|---|
| 3453 | n/a | } |
|---|
| 3454 | 134 | return PyBool_FromLong(cased); |
|---|
| 3455 | n/a | } |
|---|
| 3456 | n/a | |
|---|
| 3457 | n/a | |
|---|
| 3458 | n/a | PyDoc_STRVAR(isupper__doc__, |
|---|
| 3459 | n/a | "S.isupper() -> bool\n\ |
|---|
| 3460 | n/a | \n\ |
|---|
| 3461 | n/a | Return True if all cased characters in S are uppercase and there is\n\ |
|---|
| 3462 | n/a | at least one cased character in S, False otherwise."); |
|---|
| 3463 | n/a | |
|---|
| 3464 | n/a | static PyObject* |
|---|
| 3465 | n/a | string_isupper(PyStringObject *self) |
|---|
| 3466 | 8709 | { |
|---|
| 3467 | n/a | register const unsigned char *p |
|---|
| 3468 | 8709 | = (unsigned char *) PyString_AS_STRING(self); |
|---|
| 3469 | n/a | register const unsigned char *e; |
|---|
| 3470 | n/a | int cased; |
|---|
| 3471 | n/a | |
|---|
| 3472 | n/a | /* Shortcut for single character strings */ |
|---|
| 3473 | 8709 | if (PyString_GET_SIZE(self) == 1) |
|---|
| 3474 | 271 | return PyBool_FromLong(isupper(*p) != 0); |
|---|
| 3475 | n/a | |
|---|
| 3476 | n/a | /* Special case for empty strings */ |
|---|
| 3477 | 8438 | if (PyString_GET_SIZE(self) == 0) |
|---|
| 3478 | 3 | return PyBool_FromLong(0); |
|---|
| 3479 | n/a | |
|---|
| 3480 | 8435 | e = p + PyString_GET_SIZE(self); |
|---|
| 3481 | 8435 | cased = 0; |
|---|
| 3482 | 21308 | for (; p < e; p++) { |
|---|
| 3483 | 20943 | if (islower(*p)) |
|---|
| 3484 | 8070 | return PyBool_FromLong(0); |
|---|
| 3485 | 12873 | else if (!cased && isupper(*p)) |
|---|
| 3486 | 369 | cased = 1; |
|---|
| 3487 | n/a | } |
|---|
| 3488 | 365 | return PyBool_FromLong(cased); |
|---|
| 3489 | n/a | } |
|---|
| 3490 | n/a | |
|---|
| 3491 | n/a | |
|---|
| 3492 | n/a | PyDoc_STRVAR(istitle__doc__, |
|---|
| 3493 | n/a | "S.istitle() -> bool\n\ |
|---|
| 3494 | n/a | \n\ |
|---|
| 3495 | n/a | Return True if S is a titlecased string and there is at least one\n\ |
|---|
| 3496 | n/a | character in S, i.e. uppercase characters may only follow uncased\n\ |
|---|
| 3497 | n/a | characters and lowercase characters only cased ones. Return False\n\ |
|---|
| 3498 | n/a | otherwise."); |
|---|
| 3499 | n/a | |
|---|
| 3500 | n/a | static PyObject* |
|---|
| 3501 | n/a | string_istitle(PyStringObject *self, PyObject *uncased) |
|---|
| 3502 | 38 | { |
|---|
| 3503 | n/a | register const unsigned char *p |
|---|
| 3504 | 38 | = (unsigned char *) PyString_AS_STRING(self); |
|---|
| 3505 | n/a | register const unsigned char *e; |
|---|
| 3506 | n/a | int cased, previous_is_cased; |
|---|
| 3507 | n/a | |
|---|
| 3508 | n/a | /* Shortcut for single character strings */ |
|---|
| 3509 | 38 | if (PyString_GET_SIZE(self) == 1) |
|---|
| 3510 | 11 | return PyBool_FromLong(isupper(*p) != 0); |
|---|
| 3511 | n/a | |
|---|
| 3512 | n/a | /* Special case for empty strings */ |
|---|
| 3513 | 27 | if (PyString_GET_SIZE(self) == 0) |
|---|
| 3514 | 3 | return PyBool_FromLong(0); |
|---|
| 3515 | n/a | |
|---|
| 3516 | 24 | e = p + PyString_GET_SIZE(self); |
|---|
| 3517 | 24 | cased = 0; |
|---|
| 3518 | 24 | previous_is_cased = 0; |
|---|
| 3519 | 15687 | for (; p < e; p++) { |
|---|
| 3520 | 15676 | register const unsigned char ch = *p; |
|---|
| 3521 | n/a | |
|---|
| 3522 | 15676 | if (isupper(ch)) { |
|---|
| 3523 | 48 | if (previous_is_cased) |
|---|
| 3524 | 4 | return PyBool_FromLong(0); |
|---|
| 3525 | 44 | previous_is_cased = 1; |
|---|
| 3526 | 44 | cased = 1; |
|---|
| 3527 | n/a | } |
|---|
| 3528 | 15628 | else if (islower(ch)) { |
|---|
| 3529 | 15577 | if (!previous_is_cased) |
|---|
| 3530 | 9 | return PyBool_FromLong(0); |
|---|
| 3531 | 15568 | previous_is_cased = 1; |
|---|
| 3532 | 15568 | cased = 1; |
|---|
| 3533 | n/a | } |
|---|
| 3534 | n/a | else |
|---|
| 3535 | 51 | previous_is_cased = 0; |
|---|
| 3536 | n/a | } |
|---|
| 3537 | 11 | return PyBool_FromLong(cased); |
|---|
| 3538 | n/a | } |
|---|
| 3539 | n/a | |
|---|
| 3540 | n/a | |
|---|
| 3541 | n/a | PyDoc_STRVAR(splitlines__doc__, |
|---|
| 3542 | n/a | "S.splitlines([keepends]) -> list of strings\n\ |
|---|
| 3543 | n/a | \n\ |
|---|
| 3544 | n/a | Return a list of the lines in S, breaking at line boundaries.\n\ |
|---|
| 3545 | n/a | Line breaks are not included in the resulting list unless keepends\n\ |
|---|
| 3546 | n/a | is given and true."); |
|---|
| 3547 | n/a | |
|---|
| 3548 | n/a | static PyObject* |
|---|
| 3549 | n/a | string_splitlines(PyStringObject *self, PyObject *args) |
|---|
| 3550 | 1607 | { |
|---|
| 3551 | 1607 | int keepends = 0; |
|---|
| 3552 | n/a | |
|---|
| 3553 | 1607 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
|---|
| 3554 | 1 | return NULL; |
|---|
| 3555 | n/a | |
|---|
| 3556 | 1606 | return stringlib_splitlines( |
|---|
| 3557 | n/a | (PyObject*) self, PyString_AS_STRING(self), PyString_GET_SIZE(self), |
|---|
| 3558 | n/a | keepends |
|---|
| 3559 | n/a | ); |
|---|
| 3560 | n/a | } |
|---|
| 3561 | n/a | |
|---|
| 3562 | n/a | PyDoc_STRVAR(sizeof__doc__, |
|---|
| 3563 | n/a | "S.__sizeof__() -> size of S in memory, in bytes"); |
|---|
| 3564 | n/a | |
|---|
| 3565 | n/a | static PyObject * |
|---|
| 3566 | n/a | string_sizeof(PyStringObject *v) |
|---|
| 3567 | 2 | { |
|---|
| 3568 | n/a | Py_ssize_t res; |
|---|
| 3569 | 2 | res = PyStringObject_SIZE + PyString_GET_SIZE(v) * Py_TYPE(v)->tp_itemsize; |
|---|
| 3570 | 2 | return PyInt_FromSsize_t(res); |
|---|
| 3571 | n/a | } |
|---|
| 3572 | n/a | |
|---|
| 3573 | n/a | static PyObject * |
|---|
| 3574 | n/a | string_getnewargs(PyStringObject *v) |
|---|
| 3575 | 9 | { |
|---|
| 3576 | 9 | return Py_BuildValue("(s#)", v->ob_sval, Py_SIZE(v)); |
|---|
| 3577 | n/a | } |
|---|
| 3578 | n/a | |
|---|
| 3579 | n/a | |
|---|
| 3580 | n/a | #include "stringlib/string_format.h" |
|---|
| 3581 | n/a | |
|---|
| 3582 | n/a | PyDoc_STRVAR(format__doc__, |
|---|
| 3583 | n/a | "S.format(*args, **kwargs) -> unicode\n\ |
|---|
| 3584 | n/a | \n\ |
|---|
| 3585 | n/a | "); |
|---|
| 3586 | n/a | |
|---|
| 3587 | n/a | static PyObject * |
|---|
| 3588 | n/a | string__format__(PyObject* self, PyObject* args) |
|---|
| 3589 | 126 | { |
|---|
| 3590 | n/a | PyObject *format_spec; |
|---|
| 3591 | 126 | PyObject *result = NULL; |
|---|
| 3592 | 126 | PyObject *tmp = NULL; |
|---|
| 3593 | n/a | |
|---|
| 3594 | n/a | /* If 2.x, convert format_spec to the same type as value */ |
|---|
| 3595 | n/a | /* This is to allow things like u''.format('') */ |
|---|
| 3596 | 126 | if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
|---|
| 3597 | 0 | goto done; |
|---|
| 3598 | 126 | if (!(PyString_Check(format_spec) || PyUnicode_Check(format_spec))) { |
|---|
| 3599 | 0 | PyErr_Format(PyExc_TypeError, "__format__ arg must be str " |
|---|
| 3600 | n/a | "or unicode, not %s", Py_TYPE(format_spec)->tp_name); |
|---|
| 3601 | 0 | goto done; |
|---|
| 3602 | n/a | } |
|---|
| 3603 | 126 | tmp = PyObject_Str(format_spec); |
|---|
| 3604 | 126 | if (tmp == NULL) |
|---|
| 3605 | 0 | goto done; |
|---|
| 3606 | 126 | format_spec = tmp; |
|---|
| 3607 | n/a | |
|---|
| 3608 | 126 | result = _PyBytes_FormatAdvanced(self, |
|---|
| 3609 | n/a | PyString_AS_STRING(format_spec), |
|---|
| 3610 | n/a | PyString_GET_SIZE(format_spec)); |
|---|
| 3611 | 126 | done: |
|---|
| 3612 | 126 | Py_XDECREF(tmp); |
|---|
| 3613 | 126 | return result; |
|---|
| 3614 | n/a | } |
|---|
| 3615 | n/a | |
|---|
| 3616 | n/a | PyDoc_STRVAR(p_format__doc__, |
|---|
| 3617 | n/a | "S.__format__(format_spec) -> unicode\n\ |
|---|
| 3618 | n/a | \n\ |
|---|
| 3619 | n/a | "); |
|---|
| 3620 | n/a | |
|---|
| 3621 | n/a | |
|---|
| 3622 | n/a | static PyMethodDef |
|---|
| 3623 | n/a | string_methods[] = { |
|---|
| 3624 | n/a | /* Counterparts of the obsolete stropmodule functions; except |
|---|
| 3625 | n/a | string.maketrans(). */ |
|---|
| 3626 | n/a | {"join", (PyCFunction)string_join, METH_O, join__doc__}, |
|---|
| 3627 | n/a | {"split", (PyCFunction)string_split, METH_VARARGS, split__doc__}, |
|---|
| 3628 | n/a | {"rsplit", (PyCFunction)string_rsplit, METH_VARARGS, rsplit__doc__}, |
|---|
| 3629 | n/a | {"lower", (PyCFunction)string_lower, METH_NOARGS, lower__doc__}, |
|---|
| 3630 | n/a | {"upper", (PyCFunction)string_upper, METH_NOARGS, upper__doc__}, |
|---|
| 3631 | n/a | {"islower", (PyCFunction)string_islower, METH_NOARGS, islower__doc__}, |
|---|
| 3632 | n/a | {"isupper", (PyCFunction)string_isupper, METH_NOARGS, isupper__doc__}, |
|---|
| 3633 | n/a | {"isspace", (PyCFunction)string_isspace, METH_NOARGS, isspace__doc__}, |
|---|
| 3634 | n/a | {"isdigit", (PyCFunction)string_isdigit, METH_NOARGS, isdigit__doc__}, |
|---|
| 3635 | n/a | {"istitle", (PyCFunction)string_istitle, METH_NOARGS, istitle__doc__}, |
|---|
| 3636 | n/a | {"isalpha", (PyCFunction)string_isalpha, METH_NOARGS, isalpha__doc__}, |
|---|
| 3637 | n/a | {"isalnum", (PyCFunction)string_isalnum, METH_NOARGS, isalnum__doc__}, |
|---|
| 3638 | n/a | {"capitalize", (PyCFunction)string_capitalize, METH_NOARGS, |
|---|
| 3639 | n/a | capitalize__doc__}, |
|---|
| 3640 | n/a | {"count", (PyCFunction)string_count, METH_VARARGS, count__doc__}, |
|---|
| 3641 | n/a | {"endswith", (PyCFunction)string_endswith, METH_VARARGS, |
|---|
| 3642 | n/a | endswith__doc__}, |
|---|
| 3643 | n/a | {"partition", (PyCFunction)string_partition, METH_O, partition__doc__}, |
|---|
| 3644 | n/a | {"find", (PyCFunction)string_find, METH_VARARGS, find__doc__}, |
|---|
| 3645 | n/a | {"index", (PyCFunction)string_index, METH_VARARGS, index__doc__}, |
|---|
| 3646 | n/a | {"lstrip", (PyCFunction)string_lstrip, METH_VARARGS, lstrip__doc__}, |
|---|
| 3647 | n/a | {"replace", (PyCFunction)string_replace, METH_VARARGS, replace__doc__}, |
|---|
| 3648 | n/a | {"rfind", (PyCFunction)string_rfind, METH_VARARGS, rfind__doc__}, |
|---|
| 3649 | n/a | {"rindex", (PyCFunction)string_rindex, METH_VARARGS, rindex__doc__}, |
|---|
| 3650 | n/a | {"rstrip", (PyCFunction)string_rstrip, METH_VARARGS, rstrip__doc__}, |
|---|
| 3651 | n/a | {"rpartition", (PyCFunction)string_rpartition, METH_O, |
|---|
| 3652 | n/a | rpartition__doc__}, |
|---|
| 3653 | n/a | {"startswith", (PyCFunction)string_startswith, METH_VARARGS, |
|---|
| 3654 | n/a | startswith__doc__}, |
|---|
| 3655 | n/a | {"strip", (PyCFunction)string_strip, METH_VARARGS, strip__doc__}, |
|---|
| 3656 | n/a | {"swapcase", (PyCFunction)string_swapcase, METH_NOARGS, |
|---|
| 3657 | n/a | swapcase__doc__}, |
|---|
| 3658 | n/a | {"translate", (PyCFunction)string_translate, METH_VARARGS, |
|---|
| 3659 | n/a | translate__doc__}, |
|---|
| 3660 | n/a | {"title", (PyCFunction)string_title, METH_NOARGS, title__doc__}, |
|---|
| 3661 | n/a | {"ljust", (PyCFunction)string_ljust, METH_VARARGS, ljust__doc__}, |
|---|
| 3662 | n/a | {"rjust", (PyCFunction)string_rjust, METH_VARARGS, rjust__doc__}, |
|---|
| 3663 | n/a | {"center", (PyCFunction)string_center, METH_VARARGS, center__doc__}, |
|---|
| 3664 | n/a | {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__}, |
|---|
| 3665 | n/a | {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
|---|
| 3666 | n/a | {"__format__", (PyCFunction) string__format__, METH_VARARGS, p_format__doc__}, |
|---|
| 3667 | n/a | {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
|---|
| 3668 | n/a | {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
|---|
| 3669 | n/a | {"encode", (PyCFunction)string_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
|---|
| 3670 | n/a | {"decode", (PyCFunction)string_decode, METH_VARARGS | METH_KEYWORDS, decode__doc__}, |
|---|
| 3671 | n/a | {"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, |
|---|
| 3672 | n/a | expandtabs__doc__}, |
|---|
| 3673 | n/a | {"splitlines", (PyCFunction)string_splitlines, METH_VARARGS, |
|---|
| 3674 | n/a | splitlines__doc__}, |
|---|
| 3675 | n/a | {"__sizeof__", (PyCFunction)string_sizeof, METH_NOARGS, |
|---|
| 3676 | n/a | sizeof__doc__}, |
|---|
| 3677 | n/a | {"__getnewargs__", (PyCFunction)string_getnewargs, METH_NOARGS}, |
|---|
| 3678 | n/a | {NULL, NULL} /* sentinel */ |
|---|
| 3679 | n/a | }; |
|---|
| 3680 | n/a | |
|---|
| 3681 | n/a | static PyObject * |
|---|
| 3682 | n/a | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
|---|
| 3683 | n/a | |
|---|
| 3684 | n/a | static PyObject * |
|---|
| 3685 | n/a | string_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 3686 | 823148 | { |
|---|
| 3687 | 823148 | PyObject *x = NULL; |
|---|
| 3688 | n/a | static char *kwlist[] = {"object", 0}; |
|---|
| 3689 | n/a | |
|---|
| 3690 | 823148 | if (type != &PyString_Type) |
|---|
| 3691 | 1031 | return str_subtype_new(type, args, kwds); |
|---|
| 3692 | 822117 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x)) |
|---|
| 3693 | 1 | return NULL; |
|---|
| 3694 | 822116 | if (x == NULL) |
|---|
| 3695 | 10 | return PyString_FromString(""); |
|---|
| 3696 | 822106 | return PyObject_Str(x); |
|---|
| 3697 | n/a | } |
|---|
| 3698 | n/a | |
|---|
| 3699 | n/a | static PyObject * |
|---|
| 3700 | n/a | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 3701 | 1031 | { |
|---|
| 3702 | n/a | PyObject *tmp, *pnew; |
|---|
| 3703 | n/a | Py_ssize_t n; |
|---|
| 3704 | n/a | |
|---|
| 3705 | 1031 | assert(PyType_IsSubtype(type, &PyString_Type)); |
|---|
| 3706 | 1031 | tmp = string_new(&PyString_Type, args, kwds); |
|---|
| 3707 | 1031 | if (tmp == NULL) |
|---|
| 3708 | 0 | return NULL; |
|---|
| 3709 | 1031 | assert(PyString_CheckExact(tmp)); |
|---|
| 3710 | 1031 | n = PyString_GET_SIZE(tmp); |
|---|
| 3711 | 1031 | pnew = type->tp_alloc(type, n); |
|---|
| 3712 | 1031 | if (pnew != NULL) { |
|---|
| 3713 | 1031 | Py_MEMCPY(PyString_AS_STRING(pnew), PyString_AS_STRING(tmp), n+1); |
|---|
| 3714 | 1031 | ((PyStringObject *)pnew)->ob_shash = |
|---|
| 3715 | n/a | ((PyStringObject *)tmp)->ob_shash; |
|---|
| 3716 | 1031 | ((PyStringObject *)pnew)->ob_sstate = SSTATE_NOT_INTERNED; |
|---|
| 3717 | n/a | } |
|---|
| 3718 | 1031 | Py_DECREF(tmp); |
|---|
| 3719 | 1031 | return pnew; |
|---|
| 3720 | n/a | } |
|---|
| 3721 | n/a | |
|---|
| 3722 | n/a | static PyObject * |
|---|
| 3723 | n/a | basestring_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 3724 | 0 | { |
|---|
| 3725 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 3726 | n/a | "The basestring type cannot be instantiated"); |
|---|
| 3727 | 0 | return NULL; |
|---|
| 3728 | n/a | } |
|---|
| 3729 | n/a | |
|---|
| 3730 | n/a | static PyObject * |
|---|
| 3731 | n/a | string_mod(PyObject *v, PyObject *w) |
|---|
| 3732 | 6095 | { |
|---|
| 3733 | 6095 | if (!PyString_Check(v)) { |
|---|
| 3734 | 0 | Py_INCREF(Py_NotImplemented); |
|---|
| 3735 | 0 | return Py_NotImplemented; |
|---|
| 3736 | n/a | } |
|---|
| 3737 | 6095 | return PyString_Format(v, w); |
|---|
| 3738 | n/a | } |
|---|
| 3739 | n/a | |
|---|
| 3740 | n/a | PyDoc_STRVAR(basestring_doc, |
|---|
| 3741 | n/a | "Type basestring cannot be instantiated; it is the base for str and unicode."); |
|---|
| 3742 | n/a | |
|---|
| 3743 | n/a | static PyNumberMethods string_as_number = { |
|---|
| 3744 | n/a | 0, /*nb_add*/ |
|---|
| 3745 | n/a | 0, /*nb_subtract*/ |
|---|
| 3746 | n/a | 0, /*nb_multiply*/ |
|---|
| 3747 | n/a | 0, /*nb_divide*/ |
|---|
| 3748 | n/a | string_mod, /*nb_remainder*/ |
|---|
| 3749 | n/a | }; |
|---|
| 3750 | n/a | |
|---|
| 3751 | n/a | |
|---|
| 3752 | n/a | PyTypeObject PyBaseString_Type = { |
|---|
| 3753 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|---|
| 3754 | n/a | "basestring", |
|---|
| 3755 | n/a | 0, |
|---|
| 3756 | n/a | 0, |
|---|
| 3757 | n/a | 0, /* tp_dealloc */ |
|---|
| 3758 | n/a | 0, /* tp_print */ |
|---|
| 3759 | n/a | 0, /* tp_getattr */ |
|---|
| 3760 | n/a | 0, /* tp_setattr */ |
|---|
| 3761 | n/a | 0, /* tp_compare */ |
|---|
| 3762 | n/a | 0, /* tp_repr */ |
|---|
| 3763 | n/a | 0, /* tp_as_number */ |
|---|
| 3764 | n/a | 0, /* tp_as_sequence */ |
|---|
| 3765 | n/a | 0, /* tp_as_mapping */ |
|---|
| 3766 | n/a | 0, /* tp_hash */ |
|---|
| 3767 | n/a | 0, /* tp_call */ |
|---|
| 3768 | n/a | 0, /* tp_str */ |
|---|
| 3769 | n/a | 0, /* tp_getattro */ |
|---|
| 3770 | n/a | 0, /* tp_setattro */ |
|---|
| 3771 | n/a | 0, /* tp_as_buffer */ |
|---|
| 3772 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 3773 | n/a | basestring_doc, /* tp_doc */ |
|---|
| 3774 | n/a | 0, /* tp_traverse */ |
|---|
| 3775 | n/a | 0, /* tp_clear */ |
|---|
| 3776 | n/a | 0, /* tp_richcompare */ |
|---|
| 3777 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 3778 | n/a | 0, /* tp_iter */ |
|---|
| 3779 | n/a | 0, /* tp_iternext */ |
|---|
| 3780 | n/a | 0, /* tp_methods */ |
|---|
| 3781 | n/a | 0, /* tp_members */ |
|---|
| 3782 | n/a | 0, /* tp_getset */ |
|---|
| 3783 | n/a | &PyBaseObject_Type, /* tp_base */ |
|---|
| 3784 | n/a | 0, /* tp_dict */ |
|---|
| 3785 | n/a | 0, /* tp_descr_get */ |
|---|
| 3786 | n/a | 0, /* tp_descr_set */ |
|---|
| 3787 | n/a | 0, /* tp_dictoffset */ |
|---|
| 3788 | n/a | 0, /* tp_init */ |
|---|
| 3789 | n/a | 0, /* tp_alloc */ |
|---|
| 3790 | n/a | basestring_new, /* tp_new */ |
|---|
| 3791 | n/a | 0, /* tp_free */ |
|---|
| 3792 | n/a | }; |
|---|
| 3793 | n/a | |
|---|
| 3794 | n/a | PyDoc_STRVAR(string_doc, |
|---|
| 3795 | n/a | "str(object) -> string\n\ |
|---|
| 3796 | n/a | \n\ |
|---|
| 3797 | n/a | Return a nice string representation of the object.\n\ |
|---|
| 3798 | n/a | If the argument is a string, the return value is the same object."); |
|---|
| 3799 | n/a | |
|---|
| 3800 | n/a | PyTypeObject PyString_Type = { |
|---|
| 3801 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|---|
| 3802 | n/a | "str", |
|---|
| 3803 | n/a | PyStringObject_SIZE, |
|---|
| 3804 | n/a | sizeof(char), |
|---|
| 3805 | n/a | string_dealloc, /* tp_dealloc */ |
|---|
| 3806 | n/a | (printfunc)string_print, /* tp_print */ |
|---|
| 3807 | n/a | 0, /* tp_getattr */ |
|---|
| 3808 | n/a | 0, /* tp_setattr */ |
|---|
| 3809 | n/a | 0, /* tp_compare */ |
|---|
| 3810 | n/a | string_repr, /* tp_repr */ |
|---|
| 3811 | n/a | &string_as_number, /* tp_as_number */ |
|---|
| 3812 | n/a | &string_as_sequence, /* tp_as_sequence */ |
|---|
| 3813 | n/a | &string_as_mapping, /* tp_as_mapping */ |
|---|
| 3814 | n/a | (hashfunc)string_hash, /* tp_hash */ |
|---|
| 3815 | n/a | 0, /* tp_call */ |
|---|
| 3816 | n/a | string_str, /* tp_str */ |
|---|
| 3817 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 3818 | n/a | 0, /* tp_setattro */ |
|---|
| 3819 | n/a | &string_as_buffer, /* tp_as_buffer */ |
|---|
| 3820 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
|---|
| 3821 | n/a | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_STRING_SUBCLASS | |
|---|
| 3822 | n/a | Py_TPFLAGS_HAVE_NEWBUFFER, /* tp_flags */ |
|---|
| 3823 | n/a | string_doc, /* tp_doc */ |
|---|
| 3824 | n/a | 0, /* tp_traverse */ |
|---|
| 3825 | n/a | 0, /* tp_clear */ |
|---|
| 3826 | n/a | (richcmpfunc)string_richcompare, /* tp_richcompare */ |
|---|
| 3827 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 3828 | n/a | 0, /* tp_iter */ |
|---|
| 3829 | n/a | 0, /* tp_iternext */ |
|---|
| 3830 | n/a | string_methods, /* tp_methods */ |
|---|
| 3831 | n/a | 0, /* tp_members */ |
|---|
| 3832 | n/a | 0, /* tp_getset */ |
|---|
| 3833 | n/a | &PyBaseString_Type, /* tp_base */ |
|---|
| 3834 | n/a | 0, /* tp_dict */ |
|---|
| 3835 | n/a | 0, /* tp_descr_get */ |
|---|
| 3836 | n/a | 0, /* tp_descr_set */ |
|---|
| 3837 | n/a | 0, /* tp_dictoffset */ |
|---|
| 3838 | n/a | 0, /* tp_init */ |
|---|
| 3839 | n/a | 0, /* tp_alloc */ |
|---|
| 3840 | n/a | string_new, /* tp_new */ |
|---|
| 3841 | n/a | PyObject_Del, /* tp_free */ |
|---|
| 3842 | n/a | }; |
|---|
| 3843 | n/a | |
|---|
| 3844 | n/a | void |
|---|
| 3845 | n/a | PyString_Concat(register PyObject **pv, register PyObject *w) |
|---|
| 3846 | 2462356 | { |
|---|
| 3847 | n/a | register PyObject *v; |
|---|
| 3848 | 2462356 | if (*pv == NULL) |
|---|
| 3849 | 0 | return; |
|---|
| 3850 | 2462356 | if (w == NULL || !PyString_Check(*pv)) { |
|---|
| 3851 | 2 | Py_DECREF(*pv); |
|---|
| 3852 | 2 | *pv = NULL; |
|---|
| 3853 | 2 | return; |
|---|
| 3854 | n/a | } |
|---|
| 3855 | 2462354 | v = string_concat((PyStringObject *) *pv, w); |
|---|
| 3856 | 2462354 | Py_DECREF(*pv); |
|---|
| 3857 | 2462354 | *pv = v; |
|---|
| 3858 | n/a | } |
|---|
| 3859 | n/a | |
|---|
| 3860 | n/a | void |
|---|
| 3861 | n/a | PyString_ConcatAndDel(register PyObject **pv, register PyObject *w) |
|---|
| 3862 | 126361 | { |
|---|
| 3863 | 126361 | PyString_Concat(pv, w); |
|---|
| 3864 | 126361 | Py_XDECREF(w); |
|---|
| 3865 | 126361 | } |
|---|
| 3866 | n/a | |
|---|
| 3867 | n/a | |
|---|
| 3868 | n/a | /* The following function breaks the notion that strings are immutable: |
|---|
| 3869 | n/a | it changes the size of a string. We get away with this only if there |
|---|
| 3870 | n/a | is only one module referencing the object. You can also think of it |
|---|
| 3871 | n/a | as creating a new string object and destroying the old one, only |
|---|
| 3872 | n/a | more efficiently. In any case, don't use this if the string may |
|---|
| 3873 | n/a | already be known to some other part of the code... |
|---|
| 3874 | n/a | Note that if there's not enough memory to resize the string, the original |
|---|
| 3875 | n/a | string object at *pv is deallocated, *pv is set to NULL, an "out of |
|---|
| 3876 | n/a | memory" exception is set, and -1 is returned. Else (on success) 0 is |
|---|
| 3877 | n/a | returned, and the value in *pv may or may not be the same as on input. |
|---|
| 3878 | n/a | As always, an extra byte is allocated for a trailing \0 byte (newsize |
|---|
| 3879 | n/a | does *not* include that), and a trailing \0 byte is stored. |
|---|
| 3880 | n/a | */ |
|---|
| 3881 | n/a | |
|---|
| 3882 | n/a | int |
|---|
| 3883 | n/a | _PyString_Resize(PyObject **pv, Py_ssize_t newsize) |
|---|
| 3884 | 7925742 | { |
|---|
| 3885 | n/a | register PyObject *v; |
|---|
| 3886 | n/a | register PyStringObject *sv; |
|---|
| 3887 | 7925742 | v = *pv; |
|---|
| 3888 | 7925742 | if (!PyString_Check(v) || Py_REFCNT(v) != 1 || newsize < 0 || |
|---|
| 3889 | n/a | PyString_CHECK_INTERNED(v)) { |
|---|
| 3890 | 0 | *pv = 0; |
|---|
| 3891 | 0 | Py_DECREF(v); |
|---|
| 3892 | 0 | PyErr_BadInternalCall(); |
|---|
| 3893 | 0 | return -1; |
|---|
| 3894 | n/a | } |
|---|
| 3895 | n/a | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
|---|
| 3896 | 7925742 | _Py_DEC_REFTOTAL; |
|---|
| 3897 | 7925742 | _Py_ForgetReference(v); |
|---|
| 3898 | 7925742 | *pv = (PyObject *) |
|---|
| 3899 | n/a | PyObject_REALLOC((char *)v, PyStringObject_SIZE + newsize); |
|---|
| 3900 | 7925742 | if (*pv == NULL) { |
|---|
| 3901 | 0 | PyObject_Del(v); |
|---|
| 3902 | 0 | PyErr_NoMemory(); |
|---|
| 3903 | 0 | return -1; |
|---|
| 3904 | n/a | } |
|---|
| 3905 | 7925742 | _Py_NewReference(*pv); |
|---|
| 3906 | 7925742 | sv = (PyStringObject *) *pv; |
|---|
| 3907 | 7925742 | Py_SIZE(sv) = newsize; |
|---|
| 3908 | 7925742 | sv->ob_sval[newsize] = '\0'; |
|---|
| 3909 | 7925742 | sv->ob_shash = -1; /* invalidate cached hash value */ |
|---|
| 3910 | 7925742 | return 0; |
|---|
| 3911 | n/a | } |
|---|
| 3912 | n/a | |
|---|
| 3913 | n/a | /* Helpers for formatstring */ |
|---|
| 3914 | n/a | |
|---|
| 3915 | n/a | Py_LOCAL_INLINE(PyObject *) |
|---|
| 3916 | n/a | getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx) |
|---|
| 3917 | 1135861 | { |
|---|
| 3918 | 1135861 | Py_ssize_t argidx = *p_argidx; |
|---|
| 3919 | 1135861 | if (argidx < arglen) { |
|---|
| 3920 | 1135856 | (*p_argidx)++; |
|---|
| 3921 | 1135856 | if (arglen < 0) |
|---|
| 3922 | 682661 | return args; |
|---|
| 3923 | n/a | else |
|---|
| 3924 | 453195 | return PyTuple_GetItem(args, argidx); |
|---|
| 3925 | n/a | } |
|---|
| 3926 | 5 | PyErr_SetString(PyExc_TypeError, |
|---|
| 3927 | n/a | "not enough arguments for format string"); |
|---|
| 3928 | 5 | return NULL; |
|---|
| 3929 | n/a | } |
|---|
| 3930 | n/a | |
|---|
| 3931 | n/a | /* Format codes |
|---|
| 3932 | n/a | * F_LJUST '-' |
|---|
| 3933 | n/a | * F_SIGN '+' |
|---|
| 3934 | n/a | * F_BLANK ' ' |
|---|
| 3935 | n/a | * F_ALT '#' |
|---|
| 3936 | n/a | * F_ZERO '0' |
|---|
| 3937 | n/a | */ |
|---|
| 3938 | n/a | #define F_LJUST (1<<0) |
|---|
| 3939 | n/a | #define F_SIGN (1<<1) |
|---|
| 3940 | n/a | #define F_BLANK (1<<2) |
|---|
| 3941 | n/a | #define F_ALT (1<<3) |
|---|
| 3942 | n/a | #define F_ZERO (1<<4) |
|---|
| 3943 | n/a | |
|---|
| 3944 | n/a | /* Returns a new reference to a PyString object, or NULL on failure. */ |
|---|
| 3945 | n/a | |
|---|
| 3946 | n/a | static PyObject * |
|---|
| 3947 | n/a | formatfloat(PyObject *v, int flags, int prec, int type) |
|---|
| 3948 | 50112 | { |
|---|
| 3949 | n/a | char *p; |
|---|
| 3950 | n/a | PyObject *result; |
|---|
| 3951 | n/a | double x; |
|---|
| 3952 | n/a | |
|---|
| 3953 | 50112 | x = PyFloat_AsDouble(v); |
|---|
| 3954 | 50112 | if (x == -1.0 && PyErr_Occurred()) { |
|---|
| 3955 | 1 | PyErr_Format(PyExc_TypeError, "float argument required, " |
|---|
| 3956 | n/a | "not %.200s", Py_TYPE(v)->tp_name); |
|---|
| 3957 | 1 | return NULL; |
|---|
| 3958 | n/a | } |
|---|
| 3959 | n/a | |
|---|
| 3960 | 50111 | if (prec < 0) |
|---|
| 3961 | 29404 | prec = 6; |
|---|
| 3962 | n/a | |
|---|
| 3963 | 50111 | p = PyOS_double_to_string(x, type, prec, |
|---|
| 3964 | n/a | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
|---|
| 3965 | n/a | |
|---|
| 3966 | 50111 | if (p == NULL) |
|---|
| 3967 | 0 | return NULL; |
|---|
| 3968 | 50111 | result = PyString_FromStringAndSize(p, strlen(p)); |
|---|
| 3969 | 50111 | PyMem_Free(p); |
|---|
| 3970 | 50111 | return result; |
|---|
| 3971 | n/a | } |
|---|
| 3972 | n/a | |
|---|
| 3973 | n/a | /* _PyString_FormatLong emulates the format codes d, u, o, x and X, and |
|---|
| 3974 | n/a | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
|---|
| 3975 | n/a | * Python's regular ints. |
|---|
| 3976 | n/a | * Return value: a new PyString*, or NULL if error. |
|---|
| 3977 | n/a | * . *pbuf is set to point into it, |
|---|
| 3978 | n/a | * *plen set to the # of chars following that. |
|---|
| 3979 | n/a | * Caller must decref it when done using pbuf. |
|---|
| 3980 | n/a | * The string starting at *pbuf is of the form |
|---|
| 3981 | n/a | * "-"? ("0x" | "0X")? digit+ |
|---|
| 3982 | n/a | * "0x"/"0X" are present only for x and X conversions, with F_ALT |
|---|
| 3983 | n/a | * set in flags. The case of hex digits will be correct, |
|---|
| 3984 | n/a | * There will be at least prec digits, zero-filled on the left if |
|---|
| 3985 | n/a | * necessary to get that many. |
|---|
| 3986 | n/a | * val object to be converted |
|---|
| 3987 | n/a | * flags bitmask of format flags; only F_ALT is looked at |
|---|
| 3988 | n/a | * prec minimum number of digits; 0-fill on left if needed |
|---|
| 3989 | n/a | * type a character in [duoxX]; u acts the same as d |
|---|
| 3990 | n/a | * |
|---|
| 3991 | n/a | * CAUTION: o, x and X conversions on regular ints can never |
|---|
| 3992 | n/a | * produce a '-' sign, but can for Python's unbounded ints. |
|---|
| 3993 | n/a | */ |
|---|
| 3994 | n/a | PyObject* |
|---|
| 3995 | n/a | _PyString_FormatLong(PyObject *val, int flags, int prec, int type, |
|---|
| 3996 | n/a | char **pbuf, int *plen) |
|---|
| 3997 | 36405 | { |
|---|
| 3998 | 36405 | PyObject *result = NULL; |
|---|
| 3999 | n/a | char *buf; |
|---|
| 4000 | n/a | Py_ssize_t i; |
|---|
| 4001 | n/a | int sign; /* 1 if '-', else 0 */ |
|---|
| 4002 | n/a | int len; /* number of characters */ |
|---|
| 4003 | n/a | Py_ssize_t llen; |
|---|
| 4004 | n/a | int numdigits; /* len == numnondigits + numdigits */ |
|---|
| 4005 | 36405 | int numnondigits = 0; |
|---|
| 4006 | n/a | |
|---|
| 4007 | 36405 | switch (type) { |
|---|
| 4008 | n/a | case 'd': |
|---|
| 4009 | n/a | case 'u': |
|---|
| 4010 | 1190 | result = Py_TYPE(val)->tp_str(val); |
|---|
| 4011 | 1190 | break; |
|---|
| 4012 | n/a | case 'o': |
|---|
| 4013 | 178 | result = Py_TYPE(val)->tp_as_number->nb_oct(val); |
|---|
| 4014 | 178 | break; |
|---|
| 4015 | n/a | case 'x': |
|---|
| 4016 | n/a | case 'X': |
|---|
| 4017 | 35037 | numnondigits = 2; |
|---|
| 4018 | 35037 | result = Py_TYPE(val)->tp_as_number->nb_hex(val); |
|---|
| 4019 | 35037 | break; |
|---|
| 4020 | n/a | default: |
|---|
| 4021 | 0 | assert(!"'type' not in [duoxX]"); |
|---|
| 4022 | n/a | } |
|---|
| 4023 | 36405 | if (!result) |
|---|
| 4024 | 0 | return NULL; |
|---|
| 4025 | n/a | |
|---|
| 4026 | 36405 | buf = PyString_AsString(result); |
|---|
| 4027 | 36405 | if (!buf) { |
|---|
| 4028 | 1 | Py_DECREF(result); |
|---|
| 4029 | 1 | return NULL; |
|---|
| 4030 | n/a | } |
|---|
| 4031 | n/a | |
|---|
| 4032 | n/a | /* To modify the string in-place, there can only be one reference. */ |
|---|
| 4033 | 36404 | if (Py_REFCNT(result) != 1) { |
|---|
| 4034 | 0 | PyErr_BadInternalCall(); |
|---|
| 4035 | 0 | return NULL; |
|---|
| 4036 | n/a | } |
|---|
| 4037 | 36404 | llen = PyString_Size(result); |
|---|
| 4038 | 36404 | if (llen > INT_MAX) { |
|---|
| 4039 | 0 | PyErr_SetString(PyExc_ValueError, "string too large in _PyString_FormatLong"); |
|---|
| 4040 | 0 | return NULL; |
|---|
| 4041 | n/a | } |
|---|
| 4042 | 36404 | len = (int)llen; |
|---|
| 4043 | 36404 | if (buf[len-1] == 'L') { |
|---|
| 4044 | 35214 | --len; |
|---|
| 4045 | 35214 | buf[len] = '\0'; |
|---|
| 4046 | n/a | } |
|---|
| 4047 | 36404 | sign = buf[0] == '-'; |
|---|
| 4048 | 36404 | numnondigits += sign; |
|---|
| 4049 | 36404 | numdigits = len - numnondigits; |
|---|
| 4050 | 36404 | assert(numdigits > 0); |
|---|
| 4051 | n/a | |
|---|
| 4052 | n/a | /* Get rid of base marker unless F_ALT */ |
|---|
| 4053 | 36404 | if ((flags & F_ALT) == 0) { |
|---|
| 4054 | n/a | /* Need to skip 0x, 0X or 0. */ |
|---|
| 4055 | 36342 | int skipped = 0; |
|---|
| 4056 | 36342 | switch (type) { |
|---|
| 4057 | n/a | case 'o': |
|---|
| 4058 | 149 | assert(buf[sign] == '0'); |
|---|
| 4059 | n/a | /* If 0 is only digit, leave it alone. */ |
|---|
| 4060 | 149 | if (numdigits > 1) { |
|---|
| 4061 | 52 | skipped = 1; |
|---|
| 4062 | 52 | --numdigits; |
|---|
| 4063 | n/a | } |
|---|
| 4064 | 149 | break; |
|---|
| 4065 | n/a | case 'x': |
|---|
| 4066 | n/a | case 'X': |
|---|
| 4067 | 35003 | assert(buf[sign] == '0'); |
|---|
| 4068 | 35003 | assert(buf[sign + 1] == 'x'); |
|---|
| 4069 | 35003 | skipped = 2; |
|---|
| 4070 | 35003 | numnondigits -= 2; |
|---|
| 4071 | n/a | break; |
|---|
| 4072 | n/a | } |
|---|
| 4073 | 36342 | if (skipped) { |
|---|
| 4074 | 35055 | buf += skipped; |
|---|
| 4075 | 35055 | len -= skipped; |
|---|
| 4076 | 35055 | if (sign) |
|---|
| 4077 | 36 | buf[0] = '-'; |
|---|
| 4078 | n/a | } |
|---|
| 4079 | 36342 | assert(len == numnondigits + numdigits); |
|---|
| 4080 | 36342 | assert(numdigits > 0); |
|---|
| 4081 | n/a | } |
|---|
| 4082 | n/a | |
|---|
| 4083 | n/a | /* Fill with leading zeroes to meet minimum width. */ |
|---|
| 4084 | 36404 | if (prec > numdigits) { |
|---|
| 4085 | n/a | PyObject *r1 = PyString_FromStringAndSize(NULL, |
|---|
| 4086 | 54 | numnondigits + prec); |
|---|
| 4087 | n/a | char *b1; |
|---|
| 4088 | 54 | if (!r1) { |
|---|
| 4089 | 0 | Py_DECREF(result); |
|---|
| 4090 | 0 | return NULL; |
|---|
| 4091 | n/a | } |
|---|
| 4092 | 54 | b1 = PyString_AS_STRING(r1); |
|---|
| 4093 | 98 | for (i = 0; i < numnondigits; ++i) |
|---|
| 4094 | 44 | *b1++ = *buf++; |
|---|
| 4095 | 128 | for (i = 0; i < prec - numdigits; i++) |
|---|
| 4096 | 74 | *b1++ = '0'; |
|---|
| 4097 | 1502 | for (i = 0; i < numdigits; i++) |
|---|
| 4098 | 1448 | *b1++ = *buf++; |
|---|
| 4099 | 54 | *b1 = '\0'; |
|---|
| 4100 | 54 | Py_DECREF(result); |
|---|
| 4101 | 54 | result = r1; |
|---|
| 4102 | 54 | buf = PyString_AS_STRING(result); |
|---|
| 4103 | 54 | len = numnondigits + prec; |
|---|
| 4104 | n/a | } |
|---|
| 4105 | n/a | |
|---|
| 4106 | n/a | /* Fix up case for hex conversions. */ |
|---|
| 4107 | 36404 | if (type == 'X') { |
|---|
| 4108 | n/a | /* Need to convert all lower case letters to upper case. |
|---|
| 4109 | n/a | and need to convert 0x to 0X (and -0x to -0X). */ |
|---|
| 4110 | 472 | for (i = 0; i < len; i++) |
|---|
| 4111 | 450 | if (buf[i] >= 'a' && buf[i] <= 'x') |
|---|
| 4112 | 128 | buf[i] -= 'a'-'A'; |
|---|
| 4113 | n/a | } |
|---|
| 4114 | 36404 | *pbuf = buf; |
|---|
| 4115 | 36404 | *plen = len; |
|---|
| 4116 | 36404 | return result; |
|---|
| 4117 | n/a | } |
|---|
| 4118 | n/a | |
|---|
| 4119 | n/a | Py_LOCAL_INLINE(int) |
|---|
| 4120 | n/a | formatint(char *buf, size_t buflen, int flags, |
|---|
| 4121 | n/a | int prec, int type, PyObject *v) |
|---|
| 4122 | 689802 | { |
|---|
| 4123 | n/a | /* fmt = '%#.' + `prec` + 'l' + `type` |
|---|
| 4124 | n/a | worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
|---|
| 4125 | n/a | + 1 + 1 = 24 */ |
|---|
| 4126 | n/a | char fmt[64]; /* plenty big enough! */ |
|---|
| 4127 | n/a | char *sign; |
|---|
| 4128 | n/a | long x; |
|---|
| 4129 | n/a | |
|---|
| 4130 | 689802 | x = PyInt_AsLong(v); |
|---|
| 4131 | 689802 | if (x == -1 && PyErr_Occurred()) { |
|---|
| 4132 | 0 | PyErr_Format(PyExc_TypeError, "int argument required, not %.200s", |
|---|
| 4133 | n/a | Py_TYPE(v)->tp_name); |
|---|
| 4134 | 0 | return -1; |
|---|
| 4135 | n/a | } |
|---|
| 4136 | 689802 | if (x < 0 && type == 'u') { |
|---|
| 4137 | 0 | type = 'd'; |
|---|
| 4138 | n/a | } |
|---|
| 4139 | 689804 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
|---|
| 4140 | 2 | sign = "-"; |
|---|
| 4141 | n/a | else |
|---|
| 4142 | 689800 | sign = ""; |
|---|
| 4143 | 689802 | if (prec < 0) |
|---|
| 4144 | 624261 | prec = 1; |
|---|
| 4145 | n/a | |
|---|
| 4146 | 689813 | if ((flags & F_ALT) && |
|---|
| 4147 | n/a | (type == 'x' || type == 'X')) { |
|---|
| 4148 | n/a | /* When converting under %#x or %#X, there are a number |
|---|
| 4149 | n/a | * of issues that cause pain: |
|---|
| 4150 | n/a | * - when 0 is being converted, the C standard leaves off |
|---|
| 4151 | n/a | * the '0x' or '0X', which is inconsistent with other |
|---|
| 4152 | n/a | * %#x/%#X conversions and inconsistent with Python's |
|---|
| 4153 | n/a | * hex() function |
|---|
| 4154 | n/a | * - there are platforms that violate the standard and |
|---|
| 4155 | n/a | * convert 0 with the '0x' or '0X' |
|---|
| 4156 | n/a | * (Metrowerks, Compaq Tru64) |
|---|
| 4157 | n/a | * - there are platforms that give '0x' when converting |
|---|
| 4158 | n/a | * under %#X, but convert 0 in accordance with the |
|---|
| 4159 | n/a | * standard (OS/2 EMX) |
|---|
| 4160 | n/a | * |
|---|
| 4161 | n/a | * We can achieve the desired consistency by inserting our |
|---|
| 4162 | n/a | * own '0x' or '0X' prefix, and substituting %x/%X in place |
|---|
| 4163 | n/a | * of %#x/%#X. |
|---|
| 4164 | n/a | * |
|---|
| 4165 | n/a | * Note that this is the same approach as used in |
|---|
| 4166 | n/a | * formatint() in unicodeobject.c |
|---|
| 4167 | n/a | */ |
|---|
| 4168 | 11 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
|---|
| 4169 | n/a | sign, type, prec, type); |
|---|
| 4170 | n/a | } |
|---|
| 4171 | n/a | else { |
|---|
| 4172 | 689791 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
|---|
| 4173 | n/a | sign, (flags&F_ALT) ? "#" : "", |
|---|
| 4174 | n/a | prec, type); |
|---|
| 4175 | n/a | } |
|---|
| 4176 | n/a | |
|---|
| 4177 | n/a | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
|---|
| 4178 | n/a | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
|---|
| 4179 | n/a | */ |
|---|
| 4180 | 689802 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
|---|
| 4181 | 2 | PyErr_SetString(PyExc_OverflowError, |
|---|
| 4182 | n/a | "formatted integer is too long (precision too large?)"); |
|---|
| 4183 | 2 | return -1; |
|---|
| 4184 | n/a | } |
|---|
| 4185 | 689800 | if (sign[0]) |
|---|
| 4186 | 2 | PyOS_snprintf(buf, buflen, fmt, -x); |
|---|
| 4187 | n/a | else |
|---|
| 4188 | 689798 | PyOS_snprintf(buf, buflen, fmt, x); |
|---|
| 4189 | 689800 | return (int)strlen(buf); |
|---|
| 4190 | n/a | } |
|---|
| 4191 | n/a | |
|---|
| 4192 | n/a | Py_LOCAL_INLINE(int) |
|---|
| 4193 | n/a | formatchar(char *buf, size_t buflen, PyObject *v) |
|---|
| 4194 | 109473 | { |
|---|
| 4195 | n/a | /* presume that the buffer is at least 2 characters long */ |
|---|
| 4196 | 109473 | if (PyString_Check(v)) { |
|---|
| 4197 | 36532 | if (!PyArg_Parse(v, "c;%c requires int or char", &buf[0])) |
|---|
| 4198 | 0 | return -1; |
|---|
| 4199 | n/a | } |
|---|
| 4200 | n/a | else { |
|---|
| 4201 | 72941 | if (!PyArg_Parse(v, "b;%c requires int or char", &buf[0])) |
|---|
| 4202 | 10 | return -1; |
|---|
| 4203 | n/a | } |
|---|
| 4204 | 109463 | buf[1] = '\0'; |
|---|
| 4205 | 109463 | return 1; |
|---|
| 4206 | n/a | } |
|---|
| 4207 | n/a | |
|---|
| 4208 | n/a | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
|---|
| 4209 | n/a | |
|---|
| 4210 | n/a | FORMATBUFLEN is the length of the buffer in which the ints & |
|---|
| 4211 | n/a | chars are formatted. XXX This is a magic number. Each formatting |
|---|
| 4212 | n/a | routine does bounds checking to ensure no overflow, but a better |
|---|
| 4213 | n/a | solution may be to malloc a buffer of appropriate size for each |
|---|
| 4214 | n/a | format. For now, the current solution is sufficient. |
|---|
| 4215 | n/a | */ |
|---|
| 4216 | n/a | #define FORMATBUFLEN (size_t)120 |
|---|
| 4217 | n/a | |
|---|
| 4218 | n/a | PyObject * |
|---|
| 4219 | n/a | PyString_Format(PyObject *format, PyObject *args) |
|---|
| 4220 | 872100 | { |
|---|
| 4221 | n/a | char *fmt, *res; |
|---|
| 4222 | n/a | Py_ssize_t arglen, argidx; |
|---|
| 4223 | n/a | Py_ssize_t reslen, rescnt, fmtcnt; |
|---|
| 4224 | 872100 | int args_owned = 0; |
|---|
| 4225 | n/a | PyObject *result, *orig_args; |
|---|
| 4226 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 4227 | n/a | PyObject *v, *w; |
|---|
| 4228 | n/a | #endif |
|---|
| 4229 | 872100 | PyObject *dict = NULL; |
|---|
| 4230 | 872100 | if (format == NULL || !PyString_Check(format) || args == NULL) { |
|---|
| 4231 | 0 | PyErr_BadInternalCall(); |
|---|
| 4232 | 0 | return NULL; |
|---|
| 4233 | n/a | } |
|---|
| 4234 | 872100 | orig_args = args; |
|---|
| 4235 | 872100 | fmt = PyString_AS_STRING(format); |
|---|
| 4236 | 872100 | fmtcnt = PyString_GET_SIZE(format); |
|---|
| 4237 | 872100 | reslen = rescnt = fmtcnt + 100; |
|---|
| 4238 | 872100 | result = PyString_FromStringAndSize((char *)NULL, reslen); |
|---|
| 4239 | 872100 | if (result == NULL) |
|---|
| 4240 | 0 | return NULL; |
|---|
| 4241 | 872100 | res = PyString_AsString(result); |
|---|
| 4242 | 872100 | if (PyTuple_Check(args)) { |
|---|
| 4243 | 191358 | arglen = PyTuple_GET_SIZE(args); |
|---|
| 4244 | 191358 | argidx = 0; |
|---|
| 4245 | n/a | } |
|---|
| 4246 | n/a | else { |
|---|
| 4247 | 680742 | arglen = -1; |
|---|
| 4248 | 680742 | argidx = -2; |
|---|
| 4249 | n/a | } |
|---|
| 4250 | 872100 | if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
|---|
| 4251 | n/a | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
|---|
| 4252 | 7680 | dict = args; |
|---|
| 4253 | 9731912 | while (--fmtcnt >= 0) { |
|---|
| 4254 | 7990334 | if (*fmt != '%') { |
|---|
| 4255 | 6857230 | if (--rescnt < 0) { |
|---|
| 4256 | 182 | rescnt = fmtcnt + 100; |
|---|
| 4257 | 182 | reslen += rescnt; |
|---|
| 4258 | 182 | if (_PyString_Resize(&result, reslen)) |
|---|
| 4259 | 0 | return NULL; |
|---|
| 4260 | 182 | res = PyString_AS_STRING(result) |
|---|
| 4261 | n/a | + reslen - rescnt; |
|---|
| 4262 | 182 | --rescnt; |
|---|
| 4263 | n/a | } |
|---|
| 4264 | 6857230 | *res++ = *fmt++; |
|---|
| 4265 | n/a | } |
|---|
| 4266 | n/a | else { |
|---|
| 4267 | n/a | /* Got a format specifier */ |
|---|
| 4268 | 1133104 | int flags = 0; |
|---|
| 4269 | 1133104 | Py_ssize_t width = -1; |
|---|
| 4270 | 1133104 | int prec = -1; |
|---|
| 4271 | 1133104 | int c = '\0'; |
|---|
| 4272 | n/a | int fill; |
|---|
| 4273 | n/a | int isnumok; |
|---|
| 4274 | 1133104 | PyObject *v = NULL; |
|---|
| 4275 | 1133104 | PyObject *temp = NULL; |
|---|
| 4276 | n/a | char *pbuf; |
|---|
| 4277 | n/a | int sign; |
|---|
| 4278 | n/a | Py_ssize_t len; |
|---|
| 4279 | n/a | char formatbuf[FORMATBUFLEN]; |
|---|
| 4280 | n/a | /* For format{int,char}() */ |
|---|
| 4281 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 4282 | 1133104 | char *fmt_start = fmt; |
|---|
| 4283 | 1133104 | Py_ssize_t argidx_start = argidx; |
|---|
| 4284 | n/a | #endif |
|---|
| 4285 | n/a | |
|---|
| 4286 | 1133104 | fmt++; |
|---|
| 4287 | 1133104 | if (*fmt == '(') { |
|---|
| 4288 | n/a | char *keystart; |
|---|
| 4289 | n/a | Py_ssize_t keylen; |
|---|
| 4290 | n/a | PyObject *key; |
|---|
| 4291 | 9567 | int pcount = 1; |
|---|
| 4292 | n/a | |
|---|
| 4293 | 9567 | if (dict == NULL) { |
|---|
| 4294 | 9 | PyErr_SetString(PyExc_TypeError, |
|---|
| 4295 | n/a | "format requires a mapping"); |
|---|
| 4296 | 9 | goto error; |
|---|
| 4297 | n/a | } |
|---|
| 4298 | 9558 | ++fmt; |
|---|
| 4299 | 9558 | --fmtcnt; |
|---|
| 4300 | 9558 | keystart = fmt; |
|---|
| 4301 | n/a | /* Skip over balanced parentheses */ |
|---|
| 4302 | 84524 | while (pcount > 0 && --fmtcnt >= 0) { |
|---|
| 4303 | 65408 | if (*fmt == ')') |
|---|
| 4304 | 9556 | --pcount; |
|---|
| 4305 | 55852 | else if (*fmt == '(') |
|---|
| 4306 | 3 | ++pcount; |
|---|
| 4307 | 65408 | fmt++; |
|---|
| 4308 | n/a | } |
|---|
| 4309 | 9558 | keylen = fmt - keystart - 1; |
|---|
| 4310 | 9558 | if (fmtcnt < 0 || pcount > 0) { |
|---|
| 4311 | 5 | PyErr_SetString(PyExc_ValueError, |
|---|
| 4312 | n/a | "incomplete format key"); |
|---|
| 4313 | 5 | goto error; |
|---|
| 4314 | n/a | } |
|---|
| 4315 | 9553 | key = PyString_FromStringAndSize(keystart, |
|---|
| 4316 | n/a | keylen); |
|---|
| 4317 | 9553 | if (key == NULL) |
|---|
| 4318 | 0 | goto error; |
|---|
| 4319 | 9553 | if (args_owned) { |
|---|
| 4320 | 3811 | Py_DECREF(args); |
|---|
| 4321 | 3811 | args_owned = 0; |
|---|
| 4322 | n/a | } |
|---|
| 4323 | 9553 | args = PyObject_GetItem(dict, key); |
|---|
| 4324 | 9553 | Py_DECREF(key); |
|---|
| 4325 | 9553 | if (args == NULL) { |
|---|
| 4326 | 1 | goto error; |
|---|
| 4327 | n/a | } |
|---|
| 4328 | 9552 | args_owned = 1; |
|---|
| 4329 | 9552 | arglen = -1; |
|---|
| 4330 | 9552 | argidx = -2; |
|---|
| 4331 | n/a | } |
|---|
| 4332 | 2483941 | while (--fmtcnt >= 0) { |
|---|
| 4333 | 1350847 | switch (c = *fmt++) { |
|---|
| 4334 | 2903 | case '-': flags |= F_LJUST; continue; |
|---|
| 4335 | 12953 | case '+': flags |= F_SIGN; continue; |
|---|
| 4336 | 50 | case ' ': flags |= F_BLANK; continue; |
|---|
| 4337 | 270 | case '#': flags |= F_ALT; continue; |
|---|
| 4338 | 201587 | case '0': flags |= F_ZERO; continue; |
|---|
| 4339 | n/a | } |
|---|
| 4340 | 1133084 | break; |
|---|
| 4341 | n/a | } |
|---|
| 4342 | 1133089 | if (c == '*') { |
|---|
| 4343 | 10135 | v = getnextarg(args, arglen, &argidx); |
|---|
| 4344 | 10135 | if (v == NULL) |
|---|
| 4345 | 0 | goto error; |
|---|
| 4346 | 10135 | if (!PyInt_Check(v)) { |
|---|
| 4347 | 7 | PyErr_SetString(PyExc_TypeError, |
|---|
| 4348 | n/a | "* wants int"); |
|---|
| 4349 | 7 | goto error; |
|---|
| 4350 | n/a | } |
|---|
| 4351 | 10128 | width = PyInt_AsLong(v); |
|---|
| 4352 | 10128 | if (width < 0) { |
|---|
| 4353 | 1 | flags |= F_LJUST; |
|---|
| 4354 | 1 | width = -width; |
|---|
| 4355 | n/a | } |
|---|
| 4356 | 10128 | if (--fmtcnt >= 0) |
|---|
| 4357 | 10128 | c = *fmt++; |
|---|
| 4358 | n/a | } |
|---|
| 4359 | 1122954 | else if (c >= 0 && isdigit(c)) { |
|---|
| 4360 | 390420 | width = c - '0'; |
|---|
| 4361 | 782234 | while (--fmtcnt >= 0) { |
|---|
| 4362 | 391811 | c = Py_CHARMASK(*fmt++); |
|---|
| 4363 | 391811 | if (!isdigit(c)) |
|---|
| 4364 | 390417 | break; |
|---|
| 4365 | 1394 | if ((width*10) / 10 != width) { |
|---|
| 4366 | 0 | PyErr_SetString( |
|---|
| 4367 | n/a | PyExc_ValueError, |
|---|
| 4368 | n/a | "width too big"); |
|---|
| 4369 | 0 | goto error; |
|---|
| 4370 | n/a | } |
|---|
| 4371 | 1394 | width = width*10 + (c - '0'); |
|---|
| 4372 | n/a | } |
|---|
| 4373 | n/a | } |
|---|
| 4374 | 1133082 | if (c == '.') { |
|---|
| 4375 | 87501 | prec = 0; |
|---|
| 4376 | 87501 | if (--fmtcnt >= 0) |
|---|
| 4377 | 87501 | c = *fmt++; |
|---|
| 4378 | 87501 | if (c == '*') { |
|---|
| 4379 | 362 | v = getnextarg(args, arglen, &argidx); |
|---|
| 4380 | 362 | if (v == NULL) |
|---|
| 4381 | 0 | goto error; |
|---|
| 4382 | 362 | if (!PyInt_Check(v)) { |
|---|
| 4383 | 3 | PyErr_SetString( |
|---|
| 4384 | n/a | PyExc_TypeError, |
|---|
| 4385 | n/a | "* wants int"); |
|---|
| 4386 | 3 | goto error; |
|---|
| 4387 | n/a | } |
|---|
| 4388 | 359 | prec = PyInt_AsLong(v); |
|---|
| 4389 | 359 | if (prec < 0) |
|---|
| 4390 | 1 | prec = 0; |
|---|
| 4391 | 359 | if (--fmtcnt >= 0) |
|---|
| 4392 | 359 | c = *fmt++; |
|---|
| 4393 | n/a | } |
|---|
| 4394 | 87139 | else if (c >= 0 && isdigit(c)) { |
|---|
| 4395 | 87127 | prec = c - '0'; |
|---|
| 4396 | 191195 | while (--fmtcnt >= 0) { |
|---|
| 4397 | 104068 | c = Py_CHARMASK(*fmt++); |
|---|
| 4398 | 104068 | if (!isdigit(c)) |
|---|
| 4399 | 87127 | break; |
|---|
| 4400 | 16941 | if ((prec*10) / 10 != prec) { |
|---|
| 4401 | 0 | PyErr_SetString( |
|---|
| 4402 | n/a | PyExc_ValueError, |
|---|
| 4403 | n/a | "prec too big"); |
|---|
| 4404 | 0 | goto error; |
|---|
| 4405 | n/a | } |
|---|
| 4406 | 16941 | prec = prec*10 + (c - '0'); |
|---|
| 4407 | n/a | } |
|---|
| 4408 | n/a | } |
|---|
| 4409 | n/a | } /* prec */ |
|---|
| 4410 | 1133079 | if (fmtcnt >= 0) { |
|---|
| 4411 | 1133071 | if (c == 'h' || c == 'l' || c == 'L') { |
|---|
| 4412 | 3 | if (--fmtcnt >= 0) |
|---|
| 4413 | 3 | c = *fmt++; |
|---|
| 4414 | n/a | } |
|---|
| 4415 | n/a | } |
|---|
| 4416 | 1133079 | if (fmtcnt < 0) { |
|---|
| 4417 | 8 | PyErr_SetString(PyExc_ValueError, |
|---|
| 4418 | n/a | "incomplete format"); |
|---|
| 4419 | 8 | goto error; |
|---|
| 4420 | n/a | } |
|---|
| 4421 | 1133071 | if (c != '%') { |
|---|
| 4422 | 1125364 | v = getnextarg(args, arglen, &argidx); |
|---|
| 4423 | 1125364 | if (v == NULL) |
|---|
| 4424 | 5 | goto error; |
|---|
| 4425 | n/a | } |
|---|
| 4426 | 1133066 | sign = 0; |
|---|
| 4427 | 1133066 | fill = ' '; |
|---|
| 4428 | 1133066 | switch (c) { |
|---|
| 4429 | n/a | case '%': |
|---|
| 4430 | 7707 | pbuf = "%"; |
|---|
| 4431 | 7707 | len = 1; |
|---|
| 4432 | 7707 | break; |
|---|
| 4433 | n/a | case 's': |
|---|
| 4434 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 4435 | 204389 | if (PyUnicode_Check(v)) { |
|---|
| 4436 | 2461 | fmt = fmt_start; |
|---|
| 4437 | 2461 | argidx = argidx_start; |
|---|
| 4438 | 2461 | goto unicode; |
|---|
| 4439 | n/a | } |
|---|
| 4440 | n/a | #endif |
|---|
| 4441 | 201928 | temp = _PyObject_Str(v); |
|---|
| 4442 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 4443 | 201928 | if (temp != NULL && PyUnicode_Check(temp)) { |
|---|
| 4444 | 1 | Py_DECREF(temp); |
|---|
| 4445 | 1 | fmt = fmt_start; |
|---|
| 4446 | 1 | argidx = argidx_start; |
|---|
| 4447 | 1 | goto unicode; |
|---|
| 4448 | n/a | } |
|---|
| 4449 | n/a | #endif |
|---|
| 4450 | n/a | /* Fall through */ |
|---|
| 4451 | n/a | case 'r': |
|---|
| 4452 | 237105 | if (c == 'r') |
|---|
| 4453 | 35178 | temp = PyObject_Repr(v); |
|---|
| 4454 | 237105 | if (temp == NULL) |
|---|
| 4455 | 0 | goto error; |
|---|
| 4456 | 237105 | if (!PyString_Check(temp)) { |
|---|
| 4457 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 4458 | n/a | "%s argument has non-string str()"); |
|---|
| 4459 | 0 | Py_DECREF(temp); |
|---|
| 4460 | 0 | goto error; |
|---|
| 4461 | n/a | } |
|---|
| 4462 | 237105 | pbuf = PyString_AS_STRING(temp); |
|---|
| 4463 | 237105 | len = PyString_GET_SIZE(temp); |
|---|
| 4464 | 237105 | if (prec >= 0 && len > prec) |
|---|
| 4465 | 588 | len = prec; |
|---|
| 4466 | 237105 | break; |
|---|
| 4467 | n/a | case 'i': |
|---|
| 4468 | n/a | case 'd': |
|---|
| 4469 | n/a | case 'u': |
|---|
| 4470 | n/a | case 'o': |
|---|
| 4471 | n/a | case 'x': |
|---|
| 4472 | n/a | case 'X': |
|---|
| 4473 | 726204 | if (c == 'i') |
|---|
| 4474 | 1922 | c = 'd'; |
|---|
| 4475 | 726204 | isnumok = 0; |
|---|
| 4476 | 726204 | if (PyNumber_Check(v)) { |
|---|
| 4477 | 726102 | PyObject *iobj=NULL; |
|---|
| 4478 | n/a | |
|---|
| 4479 | 1451937 | if (PyInt_Check(v) || (PyLong_Check(v))) { |
|---|
| 4480 | 725835 | iobj = v; |
|---|
| 4481 | 725835 | Py_INCREF(iobj); |
|---|
| 4482 | n/a | } |
|---|
| 4483 | n/a | else { |
|---|
| 4484 | 267 | iobj = PyNumber_Int(v); |
|---|
| 4485 | 267 | if (iobj==NULL) iobj = PyNumber_Long(v); |
|---|
| 4486 | n/a | } |
|---|
| 4487 | 726102 | if (iobj!=NULL) { |
|---|
| 4488 | 726099 | if (PyInt_Check(iobj)) { |
|---|
| 4489 | 689802 | isnumok = 1; |
|---|
| 4490 | 689802 | pbuf = formatbuf; |
|---|
| 4491 | 689802 | len = formatint(pbuf, |
|---|
| 4492 | n/a | sizeof(formatbuf), |
|---|
| 4493 | n/a | flags, prec, c, iobj); |
|---|
| 4494 | 689802 | Py_DECREF(iobj); |
|---|
| 4495 | 689802 | if (len < 0) |
|---|
| 4496 | 2 | goto error; |
|---|
| 4497 | 689800 | sign = 1; |
|---|
| 4498 | n/a | } |
|---|
| 4499 | 36297 | else if (PyLong_Check(iobj)) { |
|---|
| 4500 | n/a | int ilen; |
|---|
| 4501 | n/a | |
|---|
| 4502 | 36297 | isnumok = 1; |
|---|
| 4503 | 36297 | temp = _PyString_FormatLong(iobj, flags, |
|---|
| 4504 | n/a | prec, c, &pbuf, &ilen); |
|---|
| 4505 | 36297 | Py_DECREF(iobj); |
|---|
| 4506 | 36297 | len = ilen; |
|---|
| 4507 | 36297 | if (!temp) |
|---|
| 4508 | 1 | goto error; |
|---|
| 4509 | 36296 | sign = 1; |
|---|
| 4510 | n/a | } |
|---|
| 4511 | n/a | else { |
|---|
| 4512 | 0 | Py_DECREF(iobj); |
|---|
| 4513 | n/a | } |
|---|
| 4514 | n/a | } |
|---|
| 4515 | n/a | } |
|---|
| 4516 | 726201 | if (!isnumok) { |
|---|
| 4517 | 105 | PyErr_Format(PyExc_TypeError, |
|---|
| 4518 | n/a | "%%%c format: a number is required, " |
|---|
| 4519 | n/a | "not %.200s", c, Py_TYPE(v)->tp_name); |
|---|
| 4520 | 105 | goto error; |
|---|
| 4521 | n/a | } |
|---|
| 4522 | 726096 | if (flags & F_ZERO) |
|---|
| 4523 | 200581 | fill = '0'; |
|---|
| 4524 | 726096 | break; |
|---|
| 4525 | n/a | case 'e': |
|---|
| 4526 | n/a | case 'E': |
|---|
| 4527 | n/a | case 'f': |
|---|
| 4528 | n/a | case 'F': |
|---|
| 4529 | n/a | case 'g': |
|---|
| 4530 | n/a | case 'G': |
|---|
| 4531 | 50112 | temp = formatfloat(v, flags, prec, c); |
|---|
| 4532 | 50112 | if (temp == NULL) |
|---|
| 4533 | 1 | goto error; |
|---|
| 4534 | 50111 | pbuf = PyString_AS_STRING(temp); |
|---|
| 4535 | 50111 | len = PyString_GET_SIZE(temp); |
|---|
| 4536 | 50111 | sign = 1; |
|---|
| 4537 | 50111 | if (flags & F_ZERO) |
|---|
| 4538 | 1006 | fill = '0'; |
|---|
| 4539 | 50111 | break; |
|---|
| 4540 | n/a | case 'c': |
|---|
| 4541 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 4542 | 109475 | if (PyUnicode_Check(v)) { |
|---|
| 4543 | 2 | fmt = fmt_start; |
|---|
| 4544 | 2 | argidx = argidx_start; |
|---|
| 4545 | 2 | goto unicode; |
|---|
| 4546 | n/a | } |
|---|
| 4547 | n/a | #endif |
|---|
| 4548 | 109473 | pbuf = formatbuf; |
|---|
| 4549 | 109473 | len = formatchar(pbuf, sizeof(formatbuf), v); |
|---|
| 4550 | 109473 | if (len < 0) |
|---|
| 4551 | 10 | goto error; |
|---|
| 4552 | 109463 | break; |
|---|
| 4553 | n/a | default: |
|---|
| 4554 | 1 | PyErr_Format(PyExc_ValueError, |
|---|
| 4555 | n/a | "unsupported format character '%c' (0x%x) " |
|---|
| 4556 | n/a | "at index %zd", |
|---|
| 4557 | n/a | c, c, |
|---|
| 4558 | n/a | (Py_ssize_t)(fmt - 1 - |
|---|
| 4559 | n/a | PyString_AsString(format))); |
|---|
| 4560 | 1 | goto error; |
|---|
| 4561 | n/a | } |
|---|
| 4562 | 1130482 | if (sign) { |
|---|
| 4563 | 783895 | if (*pbuf == '-' || *pbuf == '+') { |
|---|
| 4564 | 7688 | sign = *pbuf++; |
|---|
| 4565 | 7688 | len--; |
|---|
| 4566 | n/a | } |
|---|
| 4567 | 768519 | else if (flags & F_SIGN) |
|---|
| 4568 | 6403 | sign = '+'; |
|---|
| 4569 | 762116 | else if (flags & F_BLANK) |
|---|
| 4570 | 38 | sign = ' '; |
|---|
| 4571 | n/a | else |
|---|
| 4572 | 762078 | sign = 0; |
|---|
| 4573 | n/a | } |
|---|
| 4574 | 1130482 | if (width < len) |
|---|
| 4575 | 731333 | width = len; |
|---|
| 4576 | 1130482 | if (rescnt - (sign != 0) < width) { |
|---|
| 4577 | 6650 | reslen -= rescnt; |
|---|
| 4578 | 6650 | rescnt = width + fmtcnt + 100; |
|---|
| 4579 | 6650 | reslen += rescnt; |
|---|
| 4580 | 6650 | if (reslen < 0) { |
|---|
| 4581 | 0 | Py_DECREF(result); |
|---|
| 4582 | 0 | Py_XDECREF(temp); |
|---|
| 4583 | 0 | return PyErr_NoMemory(); |
|---|
| 4584 | n/a | } |
|---|
| 4585 | 6650 | if (_PyString_Resize(&result, reslen)) { |
|---|
| 4586 | 0 | Py_XDECREF(temp); |
|---|
| 4587 | 0 | return NULL; |
|---|
| 4588 | n/a | } |
|---|
| 4589 | 6650 | res = PyString_AS_STRING(result) |
|---|
| 4590 | n/a | + reslen - rescnt; |
|---|
| 4591 | n/a | } |
|---|
| 4592 | 1130482 | if (sign) { |
|---|
| 4593 | 14129 | if (fill != ' ') |
|---|
| 4594 | 550 | *res++ = sign; |
|---|
| 4595 | 14129 | rescnt--; |
|---|
| 4596 | 14129 | if (width > len) |
|---|
| 4597 | 151 | width--; |
|---|
| 4598 | n/a | } |
|---|
| 4599 | 1130482 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
|---|
| 4600 | 26 | assert(pbuf[0] == '0'); |
|---|
| 4601 | 26 | assert(pbuf[1] == c); |
|---|
| 4602 | 26 | if (fill != ' ') { |
|---|
| 4603 | 1 | *res++ = *pbuf++; |
|---|
| 4604 | 1 | *res++ = *pbuf++; |
|---|
| 4605 | n/a | } |
|---|
| 4606 | 26 | rescnt -= 2; |
|---|
| 4607 | 26 | width -= 2; |
|---|
| 4608 | 26 | if (width < 0) |
|---|
| 4609 | 0 | width = 0; |
|---|
| 4610 | 26 | len -= 2; |
|---|
| 4611 | n/a | } |
|---|
| 4612 | 1130482 | if (width > len && !(flags & F_LJUST)) { |
|---|
| 4613 | n/a | do { |
|---|
| 4614 | 1329517 | --rescnt; |
|---|
| 4615 | 1329517 | *res++ = fill; |
|---|
| 4616 | 1329517 | } while (--width > len); |
|---|
| 4617 | n/a | } |
|---|
| 4618 | 1130482 | if (fill == ' ') { |
|---|
| 4619 | 928895 | if (sign) |
|---|
| 4620 | 13579 | *res++ = sign; |
|---|
| 4621 | 928895 | if ((flags & F_ALT) && |
|---|
| 4622 | n/a | (c == 'x' || c == 'X')) { |
|---|
| 4623 | 25 | assert(pbuf[0] == '0'); |
|---|
| 4624 | 25 | assert(pbuf[1] == c); |
|---|
| 4625 | 25 | *res++ = *pbuf++; |
|---|
| 4626 | 25 | *res++ = *pbuf++; |
|---|
| 4627 | n/a | } |
|---|
| 4628 | n/a | } |
|---|
| 4629 | 1130482 | Py_MEMCPY(res, pbuf, len); |
|---|
| 4630 | 1130482 | res += len; |
|---|
| 4631 | 1130482 | rescnt -= len; |
|---|
| 4632 | 2276858 | while (--width >= len) { |
|---|
| 4633 | 15894 | --rescnt; |
|---|
| 4634 | 15894 | *res++ = ' '; |
|---|
| 4635 | n/a | } |
|---|
| 4636 | 1130482 | if (dict && (argidx < arglen) && c != '%') { |
|---|
| 4637 | 0 | PyErr_SetString(PyExc_TypeError, |
|---|
| 4638 | n/a | "not all arguments converted during string formatting"); |
|---|
| 4639 | 0 | Py_XDECREF(temp); |
|---|
| 4640 | 0 | goto error; |
|---|
| 4641 | n/a | } |
|---|
| 4642 | 1130482 | Py_XDECREF(temp); |
|---|
| 4643 | n/a | } /* '%' */ |
|---|
| 4644 | n/a | } /* until end */ |
|---|
| 4645 | 869478 | if (argidx < arglen && !dict) { |
|---|
| 4646 | 3 | PyErr_SetString(PyExc_TypeError, |
|---|
| 4647 | n/a | "not all arguments converted during string formatting"); |
|---|
| 4648 | 3 | goto error; |
|---|
| 4649 | n/a | } |
|---|
| 4650 | 869475 | if (args_owned) { |
|---|
| 4651 | 5731 | Py_DECREF(args); |
|---|
| 4652 | n/a | } |
|---|
| 4653 | 869475 | if (_PyString_Resize(&result, reslen - rescnt)) |
|---|
| 4654 | 0 | return NULL; |
|---|
| 4655 | 869475 | return result; |
|---|
| 4656 | n/a | |
|---|
| 4657 | n/a | #ifdef Py_USING_UNICODE |
|---|
| 4658 | 2464 | unicode: |
|---|
| 4659 | 2464 | if (args_owned) { |
|---|
| 4660 | 8 | Py_DECREF(args); |
|---|
| 4661 | 8 | args_owned = 0; |
|---|
| 4662 | n/a | } |
|---|
| 4663 | n/a | /* Fiddle args right (remove the first argidx arguments) */ |
|---|
| 4664 | 2584 | if (PyTuple_Check(orig_args) && argidx > 0) { |
|---|
| 4665 | n/a | PyObject *v; |
|---|
| 4666 | 120 | Py_ssize_t n = PyTuple_GET_SIZE(orig_args) - argidx; |
|---|
| 4667 | 120 | v = PyTuple_New(n); |
|---|
| 4668 | 120 | if (v == NULL) |
|---|
| 4669 | 0 | goto error; |
|---|
| 4670 | 377 | while (--n >= 0) { |
|---|
| 4671 | 137 | PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx); |
|---|
| 4672 | 137 | Py_INCREF(w); |
|---|
| 4673 | 137 | PyTuple_SET_ITEM(v, n, w); |
|---|
| 4674 | n/a | } |
|---|
| 4675 | 120 | args = v; |
|---|
| 4676 | n/a | } else { |
|---|
| 4677 | 2344 | Py_INCREF(orig_args); |
|---|
| 4678 | 2344 | args = orig_args; |
|---|
| 4679 | n/a | } |
|---|
| 4680 | 2464 | args_owned = 1; |
|---|
| 4681 | n/a | /* Take what we have of the result and let the Unicode formatting |
|---|
| 4682 | n/a | function format the rest of the input. */ |
|---|
| 4683 | 2464 | rescnt = res - PyString_AS_STRING(result); |
|---|
| 4684 | 2464 | if (_PyString_Resize(&result, rescnt)) |
|---|
| 4685 | 0 | goto error; |
|---|
| 4686 | 2464 | fmtcnt = PyString_GET_SIZE(format) - \ |
|---|
| 4687 | n/a | (fmt - PyString_AS_STRING(format)); |
|---|
| 4688 | 2464 | format = PyUnicode_Decode(fmt, fmtcnt, NULL, NULL); |
|---|
| 4689 | 2464 | if (format == NULL) |
|---|
| 4690 | 0 | goto error; |
|---|
| 4691 | 2464 | v = PyUnicode_Format(format, args); |
|---|
| 4692 | 2464 | Py_DECREF(format); |
|---|
| 4693 | 2464 | if (v == NULL) |
|---|
| 4694 | 1 | goto error; |
|---|
| 4695 | n/a | /* Paste what we have (result) to what the Unicode formatting |
|---|
| 4696 | n/a | function returned (v) and return the result (or error) */ |
|---|
| 4697 | 2463 | w = PyUnicode_Concat(result, v); |
|---|
| 4698 | 2463 | Py_DECREF(result); |
|---|
| 4699 | 2463 | Py_DECREF(v); |
|---|
| 4700 | 2463 | Py_DECREF(args); |
|---|
| 4701 | 2463 | return w; |
|---|
| 4702 | n/a | #endif /* Py_USING_UNICODE */ |
|---|
| 4703 | n/a | |
|---|
| 4704 | 162 | error: |
|---|
| 4705 | 162 | Py_DECREF(result); |
|---|
| 4706 | 162 | if (args_owned) { |
|---|
| 4707 | 3 | Py_DECREF(args); |
|---|
| 4708 | n/a | } |
|---|
| 4709 | 162 | return NULL; |
|---|
| 4710 | n/a | } |
|---|
| 4711 | n/a | |
|---|
| 4712 | n/a | void |
|---|
| 4713 | n/a | PyString_InternInPlace(PyObject **p) |
|---|
| 4714 | 25398699 | { |
|---|
| 4715 | 25398699 | register PyStringObject *s = (PyStringObject *)(*p); |
|---|
| 4716 | n/a | PyObject *t; |
|---|
| 4717 | 25398699 | if (s == NULL || !PyString_Check(s)) |
|---|
| 4718 | 0 | Py_FatalError("PyString_InternInPlace: strings only please!"); |
|---|
| 4719 | n/a | /* If it's a string subclass, we don't really know what putting |
|---|
| 4720 | n/a | it in the interned dict might do. */ |
|---|
| 4721 | 25398699 | if (!PyString_CheckExact(s)) |
|---|
| 4722 | 1 | return; |
|---|
| 4723 | 25398698 | if (PyString_CHECK_INTERNED(s)) |
|---|
| 4724 | 13371891 | return; |
|---|
| 4725 | 12026807 | if (interned == NULL) { |
|---|
| 4726 | 293 | interned = PyDict_New(); |
|---|
| 4727 | 293 | if (interned == NULL) { |
|---|
| 4728 | 0 | PyErr_Clear(); /* Don't leave an exception */ |
|---|
| 4729 | 0 | return; |
|---|
| 4730 | n/a | } |
|---|
| 4731 | n/a | } |
|---|
| 4732 | 12026807 | t = PyDict_GetItem(interned, (PyObject *)s); |
|---|
| 4733 | 12026807 | if (t) { |
|---|
| 4734 | 11022097 | Py_INCREF(t); |
|---|
| 4735 | 11022097 | Py_DECREF(*p); |
|---|
| 4736 | 11022097 | *p = t; |
|---|
| 4737 | 11022097 | return; |
|---|
| 4738 | n/a | } |
|---|
| 4739 | n/a | |
|---|
| 4740 | 1004710 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
|---|
| 4741 | 0 | PyErr_Clear(); |
|---|
| 4742 | 0 | return; |
|---|
| 4743 | n/a | } |
|---|
| 4744 | n/a | /* The two references in interned are not counted by refcnt. |
|---|
| 4745 | n/a | The string deallocator will take care of this */ |
|---|
| 4746 | 1004710 | Py_REFCNT(s) -= 2; |
|---|
| 4747 | 1004710 | PyString_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
|---|
| 4748 | n/a | } |
|---|
| 4749 | n/a | |
|---|
| 4750 | n/a | void |
|---|
| 4751 | n/a | PyString_InternImmortal(PyObject **p) |
|---|
| 4752 | 0 | { |
|---|
| 4753 | 0 | PyString_InternInPlace(p); |
|---|
| 4754 | 0 | if (PyString_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
|---|
| 4755 | 0 | PyString_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
|---|
| 4756 | 0 | Py_INCREF(*p); |
|---|
| 4757 | n/a | } |
|---|
| 4758 | 0 | } |
|---|
| 4759 | n/a | |
|---|
| 4760 | n/a | |
|---|
| 4761 | n/a | PyObject * |
|---|
| 4762 | n/a | PyString_InternFromString(const char *cp) |
|---|
| 4763 | 3674460 | { |
|---|
| 4764 | 3674460 | PyObject *s = PyString_FromString(cp); |
|---|
| 4765 | 3674460 | if (s == NULL) |
|---|
| 4766 | 0 | return NULL; |
|---|
| 4767 | 3674460 | PyString_InternInPlace(&s); |
|---|
| 4768 | 3674460 | return s; |
|---|
| 4769 | n/a | } |
|---|
| 4770 | n/a | |
|---|
| 4771 | n/a | void |
|---|
| 4772 | n/a | PyString_Fini(void) |
|---|
| 4773 | 293 | { |
|---|
| 4774 | n/a | int i; |
|---|
| 4775 | 75301 | for (i = 0; i < UCHAR_MAX + 1; i++) { |
|---|
| 4776 | 75008 | Py_XDECREF(characters[i]); |
|---|
| 4777 | 75008 | characters[i] = NULL; |
|---|
| 4778 | n/a | } |
|---|
| 4779 | 293 | Py_XDECREF(nullstring); |
|---|
| 4780 | 293 | nullstring = NULL; |
|---|
| 4781 | 293 | } |
|---|
| 4782 | n/a | |
|---|
| 4783 | n/a | void _Py_ReleaseInternedStrings(void) |
|---|
| 4784 | 0 | { |
|---|
| 4785 | n/a | PyObject *keys; |
|---|
| 4786 | n/a | PyStringObject *s; |
|---|
| 4787 | n/a | Py_ssize_t i, n; |
|---|
| 4788 | 0 | Py_ssize_t immortal_size = 0, mortal_size = 0; |
|---|
| 4789 | n/a | |
|---|
| 4790 | 0 | if (interned == NULL || !PyDict_Check(interned)) |
|---|
| 4791 | 0 | return; |
|---|
| 4792 | 0 | keys = PyDict_Keys(interned); |
|---|
| 4793 | 0 | if (keys == NULL || !PyList_Check(keys)) { |
|---|
| 4794 | 0 | PyErr_Clear(); |
|---|
| 4795 | 0 | return; |
|---|
| 4796 | n/a | } |
|---|
| 4797 | n/a | |
|---|
| 4798 | n/a | /* Since _Py_ReleaseInternedStrings() is intended to help a leak |
|---|
| 4799 | n/a | detector, interned strings are not forcibly deallocated; rather, we |
|---|
| 4800 | n/a | give them their stolen references back, and then clear and DECREF |
|---|
| 4801 | n/a | the interned dict. */ |
|---|
| 4802 | n/a | |
|---|
| 4803 | 0 | n = PyList_GET_SIZE(keys); |
|---|
| 4804 | 0 | fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
|---|
| 4805 | n/a | n); |
|---|
| 4806 | 0 | for (i = 0; i < n; i++) { |
|---|
| 4807 | 0 | s = (PyStringObject *) PyList_GET_ITEM(keys, i); |
|---|
| 4808 | 0 | switch (s->ob_sstate) { |
|---|
| 4809 | n/a | case SSTATE_NOT_INTERNED: |
|---|
| 4810 | n/a | /* XXX Shouldn't happen */ |
|---|
| 4811 | 0 | break; |
|---|
| 4812 | n/a | case SSTATE_INTERNED_IMMORTAL: |
|---|
| 4813 | 0 | Py_REFCNT(s) += 1; |
|---|
| 4814 | 0 | immortal_size += Py_SIZE(s); |
|---|
| 4815 | 0 | break; |
|---|
| 4816 | n/a | case SSTATE_INTERNED_MORTAL: |
|---|
| 4817 | 0 | Py_REFCNT(s) += 2; |
|---|
| 4818 | 0 | mortal_size += Py_SIZE(s); |
|---|
| 4819 | 0 | break; |
|---|
| 4820 | n/a | default: |
|---|
| 4821 | 0 | Py_FatalError("Inconsistent interned string state."); |
|---|
| 4822 | n/a | } |
|---|
| 4823 | 0 | s->ob_sstate = SSTATE_NOT_INTERNED; |
|---|
| 4824 | n/a | } |
|---|
| 4825 | 0 | fprintf(stderr, "total size of all interned strings: " |
|---|
| 4826 | n/a | "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
|---|
| 4827 | n/a | "mortal/immortal\n", mortal_size, immortal_size); |
|---|
| 4828 | 0 | Py_DECREF(keys); |
|---|
| 4829 | 0 | PyDict_Clear(interned); |
|---|
| 4830 | 0 | Py_DECREF(interned); |
|---|
| 4831 | 0 | interned = NULL; |
|---|
| 4832 | n/a | } |
|---|