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