Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* String object implementation */ |
| 2 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3 | /* XXX This is now called 'bytes' as far as the user is concerned. |
| 4 | Many docstrings and error messages need to be cleaned up. */ |
| 5 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 6 | #define PY_SSIZE_T_CLEAN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 8 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 10 | #include "bytes_methods.h" |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 12 | static Py_ssize_t |
| 13 | _getbuffer(PyObject *obj, Py_buffer *view) |
| 14 | { |
| 15 | PyBufferProcs *buffer = Py_Type(obj)->tp_as_buffer; |
| 16 | |
| 17 | if (buffer == NULL || buffer->bf_getbuffer == NULL) |
| 18 | { |
| 19 | PyErr_Format(PyExc_TypeError, |
| 20 | "Type %.100s doesn't support the buffer API", |
| 21 | Py_Type(obj)->tp_name); |
| 22 | return -1; |
| 23 | } |
| 24 | |
| 25 | if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0) |
| 26 | return -1; |
| 27 | return view->len; |
| 28 | } |
| 29 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 30 | #ifdef COUNT_ALLOCS |
| 31 | int null_strings, one_strings; |
| 32 | #endif |
| 33 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 34 | static PyStringObject *characters[UCHAR_MAX + 1]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 35 | static PyStringObject *nullstring; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 36 | |
| 37 | /* |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 38 | For both PyString_FromString() and PyString_FromStringAndSize(), the |
| 39 | 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] | 40 | null terminating character. |
Martin v. Löwis | d132750 | 2001-12-02 18:09:41 +0000 | [diff] [blame] | 41 | |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 42 | 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] | 43 | string containing exactly `size' bytes. |
Martin v. Löwis | d132750 | 2001-12-02 18:09:41 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 45 | For PyString_FromStringAndSize(), the parameter the parameter `str' is |
| 46 | either NULL or else points to a string containing at least `size' bytes. |
| 47 | For PyString_FromStringAndSize(), the string in the `str' parameter does |
| 48 | not have to be null-terminated. (Therefore it is safe to construct a |
| 49 | substring by calling `PyString_FromStringAndSize(origstring, substrlen)'.) |
| 50 | If `str' is NULL then PyString_FromStringAndSize() will allocate `size+1' |
| 51 | bytes (setting the last byte to the null terminating character) and you can |
| 52 | fill in the data yourself. If `str' is non-NULL then the resulting |
| 53 | PyString object must be treated as immutable and you must not fill in nor |
| 54 | alter the data yourself, since the strings may be shared. |
Martin v. Löwis | 8f1ea71 | 2001-12-03 08:24:52 +0000 | [diff] [blame] | 55 | |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 56 | The PyObject member `op->ob_size', which denotes the number of "extra |
| 57 | items" in a variable-size object, will contain the number of bytes |
| 58 | allocated for string data, not counting the null terminating character. It |
| 59 | is therefore equal to the equal to the `size' parameter (for |
| 60 | PyString_FromStringAndSize()) or the length of the string in the `str' |
| 61 | parameter (for PyString_FromString()). |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 62 | */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 63 | PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 64 | PyString_FromStringAndSize(const char *str, Py_ssize_t size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 65 | { |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 66 | register PyStringObject *op; |
Michael W. Hudson | faa7648 | 2005-01-31 17:09:25 +0000 | [diff] [blame] | 67 | assert(size >= 0); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 68 | if (size == 0 && (op = nullstring) != NULL) { |
| 69 | #ifdef COUNT_ALLOCS |
| 70 | null_strings++; |
| 71 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 72 | Py_INCREF(op); |
| 73 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 74 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 75 | if (size == 1 && str != NULL && |
| 76 | (op = characters[*str & UCHAR_MAX]) != NULL) |
| 77 | { |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 78 | #ifdef COUNT_ALLOCS |
| 79 | one_strings++; |
| 80 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 81 | Py_INCREF(op); |
| 82 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 83 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 84 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 85 | /* Inline PyObject_NewVar */ |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 86 | op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 87 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 88 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 89 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 90 | op->ob_shash = -1; |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 91 | if (str != NULL) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 92 | Py_MEMCPY(op->ob_sval, str, size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 93 | op->ob_sval[size] = '\0'; |
Tim Peters | 8deda70 | 2002-03-30 10:06:07 +0000 | [diff] [blame] | 94 | /* share short strings */ |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 95 | if (size == 0) { |
| 96 | nullstring = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 97 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 98 | } else if (size == 1 && str != NULL) { |
| 99 | characters[*str & UCHAR_MAX] = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 100 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 101 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 102 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 105 | PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 106 | PyString_FromString(const char *str) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 107 | { |
Tim Peters | 62de65b | 2001-12-06 20:29:32 +0000 | [diff] [blame] | 108 | register size_t size; |
Tim Peters | 9e897f4 | 2001-05-09 07:37:07 +0000 | [diff] [blame] | 109 | register PyStringObject *op; |
Tim Peters | 62de65b | 2001-12-06 20:29:32 +0000 | [diff] [blame] | 110 | |
| 111 | assert(str != NULL); |
| 112 | size = strlen(str); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 113 | if (size > PY_SSIZE_T_MAX) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 114 | PyErr_SetString(PyExc_OverflowError, |
| 115 | "string is too long for a Python string"); |
| 116 | return NULL; |
| 117 | } |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 118 | if (size == 0 && (op = nullstring) != NULL) { |
| 119 | #ifdef COUNT_ALLOCS |
| 120 | null_strings++; |
| 121 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 122 | Py_INCREF(op); |
| 123 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 124 | } |
| 125 | if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
| 126 | #ifdef COUNT_ALLOCS |
| 127 | one_strings++; |
| 128 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 129 | Py_INCREF(op); |
| 130 | return (PyObject *)op; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 131 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 132 | |
Guido van Rossum | e3a8e7e | 2002-08-19 19:26:42 +0000 | [diff] [blame] | 133 | /* Inline PyObject_NewVar */ |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 134 | op = (PyStringObject *)PyObject_MALLOC(sizeof(PyStringObject) + size); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 135 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 136 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 137 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 138 | op->ob_shash = -1; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 139 | Py_MEMCPY(op->ob_sval, str, size+1); |
Tim Peters | 8deda70 | 2002-03-30 10:06:07 +0000 | [diff] [blame] | 140 | /* share short strings */ |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 141 | if (size == 0) { |
| 142 | nullstring = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 143 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 144 | } else if (size == 1) { |
| 145 | characters[*str & UCHAR_MAX] = op; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 146 | Py_INCREF(op); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 147 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 148 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 151 | PyObject * |
| 152 | PyString_FromFormatV(const char *format, va_list vargs) |
| 153 | { |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 154 | va_list count; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 155 | Py_ssize_t n = 0; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 156 | const char* f; |
| 157 | char *s; |
| 158 | PyObject* string; |
| 159 | |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 160 | #ifdef VA_LIST_IS_ARRAY |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 161 | Py_MEMCPY(count, vargs, sizeof(va_list)); |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 162 | #else |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 163 | #ifdef __va_copy |
| 164 | __va_copy(count, vargs); |
| 165 | #else |
Tim Peters | c15c4f1 | 2001-10-02 21:32:07 +0000 | [diff] [blame] | 166 | count = vargs; |
| 167 | #endif |
Martin v. Löwis | 75d2d94 | 2002-07-28 10:23:27 +0000 | [diff] [blame] | 168 | #endif |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 169 | /* step 1: figure out how large a buffer we need */ |
| 170 | for (f = format; *f; f++) { |
| 171 | if (*f == '%') { |
| 172 | const char* p = f; |
Guido van Rossum | 6ccd3f2 | 2007-10-09 03:46:30 +0000 | [diff] [blame] | 173 | while (*++f && *f != '%' && !ISALPHA(*f)) |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 174 | ; |
| 175 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 176 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 177 | * they don't affect the amount of space we reserve. |
| 178 | */ |
| 179 | if ((*f == 'l' || *f == 'z') && |
| 180 | (f[1] == 'd' || f[1] == 'u')) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 181 | ++f; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 182 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 183 | switch (*f) { |
| 184 | case 'c': |
| 185 | (void)va_arg(count, int); |
| 186 | /* fall through... */ |
| 187 | case '%': |
| 188 | n++; |
| 189 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 190 | case 'd': case 'u': case 'i': case 'x': |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 191 | (void) va_arg(count, int); |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 192 | /* 20 bytes is enough to hold a 64-bit |
| 193 | integer. Decimal takes the most space. |
| 194 | This isn't enough for octal. */ |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 195 | n += 20; |
| 196 | break; |
| 197 | case 's': |
| 198 | s = va_arg(count, char*); |
| 199 | n += strlen(s); |
| 200 | break; |
| 201 | case 'p': |
| 202 | (void) va_arg(count, int); |
| 203 | /* maximum 64-bit pointer representation: |
| 204 | * 0xffffffffffffffff |
| 205 | * so 19 characters is enough. |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 206 | * XXX I count 18 -- what's the extra for? |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 207 | */ |
| 208 | n += 19; |
| 209 | break; |
| 210 | default: |
| 211 | /* if we stumble upon an unknown |
| 212 | formatting code, copy the rest of |
| 213 | the format string to the output |
| 214 | string. (we cannot just skip the |
| 215 | code, since there's no way to know |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 216 | what's in the argument list) */ |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 217 | n += strlen(p); |
| 218 | goto expand; |
| 219 | } |
| 220 | } else |
| 221 | n++; |
| 222 | } |
| 223 | expand: |
| 224 | /* step 2: fill the buffer */ |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 225 | /* Since we've analyzed how much space we need for the worst case, |
| 226 | use sprintf directly instead of the slower PyOS_snprintf. */ |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 227 | string = PyString_FromStringAndSize(NULL, n); |
| 228 | if (!string) |
| 229 | return NULL; |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 230 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 231 | s = PyString_AsString(string); |
| 232 | |
| 233 | for (f = format; *f; f++) { |
| 234 | if (*f == '%') { |
| 235 | const char* p = f++; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 236 | Py_ssize_t i; |
| 237 | int longflag = 0; |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 238 | int size_tflag = 0; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 239 | /* parse the width.precision part (we're only |
| 240 | interested in the precision value, if any) */ |
| 241 | n = 0; |
Guido van Rossum | 6ccd3f2 | 2007-10-09 03:46:30 +0000 | [diff] [blame] | 242 | while (ISDIGIT(*f)) |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 243 | n = (n*10) + *f++ - '0'; |
| 244 | if (*f == '.') { |
| 245 | f++; |
| 246 | n = 0; |
Guido van Rossum | 6ccd3f2 | 2007-10-09 03:46:30 +0000 | [diff] [blame] | 247 | while (ISDIGIT(*f)) |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 248 | n = (n*10) + *f++ - '0'; |
| 249 | } |
Guido van Rossum | 6ccd3f2 | 2007-10-09 03:46:30 +0000 | [diff] [blame] | 250 | while (*f && *f != '%' && !ISALPHA(*f)) |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 251 | f++; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 252 | /* handle the long flag, but only for %ld and %lu. |
| 253 | others can be added when necessary. */ |
| 254 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 255 | longflag = 1; |
| 256 | ++f; |
| 257 | } |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 258 | /* handle the size_t flag. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 259 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
Martin v. Löwis | 2c95cc6 | 2006-02-16 06:54:25 +0000 | [diff] [blame] | 260 | size_tflag = 1; |
| 261 | ++f; |
| 262 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 263 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 264 | switch (*f) { |
| 265 | case 'c': |
| 266 | *s++ = va_arg(vargs, int); |
| 267 | break; |
| 268 | case 'd': |
| 269 | if (longflag) |
| 270 | sprintf(s, "%ld", va_arg(vargs, long)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 271 | else if (size_tflag) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 272 | sprintf(s, "%" PY_FORMAT_SIZE_T "d", |
| 273 | va_arg(vargs, Py_ssize_t)); |
| 274 | else |
| 275 | sprintf(s, "%d", va_arg(vargs, int)); |
| 276 | s += strlen(s); |
| 277 | break; |
| 278 | case 'u': |
| 279 | if (longflag) |
| 280 | sprintf(s, "%lu", |
| 281 | va_arg(vargs, unsigned long)); |
| 282 | else if (size_tflag) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 283 | sprintf(s, "%" PY_FORMAT_SIZE_T "u", |
| 284 | va_arg(vargs, size_t)); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 285 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 286 | sprintf(s, "%u", |
| 287 | va_arg(vargs, unsigned int)); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 288 | s += strlen(s); |
| 289 | break; |
| 290 | case 'i': |
| 291 | sprintf(s, "%i", va_arg(vargs, int)); |
| 292 | s += strlen(s); |
| 293 | break; |
| 294 | case 'x': |
| 295 | sprintf(s, "%x", va_arg(vargs, int)); |
| 296 | s += strlen(s); |
| 297 | break; |
| 298 | case 's': |
| 299 | p = va_arg(vargs, char*); |
| 300 | i = strlen(p); |
| 301 | if (n > 0 && i > n) |
| 302 | i = n; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 303 | Py_MEMCPY(s, p, i); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 304 | s += i; |
| 305 | break; |
| 306 | case 'p': |
| 307 | sprintf(s, "%p", va_arg(vargs, void*)); |
Tim Peters | 6af5bbb | 2001-08-25 03:02:28 +0000 | [diff] [blame] | 308 | /* %p is ill-defined: ensure leading 0x. */ |
| 309 | if (s[1] == 'X') |
| 310 | s[1] = 'x'; |
| 311 | else if (s[1] != 'x') { |
| 312 | memmove(s+2, s, strlen(s)+1); |
| 313 | s[0] = '0'; |
| 314 | s[1] = 'x'; |
| 315 | } |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 316 | s += strlen(s); |
| 317 | break; |
| 318 | case '%': |
| 319 | *s++ = '%'; |
| 320 | break; |
| 321 | default: |
| 322 | strcpy(s, p); |
| 323 | s += strlen(s); |
| 324 | goto end; |
| 325 | } |
| 326 | } else |
| 327 | *s++ = *f; |
| 328 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 329 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 330 | end: |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 331 | _PyString_Resize(&string, s - PyString_AS_STRING(string)); |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 332 | return string; |
| 333 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 334 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 335 | PyObject * |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 336 | PyString_FromFormat(const char *format, ...) |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 337 | { |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 338 | PyObject* ret; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 339 | va_list vargs; |
| 340 | |
| 341 | #ifdef HAVE_STDARG_PROTOTYPES |
| 342 | va_start(vargs, format); |
| 343 | #else |
| 344 | va_start(vargs); |
| 345 | #endif |
Barry Warsaw | 7c47beb | 2001-08-27 03:11:09 +0000 | [diff] [blame] | 346 | ret = PyString_FromFormatV(format, vargs); |
| 347 | va_end(vargs); |
| 348 | return ret; |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 351 | static void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 352 | string_dealloc(PyObject *op) |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 353 | { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 354 | Py_Type(op)->tp_free(op); |
Guido van Rossum | 719f5fa | 1992-03-27 17:31:02 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 357 | /* Unescape a backslash-escaped string. If unicode is non-zero, |
| 358 | the string is a u-literal. If recode_encoding is non-zero, |
| 359 | the string is UTF-8 encoded and should be re-encoded in the |
| 360 | specified encoding. */ |
| 361 | |
| 362 | PyObject *PyString_DecodeEscape(const char *s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 363 | Py_ssize_t len, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 364 | const char *errors, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 365 | Py_ssize_t unicode, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 366 | const char *recode_encoding) |
| 367 | { |
| 368 | int c; |
| 369 | char *p, *buf; |
| 370 | const char *end; |
| 371 | PyObject *v; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 372 | Py_ssize_t newlen = recode_encoding ? 4*len:len; |
Walter Dörwald | 8709a42 | 2002-09-03 13:53:40 +0000 | [diff] [blame] | 373 | v = PyString_FromStringAndSize((char *)NULL, newlen); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 374 | if (v == NULL) |
| 375 | return NULL; |
| 376 | p = buf = PyString_AsString(v); |
| 377 | end = s + len; |
| 378 | while (s < end) { |
| 379 | if (*s != '\\') { |
Martin v. Löwis | 2412853 | 2002-09-09 06:17:05 +0000 | [diff] [blame] | 380 | non_esc: |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 381 | if (recode_encoding && (*s & 0x80)) { |
| 382 | PyObject *u, *w; |
| 383 | char *r; |
| 384 | const char* t; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 385 | Py_ssize_t rn; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 386 | t = s; |
| 387 | /* Decode non-ASCII bytes as UTF-8. */ |
| 388 | while (t < end && (*t & 0x80)) t++; |
| 389 | u = PyUnicode_DecodeUTF8(s, t - s, errors); |
| 390 | if(!u) goto failed; |
| 391 | |
| 392 | /* Recode them in target encoding. */ |
| 393 | w = PyUnicode_AsEncodedString( |
| 394 | u, recode_encoding, errors); |
| 395 | Py_DECREF(u); |
| 396 | if (!w) goto failed; |
| 397 | |
| 398 | /* Append bytes to output buffer. */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 399 | assert(PyString_Check(w)); |
| 400 | r = PyString_AS_STRING(w); |
| 401 | rn = PyString_GET_SIZE(w); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 402 | Py_MEMCPY(p, r, rn); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 403 | p += rn; |
| 404 | Py_DECREF(w); |
| 405 | s = t; |
| 406 | } else { |
| 407 | *p++ = *s++; |
| 408 | } |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 409 | continue; |
| 410 | } |
| 411 | s++; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 412 | if (s==end) { |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 413 | PyErr_SetString(PyExc_ValueError, |
| 414 | "Trailing \\ in string"); |
| 415 | goto failed; |
| 416 | } |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 417 | switch (*s++) { |
| 418 | /* XXX This assumes ASCII! */ |
| 419 | case '\n': break; |
| 420 | case '\\': *p++ = '\\'; break; |
| 421 | case '\'': *p++ = '\''; break; |
| 422 | case '\"': *p++ = '\"'; break; |
| 423 | case 'b': *p++ = '\b'; break; |
| 424 | case 'f': *p++ = '\014'; break; /* FF */ |
| 425 | case 't': *p++ = '\t'; break; |
| 426 | case 'n': *p++ = '\n'; break; |
| 427 | case 'r': *p++ = '\r'; break; |
| 428 | case 'v': *p++ = '\013'; break; /* VT */ |
| 429 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 430 | case '0': case '1': case '2': case '3': |
| 431 | case '4': case '5': case '6': case '7': |
| 432 | c = s[-1] - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 433 | if (s < end && '0' <= *s && *s <= '7') { |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 434 | c = (c<<3) + *s++ - '0'; |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 435 | if (s < end && '0' <= *s && *s <= '7') |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 436 | c = (c<<3) + *s++ - '0'; |
| 437 | } |
| 438 | *p++ = c; |
| 439 | break; |
| 440 | case 'x': |
Guido van Rossum | 8ce8a78 | 2007-11-01 19:42:39 +0000 | [diff] [blame] | 441 | if (s+1 < end && ISXDIGIT(s[0]) && ISXDIGIT(s[1])) { |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 442 | unsigned int x = 0; |
| 443 | c = Py_CHARMASK(*s); |
| 444 | s++; |
Guido van Rossum | 6ccd3f2 | 2007-10-09 03:46:30 +0000 | [diff] [blame] | 445 | if (ISDIGIT(c)) |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 446 | x = c - '0'; |
Guido van Rossum | 6ccd3f2 | 2007-10-09 03:46:30 +0000 | [diff] [blame] | 447 | else if (ISLOWER(c)) |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 448 | x = 10 + c - 'a'; |
| 449 | else |
| 450 | x = 10 + c - 'A'; |
| 451 | x = x << 4; |
| 452 | c = Py_CHARMASK(*s); |
| 453 | s++; |
Guido van Rossum | 6ccd3f2 | 2007-10-09 03:46:30 +0000 | [diff] [blame] | 454 | if (ISDIGIT(c)) |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 455 | x += c - '0'; |
Guido van Rossum | 6ccd3f2 | 2007-10-09 03:46:30 +0000 | [diff] [blame] | 456 | else if (ISLOWER(c)) |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 457 | x += 10 + c - 'a'; |
| 458 | else |
| 459 | x += 10 + c - 'A'; |
| 460 | *p++ = x; |
| 461 | break; |
| 462 | } |
| 463 | if (!errors || strcmp(errors, "strict") == 0) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 464 | PyErr_SetString(PyExc_ValueError, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 465 | "invalid \\x escape"); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 466 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 467 | } |
| 468 | if (strcmp(errors, "replace") == 0) { |
| 469 | *p++ = '?'; |
| 470 | } else if (strcmp(errors, "ignore") == 0) |
| 471 | /* do nothing */; |
| 472 | else { |
| 473 | PyErr_Format(PyExc_ValueError, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 474 | "decoding error; unknown " |
| 475 | "error handling code: %.400s", |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 476 | errors); |
Martin v. Löwis | eb3f00a | 2002-08-14 08:22:50 +0000 | [diff] [blame] | 477 | goto failed; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 478 | } |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 479 | default: |
| 480 | *p++ = '\\'; |
Martin v. Löwis | 2412853 | 2002-09-09 06:17:05 +0000 | [diff] [blame] | 481 | s--; |
| 482 | goto non_esc; /* an arbitry number of unescaped |
| 483 | UTF-8 bytes may follow. */ |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 484 | } |
| 485 | } |
Walter Dörwald | 8709a42 | 2002-09-03 13:53:40 +0000 | [diff] [blame] | 486 | if (p-buf < newlen) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 487 | _PyString_Resize(&v, p - buf); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 488 | return v; |
| 489 | failed: |
| 490 | Py_DECREF(v); |
| 491 | return NULL; |
| 492 | } |
| 493 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 494 | /* -------------------------------------------------------------------- */ |
| 495 | /* object api */ |
| 496 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 497 | static Py_ssize_t |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 498 | string_getsize(register PyObject *op) |
| 499 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 500 | char *s; |
| 501 | Py_ssize_t len; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 502 | if (PyString_AsStringAndSize(op, &s, &len)) |
| 503 | return -1; |
| 504 | return len; |
| 505 | } |
| 506 | |
| 507 | static /*const*/ char * |
| 508 | string_getbuffer(register PyObject *op) |
| 509 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 510 | char *s; |
| 511 | Py_ssize_t len; |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 512 | if (PyString_AsStringAndSize(op, &s, &len)) |
| 513 | return NULL; |
| 514 | return s; |
| 515 | } |
| 516 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 517 | Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 518 | PyString_Size(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 519 | { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 520 | if (PyUnicode_Check(op)) { |
| 521 | op = _PyUnicode_AsDefaultEncodedString(op, NULL); |
| 522 | if (!op) |
| 523 | return -1; |
| 524 | } |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 525 | if (!PyString_Check(op)) |
| 526 | return string_getsize(op); |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 527 | return Py_Size(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /*const*/ char * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 531 | PyString_AsString(register PyObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 532 | { |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 533 | if (PyUnicode_Check(op)) { |
| 534 | op = _PyUnicode_AsDefaultEncodedString(op, NULL); |
| 535 | if (!op) |
| 536 | return NULL; |
| 537 | } |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 538 | if (!PyString_Check(op)) |
| 539 | return string_getbuffer(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 540 | return ((PyStringObject *)op) -> ob_sval; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 543 | int |
| 544 | PyString_AsStringAndSize(register PyObject *obj, |
| 545 | register char **s, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 546 | register Py_ssize_t *len) |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 547 | { |
| 548 | if (s == NULL) { |
| 549 | PyErr_BadInternalCall(); |
| 550 | return -1; |
| 551 | } |
| 552 | |
| 553 | if (!PyString_Check(obj)) { |
| 554 | if (PyUnicode_Check(obj)) { |
| 555 | obj = _PyUnicode_AsDefaultEncodedString(obj, NULL); |
| 556 | if (obj == NULL) |
| 557 | return -1; |
| 558 | } |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 559 | else |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 560 | { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 561 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 3d1d712 | 2007-06-07 17:54:36 +0000 | [diff] [blame] | 562 | "expected string, " |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 563 | "%.200s found", Py_Type(obj)->tp_name); |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 564 | return -1; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | *s = PyString_AS_STRING(obj); |
| 569 | if (len != NULL) |
| 570 | *len = PyString_GET_SIZE(obj); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 571 | else if (strlen(*s) != (size_t)PyString_GET_SIZE(obj)) { |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 572 | PyErr_SetString(PyExc_TypeError, |
| 573 | "expected string without null bytes"); |
| 574 | return -1; |
| 575 | } |
| 576 | return 0; |
| 577 | } |
| 578 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 579 | /* -------------------------------------------------------------------- */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 580 | /* Methods */ |
| 581 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 582 | #define STRINGLIB_CHAR char |
| 583 | |
| 584 | #define STRINGLIB_CMP memcmp |
| 585 | #define STRINGLIB_LEN PyString_GET_SIZE |
| 586 | #define STRINGLIB_NEW PyString_FromStringAndSize |
| 587 | #define STRINGLIB_STR PyString_AS_STRING |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 588 | /* #define STRINGLIB_WANT_CONTAINS_OBJ 1 */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 589 | |
| 590 | #define STRINGLIB_EMPTY nullstring |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 591 | #define STRINGLIB_CHECK_EXACT PyString_CheckExact |
| 592 | #define STRINGLIB_MUTABLE 0 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 593 | |
| 594 | #include "stringlib/fastsearch.h" |
| 595 | |
| 596 | #include "stringlib/count.h" |
| 597 | #include "stringlib/find.h" |
| 598 | #include "stringlib/partition.h" |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 599 | #include "stringlib/ctype.h" |
| 600 | #include "stringlib/transmogrify.h" |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 601 | |
| 602 | |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 603 | PyObject * |
| 604 | PyString_Repr(PyObject *obj, int smartquotes) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 605 | { |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 606 | static const char *hexdigits = "0123456789abcdef"; |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 607 | register PyStringObject* op = (PyStringObject*) obj; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 608 | Py_ssize_t length = Py_Size(op); |
| 609 | size_t newsize = 3 + 4 * length; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 610 | PyObject *v; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 611 | if (newsize > PY_SSIZE_T_MAX || (newsize-3) / 4 != length) { |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 612 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 613 | "bytes object is too large to make repr"); |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 614 | return NULL; |
Marc-André Lemburg | f28dd83 | 2000-06-30 10:29:57 +0000 | [diff] [blame] | 615 | } |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 616 | v = PyUnicode_FromUnicode(NULL, newsize); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 617 | if (v == NULL) { |
Guido van Rossum | bcaa31c | 1991-06-07 22:58:57 +0000 | [diff] [blame] | 618 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 619 | } |
| 620 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 621 | register Py_ssize_t i; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 622 | register Py_UNICODE c; |
| 623 | register Py_UNICODE *p = PyUnicode_AS_UNICODE(v); |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 624 | int quote; |
| 625 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 626 | /* Figure out which quote to use; single is preferred */ |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 627 | quote = '\''; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 628 | if (smartquotes) { |
Guido van Rossum | a1cdfd9 | 2007-07-03 14:52:23 +0000 | [diff] [blame] | 629 | char *test, *start; |
| 630 | start = PyString_AS_STRING(op); |
| 631 | for (test = start; test < start+length; ++test) { |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 632 | if (*test == '"') { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 633 | quote = '\''; /* back to single */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 634 | goto decided; |
| 635 | } |
| 636 | else if (*test == '\'') |
| 637 | quote = '"'; |
| 638 | } |
| 639 | decided: |
| 640 | ; |
| 641 | } |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 642 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 643 | *p++ = 'b', *p++ = quote; |
| 644 | for (i = 0; i < length; i++) { |
Tim Peters | 9161c8b | 2001-12-03 01:55:38 +0000 | [diff] [blame] | 645 | /* There's at least enough room for a hex escape |
| 646 | and a closing quote. */ |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 647 | assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 648 | c = op->ob_sval[i]; |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 649 | if (c == quote || c == '\\') |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 650 | *p++ = '\\', *p++ = c; |
Ka-Ping Yee | fa004ad | 2001-01-24 17:19:08 +0000 | [diff] [blame] | 651 | else if (c == '\t') |
| 652 | *p++ = '\\', *p++ = 't'; |
| 653 | else if (c == '\n') |
| 654 | *p++ = '\\', *p++ = 'n'; |
| 655 | else if (c == '\r') |
| 656 | *p++ = '\\', *p++ = 'r'; |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 657 | else if (c < ' ' || c >= 0x7f) { |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 658 | *p++ = '\\'; |
| 659 | *p++ = 'x'; |
| 660 | *p++ = hexdigits[(c & 0xf0) >> 4]; |
| 661 | *p++ = hexdigits[c & 0xf]; |
Martin v. Löwis | fed2405 | 2002-10-07 13:55:50 +0000 | [diff] [blame] | 662 | } |
Martin v. Löwis | a5f0907 | 2002-10-11 05:37:59 +0000 | [diff] [blame] | 663 | else |
| 664 | *p++ = c; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 665 | } |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 666 | assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1); |
Guido van Rossum | 444fc7c | 1993-10-26 15:25:16 +0000 | [diff] [blame] | 667 | *p++ = quote; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 668 | *p = '\0'; |
Walter Dörwald | 1ab8330 | 2007-05-18 17:15:44 +0000 | [diff] [blame] | 669 | if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) { |
| 670 | Py_DECREF(v); |
| 671 | return NULL; |
| 672 | } |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 673 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 674 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 677 | static PyObject * |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 678 | string_repr(PyObject *op) |
| 679 | { |
| 680 | return PyString_Repr(op, 1); |
| 681 | } |
| 682 | |
| 683 | static PyObject * |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 684 | string_str(PyObject *op) |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 685 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 686 | if (Py_BytesWarningFlag) { |
| 687 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 688 | "str() on a bytes instance", 1)) |
| 689 | return NULL; |
Tim Peters | c993315 | 2001-10-16 20:18:24 +0000 | [diff] [blame] | 690 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 691 | return string_repr(op); |
Guido van Rossum | 189f1df | 2001-05-01 16:51:53 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 694 | static Py_ssize_t |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 695 | string_length(PyStringObject *a) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 696 | { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 697 | return Py_Size(a); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 700 | /* This is also used by PyString_Concat() */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 701 | static PyObject * |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 702 | string_concat(PyObject *a, PyObject *b) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 703 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 704 | Py_ssize_t size; |
| 705 | Py_buffer va, vb; |
| 706 | PyObject *result = NULL; |
| 707 | |
| 708 | va.len = -1; |
| 709 | vb.len = -1; |
| 710 | if (_getbuffer(a, &va) < 0 || |
| 711 | _getbuffer(b, &vb) < 0) { |
| 712 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 713 | Py_Type(a)->tp_name, Py_Type(b)->tp_name); |
| 714 | goto done; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 715 | } |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 716 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 717 | /* Optimize end cases */ |
| 718 | if (va.len == 0 && PyString_CheckExact(b)) { |
| 719 | result = b; |
| 720 | Py_INCREF(result); |
| 721 | goto done; |
| 722 | } |
| 723 | if (vb.len == 0 && PyString_CheckExact(a)) { |
| 724 | result = a; |
| 725 | Py_INCREF(result); |
| 726 | goto done; |
| 727 | } |
| 728 | |
| 729 | size = va.len + vb.len; |
| 730 | if (size < 0) { |
| 731 | PyErr_NoMemory(); |
| 732 | goto done; |
| 733 | } |
| 734 | |
| 735 | result = PyString_FromStringAndSize(NULL, size); |
| 736 | if (result != NULL) { |
| 737 | memcpy(PyString_AS_STRING(result), va.buf, va.len); |
| 738 | memcpy(PyString_AS_STRING(result) + va.len, vb.buf, vb.len); |
| 739 | } |
| 740 | |
| 741 | done: |
| 742 | if (va.len != -1) |
| 743 | PyObject_ReleaseBuffer(a, &va); |
| 744 | if (vb.len != -1) |
| 745 | PyObject_ReleaseBuffer(b, &vb); |
| 746 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 749 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 750 | string_repeat(register PyStringObject *a, register Py_ssize_t n) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 751 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 752 | register Py_ssize_t i; |
| 753 | register Py_ssize_t j; |
| 754 | register Py_ssize_t size; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 755 | register PyStringObject *op; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 756 | size_t nbytes; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 757 | if (n < 0) |
| 758 | n = 0; |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 759 | /* watch out for overflows: the size can overflow int, |
| 760 | * and the # of bytes needed can overflow size_t |
| 761 | */ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 762 | size = Py_Size(a) * n; |
| 763 | if (n && size / n != Py_Size(a)) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 764 | PyErr_SetString(PyExc_OverflowError, |
| 765 | "repeated string is too long"); |
| 766 | return NULL; |
| 767 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 768 | if (size == Py_Size(a) && PyString_CheckExact(a)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 769 | Py_INCREF(a); |
| 770 | return (PyObject *)a; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 771 | } |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 772 | nbytes = (size_t)size; |
| 773 | if (nbytes + sizeof(PyStringObject) <= nbytes) { |
Tim Peters | 8f42246 | 2000-09-09 06:13:41 +0000 | [diff] [blame] | 774 | PyErr_SetString(PyExc_OverflowError, |
| 775 | "repeated string is too long"); |
| 776 | return NULL; |
| 777 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 778 | op = (PyStringObject *) |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 779 | PyObject_MALLOC(sizeof(PyStringObject) + nbytes); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 780 | if (op == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 781 | return PyErr_NoMemory(); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 782 | PyObject_INIT_VAR(op, &PyString_Type, size); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 783 | op->ob_shash = -1; |
Raymond Hettinger | 0a2f849 | 2003-01-06 22:42:41 +0000 | [diff] [blame] | 784 | op->ob_sval[size] = '\0'; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 785 | if (Py_Size(a) == 1 && n > 0) { |
Raymond Hettinger | 0a2f849 | 2003-01-06 22:42:41 +0000 | [diff] [blame] | 786 | memset(op->ob_sval, a->ob_sval[0] , n); |
| 787 | return (PyObject *) op; |
| 788 | } |
Raymond Hettinger | 698258a | 2003-01-06 10:33:56 +0000 | [diff] [blame] | 789 | i = 0; |
| 790 | if (i < size) { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 791 | Py_MEMCPY(op->ob_sval, a->ob_sval, Py_Size(a)); |
| 792 | i = Py_Size(a); |
Raymond Hettinger | 698258a | 2003-01-06 10:33:56 +0000 | [diff] [blame] | 793 | } |
| 794 | while (i < size) { |
| 795 | j = (i <= size-i) ? i : size-i; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 796 | Py_MEMCPY(op->ob_sval+i, op->ob_sval, j); |
Raymond Hettinger | 698258a | 2003-01-06 10:33:56 +0000 | [diff] [blame] | 797 | i += j; |
| 798 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 799 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 802 | static int |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 803 | string_contains(PyObject *self, PyObject *arg) |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 804 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 805 | Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
| 806 | if (ival == -1 && PyErr_Occurred()) { |
| 807 | Py_buffer varg; |
| 808 | int pos; |
| 809 | PyErr_Clear(); |
| 810 | if (_getbuffer(arg, &varg) < 0) |
| 811 | return -1; |
| 812 | pos = stringlib_find(PyString_AS_STRING(self), Py_Size(self), |
| 813 | varg.buf, varg.len, 0); |
| 814 | PyObject_ReleaseBuffer(arg, &varg); |
| 815 | return pos >= 0; |
| 816 | } |
| 817 | if (ival < 0 || ival >= 256) { |
| 818 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
| 819 | return -1; |
| 820 | } |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 821 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 822 | return memchr(PyString_AS_STRING(self), ival, Py_Size(self)) != NULL; |
| 823 | } |
| 824 | |
| 825 | static PyObject * |
| 826 | string_item(PyStringObject *a, register Py_ssize_t i) |
| 827 | { |
| 828 | if (i < 0 || i >= Py_Size(a)) { |
| 829 | PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 830 | return NULL; |
| 831 | } |
| 832 | return PyInt_FromLong((unsigned char)a->ob_sval[i]); |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 835 | static PyObject* |
| 836 | string_richcompare(PyStringObject *a, PyStringObject *b, int op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 837 | { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 838 | int c; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 839 | Py_ssize_t len_a, len_b; |
| 840 | Py_ssize_t min_len; |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 841 | PyObject *result; |
| 842 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 843 | /* Make sure both arguments are strings. */ |
| 844 | if (!(PyString_Check(a) && PyString_Check(b))) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 845 | if (Py_BytesWarningFlag && (op == Py_EQ) && |
| 846 | (PyObject_IsInstance((PyObject*)a, |
| 847 | (PyObject*)&PyUnicode_Type) || |
| 848 | PyObject_IsInstance((PyObject*)b, |
| 849 | (PyObject*)&PyUnicode_Type))) { |
| 850 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 851 | "Comparsion between bytes and string", 1)) |
| 852 | return NULL; |
| 853 | } |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 854 | result = Py_NotImplemented; |
| 855 | goto out; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 856 | } |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 857 | if (a == b) { |
| 858 | switch (op) { |
| 859 | case Py_EQ:case Py_LE:case Py_GE: |
| 860 | result = Py_True; |
| 861 | goto out; |
| 862 | case Py_NE:case Py_LT:case Py_GT: |
| 863 | result = Py_False; |
| 864 | goto out; |
| 865 | } |
| 866 | } |
| 867 | if (op == Py_EQ) { |
| 868 | /* Supporting Py_NE here as well does not save |
| 869 | much time, since Py_NE is rarely used. */ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 870 | if (Py_Size(a) == Py_Size(b) |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 871 | && (a->ob_sval[0] == b->ob_sval[0] |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 872 | && memcmp(a->ob_sval, b->ob_sval, Py_Size(a)) == 0)) { |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 873 | result = Py_True; |
| 874 | } else { |
| 875 | result = Py_False; |
| 876 | } |
| 877 | goto out; |
| 878 | } |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 879 | len_a = Py_Size(a); len_b = Py_Size(b); |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 880 | min_len = (len_a < len_b) ? len_a : len_b; |
| 881 | if (min_len > 0) { |
| 882 | c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
| 883 | if (c==0) |
| 884 | c = memcmp(a->ob_sval, b->ob_sval, min_len); |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 885 | } else |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 886 | c = 0; |
| 887 | if (c == 0) |
| 888 | c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
| 889 | switch (op) { |
| 890 | case Py_LT: c = c < 0; break; |
| 891 | case Py_LE: c = c <= 0; break; |
| 892 | case Py_EQ: assert(0); break; /* unreachable */ |
| 893 | case Py_NE: c = c != 0; break; |
| 894 | case Py_GT: c = c > 0; break; |
| 895 | case Py_GE: c = c >= 0; break; |
| 896 | default: |
| 897 | result = Py_NotImplemented; |
| 898 | goto out; |
| 899 | } |
| 900 | result = c ? Py_True : Py_False; |
| 901 | out: |
| 902 | Py_INCREF(result); |
| 903 | return result; |
| 904 | } |
| 905 | |
| 906 | int |
| 907 | _PyString_Eq(PyObject *o1, PyObject *o2) |
| 908 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 909 | PyStringObject *a = (PyStringObject*) o1; |
| 910 | PyStringObject *b = (PyStringObject*) o2; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 911 | return Py_Size(a) == Py_Size(b) |
| 912 | && *a->ob_sval == *b->ob_sval |
| 913 | && memcmp(a->ob_sval, b->ob_sval, Py_Size(a)) == 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 916 | static long |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 917 | string_hash(PyStringObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 918 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 919 | register Py_ssize_t len; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 920 | register unsigned char *p; |
| 921 | register long x; |
| 922 | |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 923 | if (a->ob_shash != -1) |
| 924 | return a->ob_shash; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 925 | len = Py_Size(a); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 926 | p = (unsigned char *) a->ob_sval; |
| 927 | x = *p << 7; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 928 | while (--len >= 0) |
Guido van Rossum | eddcb3b | 1996-09-11 20:22:48 +0000 | [diff] [blame] | 929 | x = (1000003*x) ^ *p++; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 930 | x ^= Py_Size(a); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 931 | if (x == -1) |
| 932 | x = -2; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 933 | a->ob_shash = x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 934 | return x; |
| 935 | } |
| 936 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 937 | static PyObject* |
| 938 | string_subscript(PyStringObject* self, PyObject* item) |
| 939 | { |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 940 | if (PyIndex_Check(item)) { |
| 941 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 942 | if (i == -1 && PyErr_Occurred()) |
| 943 | return NULL; |
| 944 | if (i < 0) |
| 945 | i += PyString_GET_SIZE(self); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 946 | if (i < 0 || i >= PyString_GET_SIZE(self)) { |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 947 | PyErr_SetString(PyExc_IndexError, |
| 948 | "string index out of range"); |
| 949 | return NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 950 | } |
| 951 | return PyInt_FromLong((unsigned char)self->ob_sval[i]); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 952 | } |
| 953 | else if (PySlice_Check(item)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 954 | Py_ssize_t start, stop, step, slicelength, cur, i; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 955 | char* source_buf; |
| 956 | char* result_buf; |
| 957 | PyObject* result; |
| 958 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 959 | if (PySlice_GetIndicesEx((PySliceObject*)item, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 960 | PyString_GET_SIZE(self), |
| 961 | &start, &stop, &step, &slicelength) < 0) { |
| 962 | return NULL; |
| 963 | } |
| 964 | |
| 965 | if (slicelength <= 0) { |
| 966 | return PyString_FromStringAndSize("", 0); |
| 967 | } |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 968 | else if (start == 0 && step == 1 && |
| 969 | slicelength == PyString_GET_SIZE(self) && |
| 970 | PyString_CheckExact(self)) { |
| 971 | Py_INCREF(self); |
| 972 | return (PyObject *)self; |
| 973 | } |
| 974 | else if (step == 1) { |
| 975 | return PyString_FromStringAndSize( |
| 976 | PyString_AS_STRING(self) + start, |
| 977 | slicelength); |
| 978 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 979 | else { |
| 980 | source_buf = PyString_AsString((PyObject*)self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 981 | result_buf = (char *)PyMem_Malloc(slicelength); |
Neal Norwitz | 95c1e50 | 2005-10-20 04:15:52 +0000 | [diff] [blame] | 982 | if (result_buf == NULL) |
| 983 | return PyErr_NoMemory(); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 984 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 985 | for (cur = start, i = 0; i < slicelength; |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 986 | cur += step, i++) { |
| 987 | result_buf[i] = source_buf[cur]; |
| 988 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 989 | |
| 990 | result = PyString_FromStringAndSize(result_buf, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 991 | slicelength); |
| 992 | PyMem_Free(result_buf); |
| 993 | return result; |
| 994 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 995 | } |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 996 | else { |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 997 | PyErr_Format(PyExc_TypeError, |
| 998 | "string indices must be integers, not %.200s", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 999 | Py_Type(item)->tp_name); |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1000 | return NULL; |
| 1001 | } |
| 1002 | } |
| 1003 | |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1004 | static int |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 1005 | string_buffer_getbuffer(PyStringObject *self, Py_buffer *view, int flags) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1006 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1007 | return PyBuffer_FillInfo(view, (void *)self->ob_sval, Py_Size(self), |
| 1008 | 0, flags); |
Guido van Rossum | 1db7070 | 1998-10-08 02:18:52 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1011 | static PySequenceMethods string_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1012 | (lenfunc)string_length, /*sq_length*/ |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 1013 | (binaryfunc)string_concat, /*sq_concat*/ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1014 | (ssizeargfunc)string_repeat, /*sq_repeat*/ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1015 | (ssizeargfunc)string_item, /*sq_item*/ |
Thomas Wouters | d2cf20e | 2007-08-30 22:57:53 +0000 | [diff] [blame] | 1016 | 0, /*sq_slice*/ |
Guido van Rossum | f380e66 | 1991-06-04 19:36:32 +0000 | [diff] [blame] | 1017 | 0, /*sq_ass_item*/ |
| 1018 | 0, /*sq_ass_slice*/ |
Guido van Rossum | 9284a57 | 2000-03-07 15:53:43 +0000 | [diff] [blame] | 1019 | (objobjproc)string_contains /*sq_contains*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1020 | }; |
| 1021 | |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1022 | static PyMappingMethods string_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1023 | (lenfunc)string_length, |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 1024 | (binaryfunc)string_subscript, |
| 1025 | 0, |
| 1026 | }; |
| 1027 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1028 | static PyBufferProcs string_as_buffer = { |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1029 | (getbufferproc)string_buffer_getbuffer, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1030 | NULL, |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1031 | }; |
| 1032 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1033 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1034 | #define LEFTSTRIP 0 |
| 1035 | #define RIGHTSTRIP 1 |
| 1036 | #define BOTHSTRIP 2 |
| 1037 | |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1038 | /* Arrays indexed by above */ |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 1039 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 1040 | |
| 1041 | #define STRIPNAME(i) (stripformat[i]+3) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1042 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1043 | |
| 1044 | /* Don't call if length < 2 */ |
| 1045 | #define Py_STRING_MATCH(target, offset, pattern, length) \ |
| 1046 | (target[offset] == pattern[0] && \ |
| 1047 | target[offset+length-1] == pattern[length-1] && \ |
| 1048 | !memcmp(target+offset+1, pattern+1, length-2) ) |
| 1049 | |
| 1050 | |
| 1051 | /* Overallocate the initial list to reduce the number of reallocs for small |
| 1052 | split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three |
| 1053 | resizes, to sizes 4, 8, then 16. Most observed string splits are for human |
| 1054 | text (roughly 11 words per line) and field delimited data (usually 1-10 |
| 1055 | fields). For large strings the split algorithms are bandwidth limited |
| 1056 | so increasing the preallocation likely will not improve things.*/ |
| 1057 | |
| 1058 | #define MAX_PREALLOC 12 |
| 1059 | |
| 1060 | /* 5 splits gives 6 elements */ |
| 1061 | #define PREALLOC_SIZE(maxsplit) \ |
| 1062 | (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1) |
| 1063 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1064 | #define SPLIT_ADD(data, left, right) { \ |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1065 | str = PyString_FromStringAndSize((data) + (left), \ |
| 1066 | (right) - (left)); \ |
| 1067 | if (str == NULL) \ |
| 1068 | goto onError; \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1069 | if (count < MAX_PREALLOC) { \ |
| 1070 | PyList_SET_ITEM(list, count, str); \ |
| 1071 | } else { \ |
| 1072 | if (PyList_Append(list, str)) { \ |
| 1073 | Py_DECREF(str); \ |
| 1074 | goto onError; \ |
| 1075 | } \ |
| 1076 | else \ |
| 1077 | Py_DECREF(str); \ |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1078 | } \ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1079 | count++; } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1080 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1081 | /* Always force the list to the expected size. */ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1082 | #define FIX_PREALLOC_SIZE(list) Py_Size(list) = count |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1083 | |
Guido van Rossum | 6ccd3f2 | 2007-10-09 03:46:30 +0000 | [diff] [blame] | 1084 | #define SKIP_SPACE(s, i, len) { while (i<len && ISSPACE(s[i])) i++; } |
| 1085 | #define SKIP_NONSPACE(s, i, len) { while (i<len && !ISSPACE(s[i])) i++; } |
| 1086 | #define RSKIP_SPACE(s, i) { while (i>=0 && ISSPACE(s[i])) i--; } |
| 1087 | #define RSKIP_NONSPACE(s, i) { while (i>=0 && !ISSPACE(s[i])) i--; } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1088 | |
| 1089 | Py_LOCAL_INLINE(PyObject *) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1090 | 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] | 1091 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1092 | Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1093 | PyObject *str; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1094 | PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1095 | |
| 1096 | if (list == NULL) |
| 1097 | return NULL; |
| 1098 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1099 | i = j = 0; |
| 1100 | |
| 1101 | while (maxsplit-- > 0) { |
| 1102 | SKIP_SPACE(s, i, len); |
| 1103 | if (i==len) break; |
| 1104 | j = i; i++; |
| 1105 | SKIP_NONSPACE(s, i, len); |
| 1106 | SPLIT_ADD(s, j, i); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1107 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1108 | |
| 1109 | if (i < len) { |
| 1110 | /* Only occurs when maxsplit was reached */ |
| 1111 | /* Skip any remaining whitespace and copy to end of string */ |
| 1112 | SKIP_SPACE(s, i, len); |
| 1113 | if (i != len) |
| 1114 | SPLIT_ADD(s, i, len); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1115 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1116 | FIX_PREALLOC_SIZE(list); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1117 | return list; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1118 | onError: |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1119 | Py_DECREF(list); |
| 1120 | return NULL; |
| 1121 | } |
| 1122 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1123 | Py_LOCAL_INLINE(PyObject *) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1124 | 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] | 1125 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1126 | register Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1127 | PyObject *str; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1128 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1129 | |
| 1130 | if (list == NULL) |
| 1131 | return NULL; |
| 1132 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1133 | i = j = 0; |
| 1134 | while ((j < len) && (maxcount-- > 0)) { |
| 1135 | for(; j<len; j++) { |
| 1136 | /* I found that using memchr makes no difference */ |
| 1137 | if (s[j] == ch) { |
| 1138 | SPLIT_ADD(s, i, j); |
| 1139 | i = j = j + 1; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1140 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1141 | } |
| 1142 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1143 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1144 | if (i <= len) { |
| 1145 | SPLIT_ADD(s, i, len); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1146 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1147 | FIX_PREALLOC_SIZE(list); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1148 | return list; |
| 1149 | |
| 1150 | onError: |
| 1151 | Py_DECREF(list); |
| 1152 | return NULL; |
| 1153 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1154 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1155 | PyDoc_STRVAR(split__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1156 | "B.split([sep[, maxsplit]]) -> list of bytes\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1157 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1158 | Return a list of the sections in B, using sep as the delimiter.\n\ |
| 1159 | If sep is not given, B is split on ASCII whitespace characters\n\ |
| 1160 | (space, tab, return, newline, formfeed, vertical tab).\n\ |
| 1161 | If maxsplit is given, at most maxsplit splits are done."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1162 | |
| 1163 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1164 | string_split(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1165 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1166 | Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1167 | Py_ssize_t maxsplit = -1, count=0; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1168 | const char *s = PyString_AS_STRING(self), *sub; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1169 | Py_buffer vsub; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1170 | PyObject *list, *str, *subobj = Py_None; |
| 1171 | #ifdef USE_FAST |
| 1172 | Py_ssize_t pos; |
| 1173 | #endif |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1174 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1175 | if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1176 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1177 | if (maxsplit < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1178 | maxsplit = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1179 | if (subobj == Py_None) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1180 | return split_whitespace(s, len, maxsplit); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1181 | if (_getbuffer(subobj, &vsub) < 0) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1182 | return NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1183 | sub = vsub.buf; |
| 1184 | n = vsub.len; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1185 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1186 | if (n == 0) { |
| 1187 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1188 | PyObject_ReleaseBuffer(subobj, &vsub); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1189 | return NULL; |
| 1190 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1191 | else if (n == 1) { |
| 1192 | char ch = sub[0]; |
| 1193 | PyObject_ReleaseBuffer(subobj, &vsub); |
| 1194 | return split_char(s, len, ch, maxsplit); |
| 1195 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1196 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1197 | list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1198 | if (list == NULL) { |
| 1199 | PyObject_ReleaseBuffer(subobj, &vsub); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1200 | return NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1201 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1202 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1203 | #ifdef USE_FAST |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1204 | i = j = 0; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1205 | while (maxsplit-- > 0) { |
| 1206 | pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH); |
| 1207 | if (pos < 0) |
| 1208 | break; |
| 1209 | j = i+pos; |
| 1210 | SPLIT_ADD(s, i, j); |
| 1211 | i = j + n; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1212 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1213 | #else |
| 1214 | i = j = 0; |
| 1215 | while ((j+n <= len) && (maxsplit-- > 0)) { |
| 1216 | for (; j+n <= len; j++) { |
| 1217 | if (Py_STRING_MATCH(s, j, sub, n)) { |
| 1218 | SPLIT_ADD(s, i, j); |
| 1219 | i = j = j + n; |
| 1220 | break; |
| 1221 | } |
| 1222 | } |
| 1223 | } |
| 1224 | #endif |
| 1225 | SPLIT_ADD(s, i, len); |
| 1226 | FIX_PREALLOC_SIZE(list); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1227 | PyObject_ReleaseBuffer(subobj, &vsub); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1228 | return list; |
| 1229 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1230 | onError: |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1231 | Py_DECREF(list); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1232 | PyObject_ReleaseBuffer(subobj, &vsub); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1233 | return NULL; |
| 1234 | } |
| 1235 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1236 | PyDoc_STRVAR(partition__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1237 | "B.partition(sep) -> (head, sep, tail)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1238 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1239 | Searches for the separator sep in B, and returns the part before it,\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1240 | the separator itself, and the part after it. If the separator is not\n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1241 | found, returns B and two empty bytes objects."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1242 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1243 | static PyObject * |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1244 | string_partition(PyStringObject *self, PyObject *sep_obj) |
| 1245 | { |
| 1246 | const char *sep; |
| 1247 | Py_ssize_t sep_len; |
| 1248 | |
| 1249 | if (PyString_Check(sep_obj)) { |
| 1250 | sep = PyString_AS_STRING(sep_obj); |
| 1251 | sep_len = PyString_GET_SIZE(sep_obj); |
| 1252 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1253 | else if (PyUnicode_Check(sep_obj)) |
| 1254 | return PyUnicode_Partition((PyObject *) self, sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1255 | else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
| 1256 | return NULL; |
| 1257 | |
| 1258 | return stringlib_partition( |
| 1259 | (PyObject*) self, |
| 1260 | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
| 1261 | sep_obj, sep, sep_len |
| 1262 | ); |
| 1263 | } |
| 1264 | |
| 1265 | PyDoc_STRVAR(rpartition__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1266 | "B.rpartition(sep) -> (tail, sep, head)\n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1267 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1268 | Searches for the separator sep in B, starting at the end of B,\n\ |
| 1269 | and returns the part before it, the separator itself, and the\n\ |
| 1270 | part after it. If the separator is not found, returns two empty\n\ |
| 1271 | bytes objects and B."); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1272 | |
| 1273 | static PyObject * |
| 1274 | string_rpartition(PyStringObject *self, PyObject *sep_obj) |
| 1275 | { |
| 1276 | const char *sep; |
| 1277 | Py_ssize_t sep_len; |
| 1278 | |
| 1279 | if (PyString_Check(sep_obj)) { |
| 1280 | sep = PyString_AS_STRING(sep_obj); |
| 1281 | sep_len = PyString_GET_SIZE(sep_obj); |
| 1282 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1283 | else if (PyUnicode_Check(sep_obj)) |
| 1284 | return PyUnicode_Partition((PyObject *) self, sep_obj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1285 | else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
| 1286 | return NULL; |
| 1287 | |
| 1288 | return stringlib_rpartition( |
| 1289 | (PyObject*) self, |
| 1290 | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
| 1291 | sep_obj, sep, sep_len |
| 1292 | ); |
| 1293 | } |
| 1294 | |
| 1295 | Py_LOCAL_INLINE(PyObject *) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1296 | 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] | 1297 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1298 | Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1299 | PyObject *str; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1300 | PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1301 | |
| 1302 | if (list == NULL) |
| 1303 | return NULL; |
| 1304 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1305 | i = j = len-1; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1306 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1307 | while (maxsplit-- > 0) { |
| 1308 | RSKIP_SPACE(s, i); |
| 1309 | if (i<0) break; |
| 1310 | j = i; i--; |
| 1311 | RSKIP_NONSPACE(s, i); |
| 1312 | SPLIT_ADD(s, i + 1, j + 1); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1313 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1314 | if (i >= 0) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1315 | /* Only occurs when maxsplit was reached. Skip any remaining |
| 1316 | whitespace and copy to beginning of string. */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1317 | RSKIP_SPACE(s, i); |
| 1318 | if (i >= 0) |
| 1319 | SPLIT_ADD(s, 0, i + 1); |
| 1320 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1321 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1322 | FIX_PREALLOC_SIZE(list); |
| 1323 | if (PyList_Reverse(list) < 0) |
| 1324 | goto onError; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1325 | return list; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1326 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1327 | Py_DECREF(list); |
| 1328 | return NULL; |
| 1329 | } |
| 1330 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1331 | Py_LOCAL_INLINE(PyObject *) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1332 | 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] | 1333 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1334 | register Py_ssize_t i, j, count=0; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1335 | PyObject *str; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1336 | PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1337 | |
| 1338 | if (list == NULL) |
| 1339 | return NULL; |
| 1340 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1341 | i = j = len - 1; |
| 1342 | while ((i >= 0) && (maxcount-- > 0)) { |
| 1343 | for (; i >= 0; i--) { |
| 1344 | if (s[i] == ch) { |
| 1345 | SPLIT_ADD(s, i + 1, j + 1); |
| 1346 | j = i = i - 1; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1347 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1348 | } |
| 1349 | } |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1350 | } |
| 1351 | if (j >= -1) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1352 | SPLIT_ADD(s, 0, j + 1); |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1353 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1354 | FIX_PREALLOC_SIZE(list); |
| 1355 | if (PyList_Reverse(list) < 0) |
| 1356 | goto onError; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1357 | return list; |
| 1358 | |
| 1359 | onError: |
| 1360 | Py_DECREF(list); |
| 1361 | return NULL; |
| 1362 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1363 | |
| 1364 | PyDoc_STRVAR(rsplit__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1365 | "B.rsplit([sep[, maxsplit]]) -> list of strings\n\ |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1366 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1367 | Return a list of the sections in B, using sep as the delimiter,\n\ |
| 1368 | starting at the end of B and working to the front.\n\ |
| 1369 | If sep is not given, B is split on ASCII whitespace characters\n\ |
| 1370 | (space, tab, return, newline, formfeed, vertical tab).\n\ |
| 1371 | If maxsplit is given, at most maxsplit splits are done."); |
| 1372 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1373 | |
| 1374 | static PyObject * |
| 1375 | string_rsplit(PyStringObject *self, PyObject *args) |
| 1376 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1377 | Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1378 | Py_ssize_t maxsplit = -1, count=0; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1379 | const char *s = PyString_AS_STRING(self), *sub; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1380 | Py_buffer vsub; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1381 | PyObject *list, *str, *subobj = Py_None; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1382 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1383 | if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit)) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1384 | return NULL; |
| 1385 | if (maxsplit < 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1386 | maxsplit = PY_SSIZE_T_MAX; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1387 | if (subobj == Py_None) |
| 1388 | return rsplit_whitespace(s, len, maxsplit); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1389 | if (_getbuffer(subobj, &vsub) < 0) |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1390 | return NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1391 | sub = vsub.buf; |
| 1392 | n = vsub.len; |
Hye-Shik Chang | 75c00ef | 2004-01-05 00:29:51 +0000 | [diff] [blame] | 1393 | |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1394 | if (n == 0) { |
| 1395 | PyErr_SetString(PyExc_ValueError, "empty separator"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1396 | PyObject_ReleaseBuffer(subobj, &vsub); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1397 | return NULL; |
| 1398 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1399 | else if (n == 1) { |
| 1400 | char ch = sub[0]; |
| 1401 | PyObject_ReleaseBuffer(subobj, &vsub); |
| 1402 | return rsplit_char(s, len, ch, maxsplit); |
| 1403 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1404 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1405 | list = PyList_New(PREALLOC_SIZE(maxsplit)); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1406 | if (list == NULL) { |
| 1407 | PyObject_ReleaseBuffer(subobj, &vsub); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1408 | return NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1409 | } |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1410 | |
| 1411 | j = len; |
| 1412 | i = j - n; |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1413 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1414 | while ( (i >= 0) && (maxsplit-- > 0) ) { |
| 1415 | for (; i>=0; i--) { |
| 1416 | if (Py_STRING_MATCH(s, i, sub, n)) { |
| 1417 | SPLIT_ADD(s, i + n, j); |
| 1418 | j = i; |
| 1419 | i -= n; |
| 1420 | break; |
| 1421 | } |
| 1422 | } |
| 1423 | } |
| 1424 | SPLIT_ADD(s, 0, j); |
| 1425 | FIX_PREALLOC_SIZE(list); |
| 1426 | if (PyList_Reverse(list) < 0) |
| 1427 | goto onError; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1428 | PyObject_ReleaseBuffer(subobj, &vsub); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1429 | return list; |
| 1430 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1431 | onError: |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1432 | Py_DECREF(list); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1433 | PyObject_ReleaseBuffer(subobj, &vsub); |
Hye-Shik Chang | 3ae811b | 2003-12-15 18:49:53 +0000 | [diff] [blame] | 1434 | return NULL; |
| 1435 | } |
| 1436 | |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 1437 | #undef SPLIT_ADD |
| 1438 | #undef MAX_PREALLOC |
| 1439 | #undef PREALLOC_SIZE |
| 1440 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1441 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1442 | PyDoc_STRVAR(join__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1443 | "B.join(iterable_of_bytes) -> bytes\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1444 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1445 | Concatenates any number of bytes objects, with B in between each pair.\n\ |
| 1446 | Example: b'.'.join([b'ab', b'pq', b'rs']) -> b'ab.pq.rs'."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1447 | |
| 1448 | static PyObject * |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1449 | string_join(PyObject *self, PyObject *orig) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1450 | { |
| 1451 | char *sep = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1452 | const Py_ssize_t seplen = PyString_GET_SIZE(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1453 | PyObject *res = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1454 | char *p; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1455 | Py_ssize_t seqlen = 0; |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1456 | size_t sz = 0; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1457 | Py_ssize_t i; |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1458 | PyObject *seq, *item; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1459 | |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1460 | seq = PySequence_Fast(orig, ""); |
| 1461 | if (seq == NULL) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1462 | return NULL; |
| 1463 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1464 | |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1465 | seqlen = PySequence_Size(seq); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1466 | if (seqlen == 0) { |
| 1467 | Py_DECREF(seq); |
| 1468 | return PyString_FromString(""); |
| 1469 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1470 | if (seqlen == 1) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1471 | item = PySequence_Fast_GET_ITEM(seq, 0); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1472 | if (PyString_CheckExact(item)) { |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1473 | Py_INCREF(item); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1474 | Py_DECREF(seq); |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1475 | return item; |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1476 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1477 | } |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1478 | |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1479 | /* There are at least two things to join, or else we have a subclass |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1480 | * of the builtin types in the sequence. |
Raymond Hettinger | 674f241 | 2004-08-23 23:23:54 +0000 | [diff] [blame] | 1481 | * Do a pre-pass to figure out the total amount of space we'll |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1482 | * need (sz), and see whether all argument are bytes. |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1483 | */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1484 | /* XXX Shouldn't we use _getbuffer() on these items instead? */ |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1485 | for (i = 0; i < seqlen; i++) { |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1486 | const size_t old_sz = sz; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1487 | item = PySequence_Fast_GET_ITEM(seq, i); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1488 | if (!PyString_Check(item) && !PyBytes_Check(item)) { |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1489 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1490 | "sequence item %zd: expected bytes," |
Jeremy Hylton | 88887aa | 2000-07-11 20:55:38 +0000 | [diff] [blame] | 1491 | " %.80s found", |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1492 | i, Py_Type(item)->tp_name); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1493 | Py_DECREF(seq); |
| 1494 | return NULL; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1495 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1496 | sz += Py_Size(item); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1497 | if (i != 0) |
| 1498 | sz += seplen; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1499 | if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1500 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1501 | "join() result is too long for a Python string"); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1502 | Py_DECREF(seq); |
| 1503 | return NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1504 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | /* Allocate result space. */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1508 | res = PyString_FromStringAndSize((char*)NULL, sz); |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1509 | if (res == NULL) { |
| 1510 | Py_DECREF(seq); |
| 1511 | return NULL; |
| 1512 | } |
| 1513 | |
| 1514 | /* Catenate everything. */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1515 | /* I'm not worried about a PyBytes item growing because there's |
| 1516 | nowhere in this function where we release the GIL. */ |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1517 | p = PyString_AS_STRING(res); |
| 1518 | for (i = 0; i < seqlen; ++i) { |
| 1519 | size_t n; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1520 | char *q; |
| 1521 | if (i) { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1522 | Py_MEMCPY(p, sep, seplen); |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1523 | p += seplen; |
Jeremy Hylton | 194e43e | 2000-07-10 21:30:28 +0000 | [diff] [blame] | 1524 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1525 | item = PySequence_Fast_GET_ITEM(seq, i); |
| 1526 | n = Py_Size(item); |
| 1527 | if (PyString_Check(item)) |
| 1528 | q = PyString_AS_STRING(item); |
| 1529 | else |
| 1530 | q = PyBytes_AS_STRING(item); |
| 1531 | Py_MEMCPY(p, q, n); |
| 1532 | p += n; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1533 | } |
Tim Peters | 19fe14e | 2001-01-19 03:03:47 +0000 | [diff] [blame] | 1534 | |
Jeremy Hylton | 4904829 | 2000-07-11 03:28:17 +0000 | [diff] [blame] | 1535 | Py_DECREF(seq); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1536 | return res; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Tim Peters | 52e155e | 2001-06-16 05:42:57 +0000 | [diff] [blame] | 1539 | PyObject * |
| 1540 | _PyString_Join(PyObject *sep, PyObject *x) |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1541 | { |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1542 | assert(sep != NULL && PyString_Check(sep)); |
| 1543 | assert(x != NULL); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1544 | return string_join(sep, x); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1547 | Py_LOCAL_INLINE(void) |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1548 | 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] | 1549 | { |
| 1550 | if (*end > len) |
| 1551 | *end = len; |
| 1552 | else if (*end < 0) |
| 1553 | *end += len; |
| 1554 | if (*end < 0) |
| 1555 | *end = 0; |
| 1556 | if (*start < 0) |
| 1557 | *start += len; |
| 1558 | if (*start < 0) |
| 1559 | *start = 0; |
| 1560 | } |
| 1561 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1562 | Py_LOCAL_INLINE(Py_ssize_t) |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1563 | string_find_internal(PyStringObject *self, PyObject *args, int dir) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1564 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1565 | PyObject *subobj; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1566 | const char *sub; |
| 1567 | Py_ssize_t sub_len; |
| 1568 | Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1569 | PyObject *obj_start=Py_None, *obj_end=Py_None; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1570 | |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1571 | if (!PyArg_ParseTuple(args, "O|OO:find/rfind/index/rindex", &subobj, |
| 1572 | &obj_start, &obj_end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1573 | return -2; |
Christian Heimes | 9cd1775 | 2007-11-18 19:35:23 +0000 | [diff] [blame] | 1574 | /* To support None in "start" and "end" arguments, meaning |
| 1575 | the same as if they were not passed. |
| 1576 | */ |
| 1577 | if (obj_start != Py_None) |
| 1578 | if (!_PyEval_SliceIndex(obj_start, &start)) |
| 1579 | return -2; |
| 1580 | if (obj_end != Py_None) |
| 1581 | if (!_PyEval_SliceIndex(obj_end, &end)) |
| 1582 | return -2; |
| 1583 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1584 | if (PyString_Check(subobj)) { |
| 1585 | sub = PyString_AS_STRING(subobj); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1586 | sub_len = PyString_GET_SIZE(subobj); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1587 | } |
| 1588 | else if (PyUnicode_Check(subobj)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1589 | return PyUnicode_Find( |
| 1590 | (PyObject *)self, subobj, start, end, dir); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1591 | else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len)) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1592 | /* XXX - the "expected a character buffer object" is pretty |
| 1593 | confusing for a non-expert. remap to something else ? */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1594 | return -2; |
| 1595 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1596 | if (dir > 0) |
| 1597 | return stringlib_find_slice( |
| 1598 | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
| 1599 | sub, sub_len, start, end); |
| 1600 | else |
| 1601 | return stringlib_rfind_slice( |
| 1602 | PyString_AS_STRING(self), PyString_GET_SIZE(self), |
| 1603 | sub, sub_len, start, end); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
| 1606 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1607 | PyDoc_STRVAR(find__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1608 | "B.find(sub [,start [,end]]) -> int\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1609 | \n\ |
| 1610 | Return the lowest index in S where substring sub is found,\n\ |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 1611 | such that sub is contained within s[start:end]. Optional\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1612 | arguments start and end are interpreted as in slice notation.\n\ |
| 1613 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1614 | Return -1 on failure."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1615 | |
| 1616 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1617 | string_find(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1618 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1619 | Py_ssize_t result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1620 | if (result == -2) |
| 1621 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1622 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1626 | PyDoc_STRVAR(index__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1627 | "B.index(sub [,start [,end]]) -> int\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1628 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1629 | Like B.find() but raise ValueError when the substring is not found."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1630 | |
| 1631 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1632 | string_index(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1633 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1634 | Py_ssize_t result = string_find_internal(self, args, +1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1635 | if (result == -2) |
| 1636 | return NULL; |
| 1637 | if (result == -1) { |
| 1638 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 5d5e7c0 | 2003-01-15 05:32:57 +0000 | [diff] [blame] | 1639 | "substring not found"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1640 | return NULL; |
| 1641 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1642 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1646 | PyDoc_STRVAR(rfind__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1647 | "B.rfind(sub [,start [,end]]) -> int\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1648 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1649 | Return the highest index in B where substring sub is found,\n\ |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 1650 | such that sub is contained within s[start:end]. Optional\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1651 | arguments start and end are interpreted as in slice notation.\n\ |
| 1652 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1653 | Return -1 on failure."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1654 | |
| 1655 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1656 | string_rfind(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1657 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1658 | Py_ssize_t result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1659 | if (result == -2) |
| 1660 | return NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1661 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1665 | PyDoc_STRVAR(rindex__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1666 | "B.rindex(sub [,start [,end]]) -> int\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1667 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1668 | Like B.rfind() but raise ValueError when the substring is not found."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1669 | |
| 1670 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1671 | string_rindex(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1672 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1673 | Py_ssize_t result = string_find_internal(self, args, -1); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1674 | if (result == -2) |
| 1675 | return NULL; |
| 1676 | if (result == -1) { |
| 1677 | PyErr_SetString(PyExc_ValueError, |
Raymond Hettinger | 5d5e7c0 | 2003-01-15 05:32:57 +0000 | [diff] [blame] | 1678 | "substring not found"); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1679 | return NULL; |
| 1680 | } |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1681 | return PyInt_FromSsize_t(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1685 | Py_LOCAL_INLINE(PyObject *) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1686 | do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj) |
| 1687 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1688 | Py_buffer vsep; |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1689 | char *s = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1690 | Py_ssize_t len = PyString_GET_SIZE(self); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1691 | char *sep; |
| 1692 | Py_ssize_t seplen; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1693 | Py_ssize_t i, j; |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1694 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1695 | if (_getbuffer(sepobj, &vsep) < 0) |
| 1696 | return NULL; |
| 1697 | sep = vsep.buf; |
| 1698 | seplen = vsep.len; |
| 1699 | |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1700 | i = 0; |
| 1701 | if (striptype != RIGHTSTRIP) { |
| 1702 | while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) { |
| 1703 | i++; |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | j = len; |
| 1708 | if (striptype != LEFTSTRIP) { |
| 1709 | do { |
| 1710 | j--; |
| 1711 | } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen)); |
| 1712 | j++; |
| 1713 | } |
| 1714 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1715 | PyObject_ReleaseBuffer(sepobj, &vsep); |
| 1716 | |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1717 | if (i == 0 && j == len && PyString_CheckExact(self)) { |
| 1718 | Py_INCREF(self); |
| 1719 | return (PyObject*)self; |
| 1720 | } |
| 1721 | else |
| 1722 | return PyString_FromStringAndSize(s+i, j-i); |
| 1723 | } |
| 1724 | |
| 1725 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1726 | Py_LOCAL_INLINE(PyObject *) |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1727 | do_strip(PyStringObject *self, int striptype) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1728 | { |
| 1729 | char *s = PyString_AS_STRING(self); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1730 | Py_ssize_t len = PyString_GET_SIZE(self), i, j; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1731 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1732 | i = 0; |
| 1733 | if (striptype != RIGHTSTRIP) { |
Guido van Rossum | 6ccd3f2 | 2007-10-09 03:46:30 +0000 | [diff] [blame] | 1734 | while (i < len && ISSPACE(s[i])) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1735 | i++; |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | j = len; |
| 1740 | if (striptype != LEFTSTRIP) { |
| 1741 | do { |
| 1742 | j--; |
Guido van Rossum | 6ccd3f2 | 2007-10-09 03:46:30 +0000 | [diff] [blame] | 1743 | } while (j >= i && ISSPACE(s[j])); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1744 | j++; |
| 1745 | } |
| 1746 | |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1747 | if (i == 0 && j == len && PyString_CheckExact(self)) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1748 | Py_INCREF(self); |
| 1749 | return (PyObject*)self; |
| 1750 | } |
| 1751 | else |
| 1752 | return PyString_FromStringAndSize(s+i, j-i); |
| 1753 | } |
| 1754 | |
| 1755 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1756 | Py_LOCAL_INLINE(PyObject *) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1757 | do_argstrip(PyStringObject *self, int striptype, PyObject *args) |
| 1758 | { |
| 1759 | PyObject *sep = NULL; |
| 1760 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 1761 | if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1762 | return NULL; |
| 1763 | |
| 1764 | if (sep != NULL && sep != Py_None) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1765 | return do_xstrip(self, striptype, sep); |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1766 | } |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1767 | return do_strip(self, striptype); |
| 1768 | } |
| 1769 | |
| 1770 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1771 | PyDoc_STRVAR(strip__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1772 | "B.strip([bytes]) -> bytes\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1773 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1774 | Strip leading and trailing bytes contained in the argument.\n\ |
| 1775 | If the argument is omitted, strip trailing ASCII whitespace."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1776 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1777 | string_strip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1778 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1779 | if (PyTuple_GET_SIZE(args) == 0) |
| 1780 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 1781 | else |
| 1782 | return do_argstrip(self, BOTHSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1786 | PyDoc_STRVAR(lstrip__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1787 | "B.lstrip([bytes]) -> bytes\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1788 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1789 | Strip leading bytes contained in the argument.\n\ |
| 1790 | If the argument is omitted, strip leading ASCII whitespace."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1791 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1792 | string_lstrip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1793 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1794 | if (PyTuple_GET_SIZE(args) == 0) |
| 1795 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 1796 | else |
| 1797 | return do_argstrip(self, LEFTSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1801 | PyDoc_STRVAR(rstrip__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1802 | "B.rstrip([bytes]) -> bytes\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1803 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1804 | Strip trailing bytes contained in the argument.\n\ |
| 1805 | If the argument is omitted, strip trailing ASCII whitespace."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1806 | static PyObject * |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1807 | string_rstrip(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1808 | { |
Guido van Rossum | 018b0eb | 2002-04-13 00:56:08 +0000 | [diff] [blame] | 1809 | if (PyTuple_GET_SIZE(args) == 0) |
| 1810 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 1811 | else |
| 1812 | return do_argstrip(self, RIGHTSTRIP, args); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1816 | PyDoc_STRVAR(count__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1817 | "B.count(sub [,start [,end]]) -> int\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1818 | \n\ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1819 | Return the number of non-overlapping occurrences of substring sub in\n\ |
| 1820 | string S[start:end]. Optional arguments start and end are interpreted\n\ |
| 1821 | as in slice notation."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1822 | |
| 1823 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1824 | string_count(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1825 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1826 | PyObject *sub_obj; |
| 1827 | const char *str = PyString_AS_STRING(self), *sub; |
| 1828 | Py_ssize_t sub_len; |
| 1829 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1830 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1831 | if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj, |
| 1832 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1833 | return NULL; |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 1834 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1835 | if (PyString_Check(sub_obj)) { |
| 1836 | sub = PyString_AS_STRING(sub_obj); |
| 1837 | sub_len = PyString_GET_SIZE(sub_obj); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1838 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1839 | else if (PyUnicode_Check(sub_obj)) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1840 | Py_ssize_t count; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1841 | count = PyUnicode_Count((PyObject *)self, sub_obj, start, end); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 1842 | if (count == -1) |
| 1843 | return NULL; |
| 1844 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1845 | return PyInt_FromSsize_t(count); |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 1846 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1847 | else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1848 | return NULL; |
| 1849 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1850 | string_adjust_indices(&start, &end, PyString_GET_SIZE(self)); |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 1851 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1852 | return PyInt_FromSsize_t( |
| 1853 | stringlib_count(str + start, end - start, sub, sub_len) |
| 1854 | ); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1857 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1858 | PyDoc_STRVAR(translate__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1859 | "B.translate(table[, deletechars]) -> bytes\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1860 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1861 | Return a copy of B, where all characters occurring in the\n\ |
| 1862 | optional argument deletechars are removed, and the remaining\n\ |
| 1863 | characters have been mapped through the given translation\n\ |
| 1864 | table, which must be a bytes object of length 256."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1865 | |
| 1866 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 1867 | string_translate(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1868 | { |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1869 | register char *input, *output; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1870 | const char *table; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1871 | register Py_ssize_t i, c, changed = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1872 | PyObject *input_obj = (PyObject*)self; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1873 | const char *output_start, *del_table=NULL; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1874 | Py_ssize_t inlen, tablen, dellen = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1875 | PyObject *result; |
| 1876 | int trans_table[256]; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1877 | PyObject *tableobj, *delobj = NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1878 | |
Raymond Hettinger | ea3fdf4 | 2002-12-29 16:33:45 +0000 | [diff] [blame] | 1879 | if (!PyArg_UnpackTuple(args, "translate", 1, 2, |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1880 | &tableobj, &delobj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1881 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1882 | |
| 1883 | if (PyString_Check(tableobj)) { |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1884 | table = PyString_AS_STRING(tableobj); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1885 | tablen = PyString_GET_SIZE(tableobj); |
| 1886 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1887 | else if (tableobj == Py_None) { |
| 1888 | table = NULL; |
| 1889 | tablen = 256; |
| 1890 | } |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1891 | else if (PyUnicode_Check(tableobj)) { |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 1892 | /* Unicode .translate() does not support the deletechars |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1893 | parameter; instead a mapping to None will cause characters |
| 1894 | to be deleted. */ |
| 1895 | if (delobj != NULL) { |
| 1896 | PyErr_SetString(PyExc_TypeError, |
| 1897 | "deletions are implemented differently for unicode"); |
| 1898 | return NULL; |
| 1899 | } |
| 1900 | return PyUnicode_Translate((PyObject *)self, tableobj, NULL); |
| 1901 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1902 | else if (PyObject_AsCharBuffer(tableobj, &table, &tablen)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1903 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1904 | |
Martin v. Löwis | 00b6127 | 2002-12-12 20:03:19 +0000 | [diff] [blame] | 1905 | if (tablen != 256) { |
| 1906 | PyErr_SetString(PyExc_ValueError, |
| 1907 | "translation table must be 256 characters long"); |
| 1908 | return NULL; |
| 1909 | } |
| 1910 | |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1911 | if (delobj != NULL) { |
| 1912 | if (PyString_Check(delobj)) { |
| 1913 | del_table = PyString_AS_STRING(delobj); |
| 1914 | dellen = PyString_GET_SIZE(delobj); |
| 1915 | } |
| 1916 | else if (PyUnicode_Check(delobj)) { |
| 1917 | PyErr_SetString(PyExc_TypeError, |
| 1918 | "deletions are implemented differently for unicode"); |
| 1919 | return NULL; |
| 1920 | } |
| 1921 | else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen)) |
| 1922 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 1923 | } |
| 1924 | else { |
| 1925 | del_table = NULL; |
| 1926 | dellen = 0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1929 | inlen = PyString_GET_SIZE(input_obj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1930 | result = PyString_FromStringAndSize((char *)NULL, inlen); |
| 1931 | if (result == NULL) |
| 1932 | return NULL; |
| 1933 | output_start = output = PyString_AsString(result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1934 | input = PyString_AS_STRING(input_obj); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1935 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1936 | if (dellen == 0 && table != NULL) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1937 | /* If no deletions are required, use faster code */ |
| 1938 | for (i = inlen; --i >= 0; ) { |
| 1939 | c = Py_CHARMASK(*input++); |
| 1940 | if (Py_CHARMASK((*output++ = table[c])) != c) |
| 1941 | changed = 1; |
| 1942 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1943 | if (changed || !PyString_CheckExact(input_obj)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1944 | return result; |
| 1945 | Py_DECREF(result); |
| 1946 | Py_INCREF(input_obj); |
| 1947 | return input_obj; |
| 1948 | } |
| 1949 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1950 | if (table == NULL) { |
| 1951 | for (i = 0; i < 256; i++) |
| 1952 | trans_table[i] = Py_CHARMASK(i); |
| 1953 | } else { |
| 1954 | for (i = 0; i < 256; i++) |
| 1955 | trans_table[i] = Py_CHARMASK(table[i]); |
| 1956 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1957 | |
| 1958 | for (i = 0; i < dellen; i++) |
| 1959 | trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
| 1960 | |
| 1961 | for (i = inlen; --i >= 0; ) { |
| 1962 | c = Py_CHARMASK(*input++); |
| 1963 | if (trans_table[c] != -1) |
| 1964 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 1965 | continue; |
| 1966 | changed = 1; |
| 1967 | } |
Tim Peters | 8fa5dd0 | 2001-09-12 02:18:30 +0000 | [diff] [blame] | 1968 | if (!changed && PyString_CheckExact(input_obj)) { |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1969 | Py_DECREF(result); |
| 1970 | Py_INCREF(input_obj); |
| 1971 | return input_obj; |
| 1972 | } |
| 1973 | /* Fix the size of the resulting string */ |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1974 | if (inlen > 0) |
| 1975 | _PyString_Resize(&result, output - output_start); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1976 | return result; |
| 1977 | } |
| 1978 | |
| 1979 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1980 | #define FORWARD 1 |
| 1981 | #define REVERSE -1 |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1982 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1983 | /* find and count characters and substrings */ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1984 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1985 | #define findchar(target, target_len, c) \ |
| 1986 | ((char *)memchr((const void *)(target), c, target_len)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1987 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1988 | /* String ops must return a string. */ |
| 1989 | /* If the object is subclass of string, create a copy */ |
| 1990 | Py_LOCAL(PyStringObject *) |
| 1991 | return_self(PyStringObject *self) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 1992 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1993 | if (PyString_CheckExact(self)) { |
| 1994 | Py_INCREF(self); |
| 1995 | return self; |
| 1996 | } |
| 1997 | return (PyStringObject *)PyString_FromStringAndSize( |
| 1998 | PyString_AS_STRING(self), |
| 1999 | PyString_GET_SIZE(self)); |
| 2000 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2001 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2002 | Py_LOCAL_INLINE(Py_ssize_t) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2003 | countchar(const char *target, int target_len, char c, Py_ssize_t maxcount) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2004 | { |
| 2005 | Py_ssize_t count=0; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2006 | const char *start=target; |
| 2007 | const char *end=target+target_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2008 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2009 | while ( (start=findchar(start, end-start, c)) != NULL ) { |
| 2010 | count++; |
| 2011 | if (count >= maxcount) |
| 2012 | break; |
| 2013 | start += 1; |
| 2014 | } |
| 2015 | return count; |
| 2016 | } |
| 2017 | |
| 2018 | Py_LOCAL(Py_ssize_t) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2019 | findstring(const char *target, Py_ssize_t target_len, |
| 2020 | const char *pattern, Py_ssize_t pattern_len, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2021 | Py_ssize_t start, |
| 2022 | Py_ssize_t end, |
| 2023 | int direction) |
| 2024 | { |
| 2025 | if (start < 0) { |
| 2026 | start += target_len; |
| 2027 | if (start < 0) |
| 2028 | start = 0; |
| 2029 | } |
| 2030 | if (end > target_len) { |
| 2031 | end = target_len; |
| 2032 | } else if (end < 0) { |
| 2033 | end += target_len; |
| 2034 | if (end < 0) |
| 2035 | end = 0; |
| 2036 | } |
| 2037 | |
| 2038 | /* zero-length substrings always match at the first attempt */ |
| 2039 | if (pattern_len == 0) |
| 2040 | return (direction > 0) ? start : end; |
| 2041 | |
| 2042 | end -= pattern_len; |
| 2043 | |
| 2044 | if (direction < 0) { |
| 2045 | for (; end >= start; end--) |
| 2046 | if (Py_STRING_MATCH(target, end, pattern, pattern_len)) |
| 2047 | return end; |
| 2048 | } else { |
| 2049 | for (; start <= end; start++) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2050 | if (Py_STRING_MATCH(target, start,pattern,pattern_len)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2051 | return start; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2052 | } |
| 2053 | return -1; |
| 2054 | } |
| 2055 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2056 | Py_LOCAL_INLINE(Py_ssize_t) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2057 | countstring(const char *target, Py_ssize_t target_len, |
| 2058 | const char *pattern, Py_ssize_t pattern_len, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2059 | Py_ssize_t start, |
| 2060 | Py_ssize_t end, |
| 2061 | int direction, Py_ssize_t maxcount) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2062 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2063 | Py_ssize_t count=0; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2064 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2065 | if (start < 0) { |
| 2066 | start += target_len; |
| 2067 | if (start < 0) |
| 2068 | start = 0; |
| 2069 | } |
| 2070 | if (end > target_len) { |
| 2071 | end = target_len; |
| 2072 | } else if (end < 0) { |
| 2073 | end += target_len; |
| 2074 | if (end < 0) |
| 2075 | end = 0; |
| 2076 | } |
| 2077 | |
| 2078 | /* zero-length substrings match everywhere */ |
| 2079 | if (pattern_len == 0 || maxcount == 0) { |
| 2080 | if (target_len+1 < maxcount) |
| 2081 | return target_len+1; |
| 2082 | return maxcount; |
| 2083 | } |
| 2084 | |
| 2085 | end -= pattern_len; |
| 2086 | if (direction < 0) { |
| 2087 | for (; (end >= start); end--) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2088 | if (Py_STRING_MATCH(target, end,pattern,pattern_len)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2089 | count++; |
| 2090 | if (--maxcount <= 0) break; |
| 2091 | end -= pattern_len-1; |
| 2092 | } |
| 2093 | } else { |
| 2094 | for (; (start <= end); start++) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2095 | if (Py_STRING_MATCH(target, start, |
| 2096 | pattern, pattern_len)) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2097 | count++; |
| 2098 | if (--maxcount <= 0) |
| 2099 | break; |
| 2100 | start += pattern_len-1; |
| 2101 | } |
| 2102 | } |
| 2103 | return count; |
| 2104 | } |
| 2105 | |
| 2106 | |
| 2107 | /* Algorithms for different cases of string replacement */ |
| 2108 | |
| 2109 | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
| 2110 | Py_LOCAL(PyStringObject *) |
| 2111 | replace_interleave(PyStringObject *self, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2112 | const char *to_s, Py_ssize_t to_len, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2113 | Py_ssize_t maxcount) |
| 2114 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2115 | char *self_s, *result_s; |
| 2116 | Py_ssize_t self_len, result_len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2117 | Py_ssize_t count, i, product; |
| 2118 | PyStringObject *result; |
| 2119 | |
| 2120 | self_len = PyString_GET_SIZE(self); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2121 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2122 | /* 1 at the end plus 1 after every character */ |
| 2123 | count = self_len+1; |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2124 | if (maxcount < count) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2125 | count = maxcount; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2126 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2127 | /* Check for overflow */ |
| 2128 | /* result_len = count * to_len + self_len; */ |
| 2129 | product = count * to_len; |
| 2130 | if (product / to_len != count) { |
| 2131 | PyErr_SetString(PyExc_OverflowError, |
| 2132 | "replace string is too long"); |
| 2133 | return NULL; |
| 2134 | } |
| 2135 | result_len = product + self_len; |
| 2136 | if (result_len < 0) { |
| 2137 | PyErr_SetString(PyExc_OverflowError, |
| 2138 | "replace string is too long"); |
| 2139 | return NULL; |
| 2140 | } |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2141 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2142 | if (! (result = (PyStringObject *) |
| 2143 | PyString_FromStringAndSize(NULL, result_len)) ) |
| 2144 | return NULL; |
| 2145 | |
| 2146 | self_s = PyString_AS_STRING(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2147 | result_s = PyString_AS_STRING(result); |
| 2148 | |
| 2149 | /* TODO: special case single character, which doesn't need memcpy */ |
| 2150 | |
| 2151 | /* Lay the first one down (guaranteed this will occur) */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2152 | Py_MEMCPY(result_s, to_s, to_len); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2153 | result_s += to_len; |
| 2154 | count -= 1; |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2155 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2156 | for (i=0; i<count; i++) { |
| 2157 | *result_s++ = *self_s++; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2158 | Py_MEMCPY(result_s, to_s, to_len); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2159 | result_s += to_len; |
| 2160 | } |
| 2161 | |
| 2162 | /* Copy the rest of the original string */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2163 | Py_MEMCPY(result_s, self_s, self_len-i); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2164 | |
| 2165 | return result; |
| 2166 | } |
| 2167 | |
| 2168 | /* Special case for deleting a single character */ |
| 2169 | /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
| 2170 | Py_LOCAL(PyStringObject *) |
| 2171 | replace_delete_single_character(PyStringObject *self, |
| 2172 | char from_c, Py_ssize_t maxcount) |
| 2173 | { |
| 2174 | char *self_s, *result_s; |
| 2175 | char *start, *next, *end; |
| 2176 | Py_ssize_t self_len, result_len; |
| 2177 | Py_ssize_t count; |
| 2178 | PyStringObject *result; |
| 2179 | |
| 2180 | self_len = PyString_GET_SIZE(self); |
| 2181 | self_s = PyString_AS_STRING(self); |
| 2182 | |
| 2183 | count = countchar(self_s, self_len, from_c, maxcount); |
| 2184 | if (count == 0) { |
| 2185 | return return_self(self); |
| 2186 | } |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2187 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2188 | result_len = self_len - count; /* from_len == 1 */ |
| 2189 | assert(result_len>=0); |
| 2190 | |
| 2191 | if ( (result = (PyStringObject *) |
| 2192 | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
| 2193 | return NULL; |
| 2194 | result_s = PyString_AS_STRING(result); |
| 2195 | |
| 2196 | start = self_s; |
| 2197 | end = self_s + self_len; |
| 2198 | while (count-- > 0) { |
| 2199 | next = findchar(start, end-start, from_c); |
| 2200 | if (next == NULL) |
| 2201 | break; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2202 | Py_MEMCPY(result_s, start, next-start); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2203 | result_s += (next-start); |
| 2204 | start = next+1; |
| 2205 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2206 | Py_MEMCPY(result_s, start, end-start); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2207 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2208 | return result; |
| 2209 | } |
| 2210 | |
| 2211 | /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
| 2212 | |
| 2213 | Py_LOCAL(PyStringObject *) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2214 | replace_delete_substring(PyStringObject *self, |
| 2215 | const char *from_s, Py_ssize_t from_len, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2216 | Py_ssize_t maxcount) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2217 | char *self_s, *result_s; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2218 | char *start, *next, *end; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2219 | Py_ssize_t self_len, result_len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2220 | Py_ssize_t count, offset; |
| 2221 | PyStringObject *result; |
| 2222 | |
| 2223 | self_len = PyString_GET_SIZE(self); |
| 2224 | self_s = PyString_AS_STRING(self); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2225 | |
| 2226 | count = countstring(self_s, self_len, |
| 2227 | from_s, from_len, |
| 2228 | 0, self_len, 1, |
| 2229 | maxcount); |
| 2230 | |
| 2231 | if (count == 0) { |
| 2232 | /* no matches */ |
| 2233 | return return_self(self); |
| 2234 | } |
| 2235 | |
| 2236 | result_len = self_len - (count * from_len); |
| 2237 | assert (result_len>=0); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2238 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2239 | if ( (result = (PyStringObject *) |
| 2240 | PyString_FromStringAndSize(NULL, result_len)) == NULL ) |
| 2241 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2242 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2243 | result_s = PyString_AS_STRING(result); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2244 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2245 | start = self_s; |
| 2246 | end = self_s + self_len; |
| 2247 | while (count-- > 0) { |
| 2248 | offset = findstring(start, end-start, |
| 2249 | from_s, from_len, |
| 2250 | 0, end-start, FORWARD); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2251 | if (offset == -1) |
| 2252 | break; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2253 | next = start + offset; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2254 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2255 | Py_MEMCPY(result_s, start, next-start); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2256 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2257 | result_s += (next-start); |
| 2258 | start = next+from_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2259 | } |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2260 | Py_MEMCPY(result_s, start, end-start); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2261 | return result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2264 | /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
| 2265 | Py_LOCAL(PyStringObject *) |
| 2266 | replace_single_character_in_place(PyStringObject *self, |
| 2267 | char from_c, char to_c, |
| 2268 | Py_ssize_t maxcount) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2269 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2270 | char *self_s, *result_s, *start, *end, *next; |
| 2271 | Py_ssize_t self_len; |
| 2272 | PyStringObject *result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2273 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2274 | /* The result string will be the same size */ |
| 2275 | self_s = PyString_AS_STRING(self); |
| 2276 | self_len = PyString_GET_SIZE(self); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2277 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2278 | next = findchar(self_s, self_len, from_c); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2279 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2280 | if (next == NULL) { |
| 2281 | /* No matches; return the original string */ |
| 2282 | return return_self(self); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2283 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2284 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2285 | /* Need to make a new string */ |
| 2286 | result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len); |
| 2287 | if (result == NULL) |
| 2288 | return NULL; |
| 2289 | result_s = PyString_AS_STRING(result); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2290 | Py_MEMCPY(result_s, self_s, self_len); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2291 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2292 | /* change everything in-place, starting with this one */ |
| 2293 | start = result_s + (next-self_s); |
| 2294 | *start = to_c; |
| 2295 | start++; |
| 2296 | end = result_s + self_len; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2297 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2298 | while (--maxcount > 0) { |
| 2299 | next = findchar(start, end-start, from_c); |
| 2300 | if (next == NULL) |
| 2301 | break; |
| 2302 | *next = to_c; |
| 2303 | start = next+1; |
Tim Peters | 4cd44ef | 2001-05-10 00:05:33 +0000 | [diff] [blame] | 2304 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2305 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2306 | return result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2307 | } |
| 2308 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2309 | /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
| 2310 | Py_LOCAL(PyStringObject *) |
| 2311 | replace_substring_in_place(PyStringObject *self, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2312 | const char *from_s, Py_ssize_t from_len, |
| 2313 | const char *to_s, Py_ssize_t to_len, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2314 | Py_ssize_t maxcount) |
| 2315 | { |
| 2316 | char *result_s, *start, *end; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2317 | char *self_s; |
| 2318 | Py_ssize_t self_len, offset; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2319 | PyStringObject *result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2320 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2321 | /* The result string will be the same size */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2322 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2323 | self_s = PyString_AS_STRING(self); |
| 2324 | self_len = PyString_GET_SIZE(self); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2325 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2326 | offset = findstring(self_s, self_len, |
| 2327 | from_s, from_len, |
| 2328 | 0, self_len, FORWARD); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2329 | if (offset == -1) { |
| 2330 | /* No matches; return the original string */ |
| 2331 | return return_self(self); |
| 2332 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2333 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2334 | /* Need to make a new string */ |
| 2335 | result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len); |
| 2336 | if (result == NULL) |
| 2337 | return NULL; |
| 2338 | result_s = PyString_AS_STRING(result); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2339 | Py_MEMCPY(result_s, self_s, self_len); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2340 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2341 | /* change everything in-place, starting with this one */ |
| 2342 | start = result_s + offset; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2343 | Py_MEMCPY(start, to_s, from_len); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2344 | start += from_len; |
| 2345 | end = result_s + self_len; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2346 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2347 | while ( --maxcount > 0) { |
| 2348 | offset = findstring(start, end-start, |
| 2349 | from_s, from_len, |
| 2350 | 0, end-start, FORWARD); |
| 2351 | if (offset==-1) |
| 2352 | break; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2353 | Py_MEMCPY(start+offset, to_s, from_len); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2354 | start += offset+from_len; |
| 2355 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2356 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2357 | return result; |
| 2358 | } |
| 2359 | |
| 2360 | /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
| 2361 | Py_LOCAL(PyStringObject *) |
| 2362 | replace_single_character(PyStringObject *self, |
| 2363 | char from_c, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2364 | const char *to_s, Py_ssize_t to_len, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2365 | Py_ssize_t maxcount) |
| 2366 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2367 | char *self_s, *result_s; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2368 | char *start, *next, *end; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2369 | Py_ssize_t self_len, result_len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2370 | Py_ssize_t count, product; |
| 2371 | PyStringObject *result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2372 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2373 | self_s = PyString_AS_STRING(self); |
| 2374 | self_len = PyString_GET_SIZE(self); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2375 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2376 | count = countchar(self_s, self_len, from_c, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2377 | if (count == 0) { |
| 2378 | /* no matches, return unchanged */ |
| 2379 | return return_self(self); |
| 2380 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2381 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2382 | /* use the difference between current and new, hence the "-1" */ |
| 2383 | /* result_len = self_len + count * (to_len-1) */ |
| 2384 | product = count * (to_len-1); |
| 2385 | if (product / (to_len-1) != count) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2386 | PyErr_SetString(PyExc_OverflowError, |
| 2387 | "replace string is too long"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2388 | return NULL; |
| 2389 | } |
| 2390 | result_len = self_len + product; |
| 2391 | if (result_len < 0) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2392 | PyErr_SetString(PyExc_OverflowError, |
| 2393 | "replace string is too long"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2394 | return NULL; |
| 2395 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2396 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2397 | if ( (result = (PyStringObject *) |
| 2398 | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
| 2399 | return NULL; |
| 2400 | result_s = PyString_AS_STRING(result); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2401 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2402 | start = self_s; |
| 2403 | end = self_s + self_len; |
| 2404 | while (count-- > 0) { |
| 2405 | next = findchar(start, end-start, from_c); |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2406 | if (next == NULL) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2407 | break; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2408 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2409 | if (next == start) { |
| 2410 | /* replace with the 'to' */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2411 | Py_MEMCPY(result_s, to_s, to_len); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2412 | result_s += to_len; |
| 2413 | start += 1; |
| 2414 | } else { |
| 2415 | /* copy the unchanged old then the 'to' */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2416 | Py_MEMCPY(result_s, start, next-start); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2417 | result_s += (next-start); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2418 | Py_MEMCPY(result_s, to_s, to_len); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2419 | result_s += to_len; |
| 2420 | start = next+1; |
| 2421 | } |
| 2422 | } |
| 2423 | /* Copy the remainder of the remaining string */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2424 | Py_MEMCPY(result_s, start, end-start); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2425 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2426 | return result; |
| 2427 | } |
| 2428 | |
| 2429 | /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
| 2430 | Py_LOCAL(PyStringObject *) |
| 2431 | replace_substring(PyStringObject *self, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2432 | const char *from_s, Py_ssize_t from_len, |
| 2433 | const char *to_s, Py_ssize_t to_len, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2434 | Py_ssize_t maxcount) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2435 | char *self_s, *result_s; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2436 | char *start, *next, *end; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2437 | Py_ssize_t self_len, result_len; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2438 | Py_ssize_t count, offset, product; |
| 2439 | PyStringObject *result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2440 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2441 | self_s = PyString_AS_STRING(self); |
| 2442 | self_len = PyString_GET_SIZE(self); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2443 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2444 | count = countstring(self_s, self_len, |
| 2445 | from_s, from_len, |
| 2446 | 0, self_len, FORWARD, maxcount); |
| 2447 | if (count == 0) { |
| 2448 | /* no matches, return unchanged */ |
| 2449 | return return_self(self); |
| 2450 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2451 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2452 | /* Check for overflow */ |
| 2453 | /* result_len = self_len + count * (to_len-from_len) */ |
| 2454 | product = count * (to_len-from_len); |
| 2455 | if (product / (to_len-from_len) != count) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2456 | PyErr_SetString(PyExc_OverflowError, |
| 2457 | "replace string is too long"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2458 | return NULL; |
| 2459 | } |
| 2460 | result_len = self_len + product; |
| 2461 | if (result_len < 0) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2462 | PyErr_SetString(PyExc_OverflowError, |
| 2463 | "replace string is too long"); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2464 | return NULL; |
| 2465 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2466 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2467 | if ( (result = (PyStringObject *) |
| 2468 | PyString_FromStringAndSize(NULL, result_len)) == NULL) |
| 2469 | return NULL; |
| 2470 | result_s = PyString_AS_STRING(result); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2471 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2472 | start = self_s; |
| 2473 | end = self_s + self_len; |
| 2474 | while (count-- > 0) { |
| 2475 | offset = findstring(start, end-start, |
| 2476 | from_s, from_len, |
| 2477 | 0, end-start, FORWARD); |
| 2478 | if (offset == -1) |
| 2479 | break; |
| 2480 | next = start+offset; |
| 2481 | if (next == start) { |
| 2482 | /* replace with the 'to' */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2483 | Py_MEMCPY(result_s, to_s, to_len); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2484 | result_s += to_len; |
| 2485 | start += from_len; |
| 2486 | } else { |
| 2487 | /* copy the unchanged old then the 'to' */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2488 | Py_MEMCPY(result_s, start, next-start); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2489 | result_s += (next-start); |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2490 | Py_MEMCPY(result_s, to_s, to_len); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2491 | result_s += to_len; |
| 2492 | start = next+from_len; |
| 2493 | } |
| 2494 | } |
| 2495 | /* Copy the remainder of the remaining string */ |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2496 | Py_MEMCPY(result_s, start, end-start); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2497 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2498 | return result; |
| 2499 | } |
| 2500 | |
| 2501 | |
| 2502 | Py_LOCAL(PyStringObject *) |
| 2503 | replace(PyStringObject *self, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2504 | const char *from_s, Py_ssize_t from_len, |
| 2505 | const char *to_s, Py_ssize_t to_len, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2506 | Py_ssize_t maxcount) |
| 2507 | { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2508 | if (maxcount < 0) { |
| 2509 | maxcount = PY_SSIZE_T_MAX; |
| 2510 | } else if (maxcount == 0 || PyString_GET_SIZE(self) == 0) { |
| 2511 | /* nothing to do; return the original string */ |
| 2512 | return return_self(self); |
| 2513 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2514 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2515 | if (maxcount == 0 || |
| 2516 | (from_len == 0 && to_len == 0)) { |
| 2517 | /* nothing to do; return the original string */ |
| 2518 | return return_self(self); |
| 2519 | } |
| 2520 | |
| 2521 | /* Handle zero-length special cases */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2522 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2523 | if (from_len == 0) { |
| 2524 | /* insert the 'to' string everywhere. */ |
| 2525 | /* >>> "Python".replace("", ".") */ |
| 2526 | /* '.P.y.t.h.o.n.' */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2527 | return replace_interleave(self, to_s, to_len, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
| 2531 | /* point for an empty self string to generate a non-empty string */ |
| 2532 | /* Special case so the remaining code always gets a non-empty string */ |
| 2533 | if (PyString_GET_SIZE(self) == 0) { |
| 2534 | return return_self(self); |
| 2535 | } |
| 2536 | |
| 2537 | if (to_len == 0) { |
| 2538 | /* delete all occurances of 'from' string */ |
| 2539 | if (from_len == 1) { |
| 2540 | return replace_delete_single_character( |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2541 | self, from_s[0], maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2542 | } else { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2543 | return replace_delete_substring(self, from_s, |
| 2544 | from_len, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2545 | } |
| 2546 | } |
| 2547 | |
| 2548 | /* Handle special case where both strings have the same length */ |
| 2549 | |
| 2550 | if (from_len == to_len) { |
| 2551 | if (from_len == 1) { |
| 2552 | return replace_single_character_in_place( |
| 2553 | self, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2554 | from_s[0], |
| 2555 | to_s[0], |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2556 | maxcount); |
| 2557 | } else { |
| 2558 | return replace_substring_in_place( |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2559 | self, from_s, from_len, to_s, to_len, |
| 2560 | maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2561 | } |
| 2562 | } |
| 2563 | |
| 2564 | /* Otherwise use the more generic algorithms */ |
| 2565 | if (from_len == 1) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2566 | return replace_single_character(self, from_s[0], |
| 2567 | to_s, to_len, maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2568 | } else { |
| 2569 | /* len('from')>=2, len('to')>=1 */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2570 | return replace_substring(self, from_s, from_len, to_s, to_len, |
| 2571 | maxcount); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2572 | } |
| 2573 | } |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2574 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2575 | PyDoc_STRVAR(replace__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2576 | "B.replace(old, new[, count]) -> bytes\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2577 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2578 | Return a copy of B with all occurrences of subsection\n\ |
Fred Drake | d22bb65 | 2003-10-22 02:56:40 +0000 | [diff] [blame] | 2579 | old replaced by new. If the optional argument count is\n\ |
| 2580 | given, only the first count occurrences are replaced."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2581 | |
| 2582 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2583 | string_replace(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2584 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2585 | Py_ssize_t count = -1; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2586 | PyObject *from, *to; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2587 | const char *from_s, *to_s; |
| 2588 | Py_ssize_t from_len, to_len; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2589 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2590 | if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count)) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2591 | return NULL; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2592 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2593 | if (PyString_Check(from)) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2594 | from_s = PyString_AS_STRING(from); |
| 2595 | from_len = PyString_GET_SIZE(from); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2596 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2597 | if (PyUnicode_Check(from)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2598 | return PyUnicode_Replace((PyObject *)self, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2599 | from, to, count); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2600 | else if (PyObject_AsCharBuffer(from, &from_s, &from_len)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2601 | return NULL; |
| 2602 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2603 | if (PyString_Check(to)) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2604 | to_s = PyString_AS_STRING(to); |
| 2605 | to_len = PyString_GET_SIZE(to); |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2606 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2607 | else if (PyUnicode_Check(to)) |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2608 | return PyUnicode_Replace((PyObject *)self, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2609 | from, to, count); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2610 | else if (PyObject_AsCharBuffer(to, &to_s, &to_len)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2611 | return NULL; |
| 2612 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2613 | return (PyObject *)replace((PyStringObject *) self, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2614 | from_s, from_len, |
| 2615 | to_s, to_len, count); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2618 | /** End DALKE **/ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2619 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2620 | /* Matches the end (direction >= 0) or start (direction < 0) of self |
| 2621 | * against substr, using the start and end arguments. Returns |
| 2622 | * -1 on error, 0 if not found and 1 if found. |
| 2623 | */ |
| 2624 | Py_LOCAL(int) |
| 2625 | _string_tailmatch(PyStringObject *self, PyObject *substr, Py_ssize_t start, |
| 2626 | Py_ssize_t end, int direction) |
| 2627 | { |
| 2628 | Py_ssize_t len = PyString_GET_SIZE(self); |
| 2629 | Py_ssize_t slen; |
| 2630 | const char* sub; |
| 2631 | const char* str; |
| 2632 | |
| 2633 | if (PyString_Check(substr)) { |
| 2634 | sub = PyString_AS_STRING(substr); |
| 2635 | slen = PyString_GET_SIZE(substr); |
| 2636 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2637 | else if (PyUnicode_Check(substr)) |
| 2638 | return PyUnicode_Tailmatch((PyObject *)self, |
| 2639 | substr, start, end, direction); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2640 | else if (PyObject_AsCharBuffer(substr, &sub, &slen)) |
| 2641 | return -1; |
| 2642 | str = PyString_AS_STRING(self); |
| 2643 | |
| 2644 | string_adjust_indices(&start, &end, len); |
| 2645 | |
| 2646 | if (direction < 0) { |
| 2647 | /* startswith */ |
| 2648 | if (start+slen > len) |
| 2649 | return 0; |
| 2650 | } else { |
| 2651 | /* endswith */ |
| 2652 | if (end-start < slen || start > len) |
| 2653 | return 0; |
| 2654 | |
| 2655 | if (end-slen > start) |
| 2656 | start = end - slen; |
| 2657 | } |
| 2658 | if (end-start >= slen) |
| 2659 | return ! memcmp(str+start, sub, slen); |
| 2660 | return 0; |
| 2661 | } |
| 2662 | |
| 2663 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2664 | PyDoc_STRVAR(startswith__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2665 | "B.startswith(prefix [,start [,end]]) -> bool\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2666 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2667 | Return True if B starts with the specified prefix, False otherwise.\n\ |
| 2668 | With optional start, test B beginning at that position.\n\ |
| 2669 | With optional end, stop comparing B at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2670 | prefix can also be a tuple of strings to try."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2671 | |
| 2672 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2673 | string_startswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2674 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2675 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2676 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2677 | PyObject *subobj; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2678 | int result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2679 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 2680 | if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 2681 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2682 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2683 | if (PyTuple_Check(subobj)) { |
| 2684 | Py_ssize_t i; |
| 2685 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 2686 | result = _string_tailmatch(self, |
| 2687 | PyTuple_GET_ITEM(subobj, i), |
| 2688 | start, end, -1); |
| 2689 | if (result == -1) |
| 2690 | return NULL; |
| 2691 | else if (result) { |
| 2692 | Py_RETURN_TRUE; |
| 2693 | } |
| 2694 | } |
| 2695 | Py_RETURN_FALSE; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2696 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2697 | result = _string_tailmatch(self, subobj, start, end, -1); |
| 2698 | if (result == -1) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2699 | return NULL; |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 2700 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2701 | return PyBool_FromLong(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
| 2704 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2705 | PyDoc_STRVAR(endswith__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2706 | "B.endswith(suffix [,start [,end]]) -> bool\n\ |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2707 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2708 | Return True if B ends with the specified suffix, False otherwise.\n\ |
| 2709 | With optional start, test B beginning at that position.\n\ |
| 2710 | With optional end, stop comparing B at that position.\n\ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2711 | suffix can also be a tuple of strings to try."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2712 | |
| 2713 | static PyObject * |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 2714 | string_endswith(PyStringObject *self, PyObject *args) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2715 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2716 | Py_ssize_t start = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2717 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2718 | PyObject *subobj; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2719 | int result; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2720 | |
Guido van Rossum | c682140 | 2000-05-08 14:08:05 +0000 | [diff] [blame] | 2721 | if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 2722 | _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2723 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2724 | if (PyTuple_Check(subobj)) { |
| 2725 | Py_ssize_t i; |
| 2726 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 2727 | result = _string_tailmatch(self, |
| 2728 | PyTuple_GET_ITEM(subobj, i), |
| 2729 | start, end, +1); |
| 2730 | if (result == -1) |
| 2731 | return NULL; |
| 2732 | else if (result) { |
| 2733 | Py_RETURN_TRUE; |
| 2734 | } |
| 2735 | } |
| 2736 | Py_RETURN_FALSE; |
Guido van Rossum | 4c08d55 | 2000-03-10 22:55:18 +0000 | [diff] [blame] | 2737 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2738 | result = _string_tailmatch(self, subobj, start, end, +1); |
| 2739 | if (result == -1) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2740 | return NULL; |
Neal Norwitz | 1f68fc7 | 2002-06-14 00:50:42 +0000 | [diff] [blame] | 2741 | else |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2742 | return PyBool_FromLong(result); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2743 | } |
| 2744 | |
| 2745 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2746 | PyDoc_STRVAR(decode__doc__, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2747 | "B.decode([encoding[, errors]]) -> object\n\ |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2748 | \n\ |
| 2749 | Decodes S using the codec registered for encoding. encoding defaults\n\ |
| 2750 | to the default encoding. errors may be given to set a different error\n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2751 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 2752 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
Walter Dörwald | 3aeb632 | 2002-09-02 13:14:32 +0000 | [diff] [blame] | 2753 | as well as any other name registerd with codecs.register_error that is\n\ |
| 2754 | able to handle UnicodeDecodeErrors."); |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2755 | |
| 2756 | static PyObject * |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2757 | string_decode(PyObject *self, PyObject *args) |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 2758 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2759 | const char *encoding = NULL; |
| 2760 | const char *errors = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2761 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2762 | if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
| 2763 | return NULL; |
| 2764 | if (encoding == NULL) |
| 2765 | encoding = PyUnicode_GetDefaultEncoding(); |
| 2766 | return PyCodec_Decode(self, encoding, errors); |
Marc-André Lemburg | 63f3d17 | 2000-07-06 11:29:01 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2770 | PyDoc_STRVAR(fromhex_doc, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2771 | "bytes.fromhex(string) -> bytes\n\ |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2772 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2773 | Create a bytes object from a string of hexadecimal numbers.\n\ |
| 2774 | Spaces between two numbers are accepted.\n\ |
| 2775 | Example: bytes.fromhex('B9 01EF') -> b'\\xb9\\x01\\xef'."); |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2776 | |
| 2777 | static int |
| 2778 | hex_digit_to_int(Py_UNICODE c) |
| 2779 | { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2780 | if (c >= 128) |
| 2781 | return -1; |
| 2782 | if (ISDIGIT(c)) |
| 2783 | return c - '0'; |
| 2784 | else { |
| 2785 | if (ISUPPER(c)) |
| 2786 | c = TOLOWER(c); |
| 2787 | if (c >= 'a' && c <= 'f') |
| 2788 | return c - 'a' + 10; |
| 2789 | } |
| 2790 | return -1; |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | static PyObject * |
| 2794 | string_fromhex(PyObject *cls, PyObject *args) |
| 2795 | { |
| 2796 | PyObject *newstring, *hexobj; |
| 2797 | char *buf; |
| 2798 | Py_UNICODE *hex; |
| 2799 | Py_ssize_t hexlen, byteslen, i, j; |
| 2800 | int top, bot; |
| 2801 | |
| 2802 | if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) |
| 2803 | return NULL; |
| 2804 | assert(PyUnicode_Check(hexobj)); |
| 2805 | hexlen = PyUnicode_GET_SIZE(hexobj); |
| 2806 | hex = PyUnicode_AS_UNICODE(hexobj); |
| 2807 | byteslen = hexlen/2; /* This overestimates if there are spaces */ |
| 2808 | newstring = PyString_FromStringAndSize(NULL, byteslen); |
| 2809 | if (!newstring) |
| 2810 | return NULL; |
| 2811 | buf = PyString_AS_STRING(newstring); |
| 2812 | for (i = j = 0; i < hexlen; i += 2) { |
| 2813 | /* skip over spaces in the input */ |
| 2814 | while (hex[i] == ' ') |
| 2815 | i++; |
| 2816 | if (i >= hexlen) |
| 2817 | break; |
| 2818 | top = hex_digit_to_int(hex[i]); |
| 2819 | bot = hex_digit_to_int(hex[i+1]); |
| 2820 | if (top == -1 || bot == -1) { |
| 2821 | PyErr_Format(PyExc_ValueError, |
| 2822 | "non-hexadecimal number found in " |
| 2823 | "fromhex() arg at position %zd", i); |
| 2824 | goto error; |
| 2825 | } |
| 2826 | buf[j++] = (top << 4) + bot; |
| 2827 | } |
| 2828 | if (_PyString_Resize(&newstring, j) < 0) |
| 2829 | goto error; |
| 2830 | return newstring; |
| 2831 | |
| 2832 | error: |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2833 | Py_XDECREF(newstring); |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2834 | return NULL; |
| 2835 | } |
| 2836 | |
| 2837 | |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 2838 | static PyObject * |
| 2839 | string_getnewargs(PyStringObject *v) |
| 2840 | { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2841 | return Py_BuildValue("(s#)", v->ob_sval, Py_Size(v)); |
Guido van Rossum | 5d9113d | 2003-01-29 17:58:45 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2844 | |
Tim Peters | b3d8d1f | 2001-04-28 05:38:26 +0000 | [diff] [blame] | 2845 | static PyMethodDef |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2846 | string_methods[] = { |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2847 | {"__getnewargs__", (PyCFunction)string_getnewargs, METH_NOARGS}, |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 2848 | {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
| 2849 | _Py_capitalize__doc__}, |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2850 | {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 2851 | {"count", (PyCFunction)string_count, METH_VARARGS, count__doc__}, |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2852 | {"decode", (PyCFunction)string_decode, METH_VARARGS, decode__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 2853 | {"endswith", (PyCFunction)string_endswith, METH_VARARGS, |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2854 | endswith__doc__}, |
| 2855 | {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS, |
| 2856 | expandtabs__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 2857 | {"find", (PyCFunction)string_find, METH_VARARGS, find__doc__}, |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2858 | {"fromhex", (PyCFunction)string_fromhex, METH_VARARGS|METH_CLASS, |
| 2859 | fromhex_doc}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 2860 | {"index", (PyCFunction)string_index, METH_VARARGS, index__doc__}, |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2861 | {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
| 2862 | _Py_isalnum__doc__}, |
| 2863 | {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
| 2864 | _Py_isalpha__doc__}, |
| 2865 | {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
| 2866 | _Py_isdigit__doc__}, |
| 2867 | {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
| 2868 | _Py_islower__doc__}, |
| 2869 | {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
| 2870 | _Py_isspace__doc__}, |
| 2871 | {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
| 2872 | _Py_istitle__doc__}, |
| 2873 | {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
| 2874 | _Py_isupper__doc__}, |
| 2875 | {"join", (PyCFunction)string_join, METH_O, join__doc__}, |
| 2876 | {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, |
| 2877 | {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 2878 | {"lstrip", (PyCFunction)string_lstrip, METH_VARARGS, lstrip__doc__}, |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2879 | {"partition", (PyCFunction)string_partition, METH_O, partition__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 2880 | {"replace", (PyCFunction)string_replace, METH_VARARGS, replace__doc__}, |
| 2881 | {"rfind", (PyCFunction)string_rfind, METH_VARARGS, rfind__doc__}, |
| 2882 | {"rindex", (PyCFunction)string_rindex, METH_VARARGS, rindex__doc__}, |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2883 | {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2884 | {"rpartition", (PyCFunction)string_rpartition, METH_O, |
| 2885 | rpartition__doc__}, |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2886 | {"rsplit", (PyCFunction)string_rsplit, METH_VARARGS, rsplit__doc__}, |
| 2887 | {"rstrip", (PyCFunction)string_rstrip, METH_VARARGS, rstrip__doc__}, |
| 2888 | {"split", (PyCFunction)string_split, METH_VARARGS, split__doc__}, |
| 2889 | {"splitlines", (PyCFunction)stringlib_splitlines, METH_VARARGS, |
| 2890 | splitlines__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 2891 | {"startswith", (PyCFunction)string_startswith, METH_VARARGS, |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2892 | startswith__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 2893 | {"strip", (PyCFunction)string_strip, METH_VARARGS, strip__doc__}, |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 2894 | {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
| 2895 | _Py_swapcase__doc__}, |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2896 | {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
Guido van Rossum | 3aa3fc4 | 2002-04-15 13:48:52 +0000 | [diff] [blame] | 2897 | {"translate", (PyCFunction)string_translate, METH_VARARGS, |
| 2898 | translate__doc__}, |
Guido van Rossum | ae404e2 | 2007-10-26 21:46:44 +0000 | [diff] [blame] | 2899 | {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 2900 | {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2901 | {NULL, NULL} /* sentinel */ |
| 2902 | }; |
| 2903 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 2904 | static PyObject * |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 2905 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 2906 | |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2907 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2908 | string_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 2909 | { |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 2910 | PyObject *x = NULL, *it; |
| 2911 | PyObject *(*iternext)(PyObject *); |
| 2912 | const char *encoding = NULL; |
| 2913 | const char *errors = NULL; |
| 2914 | PyObject *new = NULL; |
| 2915 | Py_ssize_t i, size; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2916 | static char *kwlist[] = {"source", "encoding", "errors", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2917 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 2918 | if (type != &PyString_Type) |
| 2919 | return str_subtype_new(type, args, kwds); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2920 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytes", kwlist, &x, |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 2921 | &encoding, &errors)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2922 | return NULL; |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 2923 | if (x == NULL) { |
| 2924 | if (encoding != NULL || errors != NULL) { |
| 2925 | PyErr_SetString(PyExc_TypeError, |
| 2926 | "encoding or errors without sequence " |
| 2927 | "argument"); |
| 2928 | return NULL; |
| 2929 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2930 | return PyString_FromString(""); |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 2931 | } |
| 2932 | |
| 2933 | if (PyUnicode_Check(x)) { |
| 2934 | /* Encode via the codec registry */ |
| 2935 | if (encoding == NULL) { |
| 2936 | PyErr_SetString(PyExc_TypeError, |
| 2937 | "string argument without an encoding"); |
| 2938 | return NULL; |
| 2939 | } |
| 2940 | new = PyCodec_Encode(x, encoding, errors); |
| 2941 | if (new == NULL) |
| 2942 | return NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2943 | assert(PyString_Check(new)); |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 2944 | return new; |
| 2945 | } |
| 2946 | |
| 2947 | /* If it's not unicode, there can't be encoding or errors */ |
| 2948 | if (encoding != NULL || errors != NULL) { |
| 2949 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2950 | "encoding or errors without a string argument"); |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 2951 | return NULL; |
| 2952 | } |
| 2953 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2954 | /* Is it an int? */ |
| 2955 | size = PyNumber_AsSsize_t(x, PyExc_ValueError); |
| 2956 | if (size == -1 && PyErr_Occurred()) { |
| 2957 | PyErr_Clear(); |
| 2958 | } |
| 2959 | else { |
| 2960 | if (size < 0) { |
| 2961 | PyErr_SetString(PyExc_ValueError, "negative count"); |
| 2962 | return NULL; |
| 2963 | } |
| 2964 | new = PyString_FromStringAndSize(NULL, size); |
| 2965 | if (new == NULL) { |
| 2966 | return NULL; |
| 2967 | } |
| 2968 | if (size > 0) { |
| 2969 | memset(((PyStringObject*)new)->ob_sval, 0, size); |
| 2970 | } |
| 2971 | return new; |
| 2972 | } |
| 2973 | |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 2974 | /* Use the modern buffer interface */ |
| 2975 | if (PyObject_CheckBuffer(x)) { |
| 2976 | Py_buffer view; |
| 2977 | if (PyObject_GetBuffer(x, &view, PyBUF_FULL_RO) < 0) |
| 2978 | return NULL; |
| 2979 | new = PyString_FromStringAndSize(NULL, view.len); |
| 2980 | if (!new) |
| 2981 | goto fail; |
| 2982 | // XXX(brett.cannon): Better way to get to internal buffer? |
| 2983 | if (PyBuffer_ToContiguous(((PyStringObject *)new)->ob_sval, |
| 2984 | &view, view.len, 'C') < 0) |
| 2985 | goto fail; |
| 2986 | PyObject_ReleaseBuffer(x, &view); |
| 2987 | return new; |
| 2988 | fail: |
| 2989 | Py_XDECREF(new); |
| 2990 | PyObject_ReleaseBuffer(x, &view); |
| 2991 | return NULL; |
| 2992 | } |
| 2993 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2994 | /* For iterator version, create a string object and resize as needed */ |
| 2995 | /* XXX(gb): is 64 a good value? also, optimize if length is known */ |
| 2996 | /* XXX(guido): perhaps use Pysequence_Fast() -- I can't imagine the |
| 2997 | input being a truly long iterator. */ |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 2998 | size = 64; |
| 2999 | new = PyString_FromStringAndSize(NULL, size); |
| 3000 | if (new == NULL) |
| 3001 | return NULL; |
| 3002 | |
| 3003 | /* XXX Optimize this if the arguments is a list, tuple */ |
| 3004 | |
| 3005 | /* Get the iterator */ |
| 3006 | it = PyObject_GetIter(x); |
| 3007 | if (it == NULL) |
| 3008 | goto error; |
| 3009 | // XXX(brett.cannon): No API for this? |
| 3010 | iternext = *Py_Type(it)->tp_iternext; |
| 3011 | |
| 3012 | /* Run the iterator to exhaustion */ |
| 3013 | for (i = 0; ; i++) { |
| 3014 | PyObject *item; |
| 3015 | Py_ssize_t value; |
| 3016 | |
| 3017 | /* Get the next item */ |
| 3018 | item = iternext(it); |
| 3019 | if (item == NULL) { |
| 3020 | if (PyErr_Occurred()) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3021 | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
| 3022 | goto error; |
| 3023 | PyErr_Clear(); |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 3024 | } |
| 3025 | break; |
| 3026 | } |
| 3027 | |
| 3028 | /* Interpret it as an int (__index__) */ |
| 3029 | value = PyNumber_AsSsize_t(item, PyExc_ValueError); |
| 3030 | Py_DECREF(item); |
| 3031 | if (value == -1 && PyErr_Occurred()) |
| 3032 | goto error; |
| 3033 | |
| 3034 | /* Range check */ |
| 3035 | if (value < 0 || value >= 256) { |
| 3036 | PyErr_SetString(PyExc_ValueError, |
| 3037 | "bytes must be in range(0, 256)"); |
| 3038 | goto error; |
| 3039 | } |
| 3040 | |
| 3041 | /* Append the byte */ |
| 3042 | if (i >= size) { |
| 3043 | size *= 2; |
| 3044 | if (_PyString_Resize(&new, size) < 0) |
| 3045 | goto error; |
| 3046 | } |
| 3047 | ((PyStringObject *)new)->ob_sval[i] = value; |
| 3048 | } |
| 3049 | _PyString_Resize(&new, i); |
| 3050 | |
| 3051 | /* Clean up and return success */ |
| 3052 | Py_DECREF(it); |
| 3053 | return new; |
| 3054 | |
| 3055 | error: |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3056 | /* Error handling when new != NULL */ |
Georg Brandl | bd1c68c | 2007-10-24 18:55:37 +0000 | [diff] [blame] | 3057 | Py_XDECREF(it); |
| 3058 | Py_DECREF(new); |
| 3059 | return NULL; |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3060 | } |
| 3061 | |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3062 | static PyObject * |
| 3063 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3064 | { |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3065 | PyObject *tmp, *pnew; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3066 | Py_ssize_t n; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3067 | |
| 3068 | assert(PyType_IsSubtype(type, &PyString_Type)); |
| 3069 | tmp = string_new(&PyString_Type, args, kwds); |
| 3070 | if (tmp == NULL) |
| 3071 | return NULL; |
Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 3072 | assert(PyString_CheckExact(tmp)); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3073 | n = PyString_GET_SIZE(tmp); |
| 3074 | pnew = type->tp_alloc(type, n); |
| 3075 | if (pnew != NULL) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3076 | Py_MEMCPY(PyString_AS_STRING(pnew), |
| 3077 | PyString_AS_STRING(tmp), n+1); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3078 | ((PyStringObject *)pnew)->ob_shash = |
| 3079 | ((PyStringObject *)tmp)->ob_shash; |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3080 | } |
Guido van Rossum | 29d55a3 | 2001-08-31 16:11:15 +0000 | [diff] [blame] | 3081 | Py_DECREF(tmp); |
Tim Peters | af90b3e | 2001-09-12 05:18:58 +0000 | [diff] [blame] | 3082 | return pnew; |
Guido van Rossum | ae960af | 2001-08-30 03:11:59 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 3085 | PyDoc_STRVAR(string_doc, |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3086 | "bytes(iterable_of_ints) -> bytes.\n\ |
| 3087 | bytes(string, encoding[, errors]) -> bytes\n\ |
| 3088 | bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer.\n\ |
| 3089 | bytes(memory_view) -> bytes.\n\ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3090 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3091 | Construct an immutable array of bytes from:\n\ |
| 3092 | - an iterable yielding integers in range(256)\n\ |
| 3093 | - a text string encoded using the specified encoding\n\ |
| 3094 | - a bytes or a buffer object\n\ |
| 3095 | - any object implementing the buffer API."); |
Barry Warsaw | 226ae6c | 1999-10-12 19:54:53 +0000 | [diff] [blame] | 3096 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3097 | static PyObject *str_iter(PyObject *seq); |
| 3098 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3099 | PyTypeObject PyString_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3100 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3101 | "bytes", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3102 | sizeof(PyStringObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3103 | sizeof(char), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3104 | string_dealloc, /* tp_dealloc */ |
Guido van Rossum | 04dbf3b | 2007-08-07 19:51:00 +0000 | [diff] [blame] | 3105 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3106 | 0, /* tp_getattr */ |
| 3107 | 0, /* tp_setattr */ |
| 3108 | 0, /* tp_compare */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3109 | (reprfunc)string_repr, /* tp_repr */ |
| 3110 | 0, /* tp_as_number */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3111 | &string_as_sequence, /* tp_as_sequence */ |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 3112 | &string_as_mapping, /* tp_as_mapping */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3113 | (hashfunc)string_hash, /* tp_hash */ |
| 3114 | 0, /* tp_call */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3115 | string_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3116 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3117 | 0, /* tp_setattro */ |
| 3118 | &string_as_buffer, /* tp_as_buffer */ |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 3119 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 3120 | Py_TPFLAGS_STRING_SUBCLASS, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3121 | string_doc, /* tp_doc */ |
| 3122 | 0, /* tp_traverse */ |
| 3123 | 0, /* tp_clear */ |
| 3124 | (richcmpfunc)string_richcompare, /* tp_richcompare */ |
| 3125 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3126 | str_iter, /* tp_iter */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3127 | 0, /* tp_iternext */ |
| 3128 | string_methods, /* tp_methods */ |
| 3129 | 0, /* tp_members */ |
| 3130 | 0, /* tp_getset */ |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 3131 | &PyBaseObject_Type, /* tp_base */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3132 | 0, /* tp_dict */ |
| 3133 | 0, /* tp_descr_get */ |
| 3134 | 0, /* tp_descr_set */ |
| 3135 | 0, /* tp_dictoffset */ |
| 3136 | 0, /* tp_init */ |
| 3137 | 0, /* tp_alloc */ |
| 3138 | string_new, /* tp_new */ |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 3139 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3140 | }; |
| 3141 | |
| 3142 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3143 | PyString_Concat(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3144 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3145 | register PyObject *v; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3146 | assert(pv != NULL); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3147 | if (*pv == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3148 | return; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3149 | if (w == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3150 | Py_DECREF(*pv); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3151 | *pv = NULL; |
| 3152 | return; |
| 3153 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3154 | v = string_concat(*pv, w); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3155 | Py_DECREF(*pv); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3156 | *pv = v; |
| 3157 | } |
| 3158 | |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3159 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3160 | PyString_ConcatAndDel(register PyObject **pv, register PyObject *w) |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3161 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3162 | PyString_Concat(pv, w); |
| 3163 | Py_XDECREF(w); |
Guido van Rossum | 013142a | 1994-08-30 08:19:36 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3167 | /* The following function breaks the notion that strings are immutable: |
| 3168 | it changes the size of a string. We get away with this only if there |
| 3169 | is only one module referencing the object. You can also think of it |
| 3170 | as creating a new string object and destroying the old one, only |
| 3171 | 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] | 3172 | already be known to some other part of the code... |
| 3173 | Note that if there's not enough memory to resize the string, the original |
| 3174 | string object at *pv is deallocated, *pv is set to NULL, an "out of |
| 3175 | memory" exception is set, and -1 is returned. Else (on success) 0 is |
| 3176 | returned, and the value in *pv may or may not be the same as on input. |
| 3177 | As always, an extra byte is allocated for a trailing \0 byte (newsize |
| 3178 | does *not* include that), and a trailing \0 byte is stored. |
| 3179 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3180 | |
| 3181 | int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3182 | _PyString_Resize(PyObject **pv, Py_ssize_t newsize) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3183 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3184 | register PyObject *v; |
| 3185 | register PyStringObject *sv; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 3186 | v = *pv; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3187 | if (!PyString_Check(v) || Py_Refcnt(v) != 1 || newsize < 0) { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3188 | *pv = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3189 | Py_DECREF(v); |
| 3190 | PyErr_BadInternalCall(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 3191 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3192 | } |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 3193 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 3194 | _Py_DEC_REFTOTAL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3195 | _Py_ForgetReference(v); |
| 3196 | *pv = (PyObject *) |
Tim Peters | e7c0532 | 2004-06-27 17:24:49 +0000 | [diff] [blame] | 3197 | PyObject_REALLOC((char *)v, sizeof(PyStringObject) + newsize); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3198 | if (*pv == NULL) { |
Neil Schemenauer | 510492e | 2002-04-12 03:05:19 +0000 | [diff] [blame] | 3199 | PyObject_Del(v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3200 | PyErr_NoMemory(); |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 3201 | return -1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3202 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 3203 | _Py_NewReference(*pv); |
| 3204 | sv = (PyStringObject *) *pv; |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3205 | Py_Size(sv) = newsize; |
Guido van Rossum | 921842f | 1990-11-18 17:30:23 +0000 | [diff] [blame] | 3206 | sv->ob_sval[newsize] = '\0'; |
Raymond Hettinger | 561fbf1 | 2004-10-26 01:52:37 +0000 | [diff] [blame] | 3207 | sv->ob_shash = -1; /* invalidate cached hash value */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3208 | return 0; |
| 3209 | } |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 3210 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3211 | /* _PyString_FormatLong emulates the format codes d, u, o, x and X, and |
| 3212 | * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
| 3213 | * Python's regular ints. |
| 3214 | * Return value: a new PyString*, or NULL if error. |
| 3215 | * . *pbuf is set to point into it, |
| 3216 | * *plen set to the # of chars following that. |
| 3217 | * Caller must decref it when done using pbuf. |
| 3218 | * The string starting at *pbuf is of the form |
| 3219 | * "-"? ("0x" | "0X")? digit+ |
| 3220 | * "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] | 3221 | * set in flags. The case of hex digits will be correct, |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3222 | * There will be at least prec digits, zero-filled on the left if |
| 3223 | * necessary to get that many. |
| 3224 | * val object to be converted |
| 3225 | * flags bitmask of format flags; only F_ALT is looked at |
| 3226 | * prec minimum number of digits; 0-fill on left if needed |
| 3227 | * type a character in [duoxX]; u acts the same as d |
| 3228 | * |
| 3229 | * CAUTION: o, x and X conversions on regular ints can never |
| 3230 | * produce a '-' sign, but can for Python's unbounded ints. |
| 3231 | */ |
| 3232 | PyObject* |
| 3233 | _PyString_FormatLong(PyObject *val, int flags, int prec, int type, |
| 3234 | char **pbuf, int *plen) |
| 3235 | { |
| 3236 | PyObject *result = NULL; |
| 3237 | char *buf; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3238 | Py_ssize_t i; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3239 | int sign; /* 1 if '-', else 0 */ |
| 3240 | int len; /* number of characters */ |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3241 | Py_ssize_t llen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3242 | int numdigits; /* len == numnondigits + numdigits */ |
| 3243 | int numnondigits = 0; |
| 3244 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 3245 | /* Avoid exceeding SSIZE_T_MAX */ |
| 3246 | if (prec > PY_SSIZE_T_MAX-3) { |
| 3247 | PyErr_SetString(PyExc_OverflowError, |
| 3248 | "precision too large"); |
| 3249 | return NULL; |
| 3250 | } |
| 3251 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3252 | switch (type) { |
| 3253 | case 'd': |
| 3254 | case 'u': |
Martin v. Löwis | ff398c6 | 2007-08-14 21:57:32 +0000 | [diff] [blame] | 3255 | /* Special-case boolean: we want 0/1 */ |
| 3256 | if (PyBool_Check(val)) |
| 3257 | result = PyNumber_ToBase(val, 10); |
| 3258 | else |
| 3259 | result = Py_Type(val)->tp_str(val); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3260 | break; |
| 3261 | case 'o': |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 3262 | numnondigits = 2; |
| 3263 | result = PyNumber_ToBase(val, 8); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3264 | break; |
| 3265 | case 'x': |
| 3266 | case 'X': |
| 3267 | numnondigits = 2; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 3268 | result = PyNumber_ToBase(val, 16); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3269 | break; |
| 3270 | default: |
| 3271 | assert(!"'type' not in [duoxX]"); |
| 3272 | } |
| 3273 | if (!result) |
| 3274 | return NULL; |
| 3275 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3276 | buf = PyString_AsString(result); |
| 3277 | if (!buf) { |
| 3278 | Py_DECREF(result); |
| 3279 | return NULL; |
| 3280 | } |
| 3281 | |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3282 | /* To modify the string in-place, there can only be one reference. */ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3283 | if (Py_Refcnt(result) != 1) { |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3284 | PyErr_BadInternalCall(); |
| 3285 | return NULL; |
| 3286 | } |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3287 | llen = PyString_Size(result); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3288 | if (llen > INT_MAX) { |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3289 | PyErr_SetString(PyExc_ValueError, |
| 3290 | "string too large in _PyString_FormatLong"); |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 3291 | return NULL; |
| 3292 | } |
| 3293 | len = (int)llen; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3294 | if (buf[len-1] == 'L') { |
| 3295 | --len; |
| 3296 | buf[len] = '\0'; |
| 3297 | } |
| 3298 | sign = buf[0] == '-'; |
| 3299 | numnondigits += sign; |
| 3300 | numdigits = len - numnondigits; |
| 3301 | assert(numdigits > 0); |
| 3302 | |
Tim Peters | fff5325 | 2001-04-12 18:38:48 +0000 | [diff] [blame] | 3303 | /* Get rid of base marker unless F_ALT */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 3304 | if (((flags & F_ALT) == 0 && |
| 3305 | (type == 'o' || type == 'x' || type == 'X'))) { |
| 3306 | assert(buf[sign] == '0'); |
| 3307 | assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3308 | buf[sign+1] == 'o'); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 3309 | numnondigits -= 2; |
| 3310 | buf += 2; |
| 3311 | len -= 2; |
| 3312 | if (sign) |
| 3313 | buf[0] = '-'; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3314 | assert(len == numnondigits + numdigits); |
| 3315 | assert(numdigits > 0); |
| 3316 | } |
| 3317 | |
| 3318 | /* Fill with leading zeroes to meet minimum width. */ |
| 3319 | if (prec > numdigits) { |
| 3320 | PyObject *r1 = PyString_FromStringAndSize(NULL, |
| 3321 | numnondigits + prec); |
| 3322 | char *b1; |
| 3323 | if (!r1) { |
| 3324 | Py_DECREF(result); |
| 3325 | return NULL; |
| 3326 | } |
| 3327 | b1 = PyString_AS_STRING(r1); |
| 3328 | for (i = 0; i < numnondigits; ++i) |
| 3329 | *b1++ = *buf++; |
| 3330 | for (i = 0; i < prec - numdigits; i++) |
| 3331 | *b1++ = '0'; |
| 3332 | for (i = 0; i < numdigits; i++) |
| 3333 | *b1++ = *buf++; |
| 3334 | *b1 = '\0'; |
| 3335 | Py_DECREF(result); |
| 3336 | result = r1; |
| 3337 | buf = PyString_AS_STRING(result); |
| 3338 | len = numnondigits + prec; |
| 3339 | } |
| 3340 | |
| 3341 | /* Fix up case for hex conversions. */ |
Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 3342 | if (type == 'X') { |
| 3343 | /* Need to convert all lower case letters to upper case. |
| 3344 | and need to convert 0x to 0X (and -0x to -0X). */ |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3345 | for (i = 0; i < len; i++) |
Raymond Hettinger | 3296e69 | 2005-06-29 23:29:56 +0000 | [diff] [blame] | 3346 | if (buf[i] >= 'a' && buf[i] <= 'x') |
| 3347 | buf[i] -= 'a'-'A'; |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 3348 | } |
| 3349 | *pbuf = buf; |
| 3350 | *plen = len; |
| 3351 | return result; |
| 3352 | } |
| 3353 | |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 3354 | void |
Fred Drake | ba09633 | 2000-07-09 07:04:36 +0000 | [diff] [blame] | 3355 | PyString_Fini(void) |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 3356 | { |
| 3357 | int i; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 3358 | for (i = 0; i < UCHAR_MAX + 1; i++) { |
| 3359 | Py_XDECREF(characters[i]); |
| 3360 | characters[i] = NULL; |
| 3361 | } |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 3362 | Py_XDECREF(nullstring); |
| 3363 | nullstring = NULL; |
Guido van Rossum | 8cf0476 | 1997-08-02 02:57:45 +0000 | [diff] [blame] | 3364 | } |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 3365 | |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3366 | /*********************** Str Iterator ****************************/ |
| 3367 | |
| 3368 | typedef struct { |
| 3369 | PyObject_HEAD |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 3370 | Py_ssize_t it_index; |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3371 | PyStringObject *it_seq; /* Set to NULL when iterator is exhausted */ |
| 3372 | } striterobject; |
| 3373 | |
| 3374 | static void |
| 3375 | striter_dealloc(striterobject *it) |
| 3376 | { |
| 3377 | _PyObject_GC_UNTRACK(it); |
| 3378 | Py_XDECREF(it->it_seq); |
| 3379 | PyObject_GC_Del(it); |
| 3380 | } |
| 3381 | |
| 3382 | static int |
| 3383 | striter_traverse(striterobject *it, visitproc visit, void *arg) |
| 3384 | { |
| 3385 | Py_VISIT(it->it_seq); |
| 3386 | return 0; |
| 3387 | } |
| 3388 | |
| 3389 | static PyObject * |
| 3390 | striter_next(striterobject *it) |
| 3391 | { |
| 3392 | PyStringObject *seq; |
| 3393 | PyObject *item; |
| 3394 | |
| 3395 | assert(it != NULL); |
| 3396 | seq = it->it_seq; |
| 3397 | if (seq == NULL) |
| 3398 | return NULL; |
| 3399 | assert(PyString_Check(seq)); |
| 3400 | |
| 3401 | if (it->it_index < PyString_GET_SIZE(seq)) { |
Guido van Rossum | 75a902d | 2007-10-19 22:06:24 +0000 | [diff] [blame] | 3402 | item = PyInt_FromLong( |
| 3403 | (unsigned char)seq->ob_sval[it->it_index]); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3404 | if (item != NULL) |
| 3405 | ++it->it_index; |
| 3406 | return item; |
| 3407 | } |
| 3408 | |
| 3409 | Py_DECREF(seq); |
| 3410 | it->it_seq = NULL; |
| 3411 | return NULL; |
| 3412 | } |
| 3413 | |
| 3414 | static PyObject * |
| 3415 | striter_len(striterobject *it) |
| 3416 | { |
| 3417 | Py_ssize_t len = 0; |
| 3418 | if (it->it_seq) |
| 3419 | len = PyString_GET_SIZE(it->it_seq) - it->it_index; |
| 3420 | return PyInt_FromSsize_t(len); |
| 3421 | } |
| 3422 | |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 3423 | PyDoc_STRVAR(length_hint_doc, |
| 3424 | "Private method returning an estimate of len(list(it))."); |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3425 | |
| 3426 | static PyMethodDef striter_methods[] = { |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 3427 | {"__length_hint__", (PyCFunction)striter_len, METH_NOARGS, |
| 3428 | length_hint_doc}, |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3429 | {NULL, NULL} /* sentinel */ |
| 3430 | }; |
| 3431 | |
| 3432 | PyTypeObject PyStringIter_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 3433 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 3434 | "bytes_iterator", /* tp_name */ |
Guido van Rossum | 49d6b07 | 2006-08-17 21:11:47 +0000 | [diff] [blame] | 3435 | sizeof(striterobject), /* tp_basicsize */ |
Guido van Rossum | 50e9fb9 | 2006-08-17 05:42:55 +0000 | [diff] [blame] | 3436 | 0, /* tp_itemsize */ |
| 3437 | /* methods */ |
| 3438 | (destructor)striter_dealloc, /* tp_dealloc */ |
| 3439 | 0, /* tp_print */ |
| 3440 | 0, /* tp_getattr */ |
| 3441 | 0, /* tp_setattr */ |
| 3442 | 0, /* tp_compare */ |
| 3443 | 0, /* tp_repr */ |
| 3444 | 0, /* tp_as_number */ |
| 3445 | 0, /* tp_as_sequence */ |
| 3446 | 0, /* tp_as_mapping */ |
| 3447 | 0, /* tp_hash */ |
| 3448 | 0, /* tp_call */ |
| 3449 | 0, /* tp_str */ |
| 3450 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3451 | 0, /* tp_setattro */ |
| 3452 | 0, /* tp_as_buffer */ |
| 3453 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 3454 | 0, /* tp_doc */ |
| 3455 | (traverseproc)striter_traverse, /* tp_traverse */ |
| 3456 | 0, /* tp_clear */ |
| 3457 | 0, /* tp_richcompare */ |
| 3458 | 0, /* tp_weaklistoffset */ |
| 3459 | PyObject_SelfIter, /* tp_iter */ |
| 3460 | (iternextfunc)striter_next, /* tp_iternext */ |
| 3461 | striter_methods, /* tp_methods */ |
| 3462 | 0, |
| 3463 | }; |
| 3464 | |
| 3465 | static PyObject * |
| 3466 | str_iter(PyObject *seq) |
| 3467 | { |
| 3468 | striterobject *it; |
| 3469 | |
| 3470 | if (!PyString_Check(seq)) { |
| 3471 | PyErr_BadInternalCall(); |
| 3472 | return NULL; |
| 3473 | } |
| 3474 | it = PyObject_GC_New(striterobject, &PyStringIter_Type); |
| 3475 | if (it == NULL) |
| 3476 | return NULL; |
| 3477 | it->it_index = 0; |
| 3478 | Py_INCREF(seq); |
| 3479 | it->it_seq = (PyStringObject *)seq; |
| 3480 | _PyObject_GC_TRACK(it); |
| 3481 | return (PyObject *)it; |
| 3482 | } |