Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* String object implementation */ |
| 2 | |
Martin v. Löwis | 5cb6936 | 2006-04-14 09:08:42 +0000 | [diff] [blame] | 3 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 6 | #include <ctype.h> |
| 7 | |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 8 | #undef USE_INLINE /* XXX - set via configure? */ |
| 9 | |
| 10 | #if defined(_MSC_VER) /* this is taken from _sre.c */ |
| 11 | #pragma warning(disable: 4710) |
| 12 | /* fastest possible local call under MSVC */ |
| 13 | #define LOCAL(type) static __inline type __fastcall |
| 14 | #elif defined(USE_INLINE) |
| 15 | #define LOCAL(type) static inline type |
| 16 | #else |
| 17 | #define LOCAL(type) static type |
| 18 | #endif |
| 19 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 20 | #ifdef COUNT_ALLOCS |
| 21 | int null_strings, one_strings; |
| 22 | #endif |
| 23 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 24 | static PyStringObject *characters[UCHAR_MAX + 1]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 25 | static PyStringObject *nullstring; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 27 | /* This dictionary holds all interned strings. Note that references to |
| 28 | strings in this dictionary are *not* counted in the string's ob_refcnt. |
| 29 | When the interned string reaches a refcnt of 0 the string deallocation |
| 30 | function will delete the reference from this dictionary. |
| 31 | |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 32 | Another way to look at this is that to say that the actual reference |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 33 | count of a string is: s->ob_refcnt + (s->ob_sstate?2:0) |
| 34 | */ |
| 35 | static PyObject *interned; |
| 36 | |
| 37 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 38 | /* |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 39 | For both PyString_FromString() and PyString_FromStringAndSize(), the |
| 40 | 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] | 41 | null terminating character. |
Martin v. Löwis | d132750 | 2001-12-02 18:09:41 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 43 | 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] | 44 | string containing exactly `size' bytes. |
Martin v. Löwis | d132750 | 2001-12-02 18:09:41 +0000 | [diff] [blame] | 45 | |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 46 | For PyString_FromStringAndSize(), the parameter the parameter `str' is |
| 47 | either NULL or else points to a string containing at least `size' bytes. |
| 48 | For PyString_FromStringAndSize(), the string in the `str' parameter does |
| 49 | not have to be null-terminated. (Therefore it is safe to construct a |
| 50 | substring by calling `PyString_FromStringAndSize(origstring, substrlen)'.) |
| 51 | If `str' is NULL then PyString_FromStringAndSize() will allocate `size+1' |
| 52 | bytes (setting the last byte to the null terminating character) and you can |
| 53 | fill in the data yourself. If `str' is non-NULL then the resulting |
| 54 | PyString object must be treated as immutable and you must not fill in nor |
| 55 | alter the data yourself, since the strings may be shared. |
Martin v. Löwis | 8f1ea71 | 2001-12-03 08:24:52 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 57 | The PyObject member `op->ob_size', which denotes the number of "extra |
| 58 | items" in a variable-size object, will contain the number of bytes |
| 59 | allocated for string data, not counting the null terminating character. It |
| 60 | is therefore equal to the equal to the `size' parameter (for |
| 61 | PyString_FromStringAndSize()) or the length of the string in the `str' |
| 62 | parameter (for PyString_FromString()). |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 63 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 64 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 65 | PyString_FromStringAndSize(const char *str, Py_ssize_t size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 67 | register PyStringObject *op; |
Michael W. Hudson | faa7648 | 2005-01-31 17:09:25 +0000 | [diff] [blame] | 68 | assert(size >= 0); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 69 | if (size == 0 && (op = nullstring) != NULL) { |
| 70 | #ifdef COUNT_ALLOCS |
| 71 | null_strings++; |
| 72 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 73 | Py_INCREF(op); |
| 74 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 75 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 76 | if (size == 1 && str != NULL && |
| 77 | (op = characters[*str & UCHAR_MAX]) != NULL) |
| 78 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 79 | #ifdef COUNT_ALLOCS |
| 80 | one_strings++; |
| 81 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 82 | Py_INCREF(op); |
| 83 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 84 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 86 | /* Inline PyObject_NewVar */ |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 87 | op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 88 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 89 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 90 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 91 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 92 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 93 | if (str != NULL) |
| 94 | memcpy(op->ob_sval, str, size); |
| 95 | op->ob_sval[size] = '\0'; |
Tim Peters | 8deda70 | 2002-03-30 10:06:07 +0000 | [diff] [blame] | 96 | /* share short strings */ |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 97 | if (size == 0) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 98 | PyObject *t = (PyObject *)op; |
| 99 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 100 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 101 | nullstring = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 102 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 103 | } else if (size == 1 && str != NULL) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 104 | PyObject *t = (PyObject *)op; |
| 105 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 106 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 107 | characters[*str & UCHAR_MAX] = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 108 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 109 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 110 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 113 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 114 | PyString_FromString(const char *str) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 115 | { |
Tim Peters | 62de65b | 2001-12-06 20:29:32 +0000 | [diff] [blame] | 116 | register size_t size; |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 117 | register PyStringObject *op; |
Tim Peters | 62de65b | 2001-12-06 20:29:32 +0000 | [diff] [blame] | 118 | |
| 119 | assert(str != NULL); |
| 120 | size = strlen(str); |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 121 | if (size > PY_SSIZE_T_MAX) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 122 | PyErr_SetString(PyExc_OverflowError, |
| 123 | "string is too long for a Python string"); |
| 124 | return NULL; |
| 125 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 126 | if (size == 0 && (op = nullstring) != NULL) { |
| 127 | #ifdef COUNT_ALLOCS |
| 128 | null_strings++; |
| 129 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 130 | Py_INCREF(op); |
| 131 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 132 | } |
| 133 | if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
| 134 | #ifdef COUNT_ALLOCS |
| 135 | one_strings++; |
| 136 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 137 | Py_INCREF(op); |
| 138 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 139 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 140 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 141 | /* Inline PyObject_NewVar */ |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 142 | op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 143 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 144 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 145 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 146 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 147 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Guido van Rossum | 169192e | 2001-12-10 15:45:54 +0000 | [diff] [blame] | 148 | memcpy(op->ob_sval, str, size+1); |
Tim Peters | 8deda70 | 2002-03-30 10:06:07 +0000 | [diff] [blame] | 149 | /* share short strings */ |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 150 | if (size == 0) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 151 | PyObject *t = (PyObject *)op; |
| 152 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 153 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 154 | nullstring = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 155 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 156 | } else if (size == 1) { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 157 | PyObject *t = (PyObject *)op; |
| 158 | PyString_InternInPlace(&t); |
Tim Peters | 4862ab7 | 2001-05-09 08:43:21 +0000 | [diff] [blame] | 159 | op = (PyStringObject *)t; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 160 | characters[*str & UCHAR_MAX] = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 161 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 162 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 163 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 166 | PyObject * |
| 167 | PyString_FromFormatV(const char *format, va_list vargs) |
| 168 | { |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 169 | va_list count; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 170 | Py_ssize_t n = 0; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 171 | const char* f; |
| 172 | char *s; |
| 173 | PyObject* string; |
| 174 | |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 175 | #ifdef VA_LIST_IS_ARRAY |
| 176 | memcpy(count, vargs, sizeof(va_list)); |
| 177 | #else |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 178 | #ifdef __va_copy |
| 179 | __va_copy(count, vargs); |
| 180 | #else |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 181 | count = vargs; |
| 182 | #endif |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 183 | #endif |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 184 | /* step 1: figure out how large a buffer we need */ |
| 185 | for (f = format; *f; f++) { |
| 186 | if (*f == '%') { |
| 187 | const char* p = f; |
| 188 | while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
| 189 | ; |
| 190 | |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 191 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 192 | * they don't affect the amount of space we reserve. |
| 193 | */ |
| 194 | if ((*f == 'l' || *f == 'z') && |
| 195 | (f[1] == 'd' || f[1] == 'u')) |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 196 | ++f; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 197 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 198 | switch (*f) { |
| 199 | case 'c': |
| 200 | (void)va_arg(count, int); |
| 201 | /* fall through... */ |
| 202 | case '%': |
| 203 | n++; |
| 204 | break; |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 205 | case 'd': case 'u': case 'i': case 'x': |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 206 | (void) va_arg(count, int); |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 207 | /* 20 bytes is enough to hold a 64-bit |
| 208 | integer. Decimal takes the most space. |
| 209 | This isn't enough for octal. */ |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 210 | n += 20; |
| 211 | break; |
| 212 | case 's': |
| 213 | s = va_arg(count, char*); |
| 214 | n += strlen(s); |
| 215 | break; |
| 216 | case 'p': |
| 217 | (void) va_arg(count, int); |
| 218 | /* maximum 64-bit pointer representation: |
| 219 | * 0xffffffffffffffff |
| 220 | * so 19 characters is enough. |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 221 | * XXX I count 18 -- what's the extra for? |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 222 | */ |
| 223 | n += 19; |
| 224 | break; |
| 225 | default: |
| 226 | /* if we stumble upon an unknown |
| 227 | formatting code, copy the rest of |
| 228 | the format string to the output |
| 229 | string. (we cannot just skip the |
| 230 | code, since there's no way to know |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 231 | what's in the argument list) */ |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 232 | n += strlen(p); |
| 233 | goto expand; |
| 234 | } |
| 235 | } else |
| 236 | n++; |
| 237 | } |
| 238 | expand: |
| 239 | /* step 2: fill the buffer */ |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 240 | /* Since we've analyzed how much space we need for the worst case, |
| 241 | use sprintf directly instead of the slower PyOS_snprintf. */ |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 242 | string = PyString_FromStringAndSize(NULL, n); |
| 243 | if (!string) |
| 244 | return NULL; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 245 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 246 | s = PyString_AsString(string); |
| 247 | |
| 248 | for (f = format; *f; f++) { |
| 249 | if (*f == '%') { |
| 250 | const char* p = f++; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 251 | Py_ssize_t i; |
| 252 | int longflag = 0; |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 253 | int size_tflag = 0; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 254 | /* parse the width.precision part (we're only |
| 255 | interested in the precision value, if any) */ |
| 256 | n = 0; |
| 257 | while (isdigit(Py_CHARMASK(*f))) |
| 258 | n = (n*10) + *f++ - '0'; |
| 259 | if (*f == '.') { |
| 260 | f++; |
| 261 | n = 0; |
| 262 | while (isdigit(Py_CHARMASK(*f))) |
| 263 | n = (n*10) + *f++ - '0'; |
| 264 | } |
| 265 | while (*f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
| 266 | f++; |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 267 | /* handle the long flag, but only for %ld and %lu. |
| 268 | others can be added when necessary. */ |
| 269 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 270 | longflag = 1; |
| 271 | ++f; |
| 272 | } |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 273 | /* handle the size_t flag. */ |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 274 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 275 | size_tflag = 1; |
| 276 | ++f; |
| 277 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 278 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 279 | switch (*f) { |
| 280 | case 'c': |
| 281 | *s++ = va_arg(vargs, int); |
| 282 | break; |
| 283 | case 'd': |
| 284 | if (longflag) |
| 285 | sprintf(s, "%ld", va_arg(vargs, long)); |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 286 | else if (size_tflag) |
Martin v. Löwis | 822f34a | 2006-05-13 13:34:04 +0000 | [diff] [blame] | 287 | sprintf(s, "%" PY_FORMAT_SIZE_T "d", |
| 288 | va_arg(vargs, Py_ssize_t)); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 289 | else |
| 290 | sprintf(s, "%d", va_arg(vargs, int)); |
| 291 | s += strlen(s); |
| 292 | break; |
Tim Peters | 8931ff1 | 2006-05-13 23:28:20 +0000 | [diff] [blame] | 293 | case 'u': |
| 294 | if (longflag) |
| 295 | sprintf(s, "%lu", |
| 296 | va_arg(vargs, unsigned long)); |
| 297 | else if (size_tflag) |
| 298 | sprintf(s, "%" PY_FORMAT_SIZE_T "u", |
| 299 | va_arg(vargs, size_t)); |
| 300 | else |
| 301 | sprintf(s, "%u", |
| 302 | va_arg(vargs, unsigned int)); |
| 303 | s += strlen(s); |
| 304 | break; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 305 | case 'i': |
| 306 | sprintf(s, "%i", va_arg(vargs, int)); |
| 307 | s += strlen(s); |
| 308 | break; |
| 309 | case 'x': |
| 310 | sprintf(s, "%x", va_arg(vargs, int)); |
| 311 | s += strlen(s); |
| 312 | break; |
| 313 | case 's': |
| 314 | p = va_arg(vargs, char*); |
| 315 | i = strlen(p); |
| 316 | if (n > 0 && i > n) |
| 317 | i = n; |
| 318 | memcpy(s, p, i); |
| 319 | s += i; |
| 320 | break; |
| 321 | case 'p': |
| 322 | sprintf(s, "%p", va_arg(vargs, void*)); |
Tim Peters | 6af5bbb | 2001-08-25 03:02:28 +0000 | [diff] [blame] | 323 | /* %p is ill-defined: ensure leading 0x. */ |
| 324 | if (s[1] == 'X') |
| 325 | s[1] = 'x'; |
| 326 | else if (s[1] != 'x') { |
| 327 | memmove(s+2, s, strlen(s)+1); |
| 328 | s[0] = '0'; |
| 329 | s[1] = 'x'; |
| 330 | } |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 331 | s += strlen(s); |
| 332 | break; |
| 333 | case '%': |
| 334 | *s++ = '%'; |
| 335 | break; |
| 336 | default: |
| 337 | strcpy(s, p); |
| 338 | s += strlen(s); |
| 339 | goto end; |
| 340 | } |
| 341 | } else |
| 342 | *s++ = *f; |
| 343 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 344 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 345 | end: |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 346 | _PyString_Resize(&string, s - PyString_AS_STRING(string)); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 347 | return string; |
| 348 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 349 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 350 | PyObject * |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 351 | PyString_FromFormat(const char *format, ...) |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 352 | { |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 353 | PyObject* ret; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 354 | va_list vargs; |
| 355 | |
| 356 | #ifdef HAVE_STDARG_PROTOTYPES |
| 357 | va_start(vargs, format); |
| 358 | #else |
| 359 | va_start(vargs); |
| 360 | #endif |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 361 | ret = PyString_FromFormatV(format, vargs); |
| 362 | va_end(vargs); |
| 363 | return ret; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 367 | PyObject *PyString_Decode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 368 | Py_ssize_t size, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 369 | const char *encoding, |
| 370 | const char *errors) |
| 371 | { |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 372 | PyObject *v, *str; |
| 373 | |
| 374 | str = PyString_FromStringAndSize(s, size); |
| 375 | if (str == NULL) |
| 376 | return NULL; |
| 377 | v = PyString_AsDecodedString(str, encoding, errors); |
| 378 | Py_DECREF(str); |
| 379 | return v; |
| 380 | } |
| 381 | |
| 382 | PyObject *PyString_AsDecodedObject(PyObject *str, |
| 383 | const char *encoding, |
| 384 | const char *errors) |
| 385 | { |
| 386 | PyObject *v; |
| 387 | |
| 388 | if (!PyString_Check(str)) { |
| 389 | PyErr_BadArgument(); |
| 390 | goto onError; |
| 391 | } |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 392 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 393 | if (encoding == NULL) { |
| 394 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 395 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 396 | #else |
| 397 | PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
| 398 | goto onError; |
| 399 | #endif |
| 400 | } |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 401 | |
| 402 | /* Decode via the codec registry */ |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 403 | v = PyCodec_Decode(str, encoding, errors); |
| 404 | if (v == NULL) |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 405 | goto onError; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 406 | |
| 407 | return v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 408 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 409 | onError: |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 410 | return NULL; |
| 411 | } |
| 412 | |
| 413 | PyObject *PyString_AsDecodedString(PyObject *str, |
| 414 | const char *encoding, |
| 415 | const char *errors) |
| 416 | { |
| 417 | PyObject *v; |
| 418 | |
| 419 | v = PyString_AsDecodedObject(str, encoding, errors); |
| 420 | if (v == NULL) |
| 421 | goto onError; |
| 422 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 423 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 424 | /* Convert Unicode to a string using the default encoding */ |
| 425 | if (PyUnicode_Check(v)) { |
| 426 | PyObject *temp = v; |
| 427 | v = PyUnicode_AsEncodedString(v, NULL, NULL); |
| 428 | Py_DECREF(temp); |
| 429 | if (v == NULL) |
| 430 | goto onError; |
| 431 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 432 | #endif |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 433 | if (!PyString_Check(v)) { |
| 434 | PyErr_Format(PyExc_TypeError, |
| 435 | "decoder did not return a string object (type=%.400s)", |
| 436 | v->ob_type->tp_name); |
| 437 | Py_DECREF(v); |
| 438 | goto onError; |
| 439 | } |
| 440 | |
| 441 | return v; |
| 442 | |
| 443 | onError: |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 444 | return NULL; |
| 445 | } |
| 446 | |
| 447 | PyObject *PyString_Encode(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 448 | Py_ssize_t size, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 449 | const char *encoding, |
| 450 | const char *errors) |
| 451 | { |
| 452 | PyObject *v, *str; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 453 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 454 | str = PyString_FromStringAndSize(s, size); |
| 455 | if (str == NULL) |
| 456 | return NULL; |
| 457 | v = PyString_AsEncodedString(str, encoding, errors); |
| 458 | Py_DECREF(str); |
| 459 | return v; |
| 460 | } |
| 461 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 462 | PyObject *PyString_AsEncodedObject(PyObject *str, |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 463 | const char *encoding, |
| 464 | const char *errors) |
| 465 | { |
| 466 | PyObject *v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 467 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 468 | if (!PyString_Check(str)) { |
| 469 | PyErr_BadArgument(); |
| 470 | goto onError; |
| 471 | } |
| 472 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 473 | if (encoding == NULL) { |
| 474 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 475 | encoding = PyUnicode_GetDefaultEncoding(); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 476 | #else |
| 477 | PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
| 478 | goto onError; |
| 479 | #endif |
| 480 | } |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 481 | |
| 482 | /* Encode via the codec registry */ |
| 483 | v = PyCodec_Encode(str, encoding, errors); |
| 484 | if (v == NULL) |
| 485 | goto onError; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 486 | |
| 487 | return v; |
| 488 | |
| 489 | onError: |
| 490 | return NULL; |
| 491 | } |
| 492 | |
| 493 | PyObject *PyString_AsEncodedString(PyObject *str, |
| 494 | const char *encoding, |
| 495 | const char *errors) |
| 496 | { |
| 497 | PyObject *v; |
| 498 | |
Marc-André Lemburg | 8c2133d | 2001-06-12 13:14:10 +0000 | [diff] [blame] | 499 | v = PyString_AsEncodedObject(str, encoding, errors); |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 500 | if (v == NULL) |
| 501 | goto onError; |
| 502 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 503 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 504 | /* Convert Unicode to a string using the default encoding */ |
| 505 | if (PyUnicode_Check(v)) { |
| 506 | PyObject *temp = v; |
| 507 | v = PyUnicode_AsEncodedString(v, NULL, NULL); |
| 508 | Py_DECREF(temp); |
| 509 | if (v == NULL) |
| 510 | goto onError; |
| 511 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 512 | #endif |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 513 | if (!PyString_Check(v)) { |
| 514 | PyErr_Format(PyExc_TypeError, |
| 515 | "encoder did not return a string object (type=%.400s)", |
| 516 | v->ob_type->tp_name); |
| 517 | Py_DECREF(v); |
| 518 | goto onError; |
| 519 | } |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 520 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 521 | return v; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 522 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 523 | onError: |
| 524 | return NULL; |
| 525 | } |
| 526 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 527 | static void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 528 | string_dealloc(PyObject *op) |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 529 | { |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 530 | switch (PyString_CHECK_INTERNED(op)) { |
| 531 | case SSTATE_NOT_INTERNED: |
| 532 | break; |
| 533 | |
| 534 | case SSTATE_INTERNED_MORTAL: |
| 535 | /* revive dead object temporarily for DelItem */ |
| 536 | op->ob_refcnt = 3; |
| 537 | if (PyDict_DelItem(interned, op) != 0) |
| 538 | Py_FatalError( |
| 539 | "deletion of interned string failed"); |
| 540 | break; |
| 541 | |
| 542 | case SSTATE_INTERNED_IMMORTAL: |
| 543 | Py_FatalError("Immortal interned string died."); |
| 544 | |
| 545 | default: |
| 546 | Py_FatalError("Inconsistent interned string state."); |
| 547 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 548 | op->ob_type->tp_free(op); |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 551 | /* Unescape a backslash-escaped string. If unicode is non-zero, |
| 552 | the string is a u-literal. If recode_encoding is non-zero, |
| 553 | the string is UTF-8 encoded and should be re-encoded in the |
| 554 | specified encoding. */ |
| 555 | |
| 556 | PyObject *PyString_DecodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 557 | Py_ssize_t len, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 558 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 559 | Py_ssize_t unicode, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 560 | const char *recode_encoding) |
| 561 | { |
| 562 | int c; |
| 563 | char *p, *buf; |
| 564 | const char *end; |
| 565 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 566 | Py_ssize_t newlen = recode_encoding ? 4*len:len; |
Walter Dörwald | 8709a42 | 2002-09-03 13:53:40 +0000 | [diff] [blame] | 567 | v = PyString_FromStringAndSize((char *)NULL, newlen); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 568 | if (v == NULL) |
| 569 | return NULL; |
| 570 | p = buf = PyString_AsString(v); |
| 571 | end = s + len; |
| 572 | while (s < end) { |
| 573 | if (*s != '\\') { |
Martin v. Löwis | 2412853 | 2002-09-09 06:17:05 +0000 | [diff] [blame] | 574 | non_esc: |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 575 | #ifdef Py_USING_UNICODE |
| 576 | if (recode_encoding && (*s & 0x80)) { |
| 577 | PyObject *u, *w; |
| 578 | char *r; |
| 579 | const char* t; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 580 | Py_ssize_t rn; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 581 | t = s; |
| 582 | /* Decode non-ASCII bytes as UTF-8. */ |
| 583 | while (t < end && (*t & 0x80)) t++; |
| 584 | u = PyUnicode_DecodeUTF8(s, t - s, errors); |
| 585 | if(!u) goto failed; |
| 586 | |
| 587 | /* Recode them in target encoding. */ |
| 588 | w = PyUnicode_AsEncodedString( |
| 589 | u, recode_encoding, errors); |
| 590 | Py_DECREF(u); |
| 591 | if (!w) goto failed; |
| 592 | |
| 593 | /* Append bytes to output buffer. */ |
Neal Norwitz | 2aa9a5d | 2006-03-20 01:53:23 +0000 | [diff] [blame] | 594 | assert(PyString_Check(w)); |
| 595 | r = PyString_AS_STRING(w); |
| 596 | rn = PyString_GET_SIZE(w); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 597 | memcpy(p, r, rn); |
| 598 | p += rn; |
| 599 | Py_DECREF(w); |
| 600 | s = t; |
| 601 | } else { |
| 602 | *p++ = *s++; |
| 603 | } |
| 604 | #else |
| 605 | *p++ = *s++; |
| 606 | #endif |
| 607 | continue; |
| 608 | } |
| 609 | s++; |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 610 | if (s==end) { |
| 611 | PyErr_SetString(PyExc_ValueError, |
| 612 | "Trailing \\ in string"); |
| 613 | goto failed; |
| 614 | } |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 615 | switch (*s++) { |
| 616 | /* XXX This assumes ASCII! */ |
| 617 | case '\n': break; |
| 618 | case '\\': *p++ = '\\'; break; |
| 619 | case '\'': *p++ = '\''; break; |
| 620 | case '\"': *p++ = '\"'; break; |
| 621 | case 'b': *p++ = '\b'; break; |
| 622 | case 'f': *p++ = '\014'; break; /* FF */ |
| 623 | case 't': *p++ = '\t'; break; |
| 624 | case 'n': *p++ = '\n'; break; |
| 625 | case 'r': *p++ = '\r'; break; |
| 626 | case 'v': *p++ = '\013'; break; /* VT */ |
| 627 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 628 | case '0': case '1': case '2': case '3': |
| 629 | case '4': case '5': case '6': case '7': |
| 630 | c = s[-1] - '0'; |
| 631 | if ('0' <= *s && *s <= '7') { |
| 632 | c = (c<<3) + *s++ - '0'; |
| 633 | if ('0' <= *s && *s <= '7') |
| 634 | c = (c<<3) + *s++ - '0'; |
| 635 | } |
| 636 | *p++ = c; |
| 637 | break; |
| 638 | case 'x': |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 639 | if (isxdigit(Py_CHARMASK(s[0])) |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 640 | && isxdigit(Py_CHARMASK(s[1]))) { |
| 641 | unsigned int x = 0; |
| 642 | c = Py_CHARMASK(*s); |
| 643 | s++; |
| 644 | if (isdigit(c)) |
| 645 | x = c - '0'; |
| 646 | else if (islower(c)) |
| 647 | x = 10 + c - 'a'; |
| 648 | else |
| 649 | x = 10 + c - 'A'; |
| 650 | x = x << 4; |
| 651 | c = Py_CHARMASK(*s); |
| 652 | s++; |
| 653 | if (isdigit(c)) |
| 654 | x += c - '0'; |
| 655 | else if (islower(c)) |
| 656 | x += 10 + c - 'a'; |
| 657 | else |
| 658 | x += 10 + c - 'A'; |
| 659 | *p++ = x; |
| 660 | break; |
| 661 | } |
| 662 | if (!errors || strcmp(errors, "strict") == 0) { |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 663 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 664 | "invalid \\x escape"); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 665 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 666 | } |
| 667 | if (strcmp(errors, "replace") == 0) { |
| 668 | *p++ = '?'; |
| 669 | } else if (strcmp(errors, "ignore") == 0) |
| 670 | /* do nothing */; |
| 671 | else { |
| 672 | PyErr_Format(PyExc_ValueError, |
| 673 | "decoding error; " |
| 674 | "unknown error handling code: %.400s", |
| 675 | errors); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 676 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 677 | } |
| 678 | #ifndef Py_USING_UNICODE |
| 679 | case 'u': |
| 680 | case 'U': |
| 681 | case 'N': |
| 682 | if (unicode) { |
Neal Norwitz | b898d9f | 2002-08-16 23:20:39 +0000 | [diff] [blame] | 683 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 684 | "Unicode escapes not legal " |
| 685 | "when Unicode disabled"); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 686 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 687 | } |
| 688 | #endif |
| 689 | default: |
| 690 | *p++ = '\\'; |
Martin v. Löwis | 2412853 | 2002-09-09 06:17:05 +0000 | [diff] [blame] | 691 | s--; |
| 692 | goto non_esc; /* an arbitry number of unescaped |
| 693 | UTF-8 bytes may follow. */ |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
Walter Dörwald | 8709a42 | 2002-09-03 13:53:40 +0000 | [diff] [blame] | 696 | if (p-buf < newlen) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 697 | _PyString_Resize(&v, p - buf); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 698 | return v; |
| 699 | failed: |
| 700 | Py_DECREF(v); |
| 701 | return NULL; |
| 702 | } |
| 703 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 704 | static Py_ssize_t |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 705 | string_getsize(register PyObject *op) |
| 706 | { |
| 707 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 708 | Py_ssize_t len; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 709 | if (PyString_AsStringAndSize(op, &s, &len)) |
| 710 | return -1; |
| 711 | return len; |
| 712 | } |
| 713 | |
| 714 | static /*const*/ char * |
| 715 | string_getbuffer(register PyObject *op) |
| 716 | { |
| 717 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 718 | Py_ssize_t len; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 719 | if (PyString_AsStringAndSize(op, &s, &len)) |
| 720 | return NULL; |
| 721 | return s; |
| 722 | } |
| 723 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 724 | Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 725 | PyString_Size(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 726 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 727 | if (!PyString_Check(op)) |
| 728 | return string_getsize(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 729 | return ((PyStringObject *)op) -> ob_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /*const*/ char * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 733 | PyString_AsString(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 734 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 735 | if (!PyString_Check(op)) |
| 736 | return string_getbuffer(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 737 | return ((PyStringObject *)op) -> ob_sval; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 740 | int |
| 741 | PyString_AsStringAndSize(register PyObject *obj, |
| 742 | register char **s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 743 | register Py_ssize_t *len) |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 744 | { |
| 745 | if (s == NULL) { |
| 746 | PyErr_BadInternalCall(); |
| 747 | return -1; |
| 748 | } |
| 749 | |
| 750 | if (!PyString_Check(obj)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 751 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 752 | if (PyUnicode_Check(obj)) { |
| 753 | obj = _PyUnicode_AsDefaultEncodedString(obj, NULL); |
| 754 | if (obj == NULL) |
| 755 | return -1; |
| 756 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 757 | else |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 758 | #endif |
| 759 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 760 | PyErr_Format(PyExc_TypeError, |
| 761 | "expected string or Unicode object, " |
| 762 | "%.200s found", obj->ob_type->tp_name); |
| 763 | return -1; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | *s = PyString_AS_STRING(obj); |
| 768 | if (len != NULL) |
| 769 | *len = PyString_GET_SIZE(obj); |
Skip Montanaro | 429433b | 2006-04-18 00:35:43 +0000 | [diff] [blame] | 770 | else if (strlen(*s) != (size_t)PyString_GET_SIZE(obj)) { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 771 | PyErr_SetString(PyExc_TypeError, |
| 772 | "expected string without null bytes"); |
| 773 | return -1; |
| 774 | } |
| 775 | return 0; |
| 776 | } |
| 777 | |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 778 | /* -------------------------------------------------------------------- */ |
| 779 | /* Helpers */ |
| 780 | |
| 781 | #define USE_FAST /* experimental fast search implementation */ |
| 782 | |
| 783 | /* XXX - this code is copied from unicodeobject.c. we really should |
| 784 | refactor the core implementations (see _sre.c for how this can be |
| 785 | done), but that'll have to wait -- fredrik */ |
| 786 | |
| 787 | /* fast search/count implementation, based on a mix between boyer- |
| 788 | moore and horspool, with a few more bells and whistles on the top. |
| 789 | for some more background, see: http://effbot.org/stringlib */ |
| 790 | |
| 791 | /* note: fastsearch may access s[n], which isn't a problem when using |
| 792 | Python's ordinary string types, but may cause problems if you're |
| 793 | using this code in other contexts. also, the count mode returns -1 |
Andrew M. Kuchling | f344c94 | 2006-05-25 18:11:16 +0000 | [diff] [blame] | 794 | if there cannot possibly be a match in the target string, and 0 if |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 795 | it has actually checked for matches, but didn't find any. callers |
| 796 | beware! */ |
| 797 | |
| 798 | #define FAST_COUNT 0 |
| 799 | #define FAST_SEARCH 1 |
| 800 | |
| 801 | LOCAL(Py_ssize_t) |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 802 | fastsearch(const char* s, Py_ssize_t n, const char* p, Py_ssize_t m, int mode) |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 803 | { |
| 804 | long mask; |
| 805 | int skip, count = 0; |
| 806 | Py_ssize_t i, j, mlast, w; |
| 807 | |
| 808 | w = n - m; |
| 809 | |
| 810 | if (w < 0) |
| 811 | return -1; |
| 812 | |
| 813 | /* look for special cases */ |
| 814 | if (m <= 1) { |
| 815 | if (m <= 0) |
| 816 | return -1; |
| 817 | /* use special case for 1-character strings */ |
| 818 | if (mode == FAST_COUNT) { |
| 819 | for (i = 0; i < n; i++) |
| 820 | if (s[i] == p[0]) |
| 821 | count++; |
| 822 | return count; |
| 823 | } else { |
| 824 | for (i = 0; i < n; i++) |
| 825 | if (s[i] == p[0]) |
| 826 | return i; |
| 827 | } |
| 828 | return -1; |
| 829 | } |
| 830 | |
| 831 | mlast = m - 1; |
| 832 | |
| 833 | /* create compressed boyer-moore delta 1 table */ |
| 834 | skip = mlast - 1; |
| 835 | /* process pattern[:-1] */ |
| 836 | for (mask = i = 0; i < mlast; i++) { |
| 837 | mask |= (1 << (p[i] & 0x1F)); |
| 838 | if (p[i] == p[mlast]) |
| 839 | skip = mlast - i - 1; |
| 840 | } |
| 841 | /* process pattern[-1] outside the loop */ |
| 842 | mask |= (1 << (p[mlast] & 0x1F)); |
| 843 | |
| 844 | for (i = 0; i <= w; i++) { |
| 845 | /* note: using mlast in the skip path slows things down on x86 */ |
| 846 | if (s[i+m-1] == p[m-1]) { |
| 847 | /* candidate match */ |
| 848 | for (j = 0; j < mlast; j++) |
| 849 | if (s[i+j] != p[j]) |
| 850 | break; |
| 851 | if (j == mlast) { |
| 852 | /* got a match! */ |
| 853 | if (mode != FAST_COUNT) |
| 854 | return i; |
| 855 | count++; |
| 856 | i = i + mlast; |
| 857 | continue; |
| 858 | } |
| 859 | /* miss: check if next character is part of pattern */ |
| 860 | if (!(mask & (1 << (s[i+m] & 0x1F)))) |
| 861 | i = i + m; |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 862 | else |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 863 | i = i + skip; |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 864 | } else { |
| 865 | /* skip: check if next character is part of pattern */ |
| 866 | if (!(mask & (1 << (s[i+m] & 0x1F)))) |
| 867 | i = i + m; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | if (mode != FAST_COUNT) |
| 872 | return -1; |
| 873 | return count; |
| 874 | } |
| 875 | |
| 876 | /* -------------------------------------------------------------------- */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 877 | /* Methods */ |
| 878 | |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 879 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 880 | string_print(PyStringObject *op, FILE *fp, int flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 881 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 882 | Py_ssize_t i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 883 | char c; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 884 | int quote; |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 885 | |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 886 | /* XXX Ought to check for interrupts when writing long strings */ |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 887 | if (! PyString_CheckExact(op)) { |
| 888 | int ret; |
| 889 | /* A str subclass may have its own __str__ method. */ |
| 890 | op = (PyStringObject *) PyObject_Str((PyObject *)op); |
| 891 | if (op == NULL) |
| 892 | return -1; |
| 893 | ret = string_print(op, fp, flags); |
| 894 | Py_DECREF(op); |
| 895 | return ret; |
| 896 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 897 | if (flags & Py_PRINT_RAW) { |
Martin v. Löwis | 79acb9e | 2002-12-06 12:48:53 +0000 | [diff] [blame] | 898 | #ifdef __VMS |
| 899 | if (op->ob_size) fwrite(op->ob_sval, (int) op->ob_size, 1, fp); |
| 900 | #else |
| 901 | fwrite(op->ob_sval, 1, (int) op->ob_size, fp); |
| 902 | #endif |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 903 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 904 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 905 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 906 | /* figure out which quote to use; single is preferred */ |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 907 | quote = '\''; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 908 | if (memchr(op->ob_sval, '\'', op->ob_size) && |
| 909 | !memchr(op->ob_sval, '"', op->ob_size)) |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 910 | quote = '"'; |
| 911 | |
| 912 | fputc(quote, fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 913 | for (i = 0; i < op->ob_size; i++) { |
| 914 | c = op->ob_sval[i]; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 915 | if (c == quote || c == '\\') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 916 | fprintf(fp, "\\%c", c); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 917 | else if (c == '\t') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 918 | fprintf(fp, "\\t"); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 919 | else if (c == '\n') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 920 | fprintf(fp, "\\n"); |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 921 | else if (c == '\r') |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 922 | fprintf(fp, "\\r"); |
| 923 | else if (c < ' ' || c >= 0x7f) |
| 924 | fprintf(fp, "\\x%02x", c & 0xff); |
Martin v. Löwis | fed2405 | 2002-10-07 13:55:50 +0000 | [diff] [blame] | 925 | else |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 926 | fputc(c, fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 927 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 928 | fputc(quote, fp); |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 929 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 932 | PyObject * |
| 933 | PyString_Repr(PyObject *obj, int smartquotes) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 934 | { |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 935 | register PyStringObject* op = (PyStringObject*) obj; |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 936 | size_t newsize = 2 + 4 * op->ob_size; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 937 | PyObject *v; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 938 | if (newsize > PY_SSIZE_T_MAX) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 939 | PyErr_SetString(PyExc_OverflowError, |
| 940 | "string is too large to make repr"); |
| 941 | } |
| 942 | v = PyString_FromStringAndSize((char *)NULL, newsize); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 943 | if (v == NULL) { |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 944 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 945 | } |
| 946 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 947 | register Py_ssize_t i; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 948 | register char c; |
| 949 | register char *p; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 950 | int quote; |
| 951 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 952 | /* figure out which quote to use; single is preferred */ |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 953 | quote = '\''; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 954 | if (smartquotes && |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 955 | memchr(op->ob_sval, '\'', op->ob_size) && |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 956 | !memchr(op->ob_sval, '"', op->ob_size)) |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 957 | quote = '"'; |
| 958 | |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 959 | p = PyString_AS_STRING(v); |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 960 | *p++ = quote; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 961 | for (i = 0; i < op->ob_size; i++) { |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 962 | /* There's at least enough room for a hex escape |
| 963 | and a closing quote. */ |
| 964 | assert(newsize - (p - PyString_AS_STRING(v)) >= 5); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 965 | c = op->ob_sval[i]; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 966 | if (c == quote || c == '\\') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 967 | *p++ = '\\', *p++ = c; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 968 | else if (c == '\t') |
| 969 | *p++ = '\\', *p++ = 't'; |
| 970 | else if (c == '\n') |
| 971 | *p++ = '\\', *p++ = 'n'; |
| 972 | else if (c == '\r') |
| 973 | *p++ = '\\', *p++ = 'r'; |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 974 | else if (c < ' ' || c >= 0x7f) { |
| 975 | /* For performance, we don't want to call |
| 976 | PyOS_snprintf here (extra layers of |
| 977 | function call). */ |
| 978 | sprintf(p, "\\x%02x", c & 0xff); |
| 979 | p += 4; |
Martin v. Löwis | fed2405 | 2002-10-07 13:55:50 +0000 | [diff] [blame] | 980 | } |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 981 | else |
| 982 | *p++ = c; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 983 | } |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 984 | assert(newsize - (p - PyString_AS_STRING(v)) >= 1); |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 985 | *p++ = quote; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 986 | *p = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 987 | _PyString_Resize( |
Thomas Wouters | 568f1d0 | 2006-04-21 13:54:43 +0000 | [diff] [blame] | 988 | &v, (p - PyString_AS_STRING(v))); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 989 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 990 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 993 | static PyObject * |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 994 | string_repr(PyObject *op) |
| 995 | { |
| 996 | return PyString_Repr(op, 1); |
| 997 | } |
| 998 | |
| 999 | static PyObject * |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 1000 | string_str(PyObject *s) |
| 1001 | { |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 1002 | assert(PyString_Check(s)); |
| 1003 | if (PyString_CheckExact(s)) { |
| 1004 | Py_INCREF(s); |
| 1005 | return s; |
| 1006 | } |
| 1007 | else { |
| 1008 | /* Subtype -- return genuine string with the same value. */ |
| 1009 | PyStringObject *t = (PyStringObject *) s; |
| 1010 | return PyString_FromStringAndSize(t->ob_sval, t->ob_size); |
| 1011 | } |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1014 | static Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1015 | string_length(PyStringObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1016 | { |
| 1017 | return a->ob_size; |
| 1018 | } |
| 1019 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1020 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1021 | string_concat(register PyStringObject *a, register PyObject *bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1022 | { |
Andrew Dalke | 598710c | 2006-05-25 18:18:39 +0000 | [diff] [blame] | 1023 | register Py_ssize_t size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1024 | register PyStringObject *op; |
| 1025 | if (!PyString_Check(bb)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1026 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1027 | if (PyUnicode_Check(bb)) |
| 1028 | return PyUnicode_Concat((PyObject *)a, bb); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1029 | #endif |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1030 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 5c66a26 | 2001-10-22 04:12:44 +0000 | [diff] [blame] | 1031 | "cannot concatenate 'str' and '%.200s' objects", |
Fred Drake | b6a9ada | 2000-06-01 03:12:13 +0000 | [diff] [blame] | 1032 | bb->ob_type->tp_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1033 | return NULL; |
| 1034 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1035 | #define b ((PyStringObject *)bb) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1036 | /* Optimize cases with empty left or right operand */ |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1037 | if ((a->ob_size == 0 || b->ob_size == 0) && |
| 1038 | PyString_CheckExact(a) && PyString_CheckExact(b)) { |
| 1039 | if (a->ob_size == 0) { |
| 1040 | Py_INCREF(bb); |
| 1041 | return bb; |
| 1042 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1043 | Py_INCREF(a); |
| 1044 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1045 | } |
| 1046 | size = a->ob_size + b->ob_size; |
Andrew Dalke | 598710c | 2006-05-25 18:18:39 +0000 | [diff] [blame] | 1047 | if (size < 0) { |
| 1048 | PyErr_SetString(PyExc_OverflowError, |
| 1049 | "strings are too large to concat"); |
| 1050 | return NULL; |
| 1051 | } |
| 1052 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 1053 | /* Inline PyObject_NewVar */ |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 1054 | op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 1055 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1056 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1057 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1058 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 1059 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1060 | memcpy(op->ob_sval, a->ob_sval, a->ob_size); |
| 1061 | memcpy(op->ob_sval + a->ob_size, b->ob_sval, b->ob_size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 1062 | op->ob_sval[size] = '\0'; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1063 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1064 | #undef b |
| 1065 | } |
| 1066 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1067 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1068 | string_repeat(register PyStringObject *a, register Py_ssize_t n) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1069 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1070 | register Py_ssize_t i; |
| 1071 | register Py_ssize_t j; |
| 1072 | register Py_ssize_t size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1073 | register PyStringObject *op; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 1074 | size_t nbytes; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1075 | if (n < 0) |
| 1076 | n = 0; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 1077 | /* watch out for overflows: the size can overflow int, |
| 1078 | * and the # of bytes needed can overflow size_t |
| 1079 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1080 | size = a->ob_size * n; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 1081 | if (n && size / n != a->ob_size) { |
| 1082 | PyErr_SetString(PyExc_OverflowError, |
| 1083 | "repeated string is too long"); |
| 1084 | return NULL; |
| 1085 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1086 | if (size == a->ob_size && PyString_CheckExact(a)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1087 | Py_INCREF(a); |
| 1088 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1089 | } |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 1090 | nbytes = (size_t)size; |
| 1091 | if (nbytes + sizeof(PyStringObject) <= nbytes) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 1092 | PyErr_SetString(PyExc_OverflowError, |
| 1093 | "repeated string is too long"); |
| 1094 | return NULL; |
| 1095 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1096 | op = (PyStringObject *) |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 1097 | PyObject_MALLOC(sizeof(PyStringObject) + nbytes); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 1098 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1099 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1100 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1101 | op->ob_shash = -1; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 1102 | op->ob_sstate = SSTATE_NOT_INTERNED; |
Raymond Hettinger | 0a2f849 | 2003-01-06 22:42:41 +0000 | [diff] [blame] | 1103 | op->ob_sval[size] = '\0'; |
| 1104 | if (a->ob_size == 1 && n > 0) { |
| 1105 | memset(op->ob_sval, a->ob_sval[0] , n); |
| 1106 | return (PyObject *) op; |
| 1107 | } |
Raymond Hettinger | 698258a | 2003-01-06 10:33:56 +0000 | [diff] [blame] | 1108 | i = 0; |
| 1109 | if (i < size) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1110 | memcpy(op->ob_sval, a->ob_sval, a->ob_size); |
| 1111 | i = a->ob_size; |
Raymond Hettinger | 698258a | 2003-01-06 10:33:56 +0000 | [diff] [blame] | 1112 | } |
| 1113 | while (i < size) { |
| 1114 | j = (i <= size-i) ? i : size-i; |
| 1115 | memcpy(op->ob_sval+i, op->ob_sval, j); |
| 1116 | i += j; |
| 1117 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1118 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ |
| 1122 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1123 | static PyObject * |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1124 | string_slice(register PyStringObject *a, register Py_ssize_t i, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1125 | register Py_ssize_t j) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1126 | /* j -- may be negative! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1127 | { |
| 1128 | if (i < 0) |
| 1129 | i = 0; |
| 1130 | if (j < 0) |
| 1131 | j = 0; /* Avoid signed/unsigned bug in next line */ |
| 1132 | if (j > a->ob_size) |
| 1133 | j = a->ob_size; |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1134 | if (i == 0 && j == a->ob_size && PyString_CheckExact(a)) { |
| 1135 | /* It's the same as a */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1136 | Py_INCREF(a); |
| 1137 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1138 | } |
| 1139 | if (j < i) |
| 1140 | j = i; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1141 | return PyString_FromStringAndSize(a->ob_sval + i, j-i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1144 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1145 | string_contains(PyObject *a, PyObject *el) |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1146 | { |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 1147 | char *s = PyString_AS_STRING(a); |
| 1148 | const char *sub = PyString_AS_STRING(el); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1149 | Py_ssize_t len_sub = PyString_GET_SIZE(el); |
Fredrik Lundh | c3434b3 | 2006-05-25 18:44:29 +0000 | [diff] [blame] | 1150 | #ifdef USE_FAST |
| 1151 | Py_ssize_t pos; |
| 1152 | #else |
| 1153 | char *last; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 1154 | Py_ssize_t shortsub; |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 1155 | char firstchar, lastchar; |
Fredrik Lundh | c3434b3 | 2006-05-25 18:44:29 +0000 | [diff] [blame] | 1156 | #endif |
Guido van Rossum | bf935fd | 2002-08-24 06:57:49 +0000 | [diff] [blame] | 1157 | |
| 1158 | if (!PyString_CheckExact(el)) { |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1159 | #ifdef Py_USING_UNICODE |
Guido van Rossum | bf935fd | 2002-08-24 06:57:49 +0000 | [diff] [blame] | 1160 | if (PyUnicode_Check(el)) |
| 1161 | return PyUnicode_Contains(a, el); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1162 | #endif |
Guido van Rossum | bf935fd | 2002-08-24 06:57:49 +0000 | [diff] [blame] | 1163 | if (!PyString_Check(el)) { |
| 1164 | PyErr_SetString(PyExc_TypeError, |
| 1165 | "'in <string>' requires string as left operand"); |
| 1166 | return -1; |
| 1167 | } |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1168 | } |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 1169 | |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 1170 | if (len_sub == 0) |
| 1171 | return 1; |
Fredrik Lundh | c3434b3 | 2006-05-25 18:44:29 +0000 | [diff] [blame] | 1172 | |
| 1173 | #ifdef USE_FAST |
| 1174 | pos = fastsearch( |
| 1175 | s, PyString_GET_SIZE(a), |
| 1176 | sub, len_sub, FAST_SEARCH |
| 1177 | ); |
| 1178 | return (pos != -1); |
| 1179 | #else |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1180 | /* last points to one char beyond the start of the rightmost |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 1181 | substring. When s<last, there is still room for a possible match |
| 1182 | and s[0] through s[len_sub-1] will be in bounds. |
| 1183 | shortsub is len_sub minus the last character which is checked |
| 1184 | separately just before the memcmp(). That check helps prevent |
| 1185 | false starts and saves the setup time for memcmp(). |
| 1186 | */ |
| 1187 | firstchar = sub[0]; |
| 1188 | shortsub = len_sub - 1; |
| 1189 | lastchar = sub[shortsub]; |
| 1190 | last = s + PyString_GET_SIZE(a) - len_sub + 1; |
| 1191 | while (s < last) { |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1192 | s = (char *)memchr(s, firstchar, last-s); |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 1193 | if (s == NULL) |
| 1194 | return 0; |
| 1195 | assert(s < last); |
| 1196 | if (s[shortsub] == lastchar && memcmp(s, sub, shortsub) == 0) |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1197 | return 1; |
Raymond Hettinger | 7cbf1bc | 2005-02-20 04:07:08 +0000 | [diff] [blame] | 1198 | s++; |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1199 | } |
Fredrik Lundh | c3434b3 | 2006-05-25 18:44:29 +0000 | [diff] [blame] | 1200 | #endif |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1201 | return 0; |
| 1202 | } |
| 1203 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1204 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1205 | string_item(PyStringObject *a, register Py_ssize_t i) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1206 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1207 | PyObject *v; |
Tim Peters | 5b4d477 | 2001-05-08 22:33:50 +0000 | [diff] [blame] | 1208 | char *pchar; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1209 | if (i < 0 || i >= a->ob_size) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1210 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1211 | return NULL; |
| 1212 | } |
Tim Peters | 5b4d477 | 2001-05-08 22:33:50 +0000 | [diff] [blame] | 1213 | pchar = a->ob_sval + i; |
Tim Peters | cf5ad5d | 2001-05-09 00:24:55 +0000 | [diff] [blame] | 1214 | v = (PyObject *)characters[*pchar & UCHAR_MAX]; |
Tim Peters | 5b4d477 | 2001-05-08 22:33:50 +0000 | [diff] [blame] | 1215 | if (v == NULL) |
| 1216 | v = PyString_FromStringAndSize(pchar, 1); |
Tim Peters | b4bbcd7 | 2001-05-09 00:31:40 +0000 | [diff] [blame] | 1217 | else { |
| 1218 | #ifdef COUNT_ALLOCS |
| 1219 | one_strings++; |
| 1220 | #endif |
Tim Peters | cf5ad5d | 2001-05-09 00:24:55 +0000 | [diff] [blame] | 1221 | Py_INCREF(v); |
Tim Peters | b4bbcd7 | 2001-05-09 00:31:40 +0000 | [diff] [blame] | 1222 | } |
Guido van Rossum | daa8bb3 | 1991-04-04 10:48:33 +0000 | [diff] [blame] | 1223 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1226 | static PyObject* |
| 1227 | string_richcompare(PyStringObject *a, PyStringObject *b, int op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1228 | { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1229 | int c; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1230 | Py_ssize_t len_a, len_b; |
| 1231 | Py_ssize_t min_len; |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1232 | PyObject *result; |
| 1233 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 1234 | /* Make sure both arguments are strings. */ |
| 1235 | if (!(PyString_Check(a) && PyString_Check(b))) { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1236 | result = Py_NotImplemented; |
| 1237 | goto out; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1238 | } |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1239 | if (a == b) { |
| 1240 | switch (op) { |
| 1241 | case Py_EQ:case Py_LE:case Py_GE: |
| 1242 | result = Py_True; |
| 1243 | goto out; |
| 1244 | case Py_NE:case Py_LT:case Py_GT: |
| 1245 | result = Py_False; |
| 1246 | goto out; |
| 1247 | } |
| 1248 | } |
| 1249 | if (op == Py_EQ) { |
| 1250 | /* Supporting Py_NE here as well does not save |
| 1251 | much time, since Py_NE is rarely used. */ |
| 1252 | if (a->ob_size == b->ob_size |
| 1253 | && (a->ob_sval[0] == b->ob_sval[0] |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 1254 | && memcmp(a->ob_sval, b->ob_sval, |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 1255 | a->ob_size) == 0)) { |
| 1256 | result = Py_True; |
| 1257 | } else { |
| 1258 | result = Py_False; |
| 1259 | } |
| 1260 | goto out; |
| 1261 | } |
| 1262 | len_a = a->ob_size; len_b = b->ob_size; |
| 1263 | min_len = (len_a < len_b) ? len_a : len_b; |
| 1264 | if (min_len > 0) { |
| 1265 | c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
| 1266 | if (c==0) |
| 1267 | c = memcmp(a->ob_sval, b->ob_sval, min_len); |
| 1268 | }else |
| 1269 | c = 0; |
| 1270 | if (c == 0) |
| 1271 | c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
| 1272 | switch (op) { |
| 1273 | case Py_LT: c = c < 0; break; |
| 1274 | case Py_LE: c = c <= 0; break; |
| 1275 | case Py_EQ: assert(0); break; /* unreachable */ |
| 1276 | case Py_NE: c = c != 0; break; |
| 1277 | case Py_GT: c = c > 0; break; |
| 1278 | case Py_GE: c = c >= 0; break; |
| 1279 | default: |
| 1280 | result = Py_NotImplemented; |
| 1281 | goto out; |
| 1282 | } |
| 1283 | result = c ? Py_True : Py_False; |
| 1284 | out: |
| 1285 | Py_INCREF(result); |
| 1286 | return result; |
| 1287 | } |
| 1288 | |
| 1289 | int |
| 1290 | _PyString_Eq(PyObject *o1, PyObject *o2) |
| 1291 | { |
| 1292 | PyStringObject *a, *b; |
| 1293 | a = (PyStringObject*)o1; |
| 1294 | b = (PyStringObject*)o2; |
| 1295 | return a->ob_size == b->ob_size |
| 1296 | && *a->ob_sval == *b->ob_sval |
| 1297 | && memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1300 | static long |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1301 | string_hash(PyStringObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1302 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1303 | register Py_ssize_t len; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1304 | register unsigned char *p; |
| 1305 | register long x; |
| 1306 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1307 | if (a->ob_shash != -1) |
| 1308 | return a->ob_shash; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1309 | len = a->ob_size; |
| 1310 | p = (unsigned char *) a->ob_sval; |
| 1311 | x = *p << 7; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1312 | while (--len >= 0) |
Guido van Rossum | eddcb3b | 1996-09-11 20:22:48 +0000 | [diff] [blame] | 1313 | x = (1000003*x) ^ *p++; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1314 | x ^= a->ob_size; |
| 1315 | if (x == -1) |
| 1316 | x = -2; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 1317 | a->ob_shash = x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1318 | return x; |
| 1319 | } |
| 1320 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1321 | #define HASINDEX(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_HAVE_INDEX) |
| 1322 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1323 | static PyObject* |
| 1324 | string_subscript(PyStringObject* self, PyObject* item) |
| 1325 | { |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1326 | PyNumberMethods *nb = item->ob_type->tp_as_number; |
| 1327 | if (nb != NULL && HASINDEX(item) && nb->nb_index != NULL) { |
| 1328 | Py_ssize_t i = nb->nb_index(item); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1329 | if (i == -1 && PyErr_Occurred()) |
| 1330 | return NULL; |
| 1331 | if (i < 0) |
| 1332 | i += PyString_GET_SIZE(self); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1333 | return string_item(self, i); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1334 | } |
| 1335 | else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1336 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1337 | char* source_buf; |
| 1338 | char* result_buf; |
| 1339 | PyObject* result; |
| 1340 | |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1341 | if (PySlice_GetIndicesEx((PySliceObject*)item, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1342 | PyString_GET_SIZE(self), |
| 1343 | &start, &stop, &step, &slicelength) < 0) { |
| 1344 | return NULL; |
| 1345 | } |
| 1346 | |
| 1347 | if (slicelength <= 0) { |
| 1348 | return PyString_FromStringAndSize("", 0); |
| 1349 | } |
| 1350 | else { |
| 1351 | source_buf = PyString_AsString((PyObject*)self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 1352 | result_buf = (char *)PyMem_Malloc(slicelength); |
Neal Norwitz | 95c1e50 | 2005-10-20 04:15:52 +0000 | [diff] [blame] | 1353 | if (result_buf == NULL) |
| 1354 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1355 | |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1356 | for (cur = start, i = 0; i < slicelength; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1357 | cur += step, i++) { |
| 1358 | result_buf[i] = source_buf[cur]; |
| 1359 | } |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1360 | |
| 1361 | result = PyString_FromStringAndSize(result_buf, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1362 | slicelength); |
| 1363 | PyMem_Free(result_buf); |
| 1364 | return result; |
| 1365 | } |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1366 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1367 | else { |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1368 | PyErr_SetString(PyExc_TypeError, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1369 | "string indices must be integers"); |
| 1370 | return NULL; |
| 1371 | } |
| 1372 | } |
| 1373 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1374 | static Py_ssize_t |
| 1375 | string_buffer_getreadbuf(PyStringObject *self, Py_ssize_t index, const void **ptr) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1376 | { |
| 1377 | if ( index != 0 ) { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1378 | PyErr_SetString(PyExc_SystemError, |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 1379 | "accessing non-existent string segment"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1380 | return -1; |
| 1381 | } |
| 1382 | *ptr = (void *)self->ob_sval; |
| 1383 | return self->ob_size; |
| 1384 | } |
| 1385 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1386 | static Py_ssize_t |
| 1387 | string_buffer_getwritebuf(PyStringObject *self, Py_ssize_t index, const void **ptr) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1388 | { |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 1389 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 07d7800 | 1998-10-01 15:59:48 +0000 | [diff] [blame] | 1390 | "Cannot use string as modifiable buffer"); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1391 | return -1; |
| 1392 | } |
| 1393 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1394 | static Py_ssize_t |
| 1395 | string_buffer_getsegcount(PyStringObject *self, Py_ssize_t *lenp) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1396 | { |
| 1397 | if ( lenp ) |
| 1398 | *lenp = self->ob_size; |
| 1399 | return 1; |
| 1400 | } |
| 1401 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1402 | static Py_ssize_t |
| 1403 | string_buffer_getcharbuf(PyStringObject *self, Py_ssize_t index, const char **ptr) |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 1404 | { |
| 1405 | if ( index != 0 ) { |
| 1406 | PyErr_SetString(PyExc_SystemError, |
| 1407 | "accessing non-existent string segment"); |
| 1408 | return -1; |
| 1409 | } |
| 1410 | *ptr = self->ob_sval; |
| 1411 | return self->ob_size; |
| 1412 | } |
| 1413 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1414 | static PySequenceMethods string_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1415 | (lenfunc)string_length, /*sq_length*/ |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1416 | (binaryfunc)string_concat, /*sq_concat*/ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1417 | (ssizeargfunc)string_repeat, /*sq_repeat*/ |
| 1418 | (ssizeargfunc)string_item, /*sq_item*/ |
| 1419 | (ssizessizeargfunc)string_slice, /*sq_slice*/ |
Guido van Rossum | f380e66 | 1991-06-04 19:36:32 +0000 | [diff] [blame] | 1420 | 0, /*sq_ass_item*/ |
| 1421 | 0, /*sq_ass_slice*/ |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1422 | (objobjproc)string_contains /*sq_contains*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1423 | }; |
| 1424 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1425 | static PyMappingMethods string_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1426 | (lenfunc)string_length, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1427 | (binaryfunc)string_subscript, |
| 1428 | 0, |
| 1429 | }; |
| 1430 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1431 | static PyBufferProcs string_as_buffer = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1432 | (readbufferproc)string_buffer_getreadbuf, |
| 1433 | (writebufferproc)string_buffer_getwritebuf, |
| 1434 | (segcountproc)string_buffer_getsegcount, |
| 1435 | (charbufferproc)string_buffer_getcharbuf, |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1436 | }; |
| 1437 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1438 | |
| 1439 | |
| 1440 | #define LEFTSTRIP 0 |
| 1441 | #define RIGHTSTRIP 1 |
| 1442 | #define BOTHSTRIP 2 |
| 1443 | |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1444 | /* Arrays indexed by above */ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 1445 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 1446 | |
| 1447 | #define STRIPNAME(i) (stripformat[i]+3) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1448 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1449 | #define SPLIT_APPEND(data, left, right) \ |
| 1450 | str = PyString_FromStringAndSize((data) + (left), \ |
| 1451 | (right) - (left)); \ |
| 1452 | if (str == NULL) \ |
| 1453 | goto onError; \ |
| 1454 | if (PyList_Append(list, str)) { \ |
| 1455 | Py_DECREF(str); \ |
| 1456 | goto onError; \ |
| 1457 | } \ |
| 1458 | else \ |
| 1459 | Py_DECREF(str); |
| 1460 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1461 | static PyObject * |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1462 | split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1463 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1464 | Py_ssize_t i, j; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1465 | PyObject *str; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1466 | PyObject *list = PyList_New(0); |
| 1467 | |
| 1468 | if (list == NULL) |
| 1469 | return NULL; |
| 1470 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1471 | for (i = j = 0; i < len; ) { |
| 1472 | while (i < len && isspace(Py_CHARMASK(s[i]))) |
| 1473 | i++; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1474 | j = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1475 | while (i < len && !isspace(Py_CHARMASK(s[i]))) |
| 1476 | i++; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1477 | if (j < i) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1478 | if (maxsplit-- <= 0) |
| 1479 | break; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1480 | SPLIT_APPEND(s, j, i); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1481 | while (i < len && isspace(Py_CHARMASK(s[i]))) |
| 1482 | i++; |
| 1483 | j = i; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1484 | } |
| 1485 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1486 | if (j < len) { |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1487 | SPLIT_APPEND(s, j, len); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1488 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1489 | return list; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1490 | onError: |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1491 | Py_DECREF(list); |
| 1492 | return NULL; |
| 1493 | } |
| 1494 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1495 | static PyObject * |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1496 | split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1497 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1498 | register Py_ssize_t i, j; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1499 | PyObject *str; |
| 1500 | PyObject *list = PyList_New(0); |
| 1501 | |
| 1502 | if (list == NULL) |
| 1503 | return NULL; |
| 1504 | |
| 1505 | for (i = j = 0; i < len; ) { |
| 1506 | if (s[i] == ch) { |
| 1507 | if (maxcount-- <= 0) |
| 1508 | break; |
| 1509 | SPLIT_APPEND(s, j, i); |
| 1510 | i = j = i + 1; |
| 1511 | } else |
| 1512 | i++; |
| 1513 | } |
| 1514 | if (j <= len) { |
| 1515 | SPLIT_APPEND(s, j, len); |
| 1516 | } |
| 1517 | return list; |
| 1518 | |
| 1519 | onError: |
| 1520 | Py_DECREF(list); |
| 1521 | return NULL; |
| 1522 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1523 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1524 | PyDoc_STRVAR(split__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1525 | "S.split([sep [,maxsplit]]) -> list of strings\n\ |
| 1526 | \n\ |
| 1527 | 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] | 1528 | delimiter string. If maxsplit is given, at most maxsplit\n\ |
Raymond Hettinger | bc552ce | 2002-08-05 06:28:21 +0000 | [diff] [blame] | 1529 | splits are done. If sep is not specified or is None, any\n\ |
| 1530 | whitespace string is a separator."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1531 | |
| 1532 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1533 | string_split(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1534 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1535 | Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; |
| 1536 | int err; |
Martin v. Löwis | 9c83076 | 2006-04-13 08:37:17 +0000 | [diff] [blame] | 1537 | Py_ssize_t maxsplit = -1; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1538 | const char *s = PyString_AS_STRING(self), *sub; |
| 1539 | PyObject *list, *item, *subobj = Py_None; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1540 | |
Martin v. Löwis | 9c83076 | 2006-04-13 08:37:17 +0000 | [diff] [blame] | 1541 | if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1542 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1543 | if (maxsplit < 0) |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1544 | maxsplit = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1545 | if (subobj == Py_None) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1546 | return split_whitespace(s, len, maxsplit); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1547 | if (PyString_Check(subobj)) { |
| 1548 | sub = PyString_AS_STRING(subobj); |
| 1549 | n = PyString_GET_SIZE(subobj); |
| 1550 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1551 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1552 | else if (PyUnicode_Check(subobj)) |
| 1553 | return PyUnicode_Split((PyObject *)self, subobj, maxsplit); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1554 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1555 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 1556 | return NULL; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1557 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1558 | if (n == 0) { |
| 1559 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 1560 | return NULL; |
| 1561 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1562 | else if (n == 1) |
| 1563 | return split_char(s, len, sub[0], maxsplit); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1564 | |
| 1565 | list = PyList_New(0); |
| 1566 | if (list == NULL) |
| 1567 | return NULL; |
| 1568 | |
| 1569 | i = j = 0; |
| 1570 | while (i+n <= len) { |
Fred Drake | 396f6e0 | 2000-06-20 15:47:54 +0000 | [diff] [blame] | 1571 | if (s[i] == sub[0] && memcmp(s+i, sub, n) == 0) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1572 | if (maxsplit-- <= 0) |
| 1573 | break; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1574 | item = PyString_FromStringAndSize(s+j, i-j); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1575 | if (item == NULL) |
| 1576 | goto fail; |
| 1577 | err = PyList_Append(list, item); |
| 1578 | Py_DECREF(item); |
| 1579 | if (err < 0) |
| 1580 | goto fail; |
| 1581 | i = j = i + n; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1582 | } |
| 1583 | else |
| 1584 | i++; |
| 1585 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1586 | item = PyString_FromStringAndSize(s+j, len-j); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1587 | if (item == NULL) |
| 1588 | goto fail; |
| 1589 | err = PyList_Append(list, item); |
| 1590 | Py_DECREF(item); |
| 1591 | if (err < 0) |
| 1592 | goto fail; |
| 1593 | |
| 1594 | return list; |
| 1595 | |
| 1596 | fail: |
| 1597 | Py_DECREF(list); |
| 1598 | return NULL; |
| 1599 | } |
| 1600 | |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 1601 | PyDoc_STRVAR(partition__doc__, |
| 1602 | "S.partition(sep) -> (head, sep, tail)\n\ |
| 1603 | \n\ |
| 1604 | Searches for the separator sep in S, and returns the part before it,\n\ |
| 1605 | the separator itself, and the part after it. If the separator is not\n\ |
| 1606 | found, returns S and two empty strings."); |
| 1607 | |
| 1608 | static PyObject * |
| 1609 | string_partition(PyStringObject *self, PyObject *args) |
| 1610 | { |
| 1611 | Py_ssize_t len = PyString_GET_SIZE(self), sep_len, pos; |
| 1612 | const char *str = PyString_AS_STRING(self), *sep; |
| 1613 | PyObject *sepobj; |
| 1614 | PyObject * out; |
| 1615 | |
| 1616 | if (!PyArg_ParseTuple(args, "O:partition", &sepobj)) |
| 1617 | return NULL; |
| 1618 | if (PyString_Check(sepobj)) { |
| 1619 | sep = PyString_AS_STRING(sepobj); |
| 1620 | sep_len = PyString_GET_SIZE(sepobj); |
| 1621 | } |
| 1622 | #ifdef Py_USING_UNICODE_NOTYET |
| 1623 | else if (PyUnicode_Check(sepobj)) |
| 1624 | return PyUnicode_Partition((PyObject *)self, sepobj); |
| 1625 | #endif |
| 1626 | else if (PyObject_AsCharBuffer(sepobj, &sep, &sep_len)) |
| 1627 | return NULL; |
| 1628 | |
| 1629 | if (sep_len == 0) { |
| 1630 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 1631 | return NULL; |
| 1632 | } |
| 1633 | |
| 1634 | out = PyTuple_New(3); |
| 1635 | if (!out) |
| 1636 | return NULL; |
| 1637 | |
| 1638 | pos = fastsearch(str, len, sep, sep_len, FAST_SEARCH); |
| 1639 | if (pos < 0) { |
| 1640 | Py_INCREF(self); |
| 1641 | PyTuple_SET_ITEM(out, 0, (PyObject*) self); |
| 1642 | Py_INCREF(nullstring); |
| 1643 | PyTuple_SET_ITEM(out, 1, (PyObject*) nullstring); |
| 1644 | Py_INCREF(nullstring); |
| 1645 | PyTuple_SET_ITEM(out, 2, (PyObject*) nullstring); |
| 1646 | } else { |
| 1647 | Py_INCREF(sepobj); |
| 1648 | PyTuple_SET_ITEM(out, 0, PyString_FromStringAndSize(str, pos)); |
| 1649 | PyTuple_SET_ITEM(out, 1, sepobj); |
| 1650 | PyTuple_SET_ITEM(out, 2, |
| 1651 | PyString_FromStringAndSize(str + sep_len + pos, |
| 1652 | len - sep_len - pos) |
| 1653 | ); |
| 1654 | if (PyErr_Occurred()) { |
| 1655 | Py_DECREF(out); |
| 1656 | return NULL; |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | return out; |
| 1661 | } |
| 1662 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1663 | static PyObject * |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1664 | rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxsplit) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1665 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1666 | Py_ssize_t i, j; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1667 | PyObject *str; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1668 | PyObject *list = PyList_New(0); |
| 1669 | |
| 1670 | if (list == NULL) |
| 1671 | return NULL; |
| 1672 | |
| 1673 | for (i = j = len - 1; i >= 0; ) { |
| 1674 | while (i >= 0 && isspace(Py_CHARMASK(s[i]))) |
| 1675 | i--; |
| 1676 | j = i; |
| 1677 | while (i >= 0 && !isspace(Py_CHARMASK(s[i]))) |
| 1678 | i--; |
| 1679 | if (j > i) { |
| 1680 | if (maxsplit-- <= 0) |
| 1681 | break; |
Fredrik Lundh | 554da41 | 2006-05-25 19:19:05 +0000 | [diff] [blame] | 1682 | SPLIT_APPEND(s, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1683 | while (i >= 0 && isspace(Py_CHARMASK(s[i]))) |
| 1684 | i--; |
| 1685 | j = i; |
| 1686 | } |
| 1687 | } |
| 1688 | if (j >= 0) { |
Fredrik Lundh | 554da41 | 2006-05-25 19:19:05 +0000 | [diff] [blame] | 1689 | SPLIT_APPEND(s, 0, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1690 | } |
Fredrik Lundh | 554da41 | 2006-05-25 19:19:05 +0000 | [diff] [blame] | 1691 | if (PyList_Reverse(list) < 0) |
| 1692 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1693 | return list; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1694 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1695 | Py_DECREF(list); |
| 1696 | return NULL; |
| 1697 | } |
| 1698 | |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1699 | static PyObject * |
Martin v. Löwis | 83687c9 | 2006-04-13 08:52:56 +0000 | [diff] [blame] | 1700 | rsplit_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1701 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1702 | register Py_ssize_t i, j; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1703 | PyObject *str; |
| 1704 | PyObject *list = PyList_New(0); |
| 1705 | |
| 1706 | if (list == NULL) |
| 1707 | return NULL; |
| 1708 | |
| 1709 | for (i = j = len - 1; i >= 0; ) { |
| 1710 | if (s[i] == ch) { |
| 1711 | if (maxcount-- <= 0) |
| 1712 | break; |
Fredrik Lundh | 554da41 | 2006-05-25 19:19:05 +0000 | [diff] [blame] | 1713 | SPLIT_APPEND(s, i + 1, j + 1); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1714 | j = i = i - 1; |
| 1715 | } else |
| 1716 | i--; |
| 1717 | } |
| 1718 | if (j >= -1) { |
Fredrik Lundh | 554da41 | 2006-05-25 19:19:05 +0000 | [diff] [blame] | 1719 | SPLIT_APPEND(s, 0, j + 1); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1720 | } |
Fredrik Lundh | 554da41 | 2006-05-25 19:19:05 +0000 | [diff] [blame] | 1721 | if (PyList_Reverse(list) < 0) |
| 1722 | goto onError; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1723 | return list; |
| 1724 | |
| 1725 | onError: |
| 1726 | Py_DECREF(list); |
| 1727 | return NULL; |
| 1728 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1729 | |
| 1730 | PyDoc_STRVAR(rsplit__doc__, |
| 1731 | "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
| 1732 | \n\ |
| 1733 | Return a list of the words in the string S, using sep as the\n\ |
| 1734 | delimiter string, starting at the end of the string and working\n\ |
| 1735 | to the front. If maxsplit is given, at most maxsplit splits are\n\ |
| 1736 | done. If sep is not specified or is None, any whitespace string\n\ |
| 1737 | is a separator."); |
| 1738 | |
| 1739 | static PyObject * |
| 1740 | string_rsplit(PyStringObject *self, PyObject *args) |
| 1741 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1742 | Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; |
| 1743 | int err; |
Martin v. Löwis | 9c83076 | 2006-04-13 08:37:17 +0000 | [diff] [blame] | 1744 | Py_ssize_t maxsplit = -1; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1745 | const char *s = PyString_AS_STRING(self), *sub; |
| 1746 | PyObject *list, *item, *subobj = Py_None; |
| 1747 | |
Martin v. Löwis | 9c83076 | 2006-04-13 08:37:17 +0000 | [diff] [blame] | 1748 | if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1749 | return NULL; |
| 1750 | if (maxsplit < 0) |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1751 | maxsplit = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1752 | if (subobj == Py_None) |
| 1753 | return rsplit_whitespace(s, len, maxsplit); |
| 1754 | if (PyString_Check(subobj)) { |
| 1755 | sub = PyString_AS_STRING(subobj); |
| 1756 | n = PyString_GET_SIZE(subobj); |
| 1757 | } |
| 1758 | #ifdef Py_USING_UNICODE |
| 1759 | else if (PyUnicode_Check(subobj)) |
| 1760 | return PyUnicode_RSplit((PyObject *)self, subobj, maxsplit); |
| 1761 | #endif |
| 1762 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 1763 | return NULL; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1764 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1765 | if (n == 0) { |
| 1766 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
| 1767 | return NULL; |
| 1768 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1769 | else if (n == 1) |
| 1770 | return rsplit_char(s, len, sub[0], maxsplit); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1771 | |
| 1772 | list = PyList_New(0); |
| 1773 | if (list == NULL) |
| 1774 | return NULL; |
| 1775 | |
| 1776 | j = len; |
| 1777 | i = j - n; |
| 1778 | while (i >= 0) { |
| 1779 | if (s[i] == sub[0] && memcmp(s+i, sub, n) == 0) { |
| 1780 | if (maxsplit-- <= 0) |
| 1781 | break; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1782 | item = PyString_FromStringAndSize(s+i+n, j-i-n); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1783 | if (item == NULL) |
| 1784 | goto fail; |
| 1785 | err = PyList_Insert(list, 0, item); |
| 1786 | Py_DECREF(item); |
| 1787 | if (err < 0) |
| 1788 | goto fail; |
| 1789 | j = i; |
| 1790 | i -= n; |
| 1791 | } |
| 1792 | else |
| 1793 | i--; |
| 1794 | } |
| 1795 | item = PyString_FromStringAndSize(s, j); |
| 1796 | if (item == NULL) |
| 1797 | goto fail; |
| 1798 | err = PyList_Insert(list, 0, item); |
| 1799 | Py_DECREF(item); |
| 1800 | if (err < 0) |
| 1801 | goto fail; |
| 1802 | |
| 1803 | return list; |
| 1804 | |
| 1805 | fail: |
| 1806 | Py_DECREF(list); |
| 1807 | return NULL; |
| 1808 | } |
| 1809 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1810 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1811 | PyDoc_STRVAR(join__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1812 | "S.join(sequence) -> string\n\ |
| 1813 | \n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1814 | 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] | 1815 | sequence. The separator between elements is S."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1816 | |
| 1817 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1818 | string_join(PyStringObject *self, PyObject *orig) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1819 | { |
| 1820 | char *sep = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1821 | const Py_ssize_t seplen = PyString_GET_SIZE(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1822 | PyObject *res = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1823 | char *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1824 | Py_ssize_t seqlen = 0; |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1825 | size_t sz = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1826 | Py_ssize_t i; |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1827 | PyObject *seq, *item; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1828 | |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1829 | seq = PySequence_Fast(orig, ""); |
| 1830 | if (seq == NULL) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1831 | return NULL; |
| 1832 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1833 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1834 | seqlen = PySequence_Size(seq); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1835 | if (seqlen == 0) { |
| 1836 | Py_DECREF(seq); |
| 1837 | return PyString_FromString(""); |
| 1838 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1839 | if (seqlen == 1) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1840 | item = PySequence_Fast_GET_ITEM(seq, 0); |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1841 | if (PyString_CheckExact(item) || PyUnicode_CheckExact(item)) { |
| 1842 | Py_INCREF(item); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1843 | Py_DECREF(seq); |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1844 | return item; |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1845 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1846 | } |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1847 | |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1848 | /* There are at least two things to join, or else we have a subclass |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 1849 | * of the builtin types in the sequence. |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1850 | * Do a pre-pass to figure out the total amount of space we'll |
| 1851 | * need (sz), see whether any argument is absurd, and defer to |
| 1852 | * the Unicode join if appropriate. |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1853 | */ |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1854 | for (i = 0; i < seqlen; i++) { |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1855 | const size_t old_sz = sz; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1856 | item = PySequence_Fast_GET_ITEM(seq, i); |
| 1857 | if (!PyString_Check(item)){ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1858 | #ifdef Py_USING_UNICODE |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1859 | if (PyUnicode_Check(item)) { |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 1860 | /* Defer to Unicode join. |
| 1861 | * CAUTION: There's no gurantee that the |
| 1862 | * original sequence can be iterated over |
| 1863 | * again, so we must pass seq here. |
| 1864 | */ |
| 1865 | PyObject *result; |
| 1866 | result = PyUnicode_Join((PyObject *)self, seq); |
Barry Warsaw | 771d067 | 2000-07-11 04:58:12 +0000 | [diff] [blame] | 1867 | Py_DECREF(seq); |
Tim Peters | 2cfe368 | 2001-05-05 05:36:48 +0000 | [diff] [blame] | 1868 | return result; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1869 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1870 | #endif |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1871 | PyErr_Format(PyExc_TypeError, |
Neal Norwitz | 0e2cbab | 2006-04-17 05:56:32 +0000 | [diff] [blame] | 1872 | "sequence item %zd: expected string," |
Jeremy Hylton | 88887aa | 2000-07-11 20:55:38 +0000 | [diff] [blame] | 1873 | " %.80s found", |
Neal Norwitz | 0e2cbab | 2006-04-17 05:56:32 +0000 | [diff] [blame] | 1874 | i, item->ob_type->tp_name); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1875 | Py_DECREF(seq); |
| 1876 | return NULL; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1877 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1878 | sz += PyString_GET_SIZE(item); |
| 1879 | if (i != 0) |
| 1880 | sz += seplen; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1881 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1882 | PyErr_SetString(PyExc_OverflowError, |
| 1883 | "join() is too long for a Python string"); |
| 1884 | Py_DECREF(seq); |
| 1885 | return NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1886 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | /* Allocate result space. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1890 | res = PyString_FromStringAndSize((char*)NULL, sz); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1891 | if (res == NULL) { |
| 1892 | Py_DECREF(seq); |
| 1893 | return NULL; |
| 1894 | } |
| 1895 | |
| 1896 | /* Catenate everything. */ |
| 1897 | p = PyString_AS_STRING(res); |
| 1898 | for (i = 0; i < seqlen; ++i) { |
| 1899 | size_t n; |
| 1900 | item = PySequence_Fast_GET_ITEM(seq, i); |
| 1901 | n = PyString_GET_SIZE(item); |
| 1902 | memcpy(p, PyString_AS_STRING(item), n); |
| 1903 | p += n; |
| 1904 | if (i < seqlen - 1) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1905 | memcpy(p, sep, seplen); |
| 1906 | p += seplen; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1907 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1908 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1909 | |
Jeremy Hylton | 4904829 | 2000-07-11 03:28:17 +0000 | [diff] [blame] | 1910 | Py_DECREF(seq); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1911 | return res; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Tim Peters | 52e155e | 2001-06-16 05:42:57 +0000 | [diff] [blame] | 1914 | PyObject * |
| 1915 | _PyString_Join(PyObject *sep, PyObject *x) |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1916 | { |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1917 | assert(sep != NULL && PyString_Check(sep)); |
| 1918 | assert(x != NULL); |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1919 | return string_join((PyStringObject *)sep, x); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 1922 | static void |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1923 | string_adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len) |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 1924 | { |
| 1925 | if (*end > len) |
| 1926 | *end = len; |
| 1927 | else if (*end < 0) |
| 1928 | *end += len; |
| 1929 | if (*end < 0) |
| 1930 | *end = 0; |
| 1931 | if (*start < 0) |
| 1932 | *start += len; |
| 1933 | if (*start < 0) |
| 1934 | *start = 0; |
| 1935 | } |
| 1936 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1937 | static Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1938 | string_find_internal(PyStringObject *self, PyObject *args, int dir) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1939 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1940 | const char *s = PyString_AS_STRING(self), *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1941 | Py_ssize_t len = PyString_GET_SIZE(self); |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 1942 | Py_ssize_t n, i = 0, last = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1943 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1944 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1945 | /* XXX ssize_t i */ |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1946 | if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 1947 | &subobj, _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1948 | return -2; |
| 1949 | if (PyString_Check(subobj)) { |
| 1950 | sub = PyString_AS_STRING(subobj); |
| 1951 | n = PyString_GET_SIZE(subobj); |
| 1952 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1953 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1954 | else if (PyUnicode_Check(subobj)) |
Guido van Rossum | 76afbd9 | 2002-08-20 17:29:29 +0000 | [diff] [blame] | 1955 | return PyUnicode_Find((PyObject *)self, subobj, i, last, dir); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 1956 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1957 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1958 | return -2; |
| 1959 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 1960 | string_adjust_indices(&i, &last, len); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1961 | |
Fredrik Lundh | c3434b3 | 2006-05-25 18:44:29 +0000 | [diff] [blame] | 1962 | #ifdef USE_FAST |
| 1963 | if (n == 0) |
| 1964 | return (dir > 0) ? i : last; |
| 1965 | if (dir > 0) { |
| 1966 | Py_ssize_t pos = fastsearch(s + i, last - i, sub, n, |
| 1967 | FAST_SEARCH); |
| 1968 | if (pos < 0) |
| 1969 | return pos; |
| 1970 | return pos + i; |
| 1971 | } |
| 1972 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1973 | if (dir > 0) { |
| 1974 | if (n == 0 && i <= last) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1975 | return (long)i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1976 | last -= n; |
| 1977 | for (; i <= last; ++i) |
Fred Drake | 396f6e0 | 2000-06-20 15:47:54 +0000 | [diff] [blame] | 1978 | if (s[i] == sub[0] && memcmp(&s[i], sub, n) == 0) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1979 | return (long)i; |
| 1980 | } |
| 1981 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1982 | Py_ssize_t j; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1983 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1984 | if (n == 0 && i <= last) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1985 | return last; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1986 | for (j = last-n; j >= i; --j) |
Fred Drake | 396f6e0 | 2000-06-20 15:47:54 +0000 | [diff] [blame] | 1987 | if (s[j] == sub[0] && memcmp(&s[j], sub, n) == 0) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1988 | return j; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1989 | } |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1990 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1991 | return -1; |
| 1992 | } |
| 1993 | |
| 1994 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1995 | PyDoc_STRVAR(find__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1996 | "S.find(sub [,start [,end]]) -> int\n\ |
| 1997 | \n\ |
| 1998 | Return the lowest index in S where substring sub is found,\n\ |
| 1999 | such that sub is contained within s[start,end]. Optional\n\ |
| 2000 | arguments start and end are interpreted as in slice notation.\n\ |
| 2001 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2002 | Return -1 on failure."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2003 | |
| 2004 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2005 | string_find(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2006 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2007 | Py_ssize_t result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2008 | if (result == -2) |
| 2009 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2010 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2014 | PyDoc_STRVAR(index__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2015 | "S.index(sub [,start [,end]]) -> int\n\ |
| 2016 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2017 | Like S.find() but raise ValueError when the substring is not found."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2018 | |
| 2019 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2020 | string_index(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2021 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2022 | Py_ssize_t result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2023 | if (result == -2) |
| 2024 | return NULL; |
| 2025 | if (result == -1) { |
| 2026 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 5d5e7c0 | 2003-01-15 05:32:57 +0000 | [diff] [blame] | 2027 | "substring not found"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2028 | return NULL; |
| 2029 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2030 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2031 | } |
| 2032 | |
| 2033 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2034 | PyDoc_STRVAR(rfind__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2035 | "S.rfind(sub [,start [,end]]) -> int\n\ |
| 2036 | \n\ |
| 2037 | Return the highest index in S where substring sub is found,\n\ |
| 2038 | such that sub is contained within s[start,end]. Optional\n\ |
| 2039 | arguments start and end are interpreted as in slice notation.\n\ |
| 2040 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2041 | Return -1 on failure."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2042 | |
| 2043 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2044 | string_rfind(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2045 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2046 | Py_ssize_t result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2047 | if (result == -2) |
| 2048 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2049 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2053 | PyDoc_STRVAR(rindex__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2054 | "S.rindex(sub [,start [,end]]) -> int\n\ |
| 2055 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2056 | Like S.rfind() but raise ValueError when the substring is not found."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2057 | |
| 2058 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2059 | string_rindex(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2060 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2061 | Py_ssize_t result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2062 | if (result == -2) |
| 2063 | return NULL; |
| 2064 | if (result == -1) { |
| 2065 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 5d5e7c0 | 2003-01-15 05:32:57 +0000 | [diff] [blame] | 2066 | "substring not found"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2067 | return NULL; |
| 2068 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2069 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | |
| 2073 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2074 | do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj) |
| 2075 | { |
| 2076 | char *s = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2077 | Py_ssize_t len = PyString_GET_SIZE(self); |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2078 | char *sep = PyString_AS_STRING(sepobj); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2079 | Py_ssize_t seplen = PyString_GET_SIZE(sepobj); |
| 2080 | Py_ssize_t i, j; |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2081 | |
| 2082 | i = 0; |
| 2083 | if (striptype != RIGHTSTRIP) { |
| 2084 | while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) { |
| 2085 | i++; |
| 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | j = len; |
| 2090 | if (striptype != LEFTSTRIP) { |
| 2091 | do { |
| 2092 | j--; |
| 2093 | } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen)); |
| 2094 | j++; |
| 2095 | } |
| 2096 | |
| 2097 | if (i == 0 && j == len && PyString_CheckExact(self)) { |
| 2098 | Py_INCREF(self); |
| 2099 | return (PyObject*)self; |
| 2100 | } |
| 2101 | else |
| 2102 | return PyString_FromStringAndSize(s+i, j-i); |
| 2103 | } |
| 2104 | |
| 2105 | |
| 2106 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2107 | do_strip(PyStringObject *self, int striptype) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2108 | { |
| 2109 | char *s = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2110 | Py_ssize_t len = PyString_GET_SIZE(self), i, j; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2111 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2112 | i = 0; |
| 2113 | if (striptype != RIGHTSTRIP) { |
| 2114 | while (i < len && isspace(Py_CHARMASK(s[i]))) { |
| 2115 | i++; |
| 2116 | } |
| 2117 | } |
| 2118 | |
| 2119 | j = len; |
| 2120 | if (striptype != LEFTSTRIP) { |
| 2121 | do { |
| 2122 | j--; |
| 2123 | } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
| 2124 | j++; |
| 2125 | } |
| 2126 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2127 | if (i == 0 && j == len && PyString_CheckExact(self)) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2128 | Py_INCREF(self); |
| 2129 | return (PyObject*)self; |
| 2130 | } |
| 2131 | else |
| 2132 | return PyString_FromStringAndSize(s+i, j-i); |
| 2133 | } |
| 2134 | |
| 2135 | |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2136 | static PyObject * |
| 2137 | do_argstrip(PyStringObject *self, int striptype, PyObject *args) |
| 2138 | { |
| 2139 | PyObject *sep = NULL; |
| 2140 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 2141 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2142 | return NULL; |
| 2143 | |
| 2144 | if (sep != NULL && sep != Py_None) { |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 2145 | if (PyString_Check(sep)) |
| 2146 | return do_xstrip(self, striptype, sep); |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2147 | #ifdef Py_USING_UNICODE |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 2148 | else if (PyUnicode_Check(sep)) { |
| 2149 | PyObject *uniself = PyUnicode_FromObject((PyObject *)self); |
| 2150 | PyObject *res; |
| 2151 | if (uniself==NULL) |
| 2152 | return NULL; |
| 2153 | res = _PyUnicode_XStrip((PyUnicodeObject *)uniself, |
| 2154 | striptype, sep); |
| 2155 | Py_DECREF(uniself); |
| 2156 | return res; |
| 2157 | } |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2158 | #endif |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2159 | PyErr_Format(PyExc_TypeError, |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2160 | #ifdef Py_USING_UNICODE |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2161 | "%s arg must be None, str or unicode", |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2162 | #else |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2163 | "%s arg must be None or str", |
Walter Dörwald | 775c11f | 2002-05-13 09:00:41 +0000 | [diff] [blame] | 2164 | #endif |
Neal Norwitz | 7e957d3 | 2006-04-06 08:17:41 +0000 | [diff] [blame] | 2165 | STRIPNAME(striptype)); |
| 2166 | return NULL; |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
| 2169 | return do_strip(self, striptype); |
| 2170 | } |
| 2171 | |
| 2172 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2173 | PyDoc_STRVAR(strip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2174 | "S.strip([chars]) -> string or unicode\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2175 | \n\ |
| 2176 | 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] | 2177 | whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2178 | If chars is given and not None, remove characters in chars instead.\n\ |
| 2179 | If chars is unicode, S will be converted to unicode before stripping"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2180 | |
| 2181 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2182 | string_strip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2183 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2184 | if (PyTuple_GET_SIZE(args) == 0) |
| 2185 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 2186 | else |
| 2187 | return do_argstrip(self, BOTHSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2191 | PyDoc_STRVAR(lstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2192 | "S.lstrip([chars]) -> string or unicode\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2193 | \n\ |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2194 | Return a copy of the string S with leading whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2195 | If chars is given and not None, remove characters in chars instead.\n\ |
| 2196 | If chars is unicode, S will be converted to unicode before stripping"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2197 | |
| 2198 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2199 | string_lstrip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2200 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2201 | if (PyTuple_GET_SIZE(args) == 0) |
| 2202 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 2203 | else |
| 2204 | return do_argstrip(self, LEFTSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2205 | } |
| 2206 | |
| 2207 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2208 | PyDoc_STRVAR(rstrip__doc__, |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2209 | "S.rstrip([chars]) -> string or unicode\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2210 | \n\ |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2211 | Return a copy of the string S with trailing whitespace removed.\n\ |
Neal Norwitz | ffe33b7 | 2003-04-10 22:35:32 +0000 | [diff] [blame] | 2212 | If chars is given and not None, remove characters in chars instead.\n\ |
| 2213 | If chars is unicode, S will be converted to unicode before stripping"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2214 | |
| 2215 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2216 | string_rstrip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2217 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 2218 | if (PyTuple_GET_SIZE(args) == 0) |
| 2219 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 2220 | else |
| 2221 | return do_argstrip(self, RIGHTSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2225 | PyDoc_STRVAR(lower__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2226 | "S.lower() -> string\n\ |
| 2227 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2228 | Return a copy of the string S converted to lowercase."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2229 | |
Fredrik Lundh | dfe503d | 2006-05-25 16:10:12 +0000 | [diff] [blame] | 2230 | /* _tolower and _toupper are defined by SUSv2, but they're not ISO C */ |
| 2231 | #ifndef _tolower |
| 2232 | #define _tolower tolower |
| 2233 | #endif |
| 2234 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2235 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2236 | string_lower(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2237 | { |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2238 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2239 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2240 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2241 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2242 | newobj = PyString_FromStringAndSize(NULL, n); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2243 | if (!newobj) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2244 | return NULL; |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2245 | |
| 2246 | s = PyString_AS_STRING(newobj); |
| 2247 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2248 | memcpy(s, PyString_AS_STRING(self), n); |
| 2249 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2250 | for (i = 0; i < n; i++) { |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2251 | int c = Py_CHARMASK(s[i]); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2252 | if (isupper(c)) |
| 2253 | s[i] = _tolower(c); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2254 | } |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2255 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2256 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2259 | PyDoc_STRVAR(upper__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2260 | "S.upper() -> string\n\ |
| 2261 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2262 | Return a copy of the string S converted to uppercase."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2263 | |
Fredrik Lundh | dfe503d | 2006-05-25 16:10:12 +0000 | [diff] [blame] | 2264 | #ifndef _toupper |
| 2265 | #define _toupper toupper |
| 2266 | #endif |
| 2267 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2268 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2269 | string_upper(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2270 | { |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2271 | char *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2272 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2273 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2274 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2275 | newobj = PyString_FromStringAndSize(NULL, n); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2276 | if (!newobj) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2277 | return NULL; |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2278 | |
| 2279 | s = PyString_AS_STRING(newobj); |
| 2280 | |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2281 | memcpy(s, PyString_AS_STRING(self), n); |
| 2282 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2283 | for (i = 0; i < n; i++) { |
Fredrik Lundh | 4b4e33e | 2006-05-25 15:49:45 +0000 | [diff] [blame] | 2284 | int c = Py_CHARMASK(s[i]); |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2285 | if (islower(c)) |
| 2286 | s[i] = _toupper(c); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2287 | } |
Fredrik Lundh | 39ccef6 | 2006-05-25 15:22:03 +0000 | [diff] [blame] | 2288 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2289 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2290 | } |
| 2291 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2292 | PyDoc_STRVAR(title__doc__, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2293 | "S.title() -> string\n\ |
| 2294 | \n\ |
| 2295 | 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] | 2296 | characters, all remaining cased characters have lowercase."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2297 | |
| 2298 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2299 | string_title(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2300 | { |
| 2301 | char *s = PyString_AS_STRING(self), *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2302 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2303 | int previous_is_cased = 0; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2304 | PyObject *newobj; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2305 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2306 | newobj = PyString_FromStringAndSize(NULL, n); |
| 2307 | if (newobj == NULL) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2308 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2309 | s_new = PyString_AsString(newobj); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2310 | for (i = 0; i < n; i++) { |
| 2311 | int c = Py_CHARMASK(*s++); |
| 2312 | if (islower(c)) { |
| 2313 | if (!previous_is_cased) |
| 2314 | c = toupper(c); |
| 2315 | previous_is_cased = 1; |
| 2316 | } else if (isupper(c)) { |
| 2317 | if (previous_is_cased) |
| 2318 | c = tolower(c); |
| 2319 | previous_is_cased = 1; |
| 2320 | } else |
| 2321 | previous_is_cased = 0; |
| 2322 | *s_new++ = c; |
| 2323 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2324 | return newobj; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2327 | PyDoc_STRVAR(capitalize__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2328 | "S.capitalize() -> string\n\ |
| 2329 | \n\ |
| 2330 | 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] | 2331 | capitalized."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2332 | |
| 2333 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2334 | string_capitalize(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2335 | { |
| 2336 | char *s = PyString_AS_STRING(self), *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2337 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2338 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2339 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2340 | newobj = PyString_FromStringAndSize(NULL, n); |
| 2341 | if (newobj == NULL) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2342 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2343 | s_new = PyString_AsString(newobj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2344 | if (0 < n) { |
| 2345 | int c = Py_CHARMASK(*s++); |
| 2346 | if (islower(c)) |
| 2347 | *s_new = toupper(c); |
| 2348 | else |
| 2349 | *s_new = c; |
| 2350 | s_new++; |
| 2351 | } |
| 2352 | for (i = 1; i < n; i++) { |
| 2353 | int c = Py_CHARMASK(*s++); |
| 2354 | if (isupper(c)) |
| 2355 | *s_new = tolower(c); |
| 2356 | else |
| 2357 | *s_new = c; |
| 2358 | s_new++; |
| 2359 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2360 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2364 | PyDoc_STRVAR(count__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2365 | "S.count(sub[, start[, end]]) -> int\n\ |
| 2366 | \n\ |
Fredrik Lundh | 763b50f | 2006-05-22 15:35:12 +0000 | [diff] [blame] | 2367 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 2368 | string S[start:end]. Optional arguments start and end are interpreted\n\ |
| 2369 | as in slice notation."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2370 | |
| 2371 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2372 | string_count(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2373 | { |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 2374 | const char *s = PyString_AS_STRING(self), *sub; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2375 | Py_ssize_t len = PyString_GET_SIZE(self), n; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 2376 | Py_ssize_t i = 0, last = PY_SSIZE_T_MAX; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2377 | Py_ssize_t m, r; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2378 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2379 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 2380 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &subobj, |
| 2381 | _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2382 | return NULL; |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 2383 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2384 | if (PyString_Check(subobj)) { |
| 2385 | sub = PyString_AS_STRING(subobj); |
| 2386 | n = PyString_GET_SIZE(subobj); |
| 2387 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2388 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2389 | else if (PyUnicode_Check(subobj)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2390 | Py_ssize_t count; |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 2391 | count = PyUnicode_Count((PyObject *)self, subobj, i, last); |
| 2392 | if (count == -1) |
| 2393 | return NULL; |
| 2394 | else |
| 2395 | return PyInt_FromLong((long) count); |
| 2396 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2397 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2398 | else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
| 2399 | return NULL; |
| 2400 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 2401 | string_adjust_indices(&i, &last, len); |
| 2402 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2403 | m = last + 1 - n; |
| 2404 | if (n == 0) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2405 | return PyInt_FromSsize_t(m-i); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2406 | |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 2407 | #ifdef USE_FAST |
| 2408 | r = fastsearch(s + i, last - i, sub, n, FAST_COUNT); |
| 2409 | if (r < 0) |
| 2410 | r = 0; /* no match */ |
| 2411 | #else |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2412 | r = 0; |
| 2413 | while (i < m) { |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 2414 | const char *t |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2415 | if (!memcmp(s+i, sub, n)) { |
| 2416 | r++; |
| 2417 | i += n; |
| 2418 | } else { |
| 2419 | i++; |
| 2420 | } |
Raymond Hettinger | 57e7447 | 2005-02-20 09:54:53 +0000 | [diff] [blame] | 2421 | if (i >= m) |
| 2422 | break; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2423 | t = (const char *)memchr(s+i, sub[0], m-i); |
Raymond Hettinger | 57e7447 | 2005-02-20 09:54:53 +0000 | [diff] [blame] | 2424 | if (t == NULL) |
| 2425 | break; |
| 2426 | i = t - s; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2427 | } |
Fredrik Lundh | af72237 | 2006-05-25 17:55:31 +0000 | [diff] [blame] | 2428 | #endif |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2429 | return PyInt_FromSsize_t(r); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2432 | PyDoc_STRVAR(swapcase__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2433 | "S.swapcase() -> string\n\ |
| 2434 | \n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2435 | 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] | 2436 | converted to lowercase and vice versa."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2437 | |
| 2438 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 2439 | string_swapcase(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2440 | { |
| 2441 | char *s = PyString_AS_STRING(self), *s_new; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2442 | Py_ssize_t i, n = PyString_GET_SIZE(self); |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2443 | PyObject *newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2444 | |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2445 | newobj = PyString_FromStringAndSize(NULL, n); |
| 2446 | if (newobj == NULL) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2447 | return NULL; |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2448 | s_new = PyString_AsString(newobj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2449 | for (i = 0; i < n; i++) { |
| 2450 | int c = Py_CHARMASK(*s++); |
| 2451 | if (islower(c)) { |
| 2452 | *s_new = toupper(c); |
| 2453 | } |
| 2454 | else if (isupper(c)) { |
| 2455 | *s_new = tolower(c); |
| 2456 | } |
| 2457 | else |
| 2458 | *s_new = c; |
| 2459 | s_new++; |
| 2460 | } |
Anthony Baxter | a628621 | 2006-04-11 07:42:36 +0000 | [diff] [blame] | 2461 | return newobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2462 | } |
| 2463 | |
| 2464 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2465 | PyDoc_STRVAR(translate__doc__, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2466 | "S.translate(table [,deletechars]) -> string\n\ |
| 2467 | \n\ |
| 2468 | Return a copy of the string S, where all characters occurring\n\ |
| 2469 | in the optional argument deletechars are removed, and the\n\ |
| 2470 | remaining characters have been mapped through the given\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2471 | translation table, which must be a string of length 256."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2472 | |
| 2473 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2474 | string_translate(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2475 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2476 | register char *input, *output; |
| 2477 | register const char *table; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2478 | register Py_ssize_t i, c, changed = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2479 | PyObject *input_obj = (PyObject*)self; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2480 | const char *table1, *output_start, *del_table=NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2481 | Py_ssize_t inlen, tablen, dellen = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2482 | PyObject *result; |
| 2483 | int trans_table[256]; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2484 | PyObject *tableobj, *delobj = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2485 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 2486 | if (!PyArg_UnpackTuple(args, "translate", 1, 2, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2487 | &tableobj, &delobj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2488 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2489 | |
| 2490 | if (PyString_Check(tableobj)) { |
| 2491 | table1 = PyString_AS_STRING(tableobj); |
| 2492 | tablen = PyString_GET_SIZE(tableobj); |
| 2493 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2494 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2495 | else if (PyUnicode_Check(tableobj)) { |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2496 | /* Unicode .translate() does not support the deletechars |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2497 | parameter; instead a mapping to None will cause characters |
| 2498 | to be deleted. */ |
| 2499 | if (delobj != NULL) { |
| 2500 | PyErr_SetString(PyExc_TypeError, |
| 2501 | "deletions are implemented differently for unicode"); |
| 2502 | return NULL; |
| 2503 | } |
| 2504 | return PyUnicode_Translate((PyObject *)self, tableobj, NULL); |
| 2505 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2506 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2507 | else if (PyObject_AsCharBuffer(tableobj, &table1, &tablen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2508 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2509 | |
Martin v. Löwis | 00b6127 | 2002-12-12 20:03:19 +0000 | [diff] [blame] | 2510 | if (tablen != 256) { |
| 2511 | PyErr_SetString(PyExc_ValueError, |
| 2512 | "translation table must be 256 characters long"); |
| 2513 | return NULL; |
| 2514 | } |
| 2515 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2516 | if (delobj != NULL) { |
| 2517 | if (PyString_Check(delobj)) { |
| 2518 | del_table = PyString_AS_STRING(delobj); |
| 2519 | dellen = PyString_GET_SIZE(delobj); |
| 2520 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2521 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2522 | else if (PyUnicode_Check(delobj)) { |
| 2523 | PyErr_SetString(PyExc_TypeError, |
| 2524 | "deletions are implemented differently for unicode"); |
| 2525 | return NULL; |
| 2526 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 2527 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2528 | else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen)) |
| 2529 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2530 | } |
| 2531 | else { |
| 2532 | del_table = NULL; |
| 2533 | dellen = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | table = table1; |
Neal Norwitz | 2aa9a5d | 2006-03-20 01:53:23 +0000 | [diff] [blame] | 2537 | inlen = PyString_GET_SIZE(input_obj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2538 | result = PyString_FromStringAndSize((char *)NULL, inlen); |
| 2539 | if (result == NULL) |
| 2540 | return NULL; |
| 2541 | output_start = output = PyString_AsString(result); |
Neal Norwitz | 2aa9a5d | 2006-03-20 01:53:23 +0000 | [diff] [blame] | 2542 | input = PyString_AS_STRING(input_obj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2543 | |
| 2544 | if (dellen == 0) { |
| 2545 | /* If no deletions are required, use faster code */ |
| 2546 | for (i = inlen; --i >= 0; ) { |
| 2547 | c = Py_CHARMASK(*input++); |
| 2548 | if (Py_CHARMASK((*output++ = table[c])) != c) |
| 2549 | changed = 1; |
| 2550 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2551 | if (changed || !PyString_CheckExact(input_obj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2552 | return result; |
| 2553 | Py_DECREF(result); |
| 2554 | Py_INCREF(input_obj); |
| 2555 | return input_obj; |
| 2556 | } |
| 2557 | |
| 2558 | for (i = 0; i < 256; i++) |
| 2559 | trans_table[i] = Py_CHARMASK(table[i]); |
| 2560 | |
| 2561 | for (i = 0; i < dellen; i++) |
| 2562 | trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
| 2563 | |
| 2564 | for (i = inlen; --i >= 0; ) { |
| 2565 | c = Py_CHARMASK(*input++); |
| 2566 | if (trans_table[c] != -1) |
| 2567 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 2568 | continue; |
| 2569 | changed = 1; |
| 2570 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 2571 | if (!changed && PyString_CheckExact(input_obj)) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2572 | Py_DECREF(result); |
| 2573 | Py_INCREF(input_obj); |
| 2574 | return input_obj; |
| 2575 | } |
| 2576 | /* Fix the size of the resulting string */ |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 2577 | if (inlen > 0) |
| 2578 | _PyString_Resize(&result, output - output_start); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2579 | return result; |
| 2580 | } |
| 2581 | |
| 2582 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2583 | #define FORWARD 1 |
| 2584 | #define REVERSE -1 |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2585 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2586 | /* find and count characters and substrings */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2587 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2588 | /* Don't call if length < 2 */ |
| 2589 | #define Py_STRING_MATCH(target, offset, pattern, length) \ |
| 2590 | (target[offset] == pattern[0] && \ |
| 2591 | target[offset+length-1] == pattern[length-1] && \ |
| 2592 | !memcmp(target+offset+1, pattern+1, length-2) ) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2593 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2594 | #define findchar(target, target_len, c) \ |
| 2595 | ((char *)memchr((const void *)(target), c, target_len)) |
| 2596 | |
| 2597 | /* String ops must return a string. */ |
| 2598 | /* If the object is subclass of string, create a copy */ |
| 2599 | static PyStringObject * |
| 2600 | return_self(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2601 | { |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2602 | if (PyString_CheckExact(self)) { |
| 2603 | Py_INCREF(self); |
| 2604 | return self; |
| 2605 | } |
| 2606 | return (PyStringObject *)PyString_FromStringAndSize( |
| 2607 | PyString_AS_STRING(self), |
| 2608 | PyString_GET_SIZE(self)); |
| 2609 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2610 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2611 | static Py_ssize_t |
| 2612 | countchar(char *target, int target_len, char c) |
| 2613 | { |
| 2614 | Py_ssize_t count=0; |
| 2615 | char *start=target; |
| 2616 | char *end=target+target_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2617 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2618 | while ( (start=findchar(start, end-start, c)) != NULL ) { |
| 2619 | count++; |
| 2620 | start += 1; |
| 2621 | } |
| 2622 | |
| 2623 | return count; |
| 2624 | } |
| 2625 | |
| 2626 | static Py_ssize_t |
| 2627 | findstring(char *target, Py_ssize_t target_len, |
| 2628 | char *pattern, Py_ssize_t pattern_len, |
| 2629 | Py_ssize_t start, |
| 2630 | Py_ssize_t end, |
| 2631 | int direction) |
| 2632 | { |
| 2633 | if (start < 0) { |
| 2634 | start += target_len; |
| 2635 | if (start < 0) |
| 2636 | start = 0; |
| 2637 | } |
| 2638 | if (end > target_len) { |
| 2639 | end = target_len; |
| 2640 | } else if (end < 0) { |
| 2641 | end += target_len; |
| 2642 | if (end < 0) |
| 2643 | end = 0; |
| 2644 | } |
| 2645 | |
| 2646 | /* zero-length substrings always match at the first attempt */ |
| 2647 | if (pattern_len == 0) |
| 2648 | return (direction > 0) ? start : end; |
| 2649 | |
| 2650 | end -= pattern_len; |
| 2651 | |
| 2652 | if (direction < 0) { |
| 2653 | for (; end >= start; end--) |
| 2654 | if (Py_STRING_MATCH(target, end, pattern, pattern_len)) |
| 2655 | return end; |
| 2656 | } else { |
| 2657 | for (; start <= end; start++) |
| 2658 | if (Py_STRING_MATCH(target, start, pattern, pattern_len)) |
| 2659 | return start; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2660 | } |
| 2661 | return -1; |
| 2662 | } |
| 2663 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2664 | Py_ssize_t |
| 2665 | countstring(char *target, Py_ssize_t target_len, |
| 2666 | char *pattern, Py_ssize_t pattern_len, |
| 2667 | Py_ssize_t start, |
| 2668 | Py_ssize_t end, |
| 2669 | int direction) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2670 | { |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2671 | Py_ssize_t count=0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2672 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2673 | if (start < 0) { |
| 2674 | start += target_len; |
| 2675 | if (start < 0) |
| 2676 | start = 0; |
| 2677 | } |
| 2678 | if (end > target_len) { |
| 2679 | end = target_len; |
| 2680 | } else if (end < 0) { |
| 2681 | end += target_len; |
| 2682 | if (end < 0) |
| 2683 | end = 0; |
| 2684 | } |
| 2685 | |
| 2686 | /* zero-length substrings match everywhere */ |
| 2687 | if (pattern_len == 0) |
| 2688 | return target_len+1; |
| 2689 | |
| 2690 | end -= pattern_len; |
| 2691 | |
| 2692 | if (direction < 0) { |
| 2693 | for (; end >= start; end--) |
| 2694 | if (Py_STRING_MATCH(target, end, pattern, pattern_len)) { |
| 2695 | count++; |
| 2696 | end -= pattern_len-1; |
| 2697 | } |
| 2698 | } else { |
| 2699 | for (; start <= end; start++) |
| 2700 | if (Py_STRING_MATCH(target, start, pattern, pattern_len)) { |
| 2701 | count++; |
| 2702 | start += pattern_len-1; |
| 2703 | } |
| 2704 | } |
| 2705 | return count; |
| 2706 | } |
| 2707 | |
| 2708 | |
| 2709 | /* Algorithms for difference cases of string replacement */ |
| 2710 | |
| 2711 | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
| 2712 | static PyStringObject * |
| 2713 | replace_interleave(PyStringObject *self, |
| 2714 | PyStringObject *to, |
| 2715 | Py_ssize_t maxcount) |
| 2716 | { |
| 2717 | char *self_s, *to_s, *result_s; |
| 2718 | Py_ssize_t self_len, to_len, result_len; |
| 2719 | Py_ssize_t count, i, product; |
| 2720 | PyStringObject *result; |
| 2721 | |
| 2722 | self_len = PyString_GET_SIZE(self); |
| 2723 | to_len = PyString_GET_SIZE(to); |
| 2724 | |
| 2725 | /* 1 at the end plus 1 after every character */ |
| 2726 | count = self_len+1; |
| 2727 | if (maxcount < count) |
| 2728 | count = maxcount; |
| 2729 | |
| 2730 | /* Check for overflow */ |
| 2731 | /* result_len = count * to_len + self_len; */ |
| 2732 | product = count * to_len; |
| 2733 | if (product / to_len != count) { |
| 2734 | PyErr_SetString(PyExc_OverflowError, |
| 2735 | "replace string is too long"); |
| 2736 | return NULL; |
| 2737 | } |
| 2738 | result_len = product + self_len; |
| 2739 | if (result_len < 0) { |
| 2740 | PyErr_SetString(PyExc_OverflowError, |
| 2741 | "replace string is too long"); |
| 2742 | return NULL; |
| 2743 | } |
| 2744 | |
| 2745 | if (! (result = (PyStringObject *) |
| 2746 | PyString_FromStringAndSize(NULL, result_len)) ) |
| 2747 | return NULL; |
| 2748 | |
| 2749 | self_s = PyString_AS_STRING(self); |
| 2750 | to_s = PyString_AS_STRING(to); |
| 2751 | to_len = PyString_GET_SIZE(to); |
| 2752 | result_s = PyString_AS_STRING(result); |
| 2753 | |
| 2754 | /* TODO: special case single character, which doesn't need memcpy */ |
| 2755 | |
| 2756 | /* Lay the first one down (guaranteed this will occur) */ |
| 2757 | memcpy(result_s, to_s, to_len); |
| 2758 | result_s += to_len; |
| 2759 | count -= 1; |
| 2760 | |
| 2761 | for (i=0; i<count; i++) { |
| 2762 | *result_s++ = *self_s++; |
| 2763 | memcpy(result_s, to_s, to_len); |
| 2764 | result_s += to_len; |
| 2765 | } |
| 2766 | |
| 2767 | /* Copy the rest of the original string */ |
| 2768 | memcpy(result_s, self_s, self_len-i); |
| 2769 | |
| 2770 | return result; |
| 2771 | } |
| 2772 | |
| 2773 | /* Special case for deleting a single character */ |
| 2774 | /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
| 2775 | static PyStringObject * |
| 2776 | replace_delete_single_character(PyStringObject *self, |
| 2777 | char from_c, Py_ssize_t maxcount) |
| 2778 | { |
| 2779 | char *self_s, *result_s; |
| 2780 | char *start, *next, *end; |
| 2781 | Py_ssize_t self_len, result_len; |
| 2782 | Py_ssize_t count; |
| 2783 | PyStringObject *result; |
| 2784 | |
| 2785 | self_len = PyString_GET_SIZE(self); |
| 2786 | self_s = PyString_AS_STRING(self); |
| 2787 | |
| 2788 | count = countchar(self_s, self_len, from_c); |
| 2789 | if (count == 0) { |
| 2790 | return return_self(self); |
| 2791 | } |
| 2792 | if (count > maxcount) |
| 2793 | count = maxcount; |
| 2794 | |
| 2795 | result_len = self_len - count; /* from_len == 1 */ |
| 2796 | assert(result_len>=0); |
| 2797 | |
| 2798 | if ( (result = (PyStringObject *) |
| 2799 | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
| 2800 | return NULL; |
| 2801 | result_s = PyString_AS_STRING(result); |
| 2802 | |
| 2803 | start = self_s; |
| 2804 | end = self_s + self_len; |
| 2805 | while (count-- > 0) { |
| 2806 | next = findchar(start, end-start, from_c); |
| 2807 | if (next == NULL) |
| 2808 | break; |
| 2809 | memcpy(result_s, start, next-start); |
| 2810 | result_s += (next-start); |
| 2811 | start = next+1; |
| 2812 | } |
| 2813 | memcpy(result_s, start, end-start); |
| 2814 | |
| 2815 | return result; |
| 2816 | } |
| 2817 | |
| 2818 | /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
| 2819 | |
| 2820 | static PyStringObject * |
| 2821 | replace_delete_substring(PyStringObject *self, PyStringObject *from, |
| 2822 | Py_ssize_t maxcount) { |
| 2823 | char *self_s, *from_s, *result_s; |
| 2824 | char *start, *next, *end; |
| 2825 | Py_ssize_t self_len, from_len, result_len; |
| 2826 | Py_ssize_t count, offset; |
| 2827 | PyStringObject *result; |
| 2828 | |
| 2829 | self_len = PyString_GET_SIZE(self); |
| 2830 | self_s = PyString_AS_STRING(self); |
| 2831 | from_len = PyString_GET_SIZE(from); |
| 2832 | from_s = PyString_AS_STRING(from); |
| 2833 | |
| 2834 | count = countstring(self_s, self_len, |
| 2835 | from_s, from_len, |
| 2836 | 0, self_len, 1); |
| 2837 | |
| 2838 | if (count > maxcount) |
| 2839 | count = maxcount; |
| 2840 | |
| 2841 | if (count == 0) { |
| 2842 | /* no matches */ |
| 2843 | return return_self(self); |
| 2844 | } |
| 2845 | |
| 2846 | result_len = self_len - (count * from_len); |
| 2847 | assert (result_len>=0); |
| 2848 | |
| 2849 | if ( (result = (PyStringObject *) |
| 2850 | PyString_FromStringAndSize(NULL, result_len)) == NULL ) |
| 2851 | return NULL; |
| 2852 | |
| 2853 | result_s = PyString_AS_STRING(result); |
| 2854 | |
| 2855 | start = self_s; |
| 2856 | end = self_s + self_len; |
| 2857 | while (count-- > 0) { |
| 2858 | offset = findstring(start, end-start, |
| 2859 | from_s, from_len, |
| 2860 | 0, end-start, FORWARD); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2861 | if (offset == -1) |
| 2862 | break; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2863 | next = start + offset; |
| 2864 | |
| 2865 | memcpy(result_s, start, next-start); |
| 2866 | |
| 2867 | result_s += (next-start); |
| 2868 | start = next+from_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2869 | } |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2870 | memcpy(result_s, start, end-start); |
| 2871 | return result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2874 | /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
| 2875 | static PyStringObject * |
| 2876 | replace_single_character_in_place(PyStringObject *self, |
| 2877 | char from_c, char to_c, |
| 2878 | Py_ssize_t maxcount) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2879 | { |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2880 | char *self_s, *result_s, *start, *end, *next; |
| 2881 | Py_ssize_t self_len; |
| 2882 | PyStringObject *result; |
| 2883 | |
| 2884 | /* The result string will be the same size */ |
| 2885 | self_s = PyString_AS_STRING(self); |
| 2886 | self_len = PyString_GET_SIZE(self); |
| 2887 | |
| 2888 | next = findchar(self_s, self_len, from_c); |
| 2889 | |
| 2890 | if (next == NULL) { |
| 2891 | /* No matches; return the original string */ |
| 2892 | return return_self(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2893 | } |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2894 | |
| 2895 | /* Need to make a new string */ |
Andrew Dalke | 8c90910 | 2006-05-25 17:53:00 +0000 | [diff] [blame] | 2896 | result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2897 | if (result == NULL) |
| 2898 | return NULL; |
| 2899 | result_s = PyString_AS_STRING(result); |
Andrew Dalke | 8c90910 | 2006-05-25 17:53:00 +0000 | [diff] [blame] | 2900 | memcpy(result_s, self_s, self_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2901 | |
| 2902 | /* change everything in-place, starting with this one */ |
| 2903 | start = result_s + (next-self_s); |
| 2904 | *start = to_c; |
| 2905 | start++; |
| 2906 | end = result_s + self_len; |
| 2907 | |
| 2908 | while (--maxcount > 0) { |
| 2909 | next = findchar(start, end-start, from_c); |
| 2910 | if (next == NULL) |
| 2911 | break; |
| 2912 | *next = to_c; |
| 2913 | start = next+1; |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 2914 | } |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2915 | |
| 2916 | return result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2919 | /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
| 2920 | static PyStringObject * |
| 2921 | replace_substring_in_place(PyStringObject *self, |
| 2922 | PyStringObject *from, |
| 2923 | PyStringObject *to, |
| 2924 | Py_ssize_t maxcount) |
| 2925 | { |
| 2926 | char *result_s, *start, *end; |
| 2927 | char *self_s, *from_s, *to_s; |
| 2928 | Py_ssize_t self_len, from_len, offset; |
| 2929 | PyStringObject *result; |
| 2930 | |
| 2931 | /* The result string will be the same size */ |
| 2932 | |
| 2933 | self_s = PyString_AS_STRING(self); |
| 2934 | self_len = PyString_GET_SIZE(self); |
| 2935 | |
| 2936 | from_s = PyString_AS_STRING(from); |
| 2937 | from_len = PyString_GET_SIZE(from); |
| 2938 | to_s = PyString_AS_STRING(to); |
| 2939 | |
| 2940 | offset = findstring(self_s, self_len, |
| 2941 | from_s, from_len, |
| 2942 | 0, self_len, FORWARD); |
| 2943 | |
| 2944 | if (offset == -1) { |
| 2945 | /* No matches; return the original string */ |
| 2946 | return return_self(self); |
| 2947 | } |
| 2948 | |
| 2949 | /* Need to make a new string */ |
Andrew Dalke | 8c90910 | 2006-05-25 17:53:00 +0000 | [diff] [blame] | 2950 | result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len); |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2951 | if (result == NULL) |
| 2952 | return NULL; |
| 2953 | result_s = PyString_AS_STRING(result); |
Andrew Dalke | 8c90910 | 2006-05-25 17:53:00 +0000 | [diff] [blame] | 2954 | memcpy(result_s, self_s, self_len); |
| 2955 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 2956 | |
| 2957 | /* change everything in-place, starting with this one */ |
| 2958 | start = result_s + offset; |
| 2959 | memcpy(start, to_s, from_len); |
| 2960 | start += from_len; |
| 2961 | end = result_s + self_len; |
| 2962 | |
| 2963 | while ( --maxcount > 0) { |
| 2964 | offset = findstring(start, end-start, |
| 2965 | from_s, from_len, |
| 2966 | 0, end-start, FORWARD); |
| 2967 | if (offset==-1) |
| 2968 | break; |
| 2969 | memcpy(start+offset, to_s, from_len); |
| 2970 | start += offset+from_len; |
| 2971 | } |
| 2972 | |
| 2973 | return result; |
| 2974 | } |
| 2975 | |
| 2976 | /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
| 2977 | static PyStringObject * |
| 2978 | replace_single_character(PyStringObject *self, |
| 2979 | char from_c, |
| 2980 | PyStringObject *to, |
| 2981 | Py_ssize_t maxcount) |
| 2982 | { |
| 2983 | char *self_s, *to_s, *result_s; |
| 2984 | char *start, *next, *end; |
| 2985 | Py_ssize_t self_len, to_len, result_len; |
| 2986 | Py_ssize_t count, product; |
| 2987 | PyStringObject *result; |
| 2988 | |
| 2989 | self_s = PyString_AS_STRING(self); |
| 2990 | self_len = PyString_GET_SIZE(self); |
| 2991 | |
| 2992 | count = countchar(self_s, self_len, from_c); |
| 2993 | if (count > maxcount) |
| 2994 | count = maxcount; |
| 2995 | |
| 2996 | if (count == 0) { |
| 2997 | /* no matches, return unchanged */ |
| 2998 | return return_self(self); |
| 2999 | } |
| 3000 | |
| 3001 | to_s = PyString_AS_STRING(to); |
| 3002 | to_len = PyString_GET_SIZE(to); |
| 3003 | |
| 3004 | /* use the difference between current and new, hence the "-1" */ |
| 3005 | /* result_len = self_len + count * (to_len-1) */ |
| 3006 | product = count * (to_len-1); |
| 3007 | if (product / (to_len-1) != count) { |
| 3008 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 3009 | return NULL; |
| 3010 | } |
| 3011 | result_len = self_len + product; |
| 3012 | if (result_len < 0) { |
| 3013 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 3014 | return NULL; |
| 3015 | } |
| 3016 | |
| 3017 | if ( (result = (PyStringObject *) |
| 3018 | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
| 3019 | return NULL; |
| 3020 | result_s = PyString_AS_STRING(result); |
| 3021 | |
| 3022 | start = self_s; |
| 3023 | end = self_s + self_len; |
| 3024 | while (count-- > 0) { |
| 3025 | next = findchar(start, end-start, from_c); |
| 3026 | if (next == NULL) |
| 3027 | break; |
| 3028 | |
| 3029 | if (next == start) { |
| 3030 | /* replace with the 'to' */ |
| 3031 | memcpy(result_s, to_s, to_len); |
| 3032 | result_s += to_len; |
| 3033 | start += 1; |
| 3034 | } else { |
| 3035 | /* copy the unchanged old then the 'to' */ |
| 3036 | memcpy(result_s, start, next-start); |
| 3037 | result_s += (next-start); |
| 3038 | memcpy(result_s, to_s, to_len); |
| 3039 | result_s += to_len; |
| 3040 | start = next+1; |
| 3041 | } |
| 3042 | } |
| 3043 | /* Copy the remainder of the remaining string */ |
| 3044 | memcpy(result_s, start, end-start); |
| 3045 | |
| 3046 | return result; |
| 3047 | } |
| 3048 | |
| 3049 | /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
| 3050 | static PyStringObject * |
| 3051 | replace_substring(PyStringObject *self, |
| 3052 | PyStringObject *from, |
| 3053 | PyStringObject *to, |
| 3054 | Py_ssize_t maxcount) { |
| 3055 | char *self_s, *from_s, *to_s, *result_s; |
| 3056 | char *start, *next, *end; |
| 3057 | Py_ssize_t self_len, from_len, to_len, result_len; |
| 3058 | Py_ssize_t count, offset, product; |
| 3059 | PyStringObject *result; |
| 3060 | |
| 3061 | self_s = PyString_AS_STRING(self); |
| 3062 | self_len = PyString_GET_SIZE(self); |
| 3063 | from_s = PyString_AS_STRING(from); |
| 3064 | from_len = PyString_GET_SIZE(from); |
| 3065 | |
| 3066 | count = countstring(self_s, self_len, |
| 3067 | from_s, from_len, |
| 3068 | 0, self_len, FORWARD); |
| 3069 | if (count > maxcount) |
| 3070 | count = maxcount; |
| 3071 | |
| 3072 | if (count == 0) { |
| 3073 | /* no matches, return unchanged */ |
| 3074 | return return_self(self); |
| 3075 | } |
| 3076 | |
| 3077 | to_s = PyString_AS_STRING(to); |
| 3078 | to_len = PyString_GET_SIZE(to); |
| 3079 | |
| 3080 | /* Check for overflow */ |
| 3081 | /* result_len = self_len + count * (to_len-from_len) */ |
| 3082 | product = count * (to_len-from_len); |
| 3083 | if (product / (to_len-from_len) != count) { |
| 3084 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 3085 | return NULL; |
| 3086 | } |
| 3087 | result_len = self_len + product; |
| 3088 | if (result_len < 0) { |
| 3089 | PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
| 3090 | return NULL; |
| 3091 | } |
| 3092 | |
| 3093 | if ( (result = (PyStringObject *) |
| 3094 | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
| 3095 | return NULL; |
| 3096 | result_s = PyString_AS_STRING(result); |
| 3097 | |
| 3098 | start = self_s; |
| 3099 | end = self_s + self_len; |
| 3100 | while (count-- > 0) { |
| 3101 | offset = findstring(start, end-start, |
| 3102 | from_s, from_len, |
| 3103 | 0, end-start, FORWARD); |
| 3104 | if (offset == -1) |
| 3105 | break; |
| 3106 | next = start+offset; |
| 3107 | if (next == start) { |
| 3108 | /* replace with the 'to' */ |
| 3109 | memcpy(result_s, to_s, to_len); |
| 3110 | result_s += to_len; |
| 3111 | start += from_len; |
| 3112 | } else { |
| 3113 | /* copy the unchanged old then the 'to' */ |
| 3114 | memcpy(result_s, start, next-start); |
| 3115 | result_s += (next-start); |
| 3116 | memcpy(result_s, to_s, to_len); |
| 3117 | result_s += to_len; |
| 3118 | start = next+from_len; |
| 3119 | } |
| 3120 | } |
| 3121 | /* Copy the remainder of the remaining string */ |
| 3122 | memcpy(result_s, start, end-start); |
| 3123 | |
| 3124 | return result; |
| 3125 | } |
| 3126 | |
| 3127 | |
| 3128 | static PyStringObject * |
| 3129 | replace(PyStringObject *self, |
| 3130 | PyStringObject *from, |
| 3131 | PyStringObject *to, |
| 3132 | Py_ssize_t maxcount) |
| 3133 | { |
| 3134 | Py_ssize_t from_len, to_len; |
| 3135 | |
| 3136 | if (maxcount < 0) { |
| 3137 | maxcount = PY_SSIZE_T_MAX; |
| 3138 | } else if (maxcount == 0 || PyString_GET_SIZE(self) == 0) { |
| 3139 | /* nothing to do; return the original string */ |
| 3140 | return return_self(self); |
| 3141 | } |
| 3142 | |
| 3143 | from_len = PyString_GET_SIZE(from); |
| 3144 | to_len = PyString_GET_SIZE(to); |
| 3145 | |
| 3146 | if (maxcount == 0 || |
| 3147 | (from_len == 0 && to_len == 0)) { |
| 3148 | /* nothing to do; return the original string */ |
| 3149 | return return_self(self); |
| 3150 | } |
| 3151 | |
| 3152 | /* Handle zero-length special cases */ |
| 3153 | |
| 3154 | if (from_len == 0) { |
| 3155 | /* insert the 'to' string everywhere. */ |
| 3156 | /* >>> "Python".replace("", ".") */ |
| 3157 | /* '.P.y.t.h.o.n.' */ |
| 3158 | return replace_interleave(self, to, maxcount); |
| 3159 | } |
| 3160 | |
| 3161 | /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
| 3162 | /* point for an empty self string to generate a non-empty string */ |
| 3163 | /* Special case so the remaining code always gets a non-empty string */ |
| 3164 | if (PyString_GET_SIZE(self) == 0) { |
| 3165 | return return_self(self); |
| 3166 | } |
| 3167 | |
| 3168 | if (to_len == 0) { |
| 3169 | /* delete all occurances of 'from' string */ |
| 3170 | if (from_len == 1) { |
| 3171 | return replace_delete_single_character( |
| 3172 | self, PyString_AS_STRING(from)[0], maxcount); |
| 3173 | } else { |
| 3174 | return replace_delete_substring(self, from, maxcount); |
| 3175 | } |
| 3176 | } |
| 3177 | |
| 3178 | /* Handle special case where both strings have the same length */ |
| 3179 | |
| 3180 | if (from_len == to_len) { |
| 3181 | if (from_len == 1) { |
| 3182 | return replace_single_character_in_place( |
| 3183 | self, |
| 3184 | PyString_AS_STRING(from)[0], |
| 3185 | PyString_AS_STRING(to)[0], |
| 3186 | maxcount); |
| 3187 | } else { |
| 3188 | return replace_substring_in_place( |
| 3189 | self, from, to, maxcount); |
| 3190 | } |
| 3191 | } |
| 3192 | |
| 3193 | /* Otherwise use the more generic algorithms */ |
| 3194 | if (from_len == 1) { |
| 3195 | return replace_single_character(self, PyString_AS_STRING(from)[0], |
| 3196 | to, maxcount); |
| 3197 | } else { |
| 3198 | /* len('from')>=2, len('to')>=1 */ |
| 3199 | return replace_substring(self, from, to, maxcount); |
| 3200 | } |
| 3201 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3202 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3203 | PyDoc_STRVAR(replace__doc__, |
Fred Drake | d22bb65 | 2003-10-22 02:56:40 +0000 | [diff] [blame] | 3204 | "S.replace (old, new[, count]) -> string\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3205 | \n\ |
| 3206 | Return a copy of string S with all occurrences of substring\n\ |
Fred Drake | d22bb65 | 2003-10-22 02:56:40 +0000 | [diff] [blame] | 3207 | old replaced by new. If the optional argument count is\n\ |
| 3208 | given, only the first count occurrences are replaced."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3209 | |
| 3210 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3211 | string_replace(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3212 | { |
Thomas Wouters | dc5f808 | 2006-04-19 15:38:01 +0000 | [diff] [blame] | 3213 | Py_ssize_t count = -1; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3214 | PyObject *from, *to; |
Jack Diederich | 60cbb3f | 2006-05-25 18:47:15 +0000 | [diff] [blame] | 3215 | const char *tmp_s; |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3216 | Py_ssize_t tmp_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3217 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3218 | if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3219 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3220 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3221 | if (PyString_Check(from)) { |
| 3222 | /* Can this be made a '!check' after the Unicode check? */ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3223 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3224 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3225 | if (PyUnicode_Check(from)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3226 | return PyUnicode_Replace((PyObject *)self, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3227 | from, to, count); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3228 | #endif |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3229 | else if (PyObject_AsCharBuffer(from, &tmp_s, &tmp_len)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3230 | return NULL; |
| 3231 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3232 | if (PyString_Check(to)) { |
| 3233 | /* Can this be made a '!check' after the Unicode check? */ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3234 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3235 | #ifdef Py_USING_UNICODE |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3236 | else if (PyUnicode_Check(to)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3237 | return PyUnicode_Replace((PyObject *)self, |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3238 | from, to, count); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3239 | #endif |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3240 | else if (PyObject_AsCharBuffer(to, &tmp_s, &tmp_len)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3241 | return NULL; |
| 3242 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3243 | return (PyObject *)replace((PyStringObject *) self, |
| 3244 | (PyStringObject *) from, |
| 3245 | (PyStringObject *) to, count); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
Fredrik Lundh | e68955c | 2006-05-25 17:08:14 +0000 | [diff] [blame] | 3248 | /** End DALKE **/ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3249 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3250 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3251 | "S.startswith(prefix[, start[, end]]) -> bool\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3252 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 3253 | Return True if S starts with the specified prefix, False otherwise.\n\ |
| 3254 | With optional start, test S beginning at that position.\n\ |
| 3255 | With optional end, stop comparing S at that position."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3256 | |
| 3257 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3258 | string_startswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3259 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3260 | const char* str = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3261 | Py_ssize_t len = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3262 | const char* prefix; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3263 | Py_ssize_t plen; |
| 3264 | Py_ssize_t start = 0; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 3265 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3266 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3267 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 3268 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 3269 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3270 | return NULL; |
| 3271 | if (PyString_Check(subobj)) { |
| 3272 | prefix = PyString_AS_STRING(subobj); |
| 3273 | plen = PyString_GET_SIZE(subobj); |
| 3274 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3275 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3276 | else if (PyUnicode_Check(subobj)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3277 | Py_ssize_t rc; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3278 | rc = PyUnicode_Tailmatch((PyObject *)self, |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3279 | subobj, start, end, -1); |
| 3280 | if (rc == -1) |
| 3281 | return NULL; |
| 3282 | else |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3283 | return PyBool_FromLong((long) rc); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3284 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3285 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3286 | else if (PyObject_AsCharBuffer(subobj, &prefix, &plen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3287 | return NULL; |
| 3288 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 3289 | string_adjust_indices(&start, &end, len); |
| 3290 | |
| 3291 | if (start+plen > len) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3292 | return PyBool_FromLong(0); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3293 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 3294 | if (end-start >= plen) |
| 3295 | return PyBool_FromLong(!memcmp(str+start, prefix, plen)); |
| 3296 | else |
| 3297 | return PyBool_FromLong(0); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3301 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3302 | "S.endswith(suffix[, start[, end]]) -> bool\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3303 | \n\ |
Guido van Rossum | a713218 | 2003-04-09 19:32:45 +0000 | [diff] [blame] | 3304 | Return True if S ends with the specified suffix, False otherwise.\n\ |
| 3305 | With optional start, test S beginning at that position.\n\ |
| 3306 | With optional end, stop comparing S at that position."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3307 | |
| 3308 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3309 | string_endswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3310 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3311 | const char* str = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3312 | Py_ssize_t len = PyString_GET_SIZE(self); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3313 | const char* suffix; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3314 | Py_ssize_t slen; |
| 3315 | Py_ssize_t start = 0; |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 3316 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3317 | PyObject *subobj; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3318 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 3319 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 3320 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3321 | return NULL; |
| 3322 | if (PyString_Check(subobj)) { |
| 3323 | suffix = PyString_AS_STRING(subobj); |
| 3324 | slen = PyString_GET_SIZE(subobj); |
| 3325 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3326 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3327 | else if (PyUnicode_Check(subobj)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3328 | Py_ssize_t rc; |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3329 | rc = PyUnicode_Tailmatch((PyObject *)self, |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3330 | subobj, start, end, +1); |
| 3331 | if (rc == -1) |
| 3332 | return NULL; |
| 3333 | else |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3334 | return PyBool_FromLong((long) rc); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 3335 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 3336 | #endif |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3337 | else if (PyObject_AsCharBuffer(subobj, &suffix, &slen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3338 | return NULL; |
| 3339 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 3340 | string_adjust_indices(&start, &end, len); |
| 3341 | |
| 3342 | if (end-start < slen || start > len) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3343 | return PyBool_FromLong(0); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3344 | |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 3345 | if (end-slen > start) |
| 3346 | start = end - slen; |
| 3347 | if (end-start >= slen) |
| 3348 | return PyBool_FromLong(!memcmp(str+start, suffix, slen)); |
| 3349 | else |
| 3350 | return PyBool_FromLong(0); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3354 | PyDoc_STRVAR(encode__doc__, |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3355 | "S.encode([encoding[,errors]]) -> object\n\ |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3356 | \n\ |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3357 | Encodes S using the codec registered for encoding. encoding defaults\n\ |
| 3358 | 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] | 3359 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3360 | a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
| 3361 | 'xmlcharrefreplace' as well as any other name registered with\n\ |
| 3362 | codecs.register_error that is able to handle UnicodeEncodeErrors."); |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3363 | |
| 3364 | static PyObject * |
| 3365 | string_encode(PyStringObject *self, PyObject *args) |
| 3366 | { |
| 3367 | char *encoding = NULL; |
| 3368 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3369 | PyObject *v; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 3370 | |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3371 | if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
| 3372 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3373 | v = PyString_AsEncodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3374 | if (v == NULL) |
| 3375 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3376 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 3377 | PyErr_Format(PyExc_TypeError, |
| 3378 | "encoder did not return a string/unicode object " |
| 3379 | "(type=%.400s)", |
| 3380 | v->ob_type->tp_name); |
| 3381 | Py_DECREF(v); |
| 3382 | return NULL; |
| 3383 | } |
| 3384 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3385 | |
| 3386 | onError: |
| 3387 | return NULL; |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3391 | PyDoc_STRVAR(decode__doc__, |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3392 | "S.decode([encoding[,errors]]) -> object\n\ |
| 3393 | \n\ |
| 3394 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 3395 | to the default encoding. errors may be given to set a different error\n\ |
| 3396 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 3397 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
| 3398 | as well as any other name registerd with codecs.register_error that is\n\ |
| 3399 | able to handle UnicodeDecodeErrors."); |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3400 | |
| 3401 | static PyObject * |
| 3402 | string_decode(PyStringObject *self, PyObject *args) |
| 3403 | { |
| 3404 | char *encoding = NULL; |
| 3405 | char *errors = NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3406 | PyObject *v; |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 3407 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 3408 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 3409 | return NULL; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3410 | v = PyString_AsDecodedObject((PyObject *)self, encoding, errors); |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3411 | if (v == NULL) |
| 3412 | goto onError; |
Marc-André Lemburg | d2d4598 | 2004-07-08 17:57:32 +0000 | [diff] [blame] | 3413 | if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
| 3414 | PyErr_Format(PyExc_TypeError, |
| 3415 | "decoder did not return a string/unicode object " |
| 3416 | "(type=%.400s)", |
| 3417 | v->ob_type->tp_name); |
| 3418 | Py_DECREF(v); |
| 3419 | return NULL; |
| 3420 | } |
| 3421 | return v; |
Marc-André Lemburg | 1dffb12 | 2004-07-08 19:13:55 +0000 | [diff] [blame] | 3422 | |
| 3423 | onError: |
| 3424 | return NULL; |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
| 3427 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3428 | PyDoc_STRVAR(expandtabs__doc__, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3429 | "S.expandtabs([tabsize]) -> string\n\ |
| 3430 | \n\ |
| 3431 | 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] | 3432 | 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] | 3433 | |
| 3434 | static PyObject* |
| 3435 | string_expandtabs(PyStringObject *self, PyObject *args) |
| 3436 | { |
| 3437 | const char *e, *p; |
| 3438 | char *q; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3439 | Py_ssize_t i, j; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3440 | PyObject *u; |
| 3441 | int tabsize = 8; |
| 3442 | |
| 3443 | if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
| 3444 | return NULL; |
| 3445 | |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 3446 | /* First pass: determine size of output string */ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3447 | i = j = 0; |
| 3448 | e = PyString_AS_STRING(self) + PyString_GET_SIZE(self); |
| 3449 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 3450 | if (*p == '\t') { |
| 3451 | if (tabsize > 0) |
| 3452 | j += tabsize - (j % tabsize); |
| 3453 | } |
| 3454 | else { |
| 3455 | j++; |
| 3456 | if (*p == '\n' || *p == '\r') { |
| 3457 | i += j; |
| 3458 | j = 0; |
| 3459 | } |
| 3460 | } |
| 3461 | |
| 3462 | /* Second pass: create output string and fill it */ |
| 3463 | u = PyString_FromStringAndSize(NULL, i + j); |
| 3464 | if (!u) |
| 3465 | return NULL; |
| 3466 | |
| 3467 | j = 0; |
| 3468 | q = PyString_AS_STRING(u); |
| 3469 | |
| 3470 | for (p = PyString_AS_STRING(self); p < e; p++) |
| 3471 | if (*p == '\t') { |
| 3472 | if (tabsize > 0) { |
| 3473 | i = tabsize - (j % tabsize); |
| 3474 | j += i; |
| 3475 | while (i--) |
| 3476 | *q++ = ' '; |
| 3477 | } |
| 3478 | } |
| 3479 | else { |
| 3480 | j++; |
| 3481 | *q++ = *p; |
| 3482 | if (*p == '\n' || *p == '\r') |
| 3483 | j = 0; |
| 3484 | } |
| 3485 | |
| 3486 | return u; |
| 3487 | } |
| 3488 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3489 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3490 | pad(PyStringObject *self, Py_ssize_t left, Py_ssize_t right, char fill) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3491 | { |
| 3492 | PyObject *u; |
| 3493 | |
| 3494 | if (left < 0) |
| 3495 | left = 0; |
| 3496 | if (right < 0) |
| 3497 | right = 0; |
| 3498 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3499 | if (left == 0 && right == 0 && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3500 | Py_INCREF(self); |
| 3501 | return (PyObject *)self; |
| 3502 | } |
| 3503 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3504 | u = PyString_FromStringAndSize(NULL, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3505 | left + PyString_GET_SIZE(self) + right); |
| 3506 | if (u) { |
| 3507 | if (left) |
| 3508 | memset(PyString_AS_STRING(u), fill, left); |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3509 | memcpy(PyString_AS_STRING(u) + left, |
| 3510 | PyString_AS_STRING(self), |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3511 | PyString_GET_SIZE(self)); |
| 3512 | if (right) |
| 3513 | memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self), |
| 3514 | fill, right); |
| 3515 | } |
| 3516 | |
| 3517 | return u; |
| 3518 | } |
| 3519 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3520 | PyDoc_STRVAR(ljust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3521 | "S.ljust(width[, fillchar]) -> string\n" |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3522 | "\n" |
| 3523 | "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] | 3524 | "done using the specified fill character (default is a space)."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3525 | |
| 3526 | static PyObject * |
| 3527 | string_ljust(PyStringObject *self, PyObject *args) |
| 3528 | { |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3529 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3530 | char fillchar = ' '; |
| 3531 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3532 | if (!PyArg_ParseTuple(args, "n|c:ljust", &width, &fillchar)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3533 | return NULL; |
| 3534 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3535 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3536 | Py_INCREF(self); |
| 3537 | return (PyObject*) self; |
| 3538 | } |
| 3539 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3540 | return pad(self, 0, width - PyString_GET_SIZE(self), fillchar); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3544 | PyDoc_STRVAR(rjust__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3545 | "S.rjust(width[, fillchar]) -> string\n" |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3546 | "\n" |
| 3547 | "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] | 3548 | "done using the specified fill character (default is a space)"); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3549 | |
| 3550 | static PyObject * |
| 3551 | string_rjust(PyStringObject *self, PyObject *args) |
| 3552 | { |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3553 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3554 | char fillchar = ' '; |
| 3555 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3556 | if (!PyArg_ParseTuple(args, "n|c:rjust", &width, &fillchar)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3557 | return NULL; |
| 3558 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3559 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3560 | Py_INCREF(self); |
| 3561 | return (PyObject*) self; |
| 3562 | } |
| 3563 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3564 | return pad(self, width - PyString_GET_SIZE(self), 0, fillchar); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3565 | } |
| 3566 | |
| 3567 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3568 | PyDoc_STRVAR(center__doc__, |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3569 | "S.center(width[, fillchar]) -> string\n" |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3570 | "\n" |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3571 | "Return S centered in a string of length width. Padding is\n" |
| 3572 | "done using the specified fill character (default is a space)"); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3573 | |
| 3574 | static PyObject * |
| 3575 | string_center(PyStringObject *self, PyObject *args) |
| 3576 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3577 | Py_ssize_t marg, left; |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3578 | Py_ssize_t width; |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3579 | char fillchar = ' '; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3580 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3581 | if (!PyArg_ParseTuple(args, "n|c:center", &width, &fillchar)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3582 | return NULL; |
| 3583 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 3584 | if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3585 | Py_INCREF(self); |
| 3586 | return (PyObject*) self; |
| 3587 | } |
| 3588 | |
| 3589 | marg = width - PyString_GET_SIZE(self); |
| 3590 | left = marg / 2 + (marg & width & 1); |
| 3591 | |
Raymond Hettinger | 4f8f976 | 2003-11-26 08:21:35 +0000 | [diff] [blame] | 3592 | return pad(self, left, marg - left, fillchar); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3593 | } |
| 3594 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3595 | PyDoc_STRVAR(zfill__doc__, |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3596 | "S.zfill(width) -> string\n" |
| 3597 | "\n" |
| 3598 | "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] | 3599 | "of the specified width. The string S is never truncated."); |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3600 | |
| 3601 | static PyObject * |
| 3602 | string_zfill(PyStringObject *self, PyObject *args) |
| 3603 | { |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 3604 | Py_ssize_t fill; |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3605 | PyObject *s; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3606 | char *p; |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3607 | Py_ssize_t width; |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3608 | |
Thomas Wouters | 4abb366 | 2006-04-19 14:50:15 +0000 | [diff] [blame] | 3609 | if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3610 | return NULL; |
| 3611 | |
| 3612 | if (PyString_GET_SIZE(self) >= width) { |
Walter Dörwald | 0fe940c | 2002-04-15 18:42:15 +0000 | [diff] [blame] | 3613 | if (PyString_CheckExact(self)) { |
| 3614 | Py_INCREF(self); |
| 3615 | return (PyObject*) self; |
| 3616 | } |
| 3617 | else |
| 3618 | return PyString_FromStringAndSize( |
| 3619 | PyString_AS_STRING(self), |
| 3620 | PyString_GET_SIZE(self) |
| 3621 | ); |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 3622 | } |
| 3623 | |
| 3624 | fill = width - PyString_GET_SIZE(self); |
| 3625 | |
| 3626 | s = pad(self, fill, 0, '0'); |
| 3627 | |
| 3628 | if (s == NULL) |
| 3629 | return NULL; |
| 3630 | |
| 3631 | p = PyString_AS_STRING(s); |
| 3632 | if (p[fill] == '+' || p[fill] == '-') { |
| 3633 | /* move sign to beginning of string */ |
| 3634 | p[0] = p[fill]; |
| 3635 | p[fill] = '0'; |
| 3636 | } |
| 3637 | |
| 3638 | return (PyObject*) s; |
| 3639 | } |
| 3640 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3641 | PyDoc_STRVAR(isspace__doc__, |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3642 | "S.isspace() -> bool\n\ |
| 3643 | \n\ |
| 3644 | Return True if all characters in S are whitespace\n\ |
| 3645 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3646 | |
| 3647 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3648 | string_isspace(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3649 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3650 | register const unsigned char *p |
| 3651 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3652 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3653 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3654 | /* Shortcut for single character strings */ |
| 3655 | if (PyString_GET_SIZE(self) == 1 && |
| 3656 | isspace(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3657 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3658 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3659 | /* Special case for empty strings */ |
| 3660 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3661 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3662 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3663 | e = p + PyString_GET_SIZE(self); |
| 3664 | for (; p < e; p++) { |
| 3665 | if (!isspace(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3666 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3667 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3668 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
| 3671 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3672 | PyDoc_STRVAR(isalpha__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3673 | "S.isalpha() -> bool\n\ |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3674 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3675 | Return True if all characters in S are alphabetic\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3676 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3677 | |
| 3678 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3679 | string_isalpha(PyStringObject *self) |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3680 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3681 | register const unsigned char *p |
| 3682 | = (unsigned char *) PyString_AS_STRING(self); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3683 | register const unsigned char *e; |
| 3684 | |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3685 | /* Shortcut for single character strings */ |
| 3686 | if (PyString_GET_SIZE(self) == 1 && |
| 3687 | isalpha(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3688 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3689 | |
| 3690 | /* Special case for empty strings */ |
| 3691 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3692 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3693 | |
| 3694 | e = p + PyString_GET_SIZE(self); |
| 3695 | for (; p < e; p++) { |
| 3696 | if (!isalpha(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3697 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3698 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3699 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3700 | } |
| 3701 | |
| 3702 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3703 | PyDoc_STRVAR(isalnum__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3704 | "S.isalnum() -> bool\n\ |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3705 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3706 | Return True if all characters in S are alphanumeric\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3707 | and there is at least one character in S, False otherwise."); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3708 | |
| 3709 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3710 | string_isalnum(PyStringObject *self) |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3711 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3712 | register const unsigned char *p |
| 3713 | = (unsigned char *) PyString_AS_STRING(self); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3714 | register const unsigned char *e; |
| 3715 | |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3716 | /* Shortcut for single character strings */ |
| 3717 | if (PyString_GET_SIZE(self) == 1 && |
| 3718 | isalnum(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3719 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3720 | |
| 3721 | /* Special case for empty strings */ |
| 3722 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3723 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3724 | |
| 3725 | e = p + PyString_GET_SIZE(self); |
| 3726 | for (; p < e; p++) { |
| 3727 | if (!isalnum(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3728 | return PyBool_FromLong(0); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3729 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3730 | return PyBool_FromLong(1); |
Marc-André Lemburg | 4027f8f | 2000-07-05 09:47:46 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
| 3733 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3734 | PyDoc_STRVAR(isdigit__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3735 | "S.isdigit() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3736 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3737 | Return True if all characters in S are digits\n\ |
| 3738 | and there is at least one character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3739 | |
| 3740 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3741 | string_isdigit(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3742 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3743 | register const unsigned char *p |
| 3744 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3745 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3746 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3747 | /* Shortcut for single character strings */ |
| 3748 | if (PyString_GET_SIZE(self) == 1 && |
| 3749 | isdigit(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3750 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3751 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3752 | /* Special case for empty strings */ |
| 3753 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3754 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3755 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3756 | e = p + PyString_GET_SIZE(self); |
| 3757 | for (; p < e; p++) { |
| 3758 | if (!isdigit(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3759 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3760 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3761 | return PyBool_FromLong(1); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
| 3764 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3765 | PyDoc_STRVAR(islower__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3766 | "S.islower() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3767 | \n\ |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3768 | 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] | 3769 | at least one cased character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3770 | |
| 3771 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3772 | string_islower(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3773 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3774 | register const unsigned char *p |
| 3775 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3776 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3777 | int cased; |
| 3778 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3779 | /* Shortcut for single character strings */ |
| 3780 | if (PyString_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3781 | return PyBool_FromLong(islower(*p) != 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3782 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3783 | /* Special case for empty strings */ |
| 3784 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3785 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3786 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3787 | e = p + PyString_GET_SIZE(self); |
| 3788 | cased = 0; |
| 3789 | for (; p < e; p++) { |
| 3790 | if (isupper(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3791 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3792 | else if (!cased && islower(*p)) |
| 3793 | cased = 1; |
| 3794 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3795 | return PyBool_FromLong(cased); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3796 | } |
| 3797 | |
| 3798 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3799 | PyDoc_STRVAR(isupper__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3800 | "S.isupper() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3801 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3802 | 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] | 3803 | at least one cased character in S, False otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3804 | |
| 3805 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3806 | string_isupper(PyStringObject *self) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3807 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3808 | register const unsigned char *p |
| 3809 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3810 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3811 | int cased; |
| 3812 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3813 | /* Shortcut for single character strings */ |
| 3814 | if (PyString_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3815 | return PyBool_FromLong(isupper(*p) != 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3816 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3817 | /* Special case for empty strings */ |
| 3818 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3819 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3820 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3821 | e = p + PyString_GET_SIZE(self); |
| 3822 | cased = 0; |
| 3823 | for (; p < e; p++) { |
| 3824 | if (islower(*p)) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3825 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3826 | else if (!cased && isupper(*p)) |
| 3827 | cased = 1; |
| 3828 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3829 | return PyBool_FromLong(cased); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3830 | } |
| 3831 | |
| 3832 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3833 | PyDoc_STRVAR(istitle__doc__, |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3834 | "S.istitle() -> bool\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3835 | \n\ |
Martin v. Löwis | 6828e18 | 2003-10-18 09:55:08 +0000 | [diff] [blame] | 3836 | Return True if S is a titlecased string and there is at least one\n\ |
| 3837 | character in S, i.e. uppercase characters may only follow uncased\n\ |
| 3838 | characters and lowercase characters only cased ones. Return False\n\ |
| 3839 | otherwise."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3840 | |
| 3841 | static PyObject* |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3842 | string_istitle(PyStringObject *self, PyObject *uncased) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3843 | { |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3844 | register const unsigned char *p |
| 3845 | = (unsigned char *) PyString_AS_STRING(self); |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3846 | register const unsigned char *e; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3847 | int cased, previous_is_cased; |
| 3848 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3849 | /* Shortcut for single character strings */ |
| 3850 | if (PyString_GET_SIZE(self) == 1) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3851 | return PyBool_FromLong(isupper(*p) != 0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3852 | |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3853 | /* Special case for empty strings */ |
| 3854 | if (PyString_GET_SIZE(self) == 0) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3855 | return PyBool_FromLong(0); |
Marc-André Lemburg | 60bc809 | 2000-06-14 09:18:32 +0000 | [diff] [blame] | 3856 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3857 | e = p + PyString_GET_SIZE(self); |
| 3858 | cased = 0; |
| 3859 | previous_is_cased = 0; |
| 3860 | for (; p < e; p++) { |
Guido van Rossum | b8f820c | 2000-05-05 20:44:24 +0000 | [diff] [blame] | 3861 | register const unsigned char ch = *p; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3862 | |
| 3863 | if (isupper(ch)) { |
| 3864 | if (previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3865 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3866 | previous_is_cased = 1; |
| 3867 | cased = 1; |
| 3868 | } |
| 3869 | else if (islower(ch)) { |
| 3870 | if (!previous_is_cased) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3871 | return PyBool_FromLong(0); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3872 | previous_is_cased = 1; |
| 3873 | cased = 1; |
| 3874 | } |
| 3875 | else |
| 3876 | previous_is_cased = 0; |
| 3877 | } |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 3878 | return PyBool_FromLong(cased); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3879 | } |
| 3880 | |
| 3881 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3882 | PyDoc_STRVAR(splitlines__doc__, |
Fred Drake | 2bae4fa | 2001-10-13 15:57:55 +0000 | [diff] [blame] | 3883 | "S.splitlines([keepends]) -> list of strings\n\ |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3884 | \n\ |
| 3885 | 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] | 3886 | 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] | 3887 | is given and true."); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3888 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3889 | static PyObject* |
| 3890 | string_splitlines(PyStringObject *self, PyObject *args) |
| 3891 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3892 | register Py_ssize_t i; |
| 3893 | register Py_ssize_t j; |
| 3894 | Py_ssize_t len; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3895 | int keepends = 0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3896 | PyObject *list; |
| 3897 | PyObject *str; |
| 3898 | char *data; |
| 3899 | |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3900 | if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3901 | return NULL; |
| 3902 | |
| 3903 | data = PyString_AS_STRING(self); |
| 3904 | len = PyString_GET_SIZE(self); |
| 3905 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3906 | list = PyList_New(0); |
| 3907 | if (!list) |
| 3908 | goto onError; |
| 3909 | |
| 3910 | for (i = j = 0; i < len; ) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3911 | Py_ssize_t eol; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3912 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3913 | /* Find a line and append it */ |
| 3914 | while (i < len && data[i] != '\n' && data[i] != '\r') |
| 3915 | i++; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3916 | |
| 3917 | /* Skip the line break reading CRLF as one line break */ |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3918 | eol = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3919 | if (i < len) { |
| 3920 | if (data[i] == '\r' && i + 1 < len && |
| 3921 | data[i+1] == '\n') |
| 3922 | i += 2; |
| 3923 | else |
| 3924 | i++; |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3925 | if (keepends) |
| 3926 | eol = i; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3927 | } |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 3928 | SPLIT_APPEND(data, j, eol); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3929 | j = i; |
| 3930 | } |
| 3931 | if (j < len) { |
| 3932 | SPLIT_APPEND(data, j, len); |
| 3933 | } |
| 3934 | |
| 3935 | return list; |
| 3936 | |
| 3937 | onError: |
Hye-Shik Chang | 4af5c8c | 2006-03-07 15:39:21 +0000 | [diff] [blame] | 3938 | Py_XDECREF(list); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3939 | return NULL; |
| 3940 | } |
| 3941 | |
| 3942 | #undef SPLIT_APPEND |
| 3943 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3944 | static PyObject * |
| 3945 | string_getnewargs(PyStringObject *v) |
| 3946 | { |
| 3947 | return Py_BuildValue("(s#)", v->ob_sval, v->ob_size); |
| 3948 | } |
| 3949 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3950 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 3951 | static PyMethodDef |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3952 | string_methods[] = { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 3953 | /* Counterparts of the obsolete stropmodule functions; except |
| 3954 | string.maketrans(). */ |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3955 | {"join", (PyCFunction)string_join, METH_O, join__doc__}, |
| 3956 | {"split", (PyCFunction)string_split, METH_VARARGS, split__doc__}, |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 3957 | {"rsplit", (PyCFunction)string_rsplit, METH_VARARGS, rsplit__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3958 | {"lower", (PyCFunction)string_lower, METH_NOARGS, lower__doc__}, |
| 3959 | {"upper", (PyCFunction)string_upper, METH_NOARGS, upper__doc__}, |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 3960 | {"islower", (PyCFunction)string_islower, METH_NOARGS, islower__doc__}, |
| 3961 | {"isupper", (PyCFunction)string_isupper, METH_NOARGS, isupper__doc__}, |
| 3962 | {"isspace", (PyCFunction)string_isspace, METH_NOARGS, isspace__doc__}, |
| 3963 | {"isdigit", (PyCFunction)string_isdigit, METH_NOARGS, isdigit__doc__}, |
| 3964 | {"istitle", (PyCFunction)string_istitle, METH_NOARGS, istitle__doc__}, |
| 3965 | {"isalpha", (PyCFunction)string_isalpha, METH_NOARGS, isalpha__doc__}, |
| 3966 | {"isalnum", (PyCFunction)string_isalnum, METH_NOARGS, isalnum__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3967 | {"capitalize", (PyCFunction)string_capitalize, METH_NOARGS, |
| 3968 | capitalize__doc__}, |
| 3969 | {"count", (PyCFunction)string_count, METH_VARARGS, count__doc__}, |
| 3970 | {"endswith", (PyCFunction)string_endswith, METH_VARARGS, |
| 3971 | endswith__doc__}, |
Fredrik Lundh | fe5bb7e | 2006-05-25 23:27:53 +0000 | [diff] [blame] | 3972 | {"partition", (PyCFunction)string_partition, METH_VARARGS, |
| 3973 | partition__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 3974 | {"find", (PyCFunction)string_find, METH_VARARGS, find__doc__}, |
| 3975 | {"index", (PyCFunction)string_index, METH_VARARGS, index__doc__}, |
| 3976 | {"lstrip", (PyCFunction)string_lstrip, METH_VARARGS, lstrip__doc__}, |
| 3977 | {"replace", (PyCFunction)string_replace, METH_VARARGS, replace__doc__}, |
| 3978 | {"rfind", (PyCFunction)string_rfind, METH_VARARGS, rfind__doc__}, |
| 3979 | {"rindex", (PyCFunction)string_rindex, METH_VARARGS, rindex__doc__}, |
| 3980 | {"rstrip", (PyCFunction)string_rstrip, METH_VARARGS, rstrip__doc__}, |
| 3981 | {"startswith", (PyCFunction)string_startswith, METH_VARARGS, |
| 3982 | startswith__doc__}, |
| 3983 | {"strip", (PyCFunction)string_strip, METH_VARARGS, strip__doc__}, |
| 3984 | {"swapcase", (PyCFunction)string_swapcase, METH_NOARGS, |
| 3985 | swapcase__doc__}, |
| 3986 | {"translate", (PyCFunction)string_translate, METH_VARARGS, |
| 3987 | translate__doc__}, |
| 3988 | {"title", (PyCFunction)string_title, METH_NOARGS, title__doc__}, |
| 3989 | {"ljust", (PyCFunction)string_ljust, METH_VARARGS, ljust__doc__}, |
| 3990 | {"rjust", (PyCFunction)string_rjust, METH_VARARGS, rjust__doc__}, |
| 3991 | {"center", (PyCFunction)string_center, METH_VARARGS, center__doc__}, |
| 3992 | {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__}, |
| 3993 | {"encode", (PyCFunction)string_encode, METH_VARARGS, encode__doc__}, |
| 3994 | {"decode", (PyCFunction)string_decode, METH_VARARGS, decode__doc__}, |
| 3995 | {"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, |
| 3996 | expandtabs__doc__}, |
| 3997 | {"splitlines", (PyCFunction)string_splitlines, METH_VARARGS, |
| 3998 | splitlines__doc__}, |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 3999 | {"__getnewargs__", (PyCFunction)string_getnewargs, METH_NOARGS}, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 4000 | {NULL, NULL} /* sentinel */ |
| 4001 | }; |
| 4002 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 4003 | static PyObject * |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 4004 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 4005 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 4006 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4007 | string_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 4008 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4009 | PyObject *x = NULL; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 4010 | static char *kwlist[] = {"object", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4011 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 4012 | if (type != &PyString_Type) |
| 4013 | return str_subtype_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4014 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x)) |
| 4015 | return NULL; |
| 4016 | if (x == NULL) |
| 4017 | return PyString_FromString(""); |
| 4018 | return PyObject_Str(x); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 4019 | } |
| 4020 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 4021 | static PyObject * |
| 4022 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4023 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 4024 | PyObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4025 | Py_ssize_t n; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 4026 | |
| 4027 | assert(PyType_IsSubtype(type, &PyString_Type)); |
| 4028 | tmp = string_new(&PyString_Type, args, kwds); |
| 4029 | if (tmp == NULL) |
| 4030 | return NULL; |
Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 4031 | assert(PyString_CheckExact(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 4032 | n = PyString_GET_SIZE(tmp); |
| 4033 | pnew = type->tp_alloc(type, n); |
| 4034 | if (pnew != NULL) { |
| 4035 | memcpy(PyString_AS_STRING(pnew), PyString_AS_STRING(tmp), n+1); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 4036 | ((PyStringObject *)pnew)->ob_shash = |
| 4037 | ((PyStringObject *)tmp)->ob_shash; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 4038 | ((PyStringObject *)pnew)->ob_sstate = SSTATE_NOT_INTERNED; |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 4039 | } |
Guido van Rossum | 29d55a3 | 2001-08-31 16:11:15 +0000 | [diff] [blame] | 4040 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 4041 | return pnew; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 4042 | } |
| 4043 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 4044 | static PyObject * |
| 4045 | basestring_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4046 | { |
| 4047 | PyErr_SetString(PyExc_TypeError, |
Neal Norwitz | 32a7e7f | 2002-05-31 19:58:02 +0000 | [diff] [blame] | 4048 | "The basestring type cannot be instantiated"); |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 4049 | return NULL; |
| 4050 | } |
| 4051 | |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 4052 | static PyObject * |
| 4053 | string_mod(PyObject *v, PyObject *w) |
| 4054 | { |
| 4055 | if (!PyString_Check(v)) { |
| 4056 | Py_INCREF(Py_NotImplemented); |
| 4057 | return Py_NotImplemented; |
| 4058 | } |
| 4059 | return PyString_Format(v, w); |
| 4060 | } |
| 4061 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4062 | PyDoc_STRVAR(basestring_doc, |
| 4063 | "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] | 4064 | |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 4065 | static PyNumberMethods string_as_number = { |
| 4066 | 0, /*nb_add*/ |
| 4067 | 0, /*nb_subtract*/ |
| 4068 | 0, /*nb_multiply*/ |
| 4069 | 0, /*nb_divide*/ |
| 4070 | string_mod, /*nb_remainder*/ |
| 4071 | }; |
| 4072 | |
| 4073 | |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 4074 | PyTypeObject PyBaseString_Type = { |
| 4075 | PyObject_HEAD_INIT(&PyType_Type) |
| 4076 | 0, |
Neal Norwitz | 32a7e7f | 2002-05-31 19:58:02 +0000 | [diff] [blame] | 4077 | "basestring", |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 4078 | 0, |
| 4079 | 0, |
| 4080 | 0, /* tp_dealloc */ |
| 4081 | 0, /* tp_print */ |
| 4082 | 0, /* tp_getattr */ |
| 4083 | 0, /* tp_setattr */ |
| 4084 | 0, /* tp_compare */ |
| 4085 | 0, /* tp_repr */ |
| 4086 | 0, /* tp_as_number */ |
| 4087 | 0, /* tp_as_sequence */ |
| 4088 | 0, /* tp_as_mapping */ |
| 4089 | 0, /* tp_hash */ |
| 4090 | 0, /* tp_call */ |
| 4091 | 0, /* tp_str */ |
| 4092 | 0, /* tp_getattro */ |
| 4093 | 0, /* tp_setattro */ |
| 4094 | 0, /* tp_as_buffer */ |
| 4095 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4096 | basestring_doc, /* tp_doc */ |
| 4097 | 0, /* tp_traverse */ |
| 4098 | 0, /* tp_clear */ |
| 4099 | 0, /* tp_richcompare */ |
| 4100 | 0, /* tp_weaklistoffset */ |
| 4101 | 0, /* tp_iter */ |
| 4102 | 0, /* tp_iternext */ |
| 4103 | 0, /* tp_methods */ |
| 4104 | 0, /* tp_members */ |
| 4105 | 0, /* tp_getset */ |
| 4106 | &PyBaseObject_Type, /* tp_base */ |
| 4107 | 0, /* tp_dict */ |
| 4108 | 0, /* tp_descr_get */ |
| 4109 | 0, /* tp_descr_set */ |
| 4110 | 0, /* tp_dictoffset */ |
| 4111 | 0, /* tp_init */ |
| 4112 | 0, /* tp_alloc */ |
| 4113 | basestring_new, /* tp_new */ |
| 4114 | 0, /* tp_free */ |
| 4115 | }; |
| 4116 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4117 | PyDoc_STRVAR(string_doc, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4118 | "str(object) -> string\n\ |
| 4119 | \n\ |
| 4120 | Return a nice string representation of the object.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 4121 | 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] | 4122 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4123 | PyTypeObject PyString_Type = { |
| 4124 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4125 | 0, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4126 | "str", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4127 | sizeof(PyStringObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4128 | sizeof(char), |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 4129 | string_dealloc, /* tp_dealloc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4130 | (printfunc)string_print, /* tp_print */ |
| 4131 | 0, /* tp_getattr */ |
| 4132 | 0, /* tp_setattr */ |
| 4133 | 0, /* tp_compare */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 4134 | string_repr, /* tp_repr */ |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 4135 | &string_as_number, /* tp_as_number */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4136 | &string_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 4137 | &string_as_mapping, /* tp_as_mapping */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4138 | (hashfunc)string_hash, /* tp_hash */ |
| 4139 | 0, /* tp_call */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 4140 | string_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4141 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4142 | 0, /* tp_setattro */ |
| 4143 | &string_as_buffer, /* tp_as_buffer */ |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 4144 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
Neil Schemenauer | a6cd4e6 | 2002-11-18 16:09:38 +0000 | [diff] [blame] | 4145 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4146 | string_doc, /* tp_doc */ |
| 4147 | 0, /* tp_traverse */ |
| 4148 | 0, /* tp_clear */ |
| 4149 | (richcmpfunc)string_richcompare, /* tp_richcompare */ |
| 4150 | 0, /* tp_weaklistoffset */ |
| 4151 | 0, /* tp_iter */ |
| 4152 | 0, /* tp_iternext */ |
| 4153 | string_methods, /* tp_methods */ |
| 4154 | 0, /* tp_members */ |
| 4155 | 0, /* tp_getset */ |
Guido van Rossum | cacfc07 | 2002-05-24 19:01:59 +0000 | [diff] [blame] | 4156 | &PyBaseString_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 4157 | 0, /* tp_dict */ |
| 4158 | 0, /* tp_descr_get */ |
| 4159 | 0, /* tp_descr_set */ |
| 4160 | 0, /* tp_dictoffset */ |
| 4161 | 0, /* tp_init */ |
| 4162 | 0, /* tp_alloc */ |
| 4163 | string_new, /* tp_new */ |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 4164 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4165 | }; |
| 4166 | |
| 4167 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4168 | PyString_Concat(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4169 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4170 | register PyObject *v; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4171 | if (*pv == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4172 | return; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4173 | if (w == NULL || !PyString_Check(*pv)) { |
| 4174 | Py_DECREF(*pv); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4175 | *pv = NULL; |
| 4176 | return; |
| 4177 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4178 | v = string_concat((PyStringObject *) *pv, w); |
| 4179 | Py_DECREF(*pv); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4180 | *pv = v; |
| 4181 | } |
| 4182 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4183 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4184 | PyString_ConcatAndDel(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4185 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4186 | PyString_Concat(pv, w); |
| 4187 | Py_XDECREF(w); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4188 | } |
| 4189 | |
| 4190 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4191 | /* The following function breaks the notion that strings are immutable: |
| 4192 | it changes the size of a string. We get away with this only if there |
| 4193 | is only one module referencing the object. You can also think of it |
| 4194 | as creating a new string object and destroying the old one, only |
| 4195 | 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] | 4196 | already be known to some other part of the code... |
| 4197 | Note that if there's not enough memory to resize the string, the original |
| 4198 | string object at *pv is deallocated, *pv is set to NULL, an "out of |
| 4199 | memory" exception is set, and -1 is returned. Else (on success) 0 is |
| 4200 | returned, and the value in *pv may or may not be the same as on input. |
| 4201 | As always, an extra byte is allocated for a trailing \0 byte (newsize |
| 4202 | does *not* include that), and a trailing \0 byte is stored. |
| 4203 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4204 | |
| 4205 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4206 | _PyString_Resize(PyObject **pv, Py_ssize_t newsize) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4207 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4208 | register PyObject *v; |
| 4209 | register PyStringObject *sv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 4210 | v = *pv; |
Armin Rigo | 618fbf5 | 2004-08-07 20:58:32 +0000 | [diff] [blame] | 4211 | if (!PyString_Check(v) || v->ob_refcnt != 1 || newsize < 0 || |
| 4212 | PyString_CHECK_INTERNED(v)) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4213 | *pv = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4214 | Py_DECREF(v); |
| 4215 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 4216 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4217 | } |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 4218 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 4219 | _Py_DEC_REFTOTAL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4220 | _Py_ForgetReference(v); |
| 4221 | *pv = (PyObject *) |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 4222 | PyObject_REALLOC((char *)v, sizeof(PyStringObject) + newsize); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4223 | if (*pv == NULL) { |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 4224 | PyObject_Del(v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4225 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 4226 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4227 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4228 | _Py_NewReference(*pv); |
| 4229 | sv = (PyStringObject *) *pv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 4230 | sv->ob_size = newsize; |
| 4231 | sv->ob_sval[newsize] = '\0'; |
Raymond Hettinger | 561fbf1 | 2004-10-26 01:52:37 +0000 | [diff] [blame] | 4232 | sv->ob_shash = -1; /* invalidate cached hash value */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4233 | return 0; |
| 4234 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4235 | |
| 4236 | /* Helpers for formatstring */ |
| 4237 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4238 | static PyObject * |
Thomas Wouters | 977485d | 2006-02-16 15:59:12 +0000 | [diff] [blame] | 4239 | getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4240 | { |
Thomas Wouters | 977485d | 2006-02-16 15:59:12 +0000 | [diff] [blame] | 4241 | Py_ssize_t argidx = *p_argidx; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4242 | if (argidx < arglen) { |
| 4243 | (*p_argidx)++; |
| 4244 | if (arglen < 0) |
| 4245 | return args; |
| 4246 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4247 | return PyTuple_GetItem(args, argidx); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4248 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4249 | PyErr_SetString(PyExc_TypeError, |
| 4250 | "not enough arguments for format string"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4251 | return NULL; |
| 4252 | } |
| 4253 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4254 | /* Format codes |
| 4255 | * F_LJUST '-' |
| 4256 | * F_SIGN '+' |
| 4257 | * F_BLANK ' ' |
| 4258 | * F_ALT '#' |
| 4259 | * F_ZERO '0' |
| 4260 | */ |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4261 | #define F_LJUST (1<<0) |
| 4262 | #define F_SIGN (1<<1) |
| 4263 | #define F_BLANK (1<<2) |
| 4264 | #define F_ALT (1<<3) |
| 4265 | #define F_ZERO (1<<4) |
| 4266 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4267 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4268 | formatfloat(char *buf, size_t buflen, int flags, |
| 4269 | int prec, int type, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4270 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4271 | /* fmt = '%#.' + `prec` + `type` |
| 4272 | 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] | 4273 | char fmt[20]; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4274 | double x; |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4275 | x = PyFloat_AsDouble(v); |
| 4276 | if (x == -1.0 && PyErr_Occurred()) { |
| 4277 | PyErr_SetString(PyExc_TypeError, "float argument required"); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4278 | return -1; |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4279 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4280 | if (prec < 0) |
| 4281 | prec = 6; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4282 | if (type == 'f' && fabs(x)/1e25 >= 1e25) |
| 4283 | type = 'g'; |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4284 | /* Worst case length calc to ensure no buffer overrun: |
| 4285 | |
| 4286 | 'g' formats: |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4287 | fmt = %#.<prec>g |
| 4288 | buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4289 | for any double rep.) |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4290 | len = 1 + prec + 1 + 2 + 5 = 9 + prec |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4291 | |
| 4292 | 'f' formats: |
| 4293 | buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
| 4294 | len = 1 + 50 + 1 + prec = 52 + prec |
| 4295 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4296 | If prec=0 the effective precision is 1 (the leading digit is |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 4297 | always given), therefore increase the length by one. |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4298 | |
| 4299 | */ |
| 4300 | if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || |
| 4301 | (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4302 | PyErr_SetString(PyExc_OverflowError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 4303 | "formatted float is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4304 | return -1; |
| 4305 | } |
Marc-André Lemburg | 79f5783 | 2002-12-29 19:44:06 +0000 | [diff] [blame] | 4306 | PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
| 4307 | (flags&F_ALT) ? "#" : "", |
| 4308 | prec, type); |
Martin v. Löwis | 737ea82 | 2004-06-08 18:52:54 +0000 | [diff] [blame] | 4309 | PyOS_ascii_formatd(buf, buflen, fmt, x); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4310 | return (int)strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4313 | /* _PyString_FormatLong emulates the format codes d, u, o, x and X, and |
| 4314 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 4315 | * Python's regular ints. |
| 4316 | * Return value: a new PyString*, or NULL if error. |
| 4317 | * . *pbuf is set to point into it, |
| 4318 | * *plen set to the # of chars following that. |
| 4319 | * Caller must decref it when done using pbuf. |
| 4320 | * The string starting at *pbuf is of the form |
| 4321 | * "-"? ("0x" | "0X")? digit+ |
| 4322 | * "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] | 4323 | * set in flags. The case of hex digits will be correct, |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4324 | * There will be at least prec digits, zero-filled on the left if |
| 4325 | * necessary to get that many. |
| 4326 | * val object to be converted |
| 4327 | * flags bitmask of format flags; only F_ALT is looked at |
| 4328 | * prec minimum number of digits; 0-fill on left if needed |
| 4329 | * type a character in [duoxX]; u acts the same as d |
| 4330 | * |
| 4331 | * CAUTION: o, x and X conversions on regular ints can never |
| 4332 | * produce a '-' sign, but can for Python's unbounded ints. |
| 4333 | */ |
| 4334 | PyObject* |
| 4335 | _PyString_FormatLong(PyObject *val, int flags, int prec, int type, |
| 4336 | char **pbuf, int *plen) |
| 4337 | { |
| 4338 | PyObject *result = NULL; |
| 4339 | char *buf; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4340 | Py_ssize_t i; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4341 | int sign; /* 1 if '-', else 0 */ |
| 4342 | int len; /* number of characters */ |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4343 | Py_ssize_t llen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4344 | int numdigits; /* len == numnondigits + numdigits */ |
| 4345 | int numnondigits = 0; |
| 4346 | |
| 4347 | switch (type) { |
| 4348 | case 'd': |
| 4349 | case 'u': |
| 4350 | result = val->ob_type->tp_str(val); |
| 4351 | break; |
| 4352 | case 'o': |
| 4353 | result = val->ob_type->tp_as_number->nb_oct(val); |
| 4354 | break; |
| 4355 | case 'x': |
| 4356 | case 'X': |
| 4357 | numnondigits = 2; |
| 4358 | result = val->ob_type->tp_as_number->nb_hex(val); |
| 4359 | break; |
| 4360 | default: |
| 4361 | assert(!"'type' not in [duoxX]"); |
| 4362 | } |
| 4363 | if (!result) |
| 4364 | return NULL; |
| 4365 | |
| 4366 | /* To modify the string in-place, there can only be one reference. */ |
| 4367 | if (result->ob_refcnt != 1) { |
| 4368 | PyErr_BadInternalCall(); |
| 4369 | return NULL; |
| 4370 | } |
| 4371 | buf = PyString_AsString(result); |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4372 | llen = PyString_Size(result); |
Martin v. Löwis | 8ce358f | 2006-04-13 07:22:51 +0000 | [diff] [blame] | 4373 | if (llen > PY_SSIZE_T_MAX) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4374 | PyErr_SetString(PyExc_ValueError, "string too large in _PyString_FormatLong"); |
| 4375 | return NULL; |
| 4376 | } |
| 4377 | len = (int)llen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4378 | if (buf[len-1] == 'L') { |
| 4379 | --len; |
| 4380 | buf[len] = '\0'; |
| 4381 | } |
| 4382 | sign = buf[0] == '-'; |
| 4383 | numnondigits += sign; |
| 4384 | numdigits = len - numnondigits; |
| 4385 | assert(numdigits > 0); |
| 4386 | |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4387 | /* Get rid of base marker unless F_ALT */ |
| 4388 | if ((flags & F_ALT) == 0) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4389 | /* Need to skip 0x, 0X or 0. */ |
| 4390 | int skipped = 0; |
| 4391 | switch (type) { |
| 4392 | case 'o': |
| 4393 | assert(buf[sign] == '0'); |
| 4394 | /* If 0 is only digit, leave it alone. */ |
| 4395 | if (numdigits > 1) { |
| 4396 | skipped = 1; |
| 4397 | --numdigits; |
| 4398 | } |
| 4399 | break; |
| 4400 | case 'x': |
| 4401 | case 'X': |
| 4402 | assert(buf[sign] == '0'); |
| 4403 | assert(buf[sign + 1] == 'x'); |
| 4404 | skipped = 2; |
| 4405 | numnondigits -= 2; |
| 4406 | break; |
| 4407 | } |
| 4408 | if (skipped) { |
| 4409 | buf += skipped; |
| 4410 | len -= skipped; |
| 4411 | if (sign) |
| 4412 | buf[0] = '-'; |
| 4413 | } |
| 4414 | assert(len == numnondigits + numdigits); |
| 4415 | assert(numdigits > 0); |
| 4416 | } |
| 4417 | |
| 4418 | /* Fill with leading zeroes to meet minimum width. */ |
| 4419 | if (prec > numdigits) { |
| 4420 | PyObject *r1 = PyString_FromStringAndSize(NULL, |
| 4421 | numnondigits + prec); |
| 4422 | char *b1; |
| 4423 | if (!r1) { |
| 4424 | Py_DECREF(result); |
| 4425 | return NULL; |
| 4426 | } |
| 4427 | b1 = PyString_AS_STRING(r1); |
| 4428 | for (i = 0; i < numnondigits; ++i) |
| 4429 | *b1++ = *buf++; |
| 4430 | for (i = 0; i < prec - numdigits; i++) |
| 4431 | *b1++ = '0'; |
| 4432 | for (i = 0; i < numdigits; i++) |
| 4433 | *b1++ = *buf++; |
| 4434 | *b1 = '\0'; |
| 4435 | Py_DECREF(result); |
| 4436 | result = r1; |
| 4437 | buf = PyString_AS_STRING(result); |
| 4438 | len = numnondigits + prec; |
| 4439 | } |
| 4440 | |
| 4441 | /* Fix up case for hex conversions. */ |
Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 4442 | if (type == 'X') { |
| 4443 | /* Need to convert all lower case letters to upper case. |
| 4444 | and need to convert 0x to 0X (and -0x to -0X). */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4445 | for (i = 0; i < len; i++) |
Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 4446 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 4447 | buf[i] -= 'a'-'A'; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4448 | } |
| 4449 | *pbuf = buf; |
| 4450 | *plen = len; |
| 4451 | return result; |
| 4452 | } |
| 4453 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4454 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4455 | formatint(char *buf, size_t buflen, int flags, |
| 4456 | int prec, int type, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4457 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4458 | /* fmt = '%#.' + `prec` + 'l' + `type` |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4459 | worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
| 4460 | + 1 + 1 = 24 */ |
| 4461 | char fmt[64]; /* plenty big enough! */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4462 | char *sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4463 | long x; |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4464 | |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4465 | x = PyInt_AsLong(v); |
| 4466 | if (x == -1 && PyErr_Occurred()) { |
| 4467 | PyErr_SetString(PyExc_TypeError, "int argument required"); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4468 | return -1; |
Neal Norwitz | 88fe4ff | 2002-07-28 16:44:23 +0000 | [diff] [blame] | 4469 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4470 | if (x < 0 && type == 'u') { |
| 4471 | type = 'd'; |
Guido van Rossum | 078151d | 2002-08-11 04:24:12 +0000 | [diff] [blame] | 4472 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4473 | if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
| 4474 | sign = "-"; |
| 4475 | else |
| 4476 | sign = ""; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4477 | if (prec < 0) |
| 4478 | prec = 1; |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4479 | |
| 4480 | if ((flags & F_ALT) && |
| 4481 | (type == 'x' || type == 'X')) { |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4482 | /* When converting under %#x or %#X, there are a number |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4483 | * of issues that cause pain: |
| 4484 | * - when 0 is being converted, the C standard leaves off |
| 4485 | * the '0x' or '0X', which is inconsistent with other |
| 4486 | * %#x/%#X conversions and inconsistent with Python's |
| 4487 | * hex() function |
| 4488 | * - there are platforms that violate the standard and |
| 4489 | * convert 0 with the '0x' or '0X' |
| 4490 | * (Metrowerks, Compaq Tru64) |
| 4491 | * - there are platforms that give '0x' when converting |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4492 | * under %#X, but convert 0 in accordance with the |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4493 | * standard (OS/2 EMX) |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4494 | * |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4495 | * We can achieve the desired consistency by inserting our |
| 4496 | * own '0x' or '0X' prefix, and substituting %x/%X in place |
| 4497 | * of %#x/%#X. |
| 4498 | * |
| 4499 | * Note that this is the same approach as used in |
| 4500 | * formatint() in unicodeobject.c |
| 4501 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4502 | PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
| 4503 | sign, type, prec, type); |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4504 | } |
| 4505 | else { |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4506 | PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
| 4507 | sign, (flags&F_ALT) ? "#" : "", |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4508 | prec, type); |
| 4509 | } |
| 4510 | |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4511 | /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
| 4512 | * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
Andrew MacIntyre | 5e9c80d | 2002-02-28 11:38:24 +0000 | [diff] [blame] | 4513 | */ |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4514 | if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4515 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4516 | "formatted integer is too long (precision too large?)"); |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4517 | return -1; |
| 4518 | } |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4519 | if (sign[0]) |
| 4520 | PyOS_snprintf(buf, buflen, fmt, -x); |
| 4521 | else |
| 4522 | PyOS_snprintf(buf, buflen, fmt, x); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4523 | return (int)strlen(buf); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4524 | } |
| 4525 | |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4526 | static int |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4527 | formatchar(char *buf, size_t buflen, PyObject *v) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4528 | { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4529 | /* presume that the buffer is at least 2 characters long */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4530 | if (PyString_Check(v)) { |
| 4531 | 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] | 4532 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4533 | } |
| 4534 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4535 | 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] | 4536 | return -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4537 | } |
| 4538 | buf[1] = '\0'; |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4539 | return 1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4540 | } |
| 4541 | |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4542 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 4543 | |
| 4544 | FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
| 4545 | chars are formatted. XXX This is a magic number. Each formatting |
| 4546 | routine does bounds checking to ensure no overflow, but a better |
| 4547 | solution may be to malloc a buffer of appropriate size for each |
| 4548 | format. For now, the current solution is sufficient. |
| 4549 | */ |
| 4550 | #define FORMATBUFLEN (size_t)120 |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4551 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4552 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 4553 | PyString_Format(PyObject *format, PyObject *args) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4554 | { |
| 4555 | char *fmt, *res; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4556 | Py_ssize_t arglen, argidx; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4557 | Py_ssize_t reslen, rescnt, fmtcnt; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4558 | int args_owned = 0; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4559 | PyObject *result, *orig_args; |
| 4560 | #ifdef Py_USING_UNICODE |
| 4561 | PyObject *v, *w; |
| 4562 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4563 | PyObject *dict = NULL; |
| 4564 | if (format == NULL || !PyString_Check(format) || args == NULL) { |
| 4565 | PyErr_BadInternalCall(); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4566 | return NULL; |
| 4567 | } |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4568 | orig_args = args; |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4569 | fmt = PyString_AS_STRING(format); |
| 4570 | fmtcnt = PyString_GET_SIZE(format); |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 4571 | reslen = rescnt = fmtcnt + 100; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4572 | result = PyString_FromStringAndSize((char *)NULL, reslen); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4573 | if (result == NULL) |
| 4574 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4575 | res = PyString_AsString(result); |
| 4576 | if (PyTuple_Check(args)) { |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4577 | arglen = PyTuple_GET_SIZE(args); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4578 | argidx = 0; |
| 4579 | } |
| 4580 | else { |
| 4581 | arglen = -1; |
| 4582 | argidx = -2; |
| 4583 | } |
Neal Norwitz | 80a1bf4 | 2002-11-12 23:01:12 +0000 | [diff] [blame] | 4584 | if (args->ob_type->tp_as_mapping && !PyTuple_Check(args) && |
| 4585 | !PyObject_TypeCheck(args, &PyBaseString_Type)) |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4586 | dict = args; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4587 | while (--fmtcnt >= 0) { |
| 4588 | if (*fmt != '%') { |
| 4589 | if (--rescnt < 0) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 4590 | rescnt = fmtcnt + 100; |
| 4591 | reslen += rescnt; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4592 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4593 | return NULL; |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4594 | res = PyString_AS_STRING(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4595 | + reslen - rescnt; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4596 | --rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4597 | } |
| 4598 | *res++ = *fmt++; |
| 4599 | } |
| 4600 | else { |
| 4601 | /* Got a format specifier */ |
| 4602 | int flags = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4603 | Py_ssize_t width = -1; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4604 | int prec = -1; |
Guido van Rossum | 6938a29 | 1993-11-11 14:51:57 +0000 | [diff] [blame] | 4605 | int c = '\0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4606 | int fill; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4607 | PyObject *v = NULL; |
| 4608 | PyObject *temp = NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4609 | char *pbuf; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4610 | int sign; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4611 | Py_ssize_t len; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4612 | char formatbuf[FORMATBUFLEN]; |
| 4613 | /* For format{float,int,char}() */ |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4614 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4615 | char *fmt_start = fmt; |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4616 | Py_ssize_t argidx_start = argidx; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4617 | #endif |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4618 | |
Guido van Rossum | da9c271 | 1996-12-05 21:58:58 +0000 | [diff] [blame] | 4619 | fmt++; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4620 | if (*fmt == '(') { |
| 4621 | char *keystart; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 4622 | Py_ssize_t keylen; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4623 | PyObject *key; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4624 | int pcount = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4625 | |
| 4626 | if (dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4627 | PyErr_SetString(PyExc_TypeError, |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 4628 | "format requires a mapping"); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4629 | goto error; |
| 4630 | } |
| 4631 | ++fmt; |
| 4632 | --fmtcnt; |
| 4633 | keystart = fmt; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4634 | /* Skip over balanced parentheses */ |
| 4635 | while (pcount > 0 && --fmtcnt >= 0) { |
| 4636 | if (*fmt == ')') |
| 4637 | --pcount; |
| 4638 | else if (*fmt == '(') |
| 4639 | ++pcount; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4640 | fmt++; |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4641 | } |
| 4642 | keylen = fmt - keystart - 1; |
| 4643 | if (fmtcnt < 0 || pcount > 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4644 | PyErr_SetString(PyExc_ValueError, |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4645 | "incomplete format key"); |
| 4646 | goto error; |
| 4647 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4648 | key = PyString_FromStringAndSize(keystart, |
| 4649 | keylen); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4650 | if (key == NULL) |
| 4651 | goto error; |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4652 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4653 | Py_DECREF(args); |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4654 | args_owned = 0; |
| 4655 | } |
| 4656 | args = PyObject_GetItem(dict, key); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4657 | Py_DECREF(key); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4658 | if (args == NULL) { |
| 4659 | goto error; |
| 4660 | } |
Guido van Rossum | 993952b | 1996-05-21 22:44:20 +0000 | [diff] [blame] | 4661 | args_owned = 1; |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4662 | arglen = -1; |
| 4663 | argidx = -2; |
| 4664 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4665 | while (--fmtcnt >= 0) { |
| 4666 | switch (c = *fmt++) { |
| 4667 | case '-': flags |= F_LJUST; continue; |
| 4668 | case '+': flags |= F_SIGN; continue; |
| 4669 | case ' ': flags |= F_BLANK; continue; |
| 4670 | case '#': flags |= F_ALT; continue; |
| 4671 | case '0': flags |= F_ZERO; continue; |
| 4672 | } |
| 4673 | break; |
| 4674 | } |
| 4675 | if (c == '*') { |
| 4676 | v = getnextarg(args, arglen, &argidx); |
| 4677 | if (v == NULL) |
| 4678 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4679 | if (!PyInt_Check(v)) { |
| 4680 | PyErr_SetString(PyExc_TypeError, |
| 4681 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4682 | goto error; |
| 4683 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4684 | width = PyInt_AsLong(v); |
Guido van Rossum | 98c9eba | 1999-06-07 15:12:32 +0000 | [diff] [blame] | 4685 | if (width < 0) { |
| 4686 | flags |= F_LJUST; |
| 4687 | width = -width; |
| 4688 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4689 | if (--fmtcnt >= 0) |
| 4690 | c = *fmt++; |
| 4691 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4692 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4693 | width = c - '0'; |
| 4694 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4695 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4696 | if (!isdigit(c)) |
| 4697 | break; |
| 4698 | if ((width*10) / 10 != width) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4699 | PyErr_SetString( |
| 4700 | PyExc_ValueError, |
| 4701 | "width too big"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4702 | goto error; |
| 4703 | } |
| 4704 | width = width*10 + (c - '0'); |
| 4705 | } |
| 4706 | } |
| 4707 | if (c == '.') { |
| 4708 | prec = 0; |
| 4709 | if (--fmtcnt >= 0) |
| 4710 | c = *fmt++; |
| 4711 | if (c == '*') { |
| 4712 | v = getnextarg(args, arglen, &argidx); |
| 4713 | if (v == NULL) |
| 4714 | goto error; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4715 | if (!PyInt_Check(v)) { |
| 4716 | PyErr_SetString( |
| 4717 | PyExc_TypeError, |
| 4718 | "* wants int"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4719 | goto error; |
| 4720 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4721 | prec = PyInt_AsLong(v); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4722 | if (prec < 0) |
| 4723 | prec = 0; |
| 4724 | if (--fmtcnt >= 0) |
| 4725 | c = *fmt++; |
| 4726 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4727 | else if (c >= 0 && isdigit(c)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4728 | prec = c - '0'; |
| 4729 | while (--fmtcnt >= 0) { |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4730 | c = Py_CHARMASK(*fmt++); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4731 | if (!isdigit(c)) |
| 4732 | break; |
| 4733 | if ((prec*10) / 10 != prec) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4734 | PyErr_SetString( |
| 4735 | PyExc_ValueError, |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4736 | "prec too big"); |
| 4737 | goto error; |
| 4738 | } |
| 4739 | prec = prec*10 + (c - '0'); |
| 4740 | } |
| 4741 | } |
| 4742 | } /* prec */ |
| 4743 | if (fmtcnt >= 0) { |
| 4744 | if (c == 'h' || c == 'l' || c == 'L') { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4745 | if (--fmtcnt >= 0) |
| 4746 | c = *fmt++; |
| 4747 | } |
| 4748 | } |
| 4749 | if (fmtcnt < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4750 | PyErr_SetString(PyExc_ValueError, |
| 4751 | "incomplete format"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4752 | goto error; |
| 4753 | } |
| 4754 | if (c != '%') { |
| 4755 | v = getnextarg(args, arglen, &argidx); |
| 4756 | if (v == NULL) |
| 4757 | goto error; |
| 4758 | } |
| 4759 | sign = 0; |
| 4760 | fill = ' '; |
| 4761 | switch (c) { |
| 4762 | case '%': |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4763 | pbuf = "%"; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4764 | len = 1; |
| 4765 | break; |
| 4766 | case 's': |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4767 | #ifdef Py_USING_UNICODE |
Neil Schemenauer | ab61923 | 2005-08-31 23:02:05 +0000 | [diff] [blame] | 4768 | if (PyUnicode_Check(v)) { |
| 4769 | fmt = fmt_start; |
| 4770 | argidx = argidx_start; |
| 4771 | goto unicode; |
| 4772 | } |
Georg Brandl | d45014b | 2005-10-01 17:06:00 +0000 | [diff] [blame] | 4773 | #endif |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 4774 | temp = _PyObject_Str(v); |
Georg Brandl | d45014b | 2005-10-01 17:06:00 +0000 | [diff] [blame] | 4775 | #ifdef Py_USING_UNICODE |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 4776 | if (temp != NULL && PyUnicode_Check(temp)) { |
| 4777 | Py_DECREF(temp); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4778 | fmt = fmt_start; |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 4779 | argidx = argidx_start; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4780 | goto unicode; |
| 4781 | } |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4782 | #endif |
Guido van Rossum | b00c07f | 2002-10-09 19:07:53 +0000 | [diff] [blame] | 4783 | /* Fall through */ |
Walter Dörwald | 9ff3f03 | 2003-06-18 14:17:01 +0000 | [diff] [blame] | 4784 | case 'r': |
Neil Schemenauer | cf52c07 | 2005-08-12 17:34:58 +0000 | [diff] [blame] | 4785 | if (c == 'r') |
Guido van Rossum | f0b7b04 | 2000-04-11 15:39:26 +0000 | [diff] [blame] | 4786 | temp = PyObject_Repr(v); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4787 | if (temp == NULL) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4788 | goto error; |
Guido van Rossum | 4a0144c | 1998-06-09 15:08:41 +0000 | [diff] [blame] | 4789 | if (!PyString_Check(temp)) { |
| 4790 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 8052f89 | 2002-10-09 19:14:30 +0000 | [diff] [blame] | 4791 | "%s argument has non-string str()"); |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4792 | Py_DECREF(temp); |
Guido van Rossum | 4a0144c | 1998-06-09 15:08:41 +0000 | [diff] [blame] | 4793 | goto error; |
| 4794 | } |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4795 | pbuf = PyString_AS_STRING(temp); |
| 4796 | len = PyString_GET_SIZE(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4797 | if (prec >= 0 && len > prec) |
| 4798 | len = prec; |
| 4799 | break; |
| 4800 | case 'i': |
| 4801 | case 'd': |
| 4802 | case 'u': |
| 4803 | case 'o': |
| 4804 | case 'x': |
| 4805 | case 'X': |
| 4806 | if (c == 'i') |
| 4807 | c = 'd'; |
Tim Peters | a3a3a03 | 2000-11-30 05:22:44 +0000 | [diff] [blame] | 4808 | if (PyLong_Check(v)) { |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4809 | int ilen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4810 | temp = _PyString_FormatLong(v, flags, |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 4811 | prec, c, &pbuf, &ilen); |
| 4812 | len = ilen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4813 | if (!temp) |
| 4814 | goto error; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4815 | sign = 1; |
Guido van Rossum | 4acdc23 | 1997-01-29 06:00:24 +0000 | [diff] [blame] | 4816 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4817 | else { |
| 4818 | pbuf = formatbuf; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4819 | len = formatint(pbuf, |
| 4820 | sizeof(formatbuf), |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4821 | flags, prec, c, v); |
| 4822 | if (len < 0) |
| 4823 | goto error; |
Guido van Rossum | 6c9e130 | 2003-11-29 23:52:13 +0000 | [diff] [blame] | 4824 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4825 | } |
| 4826 | if (flags & F_ZERO) |
| 4827 | fill = '0'; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4828 | break; |
| 4829 | case 'e': |
| 4830 | case 'E': |
| 4831 | case 'f': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 4832 | case 'F': |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4833 | case 'g': |
| 4834 | case 'G': |
Raymond Hettinger | 9bfe533 | 2003-08-27 04:55:52 +0000 | [diff] [blame] | 4835 | if (c == 'F') |
| 4836 | c = 'f'; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4837 | pbuf = formatbuf; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 4838 | len = formatfloat(pbuf, sizeof(formatbuf), |
| 4839 | flags, prec, c, v); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4840 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4841 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4842 | sign = 1; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4843 | if (flags & F_ZERO) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4844 | fill = '0'; |
| 4845 | break; |
| 4846 | case 'c': |
Walter Dörwald | 43440a6 | 2003-03-31 18:07:50 +0000 | [diff] [blame] | 4847 | #ifdef Py_USING_UNICODE |
| 4848 | if (PyUnicode_Check(v)) { |
| 4849 | fmt = fmt_start; |
| 4850 | argidx = argidx_start; |
| 4851 | goto unicode; |
| 4852 | } |
| 4853 | #endif |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4854 | pbuf = formatbuf; |
| 4855 | len = formatchar(pbuf, sizeof(formatbuf), v); |
Guido van Rossum | a04d47b | 1997-01-21 16:12:09 +0000 | [diff] [blame] | 4856 | if (len < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4857 | goto error; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4858 | break; |
| 4859 | default: |
Guido van Rossum | 045e688 | 1997-09-08 18:30:11 +0000 | [diff] [blame] | 4860 | PyErr_Format(PyExc_ValueError, |
Andrew M. Kuchling | 6ca8917 | 2000-12-15 13:07:46 +0000 | [diff] [blame] | 4861 | "unsupported format character '%c' (0x%x) " |
| 4862 | "at index %i", |
Guido van Rossum | efc1188 | 2002-09-12 14:43:41 +0000 | [diff] [blame] | 4863 | c, c, |
| 4864 | (int)(fmt - 1 - PyString_AsString(format))); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4865 | goto error; |
| 4866 | } |
| 4867 | if (sign) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4868 | if (*pbuf == '-' || *pbuf == '+') { |
| 4869 | sign = *pbuf++; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4870 | len--; |
| 4871 | } |
| 4872 | else if (flags & F_SIGN) |
| 4873 | sign = '+'; |
| 4874 | else if (flags & F_BLANK) |
| 4875 | sign = ' '; |
| 4876 | else |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4877 | sign = 0; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4878 | } |
| 4879 | if (width < len) |
| 4880 | width = len; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 4881 | if (rescnt - (sign != 0) < width) { |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 4882 | reslen -= rescnt; |
| 4883 | rescnt = width + fmtcnt + 100; |
| 4884 | reslen += rescnt; |
Guido van Rossum | 049cd6b | 2002-10-11 00:43:48 +0000 | [diff] [blame] | 4885 | if (reslen < 0) { |
| 4886 | Py_DECREF(result); |
| 4887 | return PyErr_NoMemory(); |
| 4888 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4889 | if (_PyString_Resize(&result, reslen) < 0) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4890 | return NULL; |
Jeremy Hylton | 7802a53 | 2001-12-06 15:18:48 +0000 | [diff] [blame] | 4891 | res = PyString_AS_STRING(result) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4892 | + reslen - rescnt; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4893 | } |
| 4894 | if (sign) { |
Guido van Rossum | 71e57d0 | 1993-11-11 15:03:51 +0000 | [diff] [blame] | 4895 | if (fill != ' ') |
| 4896 | *res++ = sign; |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4897 | rescnt--; |
| 4898 | if (width > len) |
| 4899 | width--; |
| 4900 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4901 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 4902 | assert(pbuf[0] == '0'); |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4903 | assert(pbuf[1] == c); |
| 4904 | if (fill != ' ') { |
| 4905 | *res++ = *pbuf++; |
| 4906 | *res++ = *pbuf++; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4907 | } |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4908 | rescnt -= 2; |
| 4909 | width -= 2; |
| 4910 | if (width < 0) |
| 4911 | width = 0; |
| 4912 | len -= 2; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4913 | } |
| 4914 | if (width > len && !(flags & F_LJUST)) { |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4915 | do { |
| 4916 | --rescnt; |
| 4917 | *res++ = fill; |
| 4918 | } while (--width > len); |
| 4919 | } |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4920 | if (fill == ' ') { |
| 4921 | if (sign) |
| 4922 | *res++ = sign; |
| 4923 | if ((flags & F_ALT) && |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 4924 | (c == 'x' || c == 'X')) { |
| 4925 | assert(pbuf[0] == '0'); |
| 4926 | assert(pbuf[1] == c); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 4927 | *res++ = *pbuf++; |
| 4928 | *res++ = *pbuf++; |
| 4929 | } |
| 4930 | } |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 4931 | memcpy(res, pbuf, len); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4932 | res += len; |
| 4933 | rescnt -= len; |
| 4934 | while (--width >= len) { |
| 4935 | --rescnt; |
| 4936 | *res++ = ' '; |
| 4937 | } |
Guido van Rossum | 9fa2c11 | 1995-02-10 17:00:37 +0000 | [diff] [blame] | 4938 | if (dict && (argidx < arglen) && c != '%') { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4939 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 4940 | "not all arguments converted during string formatting"); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 4941 | goto error; |
| 4942 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4943 | Py_XDECREF(temp); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4944 | } /* '%' */ |
| 4945 | } /* until end */ |
Guido van Rossum | caeaafc | 1995-02-27 10:13:23 +0000 | [diff] [blame] | 4946 | if (argidx < arglen && !dict) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4947 | PyErr_SetString(PyExc_TypeError, |
Raymond Hettinger | 0ebac97 | 2002-05-21 15:14:57 +0000 | [diff] [blame] | 4948 | "not all arguments converted during string formatting"); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4949 | goto error; |
| 4950 | } |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4951 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4952 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 4953 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4954 | _PyString_Resize(&result, reslen - rescnt); |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 4955 | return result; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4956 | |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 4957 | #ifdef Py_USING_UNICODE |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4958 | unicode: |
| 4959 | if (args_owned) { |
| 4960 | Py_DECREF(args); |
| 4961 | args_owned = 0; |
| 4962 | } |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 4963 | /* Fiddle args right (remove the first argidx arguments) */ |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4964 | if (PyTuple_Check(orig_args) && argidx > 0) { |
| 4965 | PyObject *v; |
Martin v. Löwis | eb079f1 | 2006-02-16 14:32:27 +0000 | [diff] [blame] | 4966 | Py_ssize_t n = PyTuple_GET_SIZE(orig_args) - argidx; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4967 | v = PyTuple_New(n); |
| 4968 | if (v == NULL) |
| 4969 | goto error; |
| 4970 | while (--n >= 0) { |
| 4971 | PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx); |
| 4972 | Py_INCREF(w); |
| 4973 | PyTuple_SET_ITEM(v, n, w); |
| 4974 | } |
| 4975 | args = v; |
| 4976 | } else { |
| 4977 | Py_INCREF(orig_args); |
| 4978 | args = orig_args; |
| 4979 | } |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4980 | args_owned = 1; |
| 4981 | /* Take what we have of the result and let the Unicode formatting |
| 4982 | function format the rest of the input. */ |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4983 | rescnt = res - PyString_AS_STRING(result); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4984 | if (_PyString_Resize(&result, rescnt)) |
| 4985 | goto error; |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4986 | fmtcnt = PyString_GET_SIZE(format) - \ |
| 4987 | (fmt - PyString_AS_STRING(format)); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4988 | format = PyUnicode_Decode(fmt, fmtcnt, NULL, NULL); |
| 4989 | if (format == NULL) |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4990 | goto error; |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4991 | v = PyUnicode_Format(format, args); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 4992 | Py_DECREF(format); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 4993 | if (v == NULL) |
| 4994 | goto error; |
| 4995 | /* Paste what we have (result) to what the Unicode formatting |
| 4996 | function returned (v) and return the result (or error) */ |
| 4997 | w = PyUnicode_Concat(result, v); |
| 4998 | Py_DECREF(result); |
| 4999 | Py_DECREF(v); |
Guido van Rossum | 90daa87 | 2000-04-10 13:47:21 +0000 | [diff] [blame] | 5000 | Py_DECREF(args); |
Marc-André Lemburg | 53f3d4a | 2000-10-07 08:54:09 +0000 | [diff] [blame] | 5001 | return w; |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 5002 | #endif /* Py_USING_UNICODE */ |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 5003 | |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 5004 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5005 | Py_DECREF(result); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 5006 | if (args_owned) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 5007 | Py_DECREF(args); |
Guido van Rossum | 1109fbc | 1998-04-10 22:16:39 +0000 | [diff] [blame] | 5008 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 5009 | return NULL; |
| 5010 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5011 | |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5012 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 5013 | PyString_InternInPlace(PyObject **p) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5014 | { |
| 5015 | register PyStringObject *s = (PyStringObject *)(*p); |
| 5016 | PyObject *t; |
| 5017 | if (s == NULL || !PyString_Check(s)) |
| 5018 | Py_FatalError("PyString_InternInPlace: strings only please!"); |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 5019 | /* If it's a string subclass, we don't really know what putting |
| 5020 | it in the interned dict might do. */ |
| 5021 | if (!PyString_CheckExact(s)) |
| 5022 | return; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5023 | if (PyString_CHECK_INTERNED(s)) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5024 | return; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5025 | if (interned == NULL) { |
| 5026 | interned = PyDict_New(); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5027 | if (interned == NULL) { |
| 5028 | PyErr_Clear(); /* Don't leave an exception */ |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5029 | return; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5030 | } |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5031 | } |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 5032 | t = PyDict_GetItem(interned, (PyObject *)s); |
| 5033 | if (t) { |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5034 | Py_INCREF(t); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5035 | Py_DECREF(*p); |
| 5036 | *p = t; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5037 | return; |
| 5038 | } |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5039 | |
Armin Rigo | 79f7ad2 | 2004-08-07 19:27:39 +0000 | [diff] [blame] | 5040 | if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 5041 | PyErr_Clear(); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5042 | return; |
| 5043 | } |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 5044 | /* The two references in interned are not counted by refcnt. |
| 5045 | The string deallocator will take care of this */ |
Armin Rigo | 79f7ad2 | 2004-08-07 19:27:39 +0000 | [diff] [blame] | 5046 | s->ob_refcnt -= 2; |
Jeremy Hylton | 4c989dd | 2004-08-07 19:20:05 +0000 | [diff] [blame] | 5047 | PyString_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5050 | void |
| 5051 | PyString_InternImmortal(PyObject **p) |
| 5052 | { |
| 5053 | PyString_InternInPlace(p); |
| 5054 | if (PyString_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
| 5055 | PyString_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
| 5056 | Py_INCREF(*p); |
| 5057 | } |
| 5058 | } |
| 5059 | |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5060 | |
| 5061 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 5062 | PyString_InternFromString(const char *cp) |
Guido van Rossum | 2a61e74 | 1997-01-18 07:55:05 +0000 | [diff] [blame] | 5063 | { |
| 5064 | PyObject *s = PyString_FromString(cp); |
| 5065 | if (s == NULL) |
| 5066 | return NULL; |
| 5067 | PyString_InternInPlace(&s); |
| 5068 | return s; |
| 5069 | } |
| 5070 | |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 5071 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 5072 | PyString_Fini(void) |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 5073 | { |
| 5074 | int i; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 5075 | for (i = 0; i < UCHAR_MAX + 1; i++) { |
| 5076 | Py_XDECREF(characters[i]); |
| 5077 | characters[i] = NULL; |
| 5078 | } |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 5079 | Py_XDECREF(nullstring); |
| 5080 | nullstring = NULL; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 5081 | } |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 5082 | |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 5083 | void _Py_ReleaseInternedStrings(void) |
| 5084 | { |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5085 | PyObject *keys; |
| 5086 | PyStringObject *s; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 5087 | Py_ssize_t i, n; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5088 | |
| 5089 | if (interned == NULL || !PyDict_Check(interned)) |
| 5090 | return; |
| 5091 | keys = PyDict_Keys(interned); |
| 5092 | if (keys == NULL || !PyList_Check(keys)) { |
| 5093 | PyErr_Clear(); |
| 5094 | return; |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 5095 | } |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5096 | |
| 5097 | /* Since _Py_ReleaseInternedStrings() is intended to help a leak |
| 5098 | detector, interned strings are not forcibly deallocated; rather, we |
| 5099 | give them their stolen references back, and then clear and DECREF |
| 5100 | the interned dict. */ |
Tim Peters | ae1d0c9 | 2006-03-17 03:29:34 +0000 | [diff] [blame] | 5101 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 5102 | fprintf(stderr, "releasing interned strings\n"); |
| 5103 | n = PyList_GET_SIZE(keys); |
| 5104 | for (i = 0; i < n; i++) { |
| 5105 | s = (PyStringObject *) PyList_GET_ITEM(keys, i); |
| 5106 | switch (s->ob_sstate) { |
| 5107 | case SSTATE_NOT_INTERNED: |
| 5108 | /* XXX Shouldn't happen */ |
| 5109 | break; |
| 5110 | case SSTATE_INTERNED_IMMORTAL: |
| 5111 | s->ob_refcnt += 1; |
| 5112 | break; |
| 5113 | case SSTATE_INTERNED_MORTAL: |
| 5114 | s->ob_refcnt += 2; |
| 5115 | break; |
| 5116 | default: |
| 5117 | Py_FatalError("Inconsistent interned string state."); |
| 5118 | } |
| 5119 | s->ob_sstate = SSTATE_NOT_INTERNED; |
| 5120 | } |
| 5121 | Py_DECREF(keys); |
| 5122 | PyDict_Clear(interned); |
| 5123 | Py_DECREF(interned); |
| 5124 | interned = NULL; |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 5125 | } |