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