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