Benjamin Peterson | 4116f36 | 2008-05-27 00:36:20 +0000 | [diff] [blame] | 1 | /* bytes object implementation */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3 | #define PY_SSIZE_T_CLEAN |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 5 | #include "Python.h" |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 6 | |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 7 | #include "bytes_methods.h" |
Mark Dickinson | fd24b32 | 2008-12-06 15:33:31 +0000 | [diff] [blame] | 8 | #include <stddef.h> |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 9 | |
Neal Norwitz | 2bad970 | 2007-08-27 06:19:22 +0000 | [diff] [blame] | 10 | static Py_ssize_t |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 11 | _getbuffer(PyObject *obj, Py_buffer *view) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 12 | { |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 13 | PyBufferProcs *bufferprocs; |
| 14 | if (PyBytes_CheckExact(obj)) { |
| 15 | /* Fast path, e.g. for .join() of many bytes objects */ |
| 16 | Py_INCREF(obj); |
| 17 | view->obj = obj; |
| 18 | view->buf = PyBytes_AS_STRING(obj); |
| 19 | view->len = PyBytes_GET_SIZE(obj); |
| 20 | return view->len; |
| 21 | } |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 22 | |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 23 | bufferprocs = Py_TYPE(obj)->tp_as_buffer; |
| 24 | if (bufferprocs == NULL || bufferprocs->bf_getbuffer == NULL) |
Guido van Rossum | a74184e | 2007-08-29 04:05:57 +0000 | [diff] [blame] | 25 | { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 26 | PyErr_Format(PyExc_TypeError, |
| 27 | "Type %.100s doesn't support the buffer API", |
| 28 | Py_TYPE(obj)->tp_name); |
| 29 | return -1; |
Guido van Rossum | a74184e | 2007-08-29 04:05:57 +0000 | [diff] [blame] | 30 | } |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 31 | |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 32 | if (bufferprocs->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | return -1; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 34 | return view->len; |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 35 | } |
| 36 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 37 | #ifdef COUNT_ALLOCS |
Benjamin Peterson | a4a37fe | 2009-01-11 17:13:55 +0000 | [diff] [blame] | 38 | Py_ssize_t null_strings, one_strings; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 39 | #endif |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 40 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 41 | static PyBytesObject *characters[UCHAR_MAX + 1]; |
| 42 | static PyBytesObject *nullstring; |
| 43 | |
Mark Dickinson | fd24b32 | 2008-12-06 15:33:31 +0000 | [diff] [blame] | 44 | /* PyBytesObject_SIZE gives the basic size of a string; any memory allocation |
| 45 | for a string of length n should request PyBytesObject_SIZE + n bytes. |
| 46 | |
| 47 | Using PyBytesObject_SIZE instead of sizeof(PyBytesObject) saves |
| 48 | 3 bytes per string allocation on a typical system. |
| 49 | */ |
| 50 | #define PyBytesObject_SIZE (offsetof(PyBytesObject, ob_sval) + 1) |
| 51 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 52 | /* |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 53 | For PyBytes_FromString(), the parameter `str' points to a null-terminated |
| 54 | string containing exactly `size' bytes. |
| 55 | |
| 56 | For PyBytes_FromStringAndSize(), the parameter the parameter `str' is |
| 57 | either NULL or else points to a string containing at least `size' bytes. |
| 58 | For PyBytes_FromStringAndSize(), the string in the `str' parameter does |
| 59 | not have to be null-terminated. (Therefore it is safe to construct a |
| 60 | substring by calling `PyBytes_FromStringAndSize(origstring, substrlen)'.) |
| 61 | If `str' is NULL then PyBytes_FromStringAndSize() will allocate `size+1' |
| 62 | bytes (setting the last byte to the null terminating character) and you can |
| 63 | fill in the data yourself. If `str' is non-NULL then the resulting |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 64 | PyBytes object must be treated as immutable and you must not fill in nor |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 65 | alter the data yourself, since the strings may be shared. |
| 66 | |
| 67 | The PyObject member `op->ob_size', which denotes the number of "extra |
| 68 | items" in a variable-size object, will contain the number of bytes |
Eli Bendersky | 1aef6b6 | 2011-03-24 22:32:56 +0200 | [diff] [blame] | 69 | allocated for string data, not counting the null terminating character. |
| 70 | It is therefore equal to the `size' parameter (for |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 71 | PyBytes_FromStringAndSize()) or the length of the string in the `str' |
| 72 | parameter (for PyBytes_FromString()). |
| 73 | */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 74 | PyObject * |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 75 | PyBytes_FromStringAndSize(const char *str, Py_ssize_t size) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 76 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 77 | PyBytesObject *op; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | if (size < 0) { |
| 79 | PyErr_SetString(PyExc_SystemError, |
| 80 | "Negative size passed to PyBytes_FromStringAndSize"); |
| 81 | return NULL; |
| 82 | } |
| 83 | if (size == 0 && (op = nullstring) != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 84 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | null_strings++; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 86 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | Py_INCREF(op); |
| 88 | return (PyObject *)op; |
| 89 | } |
| 90 | if (size == 1 && str != NULL && |
| 91 | (op = characters[*str & UCHAR_MAX]) != NULL) |
| 92 | { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 93 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | one_strings++; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 95 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | Py_INCREF(op); |
| 97 | return (PyObject *)op; |
| 98 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 99 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 100 | if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) { |
| 101 | PyErr_SetString(PyExc_OverflowError, |
| 102 | "byte string is too large"); |
| 103 | return NULL; |
| 104 | } |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | /* Inline PyObject_NewVar */ |
| 107 | op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size); |
| 108 | if (op == NULL) |
| 109 | return PyErr_NoMemory(); |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 110 | (void)PyObject_INIT_VAR(op, &PyBytes_Type, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | op->ob_shash = -1; |
| 112 | if (str != NULL) |
| 113 | Py_MEMCPY(op->ob_sval, str, size); |
| 114 | op->ob_sval[size] = '\0'; |
| 115 | /* share short strings */ |
| 116 | if (size == 0) { |
| 117 | nullstring = op; |
| 118 | Py_INCREF(op); |
| 119 | } else if (size == 1 && str != NULL) { |
| 120 | characters[*str & UCHAR_MAX] = op; |
| 121 | Py_INCREF(op); |
| 122 | } |
| 123 | return (PyObject *) op; |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 126 | PyObject * |
| 127 | PyBytes_FromString(const char *str) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 128 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 129 | size_t size; |
| 130 | PyBytesObject *op; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | assert(str != NULL); |
| 133 | size = strlen(str); |
| 134 | if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) { |
| 135 | PyErr_SetString(PyExc_OverflowError, |
| 136 | "byte string is too long"); |
| 137 | return NULL; |
| 138 | } |
| 139 | if (size == 0 && (op = nullstring) != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 140 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | null_strings++; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 142 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | Py_INCREF(op); |
| 144 | return (PyObject *)op; |
| 145 | } |
| 146 | if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 147 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | one_strings++; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 149 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | Py_INCREF(op); |
| 151 | return (PyObject *)op; |
| 152 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 153 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | /* Inline PyObject_NewVar */ |
| 155 | op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size); |
| 156 | if (op == NULL) |
| 157 | return PyErr_NoMemory(); |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 158 | (void)PyObject_INIT_VAR(op, &PyBytes_Type, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | op->ob_shash = -1; |
| 160 | Py_MEMCPY(op->ob_sval, str, size+1); |
| 161 | /* share short strings */ |
| 162 | if (size == 0) { |
| 163 | nullstring = op; |
| 164 | Py_INCREF(op); |
| 165 | } else if (size == 1) { |
| 166 | characters[*str & UCHAR_MAX] = op; |
| 167 | Py_INCREF(op); |
| 168 | } |
| 169 | return (PyObject *) op; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 170 | } |
Guido van Rossum | ebea9be | 2007-04-09 00:49:13 +0000 | [diff] [blame] | 171 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 172 | PyObject * |
| 173 | PyBytes_FromFormatV(const char *format, va_list vargs) |
| 174 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | va_list count; |
| 176 | Py_ssize_t n = 0; |
| 177 | const char* f; |
| 178 | char *s; |
| 179 | PyObject* string; |
Guido van Rossum | 343e97f | 2007-04-09 00:43:24 +0000 | [diff] [blame] | 180 | |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 181 | Py_VA_COPY(count, vargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | /* step 1: figure out how large a buffer we need */ |
| 183 | for (f = format; *f; f++) { |
| 184 | if (*f == '%') { |
| 185 | const char* p = f; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 186 | while (*++f && *f != '%' && !Py_ISALPHA(*f)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 | ; |
Guido van Rossum | 343e97f | 2007-04-09 00:43:24 +0000 | [diff] [blame] | 188 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 189 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 190 | * they don't affect the amount of space we reserve. |
| 191 | */ |
| 192 | if ((*f == 'l' || *f == 'z') && |
| 193 | (f[1] == 'd' || f[1] == 'u')) |
| 194 | ++f; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 196 | switch (*f) { |
| 197 | case 'c': |
Victor Stinner | c9362cf | 2013-12-13 12:14:44 +0100 | [diff] [blame] | 198 | { |
| 199 | int c = va_arg(count, int); |
| 200 | if (c < 0 || c > 255) { |
| 201 | PyErr_SetString(PyExc_OverflowError, |
| 202 | "PyBytes_FromFormatV(): %c format " |
| 203 | "expects an integer in range [0; 255]"); |
| 204 | return NULL; |
| 205 | } |
| 206 | n++; |
| 207 | break; |
| 208 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | case '%': |
| 210 | n++; |
| 211 | break; |
| 212 | case 'd': case 'u': case 'i': case 'x': |
| 213 | (void) va_arg(count, int); |
| 214 | /* 20 bytes is enough to hold a 64-bit |
| 215 | integer. Decimal takes the most space. |
| 216 | This isn't enough for octal. */ |
| 217 | n += 20; |
| 218 | break; |
| 219 | case 's': |
| 220 | s = va_arg(count, char*); |
| 221 | n += strlen(s); |
| 222 | break; |
| 223 | case 'p': |
| 224 | (void) va_arg(count, int); |
| 225 | /* maximum 64-bit pointer representation: |
| 226 | * 0xffffffffffffffff |
| 227 | * so 19 characters is enough. |
| 228 | * XXX I count 18 -- what's the extra for? |
| 229 | */ |
| 230 | n += 19; |
| 231 | break; |
| 232 | default: |
| 233 | /* if we stumble upon an unknown |
| 234 | formatting code, copy the rest of |
| 235 | the format string to the output |
| 236 | string. (we cannot just skip the |
| 237 | code, since there's no way to know |
| 238 | what's in the argument list) */ |
| 239 | n += strlen(p); |
| 240 | goto expand; |
| 241 | } |
| 242 | } else |
| 243 | n++; |
| 244 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 245 | expand: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | /* step 2: fill the buffer */ |
| 247 | /* Since we've analyzed how much space we need for the worst case, |
| 248 | use sprintf directly instead of the slower PyOS_snprintf. */ |
| 249 | string = PyBytes_FromStringAndSize(NULL, n); |
| 250 | if (!string) |
| 251 | return NULL; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | s = PyBytes_AsString(string); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 254 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | for (f = format; *f; f++) { |
| 256 | if (*f == '%') { |
| 257 | const char* p = f++; |
| 258 | Py_ssize_t i; |
| 259 | int longflag = 0; |
| 260 | int size_tflag = 0; |
| 261 | /* parse the width.precision part (we're only |
| 262 | interested in the precision value, if any) */ |
| 263 | n = 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 264 | while (Py_ISDIGIT(*f)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 265 | n = (n*10) + *f++ - '0'; |
| 266 | if (*f == '.') { |
| 267 | f++; |
| 268 | n = 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 269 | while (Py_ISDIGIT(*f)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | n = (n*10) + *f++ - '0'; |
| 271 | } |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 272 | while (*f && *f != '%' && !Py_ISALPHA(*f)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 273 | f++; |
| 274 | /* handle the long flag, but only for %ld and %lu. |
| 275 | others can be added when necessary. */ |
| 276 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 277 | longflag = 1; |
| 278 | ++f; |
| 279 | } |
| 280 | /* handle the size_t flag. */ |
| 281 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 282 | size_tflag = 1; |
| 283 | ++f; |
| 284 | } |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 286 | switch (*f) { |
| 287 | case 'c': |
Victor Stinner | c9362cf | 2013-12-13 12:14:44 +0100 | [diff] [blame] | 288 | { |
| 289 | int c = va_arg(vargs, int); |
| 290 | /* c has been checked for overflow in the first step */ |
| 291 | *s++ = (unsigned char)c; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 292 | break; |
Victor Stinner | c9362cf | 2013-12-13 12:14:44 +0100 | [diff] [blame] | 293 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 294 | case 'd': |
| 295 | if (longflag) |
| 296 | sprintf(s, "%ld", va_arg(vargs, long)); |
| 297 | else if (size_tflag) |
| 298 | sprintf(s, "%" PY_FORMAT_SIZE_T "d", |
| 299 | va_arg(vargs, Py_ssize_t)); |
| 300 | else |
| 301 | sprintf(s, "%d", va_arg(vargs, int)); |
| 302 | s += strlen(s); |
| 303 | break; |
| 304 | case 'u': |
| 305 | if (longflag) |
| 306 | sprintf(s, "%lu", |
| 307 | va_arg(vargs, unsigned long)); |
| 308 | else if (size_tflag) |
| 309 | sprintf(s, "%" PY_FORMAT_SIZE_T "u", |
| 310 | va_arg(vargs, size_t)); |
| 311 | else |
| 312 | sprintf(s, "%u", |
| 313 | va_arg(vargs, unsigned int)); |
| 314 | s += strlen(s); |
| 315 | break; |
| 316 | case 'i': |
| 317 | sprintf(s, "%i", va_arg(vargs, int)); |
| 318 | s += strlen(s); |
| 319 | break; |
| 320 | case 'x': |
| 321 | sprintf(s, "%x", va_arg(vargs, int)); |
| 322 | s += strlen(s); |
| 323 | break; |
| 324 | case 's': |
| 325 | p = va_arg(vargs, char*); |
| 326 | i = strlen(p); |
| 327 | if (n > 0 && i > n) |
| 328 | i = n; |
| 329 | Py_MEMCPY(s, p, i); |
| 330 | s += i; |
| 331 | break; |
| 332 | case 'p': |
| 333 | sprintf(s, "%p", va_arg(vargs, void*)); |
| 334 | /* %p is ill-defined: ensure leading 0x. */ |
| 335 | if (s[1] == 'X') |
| 336 | s[1] = 'x'; |
| 337 | else if (s[1] != 'x') { |
| 338 | memmove(s+2, s, strlen(s)+1); |
| 339 | s[0] = '0'; |
| 340 | s[1] = 'x'; |
| 341 | } |
| 342 | s += strlen(s); |
| 343 | break; |
| 344 | case '%': |
| 345 | *s++ = '%'; |
| 346 | break; |
| 347 | default: |
| 348 | strcpy(s, p); |
| 349 | s += strlen(s); |
| 350 | goto end; |
| 351 | } |
| 352 | } else |
| 353 | *s++ = *f; |
| 354 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 355 | |
| 356 | end: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 357 | _PyBytes_Resize(&string, s - PyBytes_AS_STRING(string)); |
| 358 | return string; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | PyObject * |
| 362 | PyBytes_FromFormat(const char *format, ...) |
| 363 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 364 | PyObject* ret; |
| 365 | va_list vargs; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 366 | |
| 367 | #ifdef HAVE_STDARG_PROTOTYPES |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 368 | va_start(vargs, format); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 369 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | va_start(vargs); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 371 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | ret = PyBytes_FromFormatV(format, vargs); |
| 373 | va_end(vargs); |
| 374 | return ret; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static void |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 378 | bytes_dealloc(PyObject *op) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | Py_TYPE(op)->tp_free(op); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 383 | /* Unescape a backslash-escaped string. If unicode is non-zero, |
| 384 | the string is a u-literal. If recode_encoding is non-zero, |
| 385 | the string is UTF-8 encoded and should be re-encoded in the |
| 386 | specified encoding. */ |
| 387 | |
| 388 | PyObject *PyBytes_DecodeEscape(const char *s, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | Py_ssize_t len, |
| 390 | const char *errors, |
| 391 | Py_ssize_t unicode, |
| 392 | const char *recode_encoding) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 393 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | int c; |
| 395 | char *p, *buf; |
| 396 | const char *end; |
| 397 | PyObject *v; |
| 398 | Py_ssize_t newlen = recode_encoding ? 4*len:len; |
| 399 | v = PyBytes_FromStringAndSize((char *)NULL, newlen); |
| 400 | if (v == NULL) |
| 401 | return NULL; |
| 402 | p = buf = PyBytes_AsString(v); |
| 403 | end = s + len; |
| 404 | while (s < end) { |
| 405 | if (*s != '\\') { |
| 406 | non_esc: |
| 407 | if (recode_encoding && (*s & 0x80)) { |
| 408 | PyObject *u, *w; |
| 409 | char *r; |
| 410 | const char* t; |
| 411 | Py_ssize_t rn; |
| 412 | t = s; |
| 413 | /* Decode non-ASCII bytes as UTF-8. */ |
| 414 | while (t < end && (*t & 0x80)) t++; |
| 415 | u = PyUnicode_DecodeUTF8(s, t - s, errors); |
| 416 | if(!u) goto failed; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | /* Recode them in target encoding. */ |
| 419 | w = PyUnicode_AsEncodedString( |
| 420 | u, recode_encoding, errors); |
| 421 | Py_DECREF(u); |
| 422 | if (!w) goto failed; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | /* Append bytes to output buffer. */ |
| 425 | assert(PyBytes_Check(w)); |
| 426 | r = PyBytes_AS_STRING(w); |
| 427 | rn = PyBytes_GET_SIZE(w); |
| 428 | Py_MEMCPY(p, r, rn); |
| 429 | p += rn; |
| 430 | Py_DECREF(w); |
| 431 | s = t; |
| 432 | } else { |
| 433 | *p++ = *s++; |
| 434 | } |
| 435 | continue; |
| 436 | } |
| 437 | s++; |
| 438 | if (s==end) { |
| 439 | PyErr_SetString(PyExc_ValueError, |
| 440 | "Trailing \\ in string"); |
| 441 | goto failed; |
| 442 | } |
| 443 | switch (*s++) { |
| 444 | /* XXX This assumes ASCII! */ |
| 445 | case '\n': break; |
| 446 | case '\\': *p++ = '\\'; break; |
| 447 | case '\'': *p++ = '\''; break; |
| 448 | case '\"': *p++ = '\"'; break; |
| 449 | case 'b': *p++ = '\b'; break; |
| 450 | case 'f': *p++ = '\014'; break; /* FF */ |
| 451 | case 't': *p++ = '\t'; break; |
| 452 | case 'n': *p++ = '\n'; break; |
| 453 | case 'r': *p++ = '\r'; break; |
| 454 | case 'v': *p++ = '\013'; break; /* VT */ |
| 455 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 456 | case '0': case '1': case '2': case '3': |
| 457 | case '4': case '5': case '6': case '7': |
| 458 | c = s[-1] - '0'; |
| 459 | if (s < end && '0' <= *s && *s <= '7') { |
| 460 | c = (c<<3) + *s++ - '0'; |
| 461 | if (s < end && '0' <= *s && *s <= '7') |
| 462 | c = (c<<3) + *s++ - '0'; |
| 463 | } |
| 464 | *p++ = c; |
| 465 | break; |
| 466 | case 'x': |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 467 | if (s+1 < end && Py_ISXDIGIT(s[0]) && Py_ISXDIGIT(s[1])) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 468 | unsigned int x = 0; |
| 469 | c = Py_CHARMASK(*s); |
| 470 | s++; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 471 | if (Py_ISDIGIT(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 472 | x = c - '0'; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 473 | else if (Py_ISLOWER(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 474 | x = 10 + c - 'a'; |
| 475 | else |
| 476 | x = 10 + c - 'A'; |
| 477 | x = x << 4; |
| 478 | c = Py_CHARMASK(*s); |
| 479 | s++; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 480 | if (Py_ISDIGIT(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | x += c - '0'; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 482 | else if (Py_ISLOWER(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | x += 10 + c - 'a'; |
| 484 | else |
| 485 | x += 10 + c - 'A'; |
| 486 | *p++ = x; |
| 487 | break; |
| 488 | } |
| 489 | if (!errors || strcmp(errors, "strict") == 0) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 490 | PyErr_Format(PyExc_ValueError, |
| 491 | "invalid \\x escape at position %d", |
Serhiy Storchaka | 801d955 | 2013-02-10 17:42:01 +0200 | [diff] [blame] | 492 | s - 2 - (end - len)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | goto failed; |
| 494 | } |
| 495 | if (strcmp(errors, "replace") == 0) { |
| 496 | *p++ = '?'; |
| 497 | } else if (strcmp(errors, "ignore") == 0) |
| 498 | /* do nothing */; |
| 499 | else { |
| 500 | PyErr_Format(PyExc_ValueError, |
| 501 | "decoding error; unknown " |
| 502 | "error handling code: %.400s", |
| 503 | errors); |
| 504 | goto failed; |
| 505 | } |
Serhiy Storchaka | ace3ad3 | 2013-01-25 23:31:43 +0200 | [diff] [blame] | 506 | /* skip \x */ |
| 507 | if (s < end && Py_ISXDIGIT(s[0])) |
| 508 | s++; /* and a hexdigit */ |
| 509 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | default: |
| 511 | *p++ = '\\'; |
| 512 | s--; |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 513 | goto non_esc; /* an arbitrary number of unescaped |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | UTF-8 bytes may follow. */ |
| 515 | } |
| 516 | } |
| 517 | if (p-buf < newlen) |
| 518 | _PyBytes_Resize(&v, p - buf); |
| 519 | return v; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 520 | failed: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | Py_DECREF(v); |
| 522 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | /* -------------------------------------------------------------------- */ |
| 526 | /* object api */ |
| 527 | |
| 528 | Py_ssize_t |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 529 | PyBytes_Size(PyObject *op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 530 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | if (!PyBytes_Check(op)) { |
| 532 | PyErr_Format(PyExc_TypeError, |
| 533 | "expected bytes, %.200s found", Py_TYPE(op)->tp_name); |
| 534 | return -1; |
| 535 | } |
| 536 | return Py_SIZE(op); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | char * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 540 | PyBytes_AsString(PyObject *op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 541 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 542 | if (!PyBytes_Check(op)) { |
| 543 | PyErr_Format(PyExc_TypeError, |
| 544 | "expected bytes, %.200s found", Py_TYPE(op)->tp_name); |
| 545 | return NULL; |
| 546 | } |
| 547 | return ((PyBytesObject *)op)->ob_sval; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | int |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 551 | PyBytes_AsStringAndSize(PyObject *obj, |
| 552 | char **s, |
| 553 | Py_ssize_t *len) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 554 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 555 | if (s == NULL) { |
| 556 | PyErr_BadInternalCall(); |
| 557 | return -1; |
| 558 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | if (!PyBytes_Check(obj)) { |
| 561 | PyErr_Format(PyExc_TypeError, |
| 562 | "expected bytes, %.200s found", Py_TYPE(obj)->tp_name); |
| 563 | return -1; |
| 564 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 566 | *s = PyBytes_AS_STRING(obj); |
| 567 | if (len != NULL) |
| 568 | *len = PyBytes_GET_SIZE(obj); |
| 569 | else if (strlen(*s) != (size_t)PyBytes_GET_SIZE(obj)) { |
| 570 | PyErr_SetString(PyExc_TypeError, |
| 571 | "expected bytes with no null"); |
| 572 | return -1; |
| 573 | } |
| 574 | return 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 575 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 576 | |
| 577 | /* -------------------------------------------------------------------- */ |
| 578 | /* Methods */ |
| 579 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 580 | #include "stringlib/stringdefs.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 581 | |
| 582 | #include "stringlib/fastsearch.h" |
| 583 | #include "stringlib/count.h" |
| 584 | #include "stringlib/find.h" |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 585 | #include "stringlib/join.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 586 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 587 | #include "stringlib/split.h" |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 588 | #include "stringlib/ctype.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 589 | |
Eric Smith | 0f78bff | 2009-11-30 01:01:42 +0000 | [diff] [blame] | 590 | #include "stringlib/transmogrify.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 591 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 592 | PyObject * |
| 593 | PyBytes_Repr(PyObject *obj, int smartquotes) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 594 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 595 | PyBytesObject* op = (PyBytesObject*) obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 596 | Py_ssize_t i, length = Py_SIZE(op); |
| 597 | size_t newsize, squotes, dquotes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 598 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 599 | unsigned char quote, *s, *p; |
| 600 | |
| 601 | /* Compute size of output string */ |
| 602 | squotes = dquotes = 0; |
| 603 | newsize = 3; /* b'' */ |
| 604 | s = (unsigned char*)op->ob_sval; |
| 605 | for (i = 0; i < length; i++) { |
| 606 | switch(s[i]) { |
| 607 | case '\'': squotes++; newsize++; break; |
| 608 | case '"': dquotes++; newsize++; break; |
| 609 | case '\\': case '\t': case '\n': case '\r': |
| 610 | newsize += 2; break; /* \C */ |
| 611 | default: |
| 612 | if (s[i] < ' ' || s[i] >= 0x7f) |
| 613 | newsize += 4; /* \xHH */ |
| 614 | else |
| 615 | newsize++; |
| 616 | } |
| 617 | } |
| 618 | quote = '\''; |
| 619 | if (smartquotes && squotes && !dquotes) |
| 620 | quote = '"'; |
| 621 | if (squotes && quote == '\'') |
| 622 | newsize += squotes; |
Victor Stinner | 6430fd5 | 2011-09-29 04:02:13 +0200 | [diff] [blame] | 623 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 624 | if (newsize > (PY_SSIZE_T_MAX - sizeof(PyUnicodeObject) - 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | PyErr_SetString(PyExc_OverflowError, |
| 626 | "bytes object is too large to make repr"); |
| 627 | return NULL; |
| 628 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 629 | |
| 630 | v = PyUnicode_New(newsize, 127); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | if (v == NULL) { |
| 632 | return NULL; |
| 633 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 634 | p = PyUnicode_1BYTE_DATA(v); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 635 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 636 | *p++ = 'b', *p++ = quote; |
| 637 | for (i = 0; i < length; i++) { |
| 638 | unsigned char c = op->ob_sval[i]; |
| 639 | if (c == quote || c == '\\') |
| 640 | *p++ = '\\', *p++ = c; |
| 641 | else if (c == '\t') |
| 642 | *p++ = '\\', *p++ = 't'; |
| 643 | else if (c == '\n') |
| 644 | *p++ = '\\', *p++ = 'n'; |
| 645 | else if (c == '\r') |
| 646 | *p++ = '\\', *p++ = 'r'; |
| 647 | else if (c < ' ' || c >= 0x7f) { |
| 648 | *p++ = '\\'; |
| 649 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 650 | *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
| 651 | *p++ = Py_hexdigits[c & 0xf]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 652 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 653 | else |
| 654 | *p++ = c; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 656 | *p++ = quote; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 657 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 658 | return v; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 661 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 662 | bytes_repr(PyObject *op) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 663 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | return PyBytes_Repr(op, 1); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 667 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 668 | bytes_str(PyObject *op) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 669 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | if (Py_BytesWarningFlag) { |
| 671 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 672 | "str() on a bytes instance", 1)) |
| 673 | return NULL; |
| 674 | } |
| 675 | return bytes_repr(op); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 678 | static Py_ssize_t |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 679 | bytes_length(PyBytesObject *a) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 680 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 681 | return Py_SIZE(a); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 682 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 683 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 684 | /* This is also used by PyBytes_Concat() */ |
| 685 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 686 | bytes_concat(PyObject *a, PyObject *b) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 687 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 688 | Py_ssize_t size; |
| 689 | Py_buffer va, vb; |
| 690 | PyObject *result = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 692 | va.len = -1; |
| 693 | vb.len = -1; |
| 694 | if (_getbuffer(a, &va) < 0 || |
| 695 | _getbuffer(b, &vb) < 0) { |
| 696 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 697 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 698 | goto done; |
| 699 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 701 | /* Optimize end cases */ |
| 702 | if (va.len == 0 && PyBytes_CheckExact(b)) { |
| 703 | result = b; |
| 704 | Py_INCREF(result); |
| 705 | goto done; |
| 706 | } |
| 707 | if (vb.len == 0 && PyBytes_CheckExact(a)) { |
| 708 | result = a; |
| 709 | Py_INCREF(result); |
| 710 | goto done; |
| 711 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 712 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 713 | size = va.len + vb.len; |
| 714 | if (size < 0) { |
| 715 | PyErr_NoMemory(); |
| 716 | goto done; |
| 717 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 719 | result = PyBytes_FromStringAndSize(NULL, size); |
| 720 | if (result != NULL) { |
| 721 | memcpy(PyBytes_AS_STRING(result), va.buf, va.len); |
| 722 | memcpy(PyBytes_AS_STRING(result) + va.len, vb.buf, vb.len); |
| 723 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 724 | |
| 725 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 726 | if (va.len != -1) |
| 727 | PyBuffer_Release(&va); |
| 728 | if (vb.len != -1) |
| 729 | PyBuffer_Release(&vb); |
| 730 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 731 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 732 | |
| 733 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 734 | bytes_repeat(PyBytesObject *a, Py_ssize_t n) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 735 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 736 | Py_ssize_t i; |
| 737 | Py_ssize_t j; |
| 738 | Py_ssize_t size; |
| 739 | PyBytesObject *op; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 740 | size_t nbytes; |
| 741 | if (n < 0) |
| 742 | n = 0; |
| 743 | /* watch out for overflows: the size can overflow int, |
| 744 | * and the # of bytes needed can overflow size_t |
| 745 | */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 746 | if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 747 | PyErr_SetString(PyExc_OverflowError, |
| 748 | "repeated bytes are too long"); |
| 749 | return NULL; |
| 750 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 751 | size = Py_SIZE(a) * n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 752 | if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) { |
| 753 | Py_INCREF(a); |
| 754 | return (PyObject *)a; |
| 755 | } |
| 756 | nbytes = (size_t)size; |
| 757 | if (nbytes + PyBytesObject_SIZE <= nbytes) { |
| 758 | PyErr_SetString(PyExc_OverflowError, |
| 759 | "repeated bytes are too long"); |
| 760 | return NULL; |
| 761 | } |
| 762 | op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + nbytes); |
| 763 | if (op == NULL) |
| 764 | return PyErr_NoMemory(); |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 765 | (void)PyObject_INIT_VAR(op, &PyBytes_Type, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 766 | op->ob_shash = -1; |
| 767 | op->ob_sval[size] = '\0'; |
| 768 | if (Py_SIZE(a) == 1 && n > 0) { |
| 769 | memset(op->ob_sval, a->ob_sval[0] , n); |
| 770 | return (PyObject *) op; |
| 771 | } |
| 772 | i = 0; |
| 773 | if (i < size) { |
| 774 | Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a)); |
| 775 | i = Py_SIZE(a); |
| 776 | } |
| 777 | while (i < size) { |
| 778 | j = (i <= size-i) ? i : size-i; |
| 779 | Py_MEMCPY(op->ob_sval+i, op->ob_sval, j); |
| 780 | i += j; |
| 781 | } |
| 782 | return (PyObject *) op; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 785 | static int |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 786 | bytes_contains(PyObject *self, PyObject *arg) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 787 | { |
| 788 | Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
| 789 | if (ival == -1 && PyErr_Occurred()) { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 790 | Py_buffer varg; |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 791 | Py_ssize_t pos; |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 792 | PyErr_Clear(); |
| 793 | if (_getbuffer(arg, &varg) < 0) |
| 794 | return -1; |
| 795 | pos = stringlib_find(PyBytes_AS_STRING(self), Py_SIZE(self), |
| 796 | varg.buf, varg.len, 0); |
| 797 | PyBuffer_Release(&varg); |
| 798 | return pos >= 0; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 799 | } |
| 800 | if (ival < 0 || ival >= 256) { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 801 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
| 802 | return -1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 805 | return memchr(PyBytes_AS_STRING(self), (int) ival, Py_SIZE(self)) != NULL; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 808 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 809 | bytes_item(PyBytesObject *a, Py_ssize_t i) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 810 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 811 | if (i < 0 || i >= Py_SIZE(a)) { |
| 812 | PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 813 | return NULL; |
| 814 | } |
| 815 | return PyLong_FromLong((unsigned char)a->ob_sval[i]); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 818 | Py_LOCAL(int) |
| 819 | bytes_compare_eq(PyBytesObject *a, PyBytesObject *b) |
| 820 | { |
| 821 | int cmp; |
| 822 | Py_ssize_t len; |
| 823 | |
| 824 | len = Py_SIZE(a); |
| 825 | if (Py_SIZE(b) != len) |
| 826 | return 0; |
| 827 | |
| 828 | if (a->ob_sval[0] != b->ob_sval[0]) |
| 829 | return 0; |
| 830 | |
| 831 | cmp = memcmp(a->ob_sval, b->ob_sval, len); |
| 832 | return (cmp == 0); |
| 833 | } |
| 834 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 835 | static PyObject* |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 836 | bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 837 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 838 | int c; |
| 839 | Py_ssize_t len_a, len_b; |
| 840 | Py_ssize_t min_len; |
| 841 | PyObject *result; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 843 | /* Make sure both arguments are strings. */ |
| 844 | if (!(PyBytes_Check(a) && PyBytes_Check(b))) { |
| 845 | if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE) && |
| 846 | (PyObject_IsInstance((PyObject*)a, |
| 847 | (PyObject*)&PyUnicode_Type) || |
| 848 | PyObject_IsInstance((PyObject*)b, |
| 849 | (PyObject*)&PyUnicode_Type))) { |
| 850 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 851 | "Comparison between bytes and string", 1)) |
| 852 | return NULL; |
| 853 | } |
| 854 | result = Py_NotImplemented; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 855 | } |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 856 | else if (a == b) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 857 | switch (op) { |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 858 | case Py_EQ: |
| 859 | case Py_LE: |
| 860 | case Py_GE: |
| 861 | /* a string is equal to itself */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 862 | result = Py_True; |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 863 | break; |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 864 | case Py_NE: |
| 865 | case Py_LT: |
| 866 | case Py_GT: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 867 | result = Py_False; |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 868 | break; |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 869 | default: |
| 870 | PyErr_BadArgument(); |
| 871 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 872 | } |
| 873 | } |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 874 | else if (op == Py_EQ || op == Py_NE) { |
| 875 | int eq = bytes_compare_eq(a, b); |
| 876 | eq ^= (op == Py_NE); |
| 877 | result = eq ? Py_True : Py_False; |
| 878 | } |
| 879 | else { |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 880 | len_a = Py_SIZE(a); |
| 881 | len_b = Py_SIZE(b); |
| 882 | min_len = Py_MIN(len_a, len_b); |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 883 | if (min_len > 0) { |
| 884 | c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 885 | if (c == 0) |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 886 | c = memcmp(a->ob_sval, b->ob_sval, min_len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | } |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 888 | else |
| 889 | c = 0; |
| 890 | if (c == 0) |
| 891 | c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
| 892 | switch (op) { |
| 893 | case Py_LT: c = c < 0; break; |
| 894 | case Py_LE: c = c <= 0; break; |
| 895 | case Py_GT: c = c > 0; break; |
| 896 | case Py_GE: c = c >= 0; break; |
| 897 | default: |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 898 | PyErr_BadArgument(); |
| 899 | return NULL; |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 900 | } |
| 901 | result = c ? Py_True : Py_False; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 902 | } |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 904 | Py_INCREF(result); |
| 905 | return result; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 908 | static Py_hash_t |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 909 | bytes_hash(PyBytesObject *a) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 910 | { |
Antoine Pitrou | ce4a9da | 2011-11-21 20:46:33 +0100 | [diff] [blame] | 911 | if (a->ob_shash == -1) { |
| 912 | /* Can't fail */ |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 913 | a->ob_shash = _Py_HashBytes(a->ob_sval, Py_SIZE(a)); |
Antoine Pitrou | ce4a9da | 2011-11-21 20:46:33 +0100 | [diff] [blame] | 914 | } |
| 915 | return a->ob_shash; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 918 | static PyObject* |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 919 | bytes_subscript(PyBytesObject* self, PyObject* item) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 920 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | if (PyIndex_Check(item)) { |
| 922 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 923 | if (i == -1 && PyErr_Occurred()) |
| 924 | return NULL; |
| 925 | if (i < 0) |
| 926 | i += PyBytes_GET_SIZE(self); |
| 927 | if (i < 0 || i >= PyBytes_GET_SIZE(self)) { |
| 928 | PyErr_SetString(PyExc_IndexError, |
| 929 | "index out of range"); |
| 930 | return NULL; |
| 931 | } |
| 932 | return PyLong_FromLong((unsigned char)self->ob_sval[i]); |
| 933 | } |
| 934 | else if (PySlice_Check(item)) { |
| 935 | Py_ssize_t start, stop, step, slicelength, cur, i; |
| 936 | char* source_buf; |
| 937 | char* result_buf; |
| 938 | PyObject* result; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 939 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 940 | if (PySlice_GetIndicesEx(item, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | PyBytes_GET_SIZE(self), |
| 942 | &start, &stop, &step, &slicelength) < 0) { |
| 943 | return NULL; |
| 944 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 946 | if (slicelength <= 0) { |
| 947 | return PyBytes_FromStringAndSize("", 0); |
| 948 | } |
| 949 | else if (start == 0 && step == 1 && |
| 950 | slicelength == PyBytes_GET_SIZE(self) && |
| 951 | PyBytes_CheckExact(self)) { |
| 952 | Py_INCREF(self); |
| 953 | return (PyObject *)self; |
| 954 | } |
| 955 | else if (step == 1) { |
| 956 | return PyBytes_FromStringAndSize( |
| 957 | PyBytes_AS_STRING(self) + start, |
| 958 | slicelength); |
| 959 | } |
| 960 | else { |
| 961 | source_buf = PyBytes_AS_STRING(self); |
| 962 | result = PyBytes_FromStringAndSize(NULL, slicelength); |
| 963 | if (result == NULL) |
| 964 | return NULL; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | result_buf = PyBytes_AS_STRING(result); |
| 967 | for (cur = start, i = 0; i < slicelength; |
| 968 | cur += step, i++) { |
| 969 | result_buf[i] = source_buf[cur]; |
| 970 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 971 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | return result; |
| 973 | } |
| 974 | } |
| 975 | else { |
| 976 | PyErr_Format(PyExc_TypeError, |
| 977 | "byte indices must be integers, not %.200s", |
| 978 | Py_TYPE(item)->tp_name); |
| 979 | return NULL; |
| 980 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | static int |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 984 | bytes_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 985 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | return PyBuffer_FillInfo(view, (PyObject*)self, (void *)self->ob_sval, Py_SIZE(self), |
| 987 | 1, flags); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 988 | } |
| 989 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 990 | static PySequenceMethods bytes_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | (lenfunc)bytes_length, /*sq_length*/ |
| 992 | (binaryfunc)bytes_concat, /*sq_concat*/ |
| 993 | (ssizeargfunc)bytes_repeat, /*sq_repeat*/ |
| 994 | (ssizeargfunc)bytes_item, /*sq_item*/ |
| 995 | 0, /*sq_slice*/ |
| 996 | 0, /*sq_ass_item*/ |
| 997 | 0, /*sq_ass_slice*/ |
| 998 | (objobjproc)bytes_contains /*sq_contains*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 999 | }; |
| 1000 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1001 | static PyMappingMethods bytes_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | (lenfunc)bytes_length, |
| 1003 | (binaryfunc)bytes_subscript, |
| 1004 | 0, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1005 | }; |
| 1006 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1007 | static PyBufferProcs bytes_as_buffer = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | (getbufferproc)bytes_buffer_getbuffer, |
| 1009 | NULL, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1010 | }; |
| 1011 | |
| 1012 | |
| 1013 | #define LEFTSTRIP 0 |
| 1014 | #define RIGHTSTRIP 1 |
| 1015 | #define BOTHSTRIP 2 |
| 1016 | |
| 1017 | /* Arrays indexed by above */ |
| 1018 | static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
| 1019 | |
| 1020 | #define STRIPNAME(i) (stripformat[i]+3) |
| 1021 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1022 | PyDoc_STRVAR(split__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 1023 | "B.split(sep=None, maxsplit=-1) -> list of bytes\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1024 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1025 | Return a list of the sections in B, using sep as the delimiter.\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1026 | If sep is not specified or is None, B is split on ASCII whitespace\n\ |
| 1027 | characters (space, tab, return, newline, formfeed, vertical tab).\n\ |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 1028 | If maxsplit is given, at most maxsplit splits are done."); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1029 | |
| 1030 | static PyObject * |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 1031 | bytes_split(PyBytesObject *self, PyObject *args, PyObject *kwds) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1032 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 1033 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1034 | Py_ssize_t len = PyBytes_GET_SIZE(self), n; |
| 1035 | Py_ssize_t maxsplit = -1; |
| 1036 | const char *s = PyBytes_AS_STRING(self), *sub; |
| 1037 | Py_buffer vsub; |
| 1038 | PyObject *list, *subobj = Py_None; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1039 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 1040 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split", |
| 1041 | kwlist, &subobj, &maxsplit)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1042 | return NULL; |
| 1043 | if (maxsplit < 0) |
| 1044 | maxsplit = PY_SSIZE_T_MAX; |
| 1045 | if (subobj == Py_None) |
| 1046 | return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); |
| 1047 | if (_getbuffer(subobj, &vsub) < 0) |
| 1048 | return NULL; |
| 1049 | sub = vsub.buf; |
| 1050 | n = vsub.len; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 1051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1052 | list = stringlib_split((PyObject*) self, s, len, sub, n, maxsplit); |
| 1053 | PyBuffer_Release(&vsub); |
| 1054 | return list; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1057 | PyDoc_STRVAR(partition__doc__, |
| 1058 | "B.partition(sep) -> (head, sep, tail)\n\ |
| 1059 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1060 | Search for the separator sep in B, and return the part before it,\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1061 | the separator itself, and the part after it. If the separator is not\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1062 | found, returns B and two empty bytes objects."); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1063 | |
| 1064 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1065 | bytes_partition(PyBytesObject *self, PyObject *sep_obj) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1066 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1067 | const char *sep; |
| 1068 | Py_ssize_t sep_len; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1069 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | if (PyBytes_Check(sep_obj)) { |
| 1071 | sep = PyBytes_AS_STRING(sep_obj); |
| 1072 | sep_len = PyBytes_GET_SIZE(sep_obj); |
| 1073 | } |
| 1074 | else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
| 1075 | return NULL; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1076 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | return stringlib_partition( |
| 1078 | (PyObject*) self, |
| 1079 | PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
| 1080 | sep_obj, sep, sep_len |
| 1081 | ); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | PyDoc_STRVAR(rpartition__doc__, |
Ezio Melotti | 5b2b242 | 2010-01-25 11:58:28 +0000 | [diff] [blame] | 1085 | "B.rpartition(sep) -> (head, sep, tail)\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1086 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1087 | Search for the separator sep in B, starting at the end of B,\n\ |
| 1088 | and return the part before it, the separator itself, and the\n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1089 | part after it. If the separator is not found, returns two empty\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1090 | bytes objects and B."); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1091 | |
| 1092 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1093 | bytes_rpartition(PyBytesObject *self, PyObject *sep_obj) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1094 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1095 | const char *sep; |
| 1096 | Py_ssize_t sep_len; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1098 | if (PyBytes_Check(sep_obj)) { |
| 1099 | sep = PyBytes_AS_STRING(sep_obj); |
| 1100 | sep_len = PyBytes_GET_SIZE(sep_obj); |
| 1101 | } |
| 1102 | else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
| 1103 | return NULL; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1105 | return stringlib_rpartition( |
| 1106 | (PyObject*) self, |
| 1107 | PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
| 1108 | sep_obj, sep, sep_len |
| 1109 | ); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1112 | PyDoc_STRVAR(rsplit__doc__, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 1113 | "B.rsplit(sep=None, maxsplit=-1) -> list of bytes\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1114 | \n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1115 | Return a list of the sections in B, using sep as the delimiter,\n\ |
| 1116 | starting at the end of B and working to the front.\n\ |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 1117 | If sep is not given, B is split on ASCII whitespace characters\n\ |
| 1118 | (space, tab, return, newline, formfeed, vertical tab).\n\ |
| 1119 | If maxsplit is given, at most maxsplit splits are done."); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1120 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1121 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1122 | static PyObject * |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 1123 | bytes_rsplit(PyBytesObject *self, PyObject *args, PyObject *kwds) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1124 | { |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 1125 | static char *kwlist[] = {"sep", "maxsplit", 0}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1126 | Py_ssize_t len = PyBytes_GET_SIZE(self), n; |
| 1127 | Py_ssize_t maxsplit = -1; |
| 1128 | const char *s = PyBytes_AS_STRING(self), *sub; |
| 1129 | Py_buffer vsub; |
| 1130 | PyObject *list, *subobj = Py_None; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1131 | |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 1132 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit", |
| 1133 | kwlist, &subobj, &maxsplit)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1134 | return NULL; |
| 1135 | if (maxsplit < 0) |
| 1136 | maxsplit = PY_SSIZE_T_MAX; |
| 1137 | if (subobj == Py_None) |
| 1138 | return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); |
| 1139 | if (_getbuffer(subobj, &vsub) < 0) |
| 1140 | return NULL; |
| 1141 | sub = vsub.buf; |
| 1142 | n = vsub.len; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 1143 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1144 | list = stringlib_rsplit((PyObject*) self, s, len, sub, n, maxsplit); |
| 1145 | PyBuffer_Release(&vsub); |
| 1146 | return list; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1149 | |
| 1150 | PyDoc_STRVAR(join__doc__, |
| 1151 | "B.join(iterable_of_bytes) -> bytes\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1152 | \n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1153 | Concatenate any number of bytes objects, with B in between each pair.\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1154 | Example: b'.'.join([b'ab', b'pq', b'rs']) -> b'ab.pq.rs'."); |
| 1155 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1156 | static PyObject * |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1157 | bytes_join(PyObject *self, PyObject *iterable) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1158 | { |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1159 | return stringlib_bytes_join(self, iterable); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1162 | PyObject * |
| 1163 | _PyBytes_Join(PyObject *sep, PyObject *x) |
| 1164 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | assert(sep != NULL && PyBytes_Check(sep)); |
| 1166 | assert(x != NULL); |
| 1167 | return bytes_join(sep, x); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1170 | /* helper macro to fixup start/end slice values */ |
| 1171 | #define ADJUST_INDICES(start, end, len) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1172 | if (end > len) \ |
| 1173 | end = len; \ |
| 1174 | else if (end < 0) { \ |
| 1175 | end += len; \ |
| 1176 | if (end < 0) \ |
| 1177 | end = 0; \ |
| 1178 | } \ |
| 1179 | if (start < 0) { \ |
| 1180 | start += len; \ |
| 1181 | if (start < 0) \ |
| 1182 | start = 0; \ |
| 1183 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1184 | |
| 1185 | Py_LOCAL_INLINE(Py_ssize_t) |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1186 | bytes_find_internal(PyBytesObject *self, PyObject *args, int dir) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1187 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | PyObject *subobj; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1189 | char byte; |
| 1190 | Py_buffer subbuf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1191 | const char *sub; |
| 1192 | Py_ssize_t sub_len; |
| 1193 | Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1194 | Py_ssize_t res; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1195 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1196 | if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex", |
| 1197 | args, &subobj, &byte, &start, &end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1198 | return -2; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1199 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1200 | if (subobj) { |
| 1201 | if (_getbuffer(subobj, &subbuf) < 0) |
| 1202 | return -2; |
| 1203 | |
| 1204 | sub = subbuf.buf; |
| 1205 | sub_len = subbuf.len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1206 | } |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1207 | else { |
| 1208 | sub = &byte; |
| 1209 | sub_len = 1; |
| 1210 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1211 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1212 | if (dir > 0) |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1213 | res = stringlib_find_slice( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1214 | PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
| 1215 | sub, sub_len, start, end); |
| 1216 | else |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1217 | res = stringlib_rfind_slice( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1218 | PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
| 1219 | sub, sub_len, start, end); |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1220 | |
| 1221 | if (subobj) |
| 1222 | PyBuffer_Release(&subbuf); |
| 1223 | |
| 1224 | return res; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | |
| 1228 | PyDoc_STRVAR(find__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1229 | "B.find(sub[, start[, end]]) -> int\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1230 | \n\ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1231 | Return the lowest index in B where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 1232 | such that sub is contained within B[start:end]. Optional\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1233 | arguments start and end are interpreted as in slice notation.\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1234 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1235 | Return -1 on failure."); |
| 1236 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1237 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1238 | bytes_find(PyBytesObject *self, PyObject *args) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1239 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1240 | Py_ssize_t result = bytes_find_internal(self, args, +1); |
| 1241 | if (result == -2) |
| 1242 | return NULL; |
| 1243 | return PyLong_FromSsize_t(result); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1246 | |
| 1247 | PyDoc_STRVAR(index__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1248 | "B.index(sub[, start[, end]]) -> int\n\ |
Alexandre Vassalotti | 09121e8 | 2007-12-04 05:51:13 +0000 | [diff] [blame] | 1249 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1250 | Like B.find() but raise ValueError when the substring is not found."); |
| 1251 | |
Alexandre Vassalotti | 09121e8 | 2007-12-04 05:51:13 +0000 | [diff] [blame] | 1252 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1253 | bytes_index(PyBytesObject *self, PyObject *args) |
Alexandre Vassalotti | 09121e8 | 2007-12-04 05:51:13 +0000 | [diff] [blame] | 1254 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1255 | Py_ssize_t result = bytes_find_internal(self, args, +1); |
| 1256 | if (result == -2) |
| 1257 | return NULL; |
| 1258 | if (result == -1) { |
| 1259 | PyErr_SetString(PyExc_ValueError, |
| 1260 | "substring not found"); |
| 1261 | return NULL; |
| 1262 | } |
| 1263 | return PyLong_FromSsize_t(result); |
Alexandre Vassalotti | 09121e8 | 2007-12-04 05:51:13 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1266 | |
| 1267 | PyDoc_STRVAR(rfind__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1268 | "B.rfind(sub[, start[, end]]) -> int\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1269 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1270 | Return the highest index in B where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 1271 | such that sub is contained within B[start:end]. Optional\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1272 | arguments start and end are interpreted as in slice notation.\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1273 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1274 | Return -1 on failure."); |
| 1275 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1276 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1277 | bytes_rfind(PyBytesObject *self, PyObject *args) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1278 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1279 | Py_ssize_t result = bytes_find_internal(self, args, -1); |
| 1280 | if (result == -2) |
| 1281 | return NULL; |
| 1282 | return PyLong_FromSsize_t(result); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1285 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1286 | PyDoc_STRVAR(rindex__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1287 | "B.rindex(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1288 | \n\ |
| 1289 | Like B.rfind() but raise ValueError when the substring is not found."); |
| 1290 | |
| 1291 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1292 | bytes_rindex(PyBytesObject *self, PyObject *args) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1293 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1294 | Py_ssize_t result = bytes_find_internal(self, args, -1); |
| 1295 | if (result == -2) |
| 1296 | return NULL; |
| 1297 | if (result == -1) { |
| 1298 | PyErr_SetString(PyExc_ValueError, |
| 1299 | "substring not found"); |
| 1300 | return NULL; |
| 1301 | } |
| 1302 | return PyLong_FromSsize_t(result); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1305 | |
| 1306 | Py_LOCAL_INLINE(PyObject *) |
| 1307 | do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1308 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | Py_buffer vsep; |
| 1310 | char *s = PyBytes_AS_STRING(self); |
| 1311 | Py_ssize_t len = PyBytes_GET_SIZE(self); |
| 1312 | char *sep; |
| 1313 | Py_ssize_t seplen; |
| 1314 | Py_ssize_t i, j; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1315 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1316 | if (_getbuffer(sepobj, &vsep) < 0) |
| 1317 | return NULL; |
| 1318 | sep = vsep.buf; |
| 1319 | seplen = vsep.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1321 | i = 0; |
| 1322 | if (striptype != RIGHTSTRIP) { |
| 1323 | while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) { |
| 1324 | i++; |
| 1325 | } |
| 1326 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1328 | j = len; |
| 1329 | if (striptype != LEFTSTRIP) { |
| 1330 | do { |
| 1331 | j--; |
| 1332 | } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen)); |
| 1333 | j++; |
| 1334 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | PyBuffer_Release(&vsep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1337 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1338 | if (i == 0 && j == len && PyBytes_CheckExact(self)) { |
| 1339 | Py_INCREF(self); |
| 1340 | return (PyObject*)self; |
| 1341 | } |
| 1342 | else |
| 1343 | return PyBytes_FromStringAndSize(s+i, j-i); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1346 | |
| 1347 | Py_LOCAL_INLINE(PyObject *) |
| 1348 | do_strip(PyBytesObject *self, int striptype) |
| 1349 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1350 | char *s = PyBytes_AS_STRING(self); |
| 1351 | Py_ssize_t len = PyBytes_GET_SIZE(self), i, j; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | i = 0; |
| 1354 | if (striptype != RIGHTSTRIP) { |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 1355 | while (i < len && Py_ISSPACE(s[i])) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1356 | i++; |
| 1357 | } |
| 1358 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 | j = len; |
| 1361 | if (striptype != LEFTSTRIP) { |
| 1362 | do { |
| 1363 | j--; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 1364 | } while (j >= i && Py_ISSPACE(s[j])); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1365 | j++; |
| 1366 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1367 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1368 | if (i == 0 && j == len && PyBytes_CheckExact(self)) { |
| 1369 | Py_INCREF(self); |
| 1370 | return (PyObject*)self; |
| 1371 | } |
| 1372 | else |
| 1373 | return PyBytes_FromStringAndSize(s+i, j-i); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | |
| 1377 | Py_LOCAL_INLINE(PyObject *) |
| 1378 | do_argstrip(PyBytesObject *self, int striptype, PyObject *args) |
| 1379 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1380 | PyObject *sep = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1381 | |
Serhiy Storchaka | c679227 | 2013-10-19 21:03:34 +0300 | [diff] [blame] | 1382 | if (!PyArg_ParseTuple(args, stripformat[striptype], &sep)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1383 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1384 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1385 | if (sep != NULL && sep != Py_None) { |
| 1386 | return do_xstrip(self, striptype, sep); |
| 1387 | } |
| 1388 | return do_strip(self, striptype); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1392 | PyDoc_STRVAR(strip__doc__, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1393 | "B.strip([bytes]) -> bytes\n\ |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1394 | \n\ |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 1395 | Strip leading and trailing bytes contained in the argument.\n\ |
Georg Brandl | beca27a | 2012-01-22 21:31:21 +0100 | [diff] [blame] | 1396 | If the argument is omitted, strip leading and trailing ASCII whitespace."); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1397 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1398 | bytes_strip(PyBytesObject *self, PyObject *args) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1399 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1400 | if (PyTuple_GET_SIZE(args) == 0) |
| 1401 | return do_strip(self, BOTHSTRIP); /* Common case */ |
| 1402 | else |
| 1403 | return do_argstrip(self, BOTHSTRIP, args); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1406 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1407 | PyDoc_STRVAR(lstrip__doc__, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1408 | "B.lstrip([bytes]) -> bytes\n\ |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1409 | \n\ |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 1410 | Strip leading bytes contained in the argument.\n\ |
| 1411 | If the argument is omitted, strip leading ASCII whitespace."); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1412 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1413 | bytes_lstrip(PyBytesObject *self, PyObject *args) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1414 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1415 | if (PyTuple_GET_SIZE(args) == 0) |
| 1416 | return do_strip(self, LEFTSTRIP); /* Common case */ |
| 1417 | else |
| 1418 | return do_argstrip(self, LEFTSTRIP, args); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1421 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1422 | PyDoc_STRVAR(rstrip__doc__, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1423 | "B.rstrip([bytes]) -> bytes\n\ |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1424 | \n\ |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 1425 | Strip trailing bytes contained in the argument.\n\ |
| 1426 | If the argument is omitted, strip trailing ASCII whitespace."); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1427 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1428 | bytes_rstrip(PyBytesObject *self, PyObject *args) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1429 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1430 | if (PyTuple_GET_SIZE(args) == 0) |
| 1431 | return do_strip(self, RIGHTSTRIP); /* Common case */ |
| 1432 | else |
| 1433 | return do_argstrip(self, RIGHTSTRIP, args); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1434 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1435 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1436 | |
| 1437 | PyDoc_STRVAR(count__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1438 | "B.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 1439 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1440 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1441 | string B[start:end]. Optional arguments start and end are interpreted\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1442 | as in slice notation."); |
| 1443 | |
| 1444 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1445 | bytes_count(PyBytesObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1446 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1447 | PyObject *sub_obj; |
| 1448 | const char *str = PyBytes_AS_STRING(self), *sub; |
| 1449 | Py_ssize_t sub_len; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1450 | char byte; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1451 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1452 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1453 | Py_buffer vsub; |
| 1454 | PyObject *count_obj; |
| 1455 | |
| 1456 | if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte, |
| 1457 | &start, &end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1458 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1459 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1460 | if (sub_obj) { |
| 1461 | if (_getbuffer(sub_obj, &vsub) < 0) |
| 1462 | return NULL; |
| 1463 | |
| 1464 | sub = vsub.buf; |
| 1465 | sub_len = vsub.len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1466 | } |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1467 | else { |
| 1468 | sub = &byte; |
| 1469 | sub_len = 1; |
| 1470 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1471 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1472 | ADJUST_INDICES(start, end, PyBytes_GET_SIZE(self)); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1473 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1474 | count_obj = PyLong_FromSsize_t( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX) |
| 1476 | ); |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1477 | |
| 1478 | if (sub_obj) |
| 1479 | PyBuffer_Release(&vsub); |
| 1480 | |
| 1481 | return count_obj; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | |
| 1485 | PyDoc_STRVAR(translate__doc__, |
| 1486 | "B.translate(table[, deletechars]) -> bytes\n\ |
| 1487 | \n\ |
| 1488 | Return a copy of B, where all characters occurring in the\n\ |
| 1489 | optional argument deletechars are removed, and the remaining\n\ |
| 1490 | characters have been mapped through the given translation\n\ |
| 1491 | table, which must be a bytes object of length 256."); |
| 1492 | |
| 1493 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1494 | bytes_translate(PyBytesObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1495 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1496 | char *input, *output; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1497 | const char *table; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1498 | Py_ssize_t i, c, changed = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1499 | PyObject *input_obj = (PyObject*)self; |
| 1500 | const char *output_start, *del_table=NULL; |
| 1501 | Py_ssize_t inlen, tablen, dellen = 0; |
| 1502 | PyObject *result; |
| 1503 | int trans_table[256]; |
| 1504 | PyObject *tableobj, *delobj = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1505 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1506 | if (!PyArg_UnpackTuple(args, "translate", 1, 2, |
| 1507 | &tableobj, &delobj)) |
| 1508 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1510 | if (PyBytes_Check(tableobj)) { |
| 1511 | table = PyBytes_AS_STRING(tableobj); |
| 1512 | tablen = PyBytes_GET_SIZE(tableobj); |
| 1513 | } |
| 1514 | else if (tableobj == Py_None) { |
| 1515 | table = NULL; |
| 1516 | tablen = 256; |
| 1517 | } |
| 1518 | else if (PyObject_AsCharBuffer(tableobj, &table, &tablen)) |
| 1519 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1520 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1521 | if (tablen != 256) { |
| 1522 | PyErr_SetString(PyExc_ValueError, |
| 1523 | "translation table must be 256 characters long"); |
| 1524 | return NULL; |
| 1525 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1526 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | if (delobj != NULL) { |
| 1528 | if (PyBytes_Check(delobj)) { |
| 1529 | del_table = PyBytes_AS_STRING(delobj); |
| 1530 | dellen = PyBytes_GET_SIZE(delobj); |
| 1531 | } |
| 1532 | else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen)) |
| 1533 | return NULL; |
| 1534 | } |
| 1535 | else { |
| 1536 | del_table = NULL; |
| 1537 | dellen = 0; |
| 1538 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1540 | inlen = PyBytes_GET_SIZE(input_obj); |
| 1541 | result = PyBytes_FromStringAndSize((char *)NULL, inlen); |
| 1542 | if (result == NULL) |
| 1543 | return NULL; |
| 1544 | output_start = output = PyBytes_AsString(result); |
| 1545 | input = PyBytes_AS_STRING(input_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1546 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1547 | if (dellen == 0 && table != NULL) { |
| 1548 | /* If no deletions are required, use faster code */ |
| 1549 | for (i = inlen; --i >= 0; ) { |
| 1550 | c = Py_CHARMASK(*input++); |
| 1551 | if (Py_CHARMASK((*output++ = table[c])) != c) |
| 1552 | changed = 1; |
| 1553 | } |
| 1554 | if (changed || !PyBytes_CheckExact(input_obj)) |
| 1555 | return result; |
| 1556 | Py_DECREF(result); |
| 1557 | Py_INCREF(input_obj); |
| 1558 | return input_obj; |
| 1559 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1561 | if (table == NULL) { |
| 1562 | for (i = 0; i < 256; i++) |
| 1563 | trans_table[i] = Py_CHARMASK(i); |
| 1564 | } else { |
| 1565 | for (i = 0; i < 256; i++) |
| 1566 | trans_table[i] = Py_CHARMASK(table[i]); |
| 1567 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1569 | for (i = 0; i < dellen; i++) |
| 1570 | trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1572 | for (i = inlen; --i >= 0; ) { |
| 1573 | c = Py_CHARMASK(*input++); |
| 1574 | if (trans_table[c] != -1) |
| 1575 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 1576 | continue; |
| 1577 | changed = 1; |
| 1578 | } |
| 1579 | if (!changed && PyBytes_CheckExact(input_obj)) { |
| 1580 | Py_DECREF(result); |
| 1581 | Py_INCREF(input_obj); |
| 1582 | return input_obj; |
| 1583 | } |
| 1584 | /* Fix the size of the resulting string */ |
| 1585 | if (inlen > 0) |
| 1586 | _PyBytes_Resize(&result, output - output_start); |
| 1587 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1591 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1592 | bytes_maketrans(PyObject *null, PyObject *args) |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1593 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1594 | return _Py_bytes_maketrans(args); |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1597 | /* find and count characters and substrings */ |
| 1598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1599 | #define findchar(target, target_len, c) \ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1600 | ((char *)memchr((const void *)(target), c, target_len)) |
| 1601 | |
| 1602 | /* String ops must return a string. */ |
| 1603 | /* If the object is subclass of string, create a copy */ |
| 1604 | Py_LOCAL(PyBytesObject *) |
| 1605 | return_self(PyBytesObject *self) |
| 1606 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1607 | if (PyBytes_CheckExact(self)) { |
| 1608 | Py_INCREF(self); |
| 1609 | return self; |
| 1610 | } |
| 1611 | return (PyBytesObject *)PyBytes_FromStringAndSize( |
| 1612 | PyBytes_AS_STRING(self), |
| 1613 | PyBytes_GET_SIZE(self)); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | Py_LOCAL_INLINE(Py_ssize_t) |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 1617 | countchar(const char *target, Py_ssize_t target_len, char c, Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1618 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | Py_ssize_t count=0; |
| 1620 | const char *start=target; |
| 1621 | const char *end=target+target_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1622 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1623 | while ( (start=findchar(start, end-start, c)) != NULL ) { |
| 1624 | count++; |
| 1625 | if (count >= maxcount) |
| 1626 | break; |
| 1627 | start += 1; |
| 1628 | } |
| 1629 | return count; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1632 | |
| 1633 | /* Algorithms for different cases of string replacement */ |
| 1634 | |
| 1635 | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
| 1636 | Py_LOCAL(PyBytesObject *) |
| 1637 | replace_interleave(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1638 | const char *to_s, Py_ssize_t to_len, |
| 1639 | Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1640 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1641 | char *self_s, *result_s; |
| 1642 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1643 | Py_ssize_t count, i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1644 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1646 | self_len = PyBytes_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1647 | |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1648 | /* 1 at the end plus 1 after every character; |
| 1649 | count = min(maxcount, self_len + 1) */ |
| 1650 | if (maxcount <= self_len) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1651 | count = maxcount; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1652 | else |
| 1653 | /* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */ |
| 1654 | count = self_len + 1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1655 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1656 | /* Check for overflow */ |
| 1657 | /* result_len = count * to_len + self_len; */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1658 | assert(count > 0); |
| 1659 | if (to_len > (PY_SSIZE_T_MAX - self_len) / count) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1660 | PyErr_SetString(PyExc_OverflowError, |
| 1661 | "replacement bytes are too long"); |
| 1662 | return NULL; |
| 1663 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1664 | result_len = count * to_len + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1666 | if (! (result = (PyBytesObject *) |
| 1667 | PyBytes_FromStringAndSize(NULL, result_len)) ) |
| 1668 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1669 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1670 | self_s = PyBytes_AS_STRING(self); |
| 1671 | result_s = PyBytes_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1673 | /* TODO: special case single character, which doesn't need memcpy */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1675 | /* Lay the first one down (guaranteed this will occur) */ |
| 1676 | Py_MEMCPY(result_s, to_s, to_len); |
| 1677 | result_s += to_len; |
| 1678 | count -= 1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1680 | for (i=0; i<count; i++) { |
| 1681 | *result_s++ = *self_s++; |
| 1682 | Py_MEMCPY(result_s, to_s, to_len); |
| 1683 | result_s += to_len; |
| 1684 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | /* Copy the rest of the original string */ |
| 1687 | Py_MEMCPY(result_s, self_s, self_len-i); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1688 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1689 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | /* Special case for deleting a single character */ |
| 1693 | /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
| 1694 | Py_LOCAL(PyBytesObject *) |
| 1695 | replace_delete_single_character(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1696 | char from_c, Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1697 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1698 | char *self_s, *result_s; |
| 1699 | char *start, *next, *end; |
| 1700 | Py_ssize_t self_len, result_len; |
| 1701 | Py_ssize_t count; |
| 1702 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1704 | self_len = PyBytes_GET_SIZE(self); |
| 1705 | self_s = PyBytes_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1706 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1707 | count = countchar(self_s, self_len, from_c, maxcount); |
| 1708 | if (count == 0) { |
| 1709 | return return_self(self); |
| 1710 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1711 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | result_len = self_len - count; /* from_len == 1 */ |
| 1713 | assert(result_len>=0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1714 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1715 | if ( (result = (PyBytesObject *) |
| 1716 | PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
| 1717 | return NULL; |
| 1718 | result_s = PyBytes_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1720 | start = self_s; |
| 1721 | end = self_s + self_len; |
| 1722 | while (count-- > 0) { |
| 1723 | next = findchar(start, end-start, from_c); |
| 1724 | if (next == NULL) |
| 1725 | break; |
| 1726 | Py_MEMCPY(result_s, start, next-start); |
| 1727 | result_s += (next-start); |
| 1728 | start = next+1; |
| 1729 | } |
| 1730 | Py_MEMCPY(result_s, start, end-start); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1731 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1732 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
| 1735 | /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
| 1736 | |
| 1737 | Py_LOCAL(PyBytesObject *) |
| 1738 | replace_delete_substring(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1739 | const char *from_s, Py_ssize_t from_len, |
| 1740 | Py_ssize_t maxcount) { |
| 1741 | char *self_s, *result_s; |
| 1742 | char *start, *next, *end; |
| 1743 | Py_ssize_t self_len, result_len; |
| 1744 | Py_ssize_t count, offset; |
| 1745 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1747 | self_len = PyBytes_GET_SIZE(self); |
| 1748 | self_s = PyBytes_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1749 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 | count = stringlib_count(self_s, self_len, |
| 1751 | from_s, from_len, |
| 1752 | maxcount); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1754 | if (count == 0) { |
| 1755 | /* no matches */ |
| 1756 | return return_self(self); |
| 1757 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1759 | result_len = self_len - (count * from_len); |
| 1760 | assert (result_len>=0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1762 | if ( (result = (PyBytesObject *) |
| 1763 | PyBytes_FromStringAndSize(NULL, result_len)) == NULL ) |
| 1764 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1766 | result_s = PyBytes_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1767 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1768 | start = self_s; |
| 1769 | end = self_s + self_len; |
| 1770 | while (count-- > 0) { |
| 1771 | offset = stringlib_find(start, end-start, |
| 1772 | from_s, from_len, |
| 1773 | 0); |
| 1774 | if (offset == -1) |
| 1775 | break; |
| 1776 | next = start + offset; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1778 | Py_MEMCPY(result_s, start, next-start); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1779 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1780 | result_s += (next-start); |
| 1781 | start = next+from_len; |
| 1782 | } |
| 1783 | Py_MEMCPY(result_s, start, end-start); |
| 1784 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
| 1788 | Py_LOCAL(PyBytesObject *) |
| 1789 | replace_single_character_in_place(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | char from_c, char to_c, |
| 1791 | Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1792 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1793 | char *self_s, *result_s, *start, *end, *next; |
| 1794 | Py_ssize_t self_len; |
| 1795 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1797 | /* The result string will be the same size */ |
| 1798 | self_s = PyBytes_AS_STRING(self); |
| 1799 | self_len = PyBytes_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1800 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1801 | next = findchar(self_s, self_len, from_c); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1803 | if (next == NULL) { |
| 1804 | /* No matches; return the original string */ |
| 1805 | return return_self(self); |
| 1806 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | /* Need to make a new string */ |
| 1809 | result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len); |
| 1810 | if (result == NULL) |
| 1811 | return NULL; |
| 1812 | result_s = PyBytes_AS_STRING(result); |
| 1813 | Py_MEMCPY(result_s, self_s, self_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1814 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1815 | /* change everything in-place, starting with this one */ |
| 1816 | start = result_s + (next-self_s); |
| 1817 | *start = to_c; |
| 1818 | start++; |
| 1819 | end = result_s + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1820 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1821 | while (--maxcount > 0) { |
| 1822 | next = findchar(start, end-start, from_c); |
| 1823 | if (next == NULL) |
| 1824 | break; |
| 1825 | *next = to_c; |
| 1826 | start = next+1; |
| 1827 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1829 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
| 1833 | Py_LOCAL(PyBytesObject *) |
| 1834 | replace_substring_in_place(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1835 | const char *from_s, Py_ssize_t from_len, |
| 1836 | const char *to_s, Py_ssize_t to_len, |
| 1837 | Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1838 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1839 | char *result_s, *start, *end; |
| 1840 | char *self_s; |
| 1841 | Py_ssize_t self_len, offset; |
| 1842 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1843 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1844 | /* The result string will be the same size */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1846 | self_s = PyBytes_AS_STRING(self); |
| 1847 | self_len = PyBytes_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1848 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1849 | offset = stringlib_find(self_s, self_len, |
| 1850 | from_s, from_len, |
| 1851 | 0); |
| 1852 | if (offset == -1) { |
| 1853 | /* No matches; return the original string */ |
| 1854 | return return_self(self); |
| 1855 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1856 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1857 | /* Need to make a new string */ |
| 1858 | result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len); |
| 1859 | if (result == NULL) |
| 1860 | return NULL; |
| 1861 | result_s = PyBytes_AS_STRING(result); |
| 1862 | Py_MEMCPY(result_s, self_s, self_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1863 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1864 | /* change everything in-place, starting with this one */ |
| 1865 | start = result_s + offset; |
| 1866 | Py_MEMCPY(start, to_s, from_len); |
| 1867 | start += from_len; |
| 1868 | end = result_s + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1870 | while ( --maxcount > 0) { |
| 1871 | offset = stringlib_find(start, end-start, |
| 1872 | from_s, from_len, |
| 1873 | 0); |
| 1874 | if (offset==-1) |
| 1875 | break; |
| 1876 | Py_MEMCPY(start+offset, to_s, from_len); |
| 1877 | start += offset+from_len; |
| 1878 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1880 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
| 1884 | Py_LOCAL(PyBytesObject *) |
| 1885 | replace_single_character(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1886 | char from_c, |
| 1887 | const char *to_s, Py_ssize_t to_len, |
| 1888 | Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1889 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1890 | char *self_s, *result_s; |
| 1891 | char *start, *next, *end; |
| 1892 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1893 | Py_ssize_t count; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1894 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1895 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1896 | self_s = PyBytes_AS_STRING(self); |
| 1897 | self_len = PyBytes_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1899 | count = countchar(self_s, self_len, from_c, maxcount); |
| 1900 | if (count == 0) { |
| 1901 | /* no matches, return unchanged */ |
| 1902 | return return_self(self); |
| 1903 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1904 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1905 | /* use the difference between current and new, hence the "-1" */ |
| 1906 | /* result_len = self_len + count * (to_len-1) */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1907 | assert(count > 0); |
| 1908 | if (to_len - 1 > (PY_SSIZE_T_MAX - self_len) / count) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | PyErr_SetString(PyExc_OverflowError, |
| 1910 | "replacement bytes are too long"); |
| 1911 | return NULL; |
| 1912 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1913 | result_len = self_len + count * (to_len - 1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1915 | if ( (result = (PyBytesObject *) |
| 1916 | PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
| 1917 | return NULL; |
| 1918 | result_s = PyBytes_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1919 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1920 | start = self_s; |
| 1921 | end = self_s + self_len; |
| 1922 | while (count-- > 0) { |
| 1923 | next = findchar(start, end-start, from_c); |
| 1924 | if (next == NULL) |
| 1925 | break; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1926 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1927 | if (next == start) { |
| 1928 | /* replace with the 'to' */ |
| 1929 | Py_MEMCPY(result_s, to_s, to_len); |
| 1930 | result_s += to_len; |
| 1931 | start += 1; |
| 1932 | } else { |
| 1933 | /* copy the unchanged old then the 'to' */ |
| 1934 | Py_MEMCPY(result_s, start, next-start); |
| 1935 | result_s += (next-start); |
| 1936 | Py_MEMCPY(result_s, to_s, to_len); |
| 1937 | result_s += to_len; |
| 1938 | start = next+1; |
| 1939 | } |
| 1940 | } |
| 1941 | /* Copy the remainder of the remaining string */ |
| 1942 | Py_MEMCPY(result_s, start, end-start); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1944 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
| 1948 | Py_LOCAL(PyBytesObject *) |
| 1949 | replace_substring(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1950 | const char *from_s, Py_ssize_t from_len, |
| 1951 | const char *to_s, Py_ssize_t to_len, |
| 1952 | Py_ssize_t maxcount) { |
| 1953 | char *self_s, *result_s; |
| 1954 | char *start, *next, *end; |
| 1955 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1956 | Py_ssize_t count, offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1957 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1959 | self_s = PyBytes_AS_STRING(self); |
| 1960 | self_len = PyBytes_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1962 | count = stringlib_count(self_s, self_len, |
| 1963 | from_s, from_len, |
| 1964 | maxcount); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1966 | if (count == 0) { |
| 1967 | /* no matches, return unchanged */ |
| 1968 | return return_self(self); |
| 1969 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1971 | /* Check for overflow */ |
| 1972 | /* result_len = self_len + count * (to_len-from_len) */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1973 | assert(count > 0); |
| 1974 | if (to_len - from_len > (PY_SSIZE_T_MAX - self_len) / count) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1975 | PyErr_SetString(PyExc_OverflowError, |
| 1976 | "replacement bytes are too long"); |
| 1977 | return NULL; |
| 1978 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1979 | result_len = self_len + count * (to_len-from_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1980 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1981 | if ( (result = (PyBytesObject *) |
| 1982 | PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
| 1983 | return NULL; |
| 1984 | result_s = PyBytes_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1986 | start = self_s; |
| 1987 | end = self_s + self_len; |
| 1988 | while (count-- > 0) { |
| 1989 | offset = stringlib_find(start, end-start, |
| 1990 | from_s, from_len, |
| 1991 | 0); |
| 1992 | if (offset == -1) |
| 1993 | break; |
| 1994 | next = start+offset; |
| 1995 | if (next == start) { |
| 1996 | /* replace with the 'to' */ |
| 1997 | Py_MEMCPY(result_s, to_s, to_len); |
| 1998 | result_s += to_len; |
| 1999 | start += from_len; |
| 2000 | } else { |
| 2001 | /* copy the unchanged old then the 'to' */ |
| 2002 | Py_MEMCPY(result_s, start, next-start); |
| 2003 | result_s += (next-start); |
| 2004 | Py_MEMCPY(result_s, to_s, to_len); |
| 2005 | result_s += to_len; |
| 2006 | start = next+from_len; |
| 2007 | } |
| 2008 | } |
| 2009 | /* Copy the remainder of the remaining string */ |
| 2010 | Py_MEMCPY(result_s, start, end-start); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2011 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2012 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | |
| 2016 | Py_LOCAL(PyBytesObject *) |
| 2017 | replace(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2018 | const char *from_s, Py_ssize_t from_len, |
| 2019 | const char *to_s, Py_ssize_t to_len, |
| 2020 | Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2021 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2022 | if (maxcount < 0) { |
| 2023 | maxcount = PY_SSIZE_T_MAX; |
| 2024 | } else if (maxcount == 0 || PyBytes_GET_SIZE(self) == 0) { |
| 2025 | /* nothing to do; return the original string */ |
| 2026 | return return_self(self); |
| 2027 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2029 | if (maxcount == 0 || |
| 2030 | (from_len == 0 && to_len == 0)) { |
| 2031 | /* nothing to do; return the original string */ |
| 2032 | return return_self(self); |
| 2033 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 | /* Handle zero-length special cases */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2036 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2037 | if (from_len == 0) { |
| 2038 | /* insert the 'to' string everywhere. */ |
| 2039 | /* >>> "Python".replace("", ".") */ |
| 2040 | /* '.P.y.t.h.o.n.' */ |
| 2041 | return replace_interleave(self, to_s, to_len, maxcount); |
| 2042 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2043 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2044 | /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
| 2045 | /* point for an empty self string to generate a non-empty string */ |
| 2046 | /* Special case so the remaining code always gets a non-empty string */ |
| 2047 | if (PyBytes_GET_SIZE(self) == 0) { |
| 2048 | return return_self(self); |
| 2049 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2051 | if (to_len == 0) { |
| 2052 | /* delete all occurrences of 'from' string */ |
| 2053 | if (from_len == 1) { |
| 2054 | return replace_delete_single_character( |
| 2055 | self, from_s[0], maxcount); |
| 2056 | } else { |
| 2057 | return replace_delete_substring(self, from_s, |
| 2058 | from_len, maxcount); |
| 2059 | } |
| 2060 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2062 | /* Handle special case where both strings have the same length */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2064 | if (from_len == to_len) { |
| 2065 | if (from_len == 1) { |
| 2066 | return replace_single_character_in_place( |
| 2067 | self, |
| 2068 | from_s[0], |
| 2069 | to_s[0], |
| 2070 | maxcount); |
| 2071 | } else { |
| 2072 | return replace_substring_in_place( |
| 2073 | self, from_s, from_len, to_s, to_len, |
| 2074 | maxcount); |
| 2075 | } |
| 2076 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2078 | /* Otherwise use the more generic algorithms */ |
| 2079 | if (from_len == 1) { |
| 2080 | return replace_single_character(self, from_s[0], |
| 2081 | to_s, to_len, maxcount); |
| 2082 | } else { |
| 2083 | /* len('from')>=2, len('to')>=1 */ |
| 2084 | return replace_substring(self, from_s, from_len, to_s, to_len, |
| 2085 | maxcount); |
| 2086 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2087 | } |
| 2088 | |
| 2089 | PyDoc_STRVAR(replace__doc__, |
| 2090 | "B.replace(old, new[, count]) -> bytes\n\ |
| 2091 | \n\ |
| 2092 | Return a copy of B with all occurrences of subsection\n\ |
| 2093 | old replaced by new. If the optional argument count is\n\ |
Senthil Kumaran | 9a9dd1c | 2010-09-08 12:50:29 +0000 | [diff] [blame] | 2094 | given, only first count occurances are replaced."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2095 | |
| 2096 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2097 | bytes_replace(PyBytesObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2098 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2099 | Py_ssize_t count = -1; |
| 2100 | PyObject *from, *to; |
| 2101 | const char *from_s, *to_s; |
| 2102 | Py_ssize_t from_len, to_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2104 | if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count)) |
| 2105 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2106 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2107 | if (PyBytes_Check(from)) { |
| 2108 | from_s = PyBytes_AS_STRING(from); |
| 2109 | from_len = PyBytes_GET_SIZE(from); |
| 2110 | } |
| 2111 | else if (PyObject_AsCharBuffer(from, &from_s, &from_len)) |
| 2112 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2114 | if (PyBytes_Check(to)) { |
| 2115 | to_s = PyBytes_AS_STRING(to); |
| 2116 | to_len = PyBytes_GET_SIZE(to); |
| 2117 | } |
| 2118 | else if (PyObject_AsCharBuffer(to, &to_s, &to_len)) |
| 2119 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2121 | return (PyObject *)replace((PyBytesObject *) self, |
| 2122 | from_s, from_len, |
| 2123 | to_s, to_len, count); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
| 2126 | /** End DALKE **/ |
| 2127 | |
| 2128 | /* Matches the end (direction >= 0) or start (direction < 0) of self |
| 2129 | * against substr, using the start and end arguments. Returns |
| 2130 | * -1 on error, 0 if not found and 1 if found. |
| 2131 | */ |
| 2132 | Py_LOCAL(int) |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2133 | _bytes_tailmatch(PyBytesObject *self, PyObject *substr, Py_ssize_t start, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | Py_ssize_t end, int direction) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2135 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2136 | Py_ssize_t len = PyBytes_GET_SIZE(self); |
| 2137 | Py_ssize_t slen; |
| 2138 | const char* sub; |
| 2139 | const char* str; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2141 | if (PyBytes_Check(substr)) { |
| 2142 | sub = PyBytes_AS_STRING(substr); |
| 2143 | slen = PyBytes_GET_SIZE(substr); |
| 2144 | } |
| 2145 | else if (PyObject_AsCharBuffer(substr, &sub, &slen)) |
| 2146 | return -1; |
| 2147 | str = PyBytes_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2148 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2149 | ADJUST_INDICES(start, end, len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2150 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2151 | if (direction < 0) { |
| 2152 | /* startswith */ |
| 2153 | if (start+slen > len) |
| 2154 | return 0; |
| 2155 | } else { |
| 2156 | /* endswith */ |
| 2157 | if (end-start < slen || start > len) |
| 2158 | return 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2159 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2160 | if (end-slen > start) |
| 2161 | start = end - slen; |
| 2162 | } |
| 2163 | if (end-start >= slen) |
| 2164 | return ! memcmp(str+start, sub, slen); |
| 2165 | return 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | |
| 2169 | PyDoc_STRVAR(startswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2170 | "B.startswith(prefix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2171 | \n\ |
| 2172 | Return True if B starts with the specified prefix, False otherwise.\n\ |
| 2173 | With optional start, test B beginning at that position.\n\ |
| 2174 | With optional end, stop comparing B at that position.\n\ |
Benjamin Peterson | 4116f36 | 2008-05-27 00:36:20 +0000 | [diff] [blame] | 2175 | prefix can also be a tuple of bytes to try."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2176 | |
| 2177 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2178 | bytes_startswith(PyBytesObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2179 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2180 | Py_ssize_t start = 0; |
| 2181 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 2182 | PyObject *subobj; |
| 2183 | int result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2184 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 2185 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2186 | return NULL; |
| 2187 | if (PyTuple_Check(subobj)) { |
| 2188 | Py_ssize_t i; |
| 2189 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 2190 | result = _bytes_tailmatch(self, |
| 2191 | PyTuple_GET_ITEM(subobj, i), |
| 2192 | start, end, -1); |
| 2193 | if (result == -1) |
| 2194 | return NULL; |
| 2195 | else if (result) { |
| 2196 | Py_RETURN_TRUE; |
| 2197 | } |
| 2198 | } |
| 2199 | Py_RETURN_FALSE; |
| 2200 | } |
| 2201 | result = _bytes_tailmatch(self, subobj, start, end, -1); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 2202 | if (result == -1) { |
| 2203 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 2204 | PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes " |
| 2205 | "or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2206 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 2207 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2208 | else |
| 2209 | return PyBool_FromLong(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | |
| 2213 | PyDoc_STRVAR(endswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2214 | "B.endswith(suffix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2215 | \n\ |
| 2216 | Return True if B ends with the specified suffix, False otherwise.\n\ |
| 2217 | With optional start, test B beginning at that position.\n\ |
| 2218 | With optional end, stop comparing B at that position.\n\ |
Benjamin Peterson | 4116f36 | 2008-05-27 00:36:20 +0000 | [diff] [blame] | 2219 | suffix can also be a tuple of bytes to try."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2220 | |
| 2221 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2222 | bytes_endswith(PyBytesObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2223 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2224 | Py_ssize_t start = 0; |
| 2225 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 2226 | PyObject *subobj; |
| 2227 | int result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2228 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 2229 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2230 | return NULL; |
| 2231 | if (PyTuple_Check(subobj)) { |
| 2232 | Py_ssize_t i; |
| 2233 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 2234 | result = _bytes_tailmatch(self, |
| 2235 | PyTuple_GET_ITEM(subobj, i), |
| 2236 | start, end, +1); |
| 2237 | if (result == -1) |
| 2238 | return NULL; |
| 2239 | else if (result) { |
| 2240 | Py_RETURN_TRUE; |
| 2241 | } |
| 2242 | } |
| 2243 | Py_RETURN_FALSE; |
| 2244 | } |
| 2245 | result = _bytes_tailmatch(self, subobj, start, end, +1); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 2246 | if (result == -1) { |
| 2247 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 2248 | PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or " |
| 2249 | "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2250 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 2251 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2252 | else |
| 2253 | return PyBool_FromLong(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | |
| 2257 | PyDoc_STRVAR(decode__doc__, |
Victor Stinner | c911bbf | 2010-11-07 19:04:46 +0000 | [diff] [blame] | 2258 | "B.decode(encoding='utf-8', errors='strict') -> str\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2259 | \n\ |
Victor Stinner | e14e212 | 2010-11-07 18:41:46 +0000 | [diff] [blame] | 2260 | Decode B using the codec registered for encoding. Default encoding\n\ |
| 2261 | is 'utf-8'. errors may be given to set a different error\n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2262 | handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
| 2263 | a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2264 | as well as any other name registerd with codecs.register_error that is\n\ |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 2265 | able to handle UnicodeDecodeErrors."); |
| 2266 | |
| 2267 | static PyObject * |
Benjamin Peterson | 308d637 | 2009-09-18 21:42:35 +0000 | [diff] [blame] | 2268 | bytes_decode(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6f1fdc | 2007-04-12 22:49:52 +0000 | [diff] [blame] | 2269 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2270 | const char *encoding = NULL; |
| 2271 | const char *errors = NULL; |
| 2272 | static char *kwlist[] = {"encoding", "errors", 0}; |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 2273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2274 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", kwlist, &encoding, &errors)) |
| 2275 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2276 | return PyUnicode_FromEncodedObject(self, encoding, errors); |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 2279 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2280 | PyDoc_STRVAR(splitlines__doc__, |
| 2281 | "B.splitlines([keepends]) -> list of lines\n\ |
| 2282 | \n\ |
| 2283 | Return a list of the lines in B, breaking at line boundaries.\n\ |
| 2284 | Line breaks are not included in the resulting list unless keepends\n\ |
| 2285 | is given and true."); |
| 2286 | |
| 2287 | static PyObject* |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 2288 | bytes_splitlines(PyObject *self, PyObject *args, PyObject *kwds) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2289 | { |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 2290 | static char *kwlist[] = {"keepends", 0}; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2291 | int keepends = 0; |
| 2292 | |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 2293 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", |
| 2294 | kwlist, &keepends)) |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 2295 | return NULL; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2296 | |
| 2297 | return stringlib_splitlines( |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 2298 | (PyObject*) self, PyBytes_AS_STRING(self), |
| 2299 | PyBytes_GET_SIZE(self), keepends |
| 2300 | ); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
| 2303 | |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2304 | PyDoc_STRVAR(fromhex_doc, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2305 | "bytes.fromhex(string) -> bytes\n\ |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2306 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2307 | Create a bytes object from a string of hexadecimal numbers.\n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2308 | Spaces between two numbers are accepted.\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2309 | Example: bytes.fromhex('B9 01EF') -> b'\\xb9\\x01\\xef'."); |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2310 | |
| 2311 | static int |
Victor Stinner | 6430fd5 | 2011-09-29 04:02:13 +0200 | [diff] [blame] | 2312 | hex_digit_to_int(Py_UCS4 c) |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2313 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2314 | if (c >= 128) |
| 2315 | return -1; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2316 | if (Py_ISDIGIT(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2317 | return c - '0'; |
| 2318 | else { |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2319 | if (Py_ISUPPER(c)) |
| 2320 | c = Py_TOLOWER(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2321 | if (c >= 'a' && c <= 'f') |
| 2322 | return c - 'a' + 10; |
| 2323 | } |
| 2324 | return -1; |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2328 | bytes_fromhex(PyObject *cls, PyObject *args) |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2329 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2330 | PyObject *newstring, *hexobj; |
| 2331 | char *buf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2332 | Py_ssize_t hexlen, byteslen, i, j; |
| 2333 | int top, bot; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2334 | void *data; |
| 2335 | unsigned int kind; |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2337 | if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) |
| 2338 | return NULL; |
| 2339 | assert(PyUnicode_Check(hexobj)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2340 | if (PyUnicode_READY(hexobj)) |
| 2341 | return NULL; |
| 2342 | kind = PyUnicode_KIND(hexobj); |
| 2343 | data = PyUnicode_DATA(hexobj); |
| 2344 | hexlen = PyUnicode_GET_LENGTH(hexobj); |
| 2345 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2346 | byteslen = hexlen/2; /* This overestimates if there are spaces */ |
| 2347 | newstring = PyBytes_FromStringAndSize(NULL, byteslen); |
| 2348 | if (!newstring) |
| 2349 | return NULL; |
| 2350 | buf = PyBytes_AS_STRING(newstring); |
| 2351 | for (i = j = 0; i < hexlen; i += 2) { |
| 2352 | /* skip over spaces in the input */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2353 | while (PyUnicode_READ(kind, data, i) == ' ') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2354 | i++; |
| 2355 | if (i >= hexlen) |
| 2356 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 2357 | top = hex_digit_to_int(PyUnicode_READ(kind, data, i)); |
| 2358 | bot = hex_digit_to_int(PyUnicode_READ(kind, data, i+1)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2359 | if (top == -1 || bot == -1) { |
| 2360 | PyErr_Format(PyExc_ValueError, |
| 2361 | "non-hexadecimal number found in " |
| 2362 | "fromhex() arg at position %zd", i); |
| 2363 | goto error; |
| 2364 | } |
| 2365 | buf[j++] = (top << 4) + bot; |
| 2366 | } |
| 2367 | if (j != byteslen && _PyBytes_Resize(&newstring, j) < 0) |
| 2368 | goto error; |
| 2369 | return newstring; |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2370 | |
| 2371 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2372 | Py_XDECREF(newstring); |
| 2373 | return NULL; |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 2374 | } |
| 2375 | |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2376 | PyDoc_STRVAR(sizeof__doc__, |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2377 | "B.__sizeof__() -> size of B in memory, in bytes"); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2378 | |
| 2379 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2380 | bytes_sizeof(PyBytesObject *v) |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2381 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2382 | Py_ssize_t res; |
| 2383 | res = PyBytesObject_SIZE + Py_SIZE(v) * Py_TYPE(v)->tp_itemsize; |
| 2384 | return PyLong_FromSsize_t(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
Guido van Rossum | 0dd32e2 | 2007-04-11 05:40:58 +0000 | [diff] [blame] | 2387 | |
| 2388 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2389 | bytes_getnewargs(PyBytesObject *v) |
Guido van Rossum | 0dd32e2 | 2007-04-11 05:40:58 +0000 | [diff] [blame] | 2390 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2391 | return Py_BuildValue("(y#)", v->ob_sval, Py_SIZE(v)); |
Guido van Rossum | 0dd32e2 | 2007-04-11 05:40:58 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2394 | |
| 2395 | static PyMethodDef |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2396 | bytes_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2397 | {"__getnewargs__", (PyCFunction)bytes_getnewargs, METH_NOARGS}, |
| 2398 | {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
| 2399 | _Py_capitalize__doc__}, |
| 2400 | {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, |
| 2401 | {"count", (PyCFunction)bytes_count, METH_VARARGS, count__doc__}, |
| 2402 | {"decode", (PyCFunction)bytes_decode, METH_VARARGS | METH_KEYWORDS, decode__doc__}, |
| 2403 | {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS, |
| 2404 | endswith__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 2405 | {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2406 | expandtabs__doc__}, |
| 2407 | {"find", (PyCFunction)bytes_find, METH_VARARGS, find__doc__}, |
| 2408 | {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS, |
| 2409 | fromhex_doc}, |
| 2410 | {"index", (PyCFunction)bytes_index, METH_VARARGS, index__doc__}, |
| 2411 | {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
| 2412 | _Py_isalnum__doc__}, |
| 2413 | {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
| 2414 | _Py_isalpha__doc__}, |
| 2415 | {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
| 2416 | _Py_isdigit__doc__}, |
| 2417 | {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
| 2418 | _Py_islower__doc__}, |
| 2419 | {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
| 2420 | _Py_isspace__doc__}, |
| 2421 | {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
| 2422 | _Py_istitle__doc__}, |
| 2423 | {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
| 2424 | _Py_isupper__doc__}, |
| 2425 | {"join", (PyCFunction)bytes_join, METH_O, join__doc__}, |
| 2426 | {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, |
| 2427 | {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
| 2428 | {"lstrip", (PyCFunction)bytes_lstrip, METH_VARARGS, lstrip__doc__}, |
| 2429 | {"maketrans", (PyCFunction)bytes_maketrans, METH_VARARGS|METH_STATIC, |
| 2430 | _Py_maketrans__doc__}, |
| 2431 | {"partition", (PyCFunction)bytes_partition, METH_O, partition__doc__}, |
| 2432 | {"replace", (PyCFunction)bytes_replace, METH_VARARGS, replace__doc__}, |
| 2433 | {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, rfind__doc__}, |
| 2434 | {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, rindex__doc__}, |
| 2435 | {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, |
| 2436 | {"rpartition", (PyCFunction)bytes_rpartition, METH_O, |
| 2437 | rpartition__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 2438 | {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS | METH_KEYWORDS, rsplit__doc__}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2439 | {"rstrip", (PyCFunction)bytes_rstrip, METH_VARARGS, rstrip__doc__}, |
Ezio Melotti | cda6b6d | 2012-02-26 09:39:55 +0200 | [diff] [blame] | 2440 | {"split", (PyCFunction)bytes_split, METH_VARARGS | METH_KEYWORDS, split__doc__}, |
Mark Dickinson | 0d5f6ad | 2011-09-24 09:14:39 +0100 | [diff] [blame] | 2441 | {"splitlines", (PyCFunction)bytes_splitlines, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2442 | splitlines__doc__}, |
| 2443 | {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS, |
| 2444 | startswith__doc__}, |
| 2445 | {"strip", (PyCFunction)bytes_strip, METH_VARARGS, strip__doc__}, |
| 2446 | {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
| 2447 | _Py_swapcase__doc__}, |
| 2448 | {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
| 2449 | {"translate", (PyCFunction)bytes_translate, METH_VARARGS, |
| 2450 | translate__doc__}, |
| 2451 | {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
| 2452 | {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, |
| 2453 | {"__sizeof__", (PyCFunction)bytes_sizeof, METH_NOARGS, |
| 2454 | sizeof__doc__}, |
| 2455 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2456 | }; |
| 2457 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2458 | static PyObject * |
| 2459 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 2460 | |
| 2461 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2462 | bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2463 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2464 | PyObject *x = NULL; |
| 2465 | const char *encoding = NULL; |
| 2466 | const char *errors = NULL; |
| 2467 | PyObject *new = NULL; |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 2468 | PyObject *func; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2469 | Py_ssize_t size; |
| 2470 | static char *kwlist[] = {"source", "encoding", "errors", 0}; |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 2471 | _Py_IDENTIFIER(__bytes__); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2473 | if (type != &PyBytes_Type) |
| 2474 | return str_subtype_new(type, args, kwds); |
| 2475 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytes", kwlist, &x, |
| 2476 | &encoding, &errors)) |
| 2477 | return NULL; |
| 2478 | if (x == NULL) { |
| 2479 | if (encoding != NULL || errors != NULL) { |
| 2480 | PyErr_SetString(PyExc_TypeError, |
| 2481 | "encoding or errors without sequence " |
| 2482 | "argument"); |
| 2483 | return NULL; |
| 2484 | } |
| 2485 | return PyBytes_FromString(""); |
| 2486 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2488 | if (PyUnicode_Check(x)) { |
| 2489 | /* Encode via the codec registry */ |
| 2490 | if (encoding == NULL) { |
| 2491 | PyErr_SetString(PyExc_TypeError, |
| 2492 | "string argument without an encoding"); |
| 2493 | return NULL; |
| 2494 | } |
| 2495 | new = PyUnicode_AsEncodedString(x, encoding, errors); |
| 2496 | if (new == NULL) |
| 2497 | return NULL; |
| 2498 | assert(PyBytes_Check(new)); |
| 2499 | return new; |
| 2500 | } |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 2501 | |
| 2502 | /* We'd like to call PyObject_Bytes here, but we need to check for an |
| 2503 | integer argument before deferring to PyBytes_FromObject, something |
| 2504 | PyObject_Bytes doesn't do. */ |
| 2505 | func = _PyObject_LookupSpecial(x, &PyId___bytes__); |
| 2506 | if (func != NULL) { |
| 2507 | new = PyObject_CallFunctionObjArgs(func, NULL); |
| 2508 | Py_DECREF(func); |
| 2509 | if (new == NULL) |
| 2510 | return NULL; |
| 2511 | if (!PyBytes_Check(new)) { |
| 2512 | PyErr_Format(PyExc_TypeError, |
| 2513 | "__bytes__ returned non-bytes (type %.200s)", |
| 2514 | Py_TYPE(new)->tp_name); |
| 2515 | Py_DECREF(new); |
| 2516 | return NULL; |
| 2517 | } |
| 2518 | return new; |
| 2519 | } |
| 2520 | else if (PyErr_Occurred()) |
| 2521 | return NULL; |
| 2522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2523 | /* Is it an integer? */ |
| 2524 | size = PyNumber_AsSsize_t(x, PyExc_OverflowError); |
| 2525 | if (size == -1 && PyErr_Occurred()) { |
| 2526 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 2527 | return NULL; |
| 2528 | PyErr_Clear(); |
| 2529 | } |
| 2530 | else if (size < 0) { |
| 2531 | PyErr_SetString(PyExc_ValueError, "negative count"); |
| 2532 | return NULL; |
| 2533 | } |
| 2534 | else { |
| 2535 | new = PyBytes_FromStringAndSize(NULL, size); |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 2536 | if (new == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2537 | return NULL; |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 2538 | if (size > 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2539 | memset(((PyBytesObject*)new)->ob_sval, 0, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2540 | return new; |
| 2541 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2542 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2543 | /* If it's not unicode, there can't be encoding or errors */ |
| 2544 | if (encoding != NULL || errors != NULL) { |
| 2545 | PyErr_SetString(PyExc_TypeError, |
| 2546 | "encoding or errors without a string argument"); |
| 2547 | return NULL; |
| 2548 | } |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 2549 | |
| 2550 | return PyBytes_FromObject(x); |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | PyObject * |
| 2554 | PyBytes_FromObject(PyObject *x) |
| 2555 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2556 | PyObject *new, *it; |
| 2557 | Py_ssize_t i, size; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2559 | if (x == NULL) { |
| 2560 | PyErr_BadInternalCall(); |
| 2561 | return NULL; |
| 2562 | } |
Larry Hastings | ca28e99 | 2012-05-24 22:58:30 -0700 | [diff] [blame] | 2563 | |
| 2564 | if (PyBytes_CheckExact(x)) { |
| 2565 | Py_INCREF(x); |
| 2566 | return x; |
| 2567 | } |
| 2568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2569 | /* Use the modern buffer interface */ |
| 2570 | if (PyObject_CheckBuffer(x)) { |
| 2571 | Py_buffer view; |
| 2572 | if (PyObject_GetBuffer(x, &view, PyBUF_FULL_RO) < 0) |
| 2573 | return NULL; |
| 2574 | new = PyBytes_FromStringAndSize(NULL, view.len); |
| 2575 | if (!new) |
| 2576 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2577 | if (PyBuffer_ToContiguous(((PyBytesObject *)new)->ob_sval, |
| 2578 | &view, view.len, 'C') < 0) |
| 2579 | goto fail; |
| 2580 | PyBuffer_Release(&view); |
| 2581 | return new; |
| 2582 | fail: |
| 2583 | Py_XDECREF(new); |
| 2584 | PyBuffer_Release(&view); |
| 2585 | return NULL; |
| 2586 | } |
| 2587 | if (PyUnicode_Check(x)) { |
| 2588 | PyErr_SetString(PyExc_TypeError, |
| 2589 | "cannot convert unicode object to bytes"); |
| 2590 | return NULL; |
| 2591 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2592 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2593 | if (PyList_CheckExact(x)) { |
| 2594 | new = PyBytes_FromStringAndSize(NULL, Py_SIZE(x)); |
| 2595 | if (new == NULL) |
| 2596 | return NULL; |
| 2597 | for (i = 0; i < Py_SIZE(x); i++) { |
| 2598 | Py_ssize_t value = PyNumber_AsSsize_t( |
| 2599 | PyList_GET_ITEM(x, i), PyExc_ValueError); |
| 2600 | if (value == -1 && PyErr_Occurred()) { |
| 2601 | Py_DECREF(new); |
| 2602 | return NULL; |
| 2603 | } |
| 2604 | if (value < 0 || value >= 256) { |
| 2605 | PyErr_SetString(PyExc_ValueError, |
| 2606 | "bytes must be in range(0, 256)"); |
| 2607 | Py_DECREF(new); |
| 2608 | return NULL; |
| 2609 | } |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 2610 | ((PyBytesObject *)new)->ob_sval[i] = (char) value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2611 | } |
| 2612 | return new; |
| 2613 | } |
| 2614 | if (PyTuple_CheckExact(x)) { |
| 2615 | new = PyBytes_FromStringAndSize(NULL, Py_SIZE(x)); |
| 2616 | if (new == NULL) |
| 2617 | return NULL; |
| 2618 | for (i = 0; i < Py_SIZE(x); i++) { |
| 2619 | Py_ssize_t value = PyNumber_AsSsize_t( |
| 2620 | PyTuple_GET_ITEM(x, i), PyExc_ValueError); |
| 2621 | if (value == -1 && PyErr_Occurred()) { |
| 2622 | Py_DECREF(new); |
| 2623 | return NULL; |
| 2624 | } |
| 2625 | if (value < 0 || value >= 256) { |
| 2626 | PyErr_SetString(PyExc_ValueError, |
| 2627 | "bytes must be in range(0, 256)"); |
| 2628 | Py_DECREF(new); |
| 2629 | return NULL; |
| 2630 | } |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 2631 | ((PyBytesObject *)new)->ob_sval[i] = (char) value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2632 | } |
| 2633 | return new; |
| 2634 | } |
Alexandre Vassalotti | a5c565a | 2010-01-09 22:14:46 +0000 | [diff] [blame] | 2635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2636 | /* For iterator version, create a string object and resize as needed */ |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 2637 | size = PyObject_LengthHint(x, 64); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2638 | if (size == -1 && PyErr_Occurred()) |
| 2639 | return NULL; |
| 2640 | /* Allocate an extra byte to prevent PyBytes_FromStringAndSize() from |
| 2641 | returning a shared empty bytes string. This required because we |
| 2642 | want to call _PyBytes_Resize() the returned object, which we can |
| 2643 | only do on bytes objects with refcount == 1. */ |
| 2644 | size += 1; |
| 2645 | new = PyBytes_FromStringAndSize(NULL, size); |
| 2646 | if (new == NULL) |
| 2647 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2648 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2649 | /* Get the iterator */ |
| 2650 | it = PyObject_GetIter(x); |
| 2651 | if (it == NULL) |
| 2652 | goto error; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2653 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2654 | /* Run the iterator to exhaustion */ |
| 2655 | for (i = 0; ; i++) { |
| 2656 | PyObject *item; |
| 2657 | Py_ssize_t value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2658 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2659 | /* Get the next item */ |
| 2660 | item = PyIter_Next(it); |
| 2661 | if (item == NULL) { |
| 2662 | if (PyErr_Occurred()) |
| 2663 | goto error; |
| 2664 | break; |
| 2665 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2666 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2667 | /* Interpret it as an int (__index__) */ |
| 2668 | value = PyNumber_AsSsize_t(item, PyExc_ValueError); |
| 2669 | Py_DECREF(item); |
| 2670 | if (value == -1 && PyErr_Occurred()) |
| 2671 | goto error; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2673 | /* Range check */ |
| 2674 | if (value < 0 || value >= 256) { |
| 2675 | PyErr_SetString(PyExc_ValueError, |
| 2676 | "bytes must be in range(0, 256)"); |
| 2677 | goto error; |
| 2678 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2680 | /* Append the byte */ |
| 2681 | if (i >= size) { |
| 2682 | size = 2 * size + 1; |
| 2683 | if (_PyBytes_Resize(&new, size) < 0) |
| 2684 | goto error; |
| 2685 | } |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 2686 | ((PyBytesObject *)new)->ob_sval[i] = (char) value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2687 | } |
| 2688 | _PyBytes_Resize(&new, i); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2690 | /* Clean up and return success */ |
| 2691 | Py_DECREF(it); |
| 2692 | return new; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2693 | |
| 2694 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2695 | Py_XDECREF(it); |
Victor Stinner | 986e224 | 2013-10-29 03:14:22 +0100 | [diff] [blame] | 2696 | Py_XDECREF(new); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2697 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | static PyObject * |
| 2701 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2702 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2703 | PyObject *tmp, *pnew; |
| 2704 | Py_ssize_t n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2706 | assert(PyType_IsSubtype(type, &PyBytes_Type)); |
| 2707 | tmp = bytes_new(&PyBytes_Type, args, kwds); |
| 2708 | if (tmp == NULL) |
| 2709 | return NULL; |
| 2710 | assert(PyBytes_CheckExact(tmp)); |
| 2711 | n = PyBytes_GET_SIZE(tmp); |
| 2712 | pnew = type->tp_alloc(type, n); |
| 2713 | if (pnew != NULL) { |
| 2714 | Py_MEMCPY(PyBytes_AS_STRING(pnew), |
| 2715 | PyBytes_AS_STRING(tmp), n+1); |
| 2716 | ((PyBytesObject *)pnew)->ob_shash = |
| 2717 | ((PyBytesObject *)tmp)->ob_shash; |
| 2718 | } |
| 2719 | Py_DECREF(tmp); |
| 2720 | return pnew; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2723 | PyDoc_STRVAR(bytes_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2724 | "bytes(iterable_of_ints) -> bytes\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2725 | bytes(string, encoding[, errors]) -> bytes\n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2726 | bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2727 | bytes(int) -> bytes object of size given by the parameter initialized with null bytes\n\ |
| 2728 | bytes() -> empty bytes object\n\ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2729 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2730 | Construct an immutable array of bytes from:\n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2731 | - an iterable yielding integers in range(256)\n\ |
| 2732 | - a text string encoded using the specified encoding\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 2733 | - any object implementing the buffer API.\n\ |
| 2734 | - an integer"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 2735 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2736 | static PyObject *bytes_iter(PyObject *seq); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2737 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2738 | PyTypeObject PyBytes_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2739 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2740 | "bytes", |
| 2741 | PyBytesObject_SIZE, |
| 2742 | sizeof(char), |
| 2743 | bytes_dealloc, /* tp_dealloc */ |
| 2744 | 0, /* tp_print */ |
| 2745 | 0, /* tp_getattr */ |
| 2746 | 0, /* tp_setattr */ |
| 2747 | 0, /* tp_reserved */ |
| 2748 | (reprfunc)bytes_repr, /* tp_repr */ |
| 2749 | 0, /* tp_as_number */ |
| 2750 | &bytes_as_sequence, /* tp_as_sequence */ |
| 2751 | &bytes_as_mapping, /* tp_as_mapping */ |
| 2752 | (hashfunc)bytes_hash, /* tp_hash */ |
| 2753 | 0, /* tp_call */ |
| 2754 | bytes_str, /* tp_str */ |
| 2755 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2756 | 0, /* tp_setattro */ |
| 2757 | &bytes_as_buffer, /* tp_as_buffer */ |
| 2758 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 2759 | Py_TPFLAGS_BYTES_SUBCLASS, /* tp_flags */ |
| 2760 | bytes_doc, /* tp_doc */ |
| 2761 | 0, /* tp_traverse */ |
| 2762 | 0, /* tp_clear */ |
| 2763 | (richcmpfunc)bytes_richcompare, /* tp_richcompare */ |
| 2764 | 0, /* tp_weaklistoffset */ |
| 2765 | bytes_iter, /* tp_iter */ |
| 2766 | 0, /* tp_iternext */ |
| 2767 | bytes_methods, /* tp_methods */ |
| 2768 | 0, /* tp_members */ |
| 2769 | 0, /* tp_getset */ |
| 2770 | &PyBaseObject_Type, /* tp_base */ |
| 2771 | 0, /* tp_dict */ |
| 2772 | 0, /* tp_descr_get */ |
| 2773 | 0, /* tp_descr_set */ |
| 2774 | 0, /* tp_dictoffset */ |
| 2775 | 0, /* tp_init */ |
| 2776 | 0, /* tp_alloc */ |
| 2777 | bytes_new, /* tp_new */ |
| 2778 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 2779 | }; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2780 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2781 | void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2782 | PyBytes_Concat(PyObject **pv, PyObject *w) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2783 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2784 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2785 | assert(pv != NULL); |
| 2786 | if (*pv == NULL) |
| 2787 | return; |
| 2788 | if (w == NULL) { |
Serhiy Storchaka | f458a03 | 2013-02-02 18:45:22 +0200 | [diff] [blame] | 2789 | Py_CLEAR(*pv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2790 | return; |
| 2791 | } |
| 2792 | v = bytes_concat(*pv, w); |
| 2793 | Py_DECREF(*pv); |
| 2794 | *pv = v; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2795 | } |
| 2796 | |
| 2797 | void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2798 | PyBytes_ConcatAndDel(PyObject **pv, PyObject *w) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2799 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2800 | PyBytes_Concat(pv, w); |
| 2801 | Py_XDECREF(w); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | |
| 2805 | /* The following function breaks the notion that strings are immutable: |
| 2806 | it changes the size of a string. We get away with this only if there |
| 2807 | is only one module referencing the object. You can also think of it |
| 2808 | as creating a new string object and destroying the old one, only |
| 2809 | more efficiently. In any case, don't use this if the string may |
| 2810 | already be known to some other part of the code... |
| 2811 | Note that if there's not enough memory to resize the string, the original |
| 2812 | string object at *pv is deallocated, *pv is set to NULL, an "out of |
| 2813 | memory" exception is set, and -1 is returned. Else (on success) 0 is |
| 2814 | returned, and the value in *pv may or may not be the same as on input. |
| 2815 | As always, an extra byte is allocated for a trailing \0 byte (newsize |
| 2816 | does *not* include that), and a trailing \0 byte is stored. |
| 2817 | */ |
| 2818 | |
| 2819 | int |
| 2820 | _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize) |
| 2821 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2822 | PyObject *v; |
| 2823 | PyBytesObject *sv; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2824 | v = *pv; |
| 2825 | if (!PyBytes_Check(v) || Py_REFCNT(v) != 1 || newsize < 0) { |
| 2826 | *pv = 0; |
| 2827 | Py_DECREF(v); |
| 2828 | PyErr_BadInternalCall(); |
| 2829 | return -1; |
| 2830 | } |
| 2831 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 2832 | _Py_DEC_REFTOTAL; |
| 2833 | _Py_ForgetReference(v); |
| 2834 | *pv = (PyObject *) |
| 2835 | PyObject_REALLOC((char *)v, PyBytesObject_SIZE + newsize); |
| 2836 | if (*pv == NULL) { |
| 2837 | PyObject_Del(v); |
| 2838 | PyErr_NoMemory(); |
| 2839 | return -1; |
| 2840 | } |
| 2841 | _Py_NewReference(*pv); |
| 2842 | sv = (PyBytesObject *) *pv; |
| 2843 | Py_SIZE(sv) = newsize; |
| 2844 | sv->ob_sval[newsize] = '\0'; |
| 2845 | sv->ob_shash = -1; /* invalidate cached hash value */ |
| 2846 | return 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2847 | } |
| 2848 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2849 | void |
| 2850 | PyBytes_Fini(void) |
| 2851 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2852 | int i; |
Serhiy Storchaka | f458a03 | 2013-02-02 18:45:22 +0200 | [diff] [blame] | 2853 | for (i = 0; i < UCHAR_MAX + 1; i++) |
| 2854 | Py_CLEAR(characters[i]); |
| 2855 | Py_CLEAR(nullstring); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
Benjamin Peterson | 4116f36 | 2008-05-27 00:36:20 +0000 | [diff] [blame] | 2858 | /*********************** Bytes Iterator ****************************/ |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2859 | |
| 2860 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2861 | PyObject_HEAD |
| 2862 | Py_ssize_t it_index; |
| 2863 | PyBytesObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2864 | } striterobject; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2865 | |
| 2866 | static void |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2867 | striter_dealloc(striterobject *it) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2868 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2869 | _PyObject_GC_UNTRACK(it); |
| 2870 | Py_XDECREF(it->it_seq); |
| 2871 | PyObject_GC_Del(it); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | static int |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2875 | striter_traverse(striterobject *it, visitproc visit, void *arg) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2876 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2877 | Py_VISIT(it->it_seq); |
| 2878 | return 0; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | static PyObject * |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2882 | striter_next(striterobject *it) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2883 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2884 | PyBytesObject *seq; |
| 2885 | PyObject *item; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2886 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2887 | assert(it != NULL); |
| 2888 | seq = it->it_seq; |
| 2889 | if (seq == NULL) |
| 2890 | return NULL; |
| 2891 | assert(PyBytes_Check(seq)); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2893 | if (it->it_index < PyBytes_GET_SIZE(seq)) { |
| 2894 | item = PyLong_FromLong( |
| 2895 | (unsigned char)seq->ob_sval[it->it_index]); |
| 2896 | if (item != NULL) |
| 2897 | ++it->it_index; |
| 2898 | return item; |
| 2899 | } |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2901 | Py_DECREF(seq); |
| 2902 | it->it_seq = NULL; |
| 2903 | return NULL; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | static PyObject * |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2907 | striter_len(striterobject *it) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2908 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2909 | Py_ssize_t len = 0; |
| 2910 | if (it->it_seq) |
| 2911 | len = PyBytes_GET_SIZE(it->it_seq) - it->it_index; |
| 2912 | return PyLong_FromSsize_t(len); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
| 2915 | PyDoc_STRVAR(length_hint_doc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2916 | "Private method returning an estimate of len(list(it))."); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2917 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2918 | static PyObject * |
| 2919 | striter_reduce(striterobject *it) |
| 2920 | { |
| 2921 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 2922 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2923 | it->it_seq, it->it_index); |
| 2924 | } else { |
| 2925 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 2926 | if (u == NULL) |
| 2927 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 2928 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 2933 | |
| 2934 | static PyObject * |
| 2935 | striter_setstate(striterobject *it, PyObject *state) |
| 2936 | { |
| 2937 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 2938 | if (index == -1 && PyErr_Occurred()) |
| 2939 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 2940 | if (it->it_seq != NULL) { |
| 2941 | if (index < 0) |
| 2942 | index = 0; |
| 2943 | else if (index > PyBytes_GET_SIZE(it->it_seq)) |
| 2944 | index = PyBytes_GET_SIZE(it->it_seq); /* iterator exhausted */ |
| 2945 | it->it_index = index; |
| 2946 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2947 | Py_RETURN_NONE; |
| 2948 | } |
| 2949 | |
| 2950 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 2951 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2952 | static PyMethodDef striter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2953 | {"__length_hint__", (PyCFunction)striter_len, METH_NOARGS, |
| 2954 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 2955 | {"__reduce__", (PyCFunction)striter_reduce, METH_NOARGS, |
| 2956 | reduce_doc}, |
| 2957 | {"__setstate__", (PyCFunction)striter_setstate, METH_O, |
| 2958 | setstate_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2959 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2960 | }; |
| 2961 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2962 | PyTypeObject PyBytesIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2963 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2964 | "bytes_iterator", /* tp_name */ |
| 2965 | sizeof(striterobject), /* tp_basicsize */ |
| 2966 | 0, /* tp_itemsize */ |
| 2967 | /* methods */ |
| 2968 | (destructor)striter_dealloc, /* tp_dealloc */ |
| 2969 | 0, /* tp_print */ |
| 2970 | 0, /* tp_getattr */ |
| 2971 | 0, /* tp_setattr */ |
| 2972 | 0, /* tp_reserved */ |
| 2973 | 0, /* tp_repr */ |
| 2974 | 0, /* tp_as_number */ |
| 2975 | 0, /* tp_as_sequence */ |
| 2976 | 0, /* tp_as_mapping */ |
| 2977 | 0, /* tp_hash */ |
| 2978 | 0, /* tp_call */ |
| 2979 | 0, /* tp_str */ |
| 2980 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2981 | 0, /* tp_setattro */ |
| 2982 | 0, /* tp_as_buffer */ |
| 2983 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 2984 | 0, /* tp_doc */ |
| 2985 | (traverseproc)striter_traverse, /* tp_traverse */ |
| 2986 | 0, /* tp_clear */ |
| 2987 | 0, /* tp_richcompare */ |
| 2988 | 0, /* tp_weaklistoffset */ |
| 2989 | PyObject_SelfIter, /* tp_iter */ |
| 2990 | (iternextfunc)striter_next, /* tp_iternext */ |
| 2991 | striter_methods, /* tp_methods */ |
| 2992 | 0, |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2993 | }; |
| 2994 | |
| 2995 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2996 | bytes_iter(PyObject *seq) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2997 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2998 | striterobject *it; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 2999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3000 | if (!PyBytes_Check(seq)) { |
| 3001 | PyErr_BadInternalCall(); |
| 3002 | return NULL; |
| 3003 | } |
| 3004 | it = PyObject_GC_New(striterobject, &PyBytesIter_Type); |
| 3005 | if (it == NULL) |
| 3006 | return NULL; |
| 3007 | it->it_index = 0; |
| 3008 | Py_INCREF(seq); |
| 3009 | it->it_seq = (PyBytesObject *)seq; |
| 3010 | _PyObject_GC_TRACK(it); |
| 3011 | return (PyObject *)it; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3012 | } |