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