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 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 10 | /*[clinic input] |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 11 | class bytes "PyBytesObject*" "&PyBytes_Type" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 12 | [clinic start generated code]*/ |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 13 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=1a1d9102afc1b00c]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 14 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 15 | #ifdef COUNT_ALLOCS |
Benjamin Peterson | a4a37fe | 2009-01-11 17:13:55 +0000 | [diff] [blame] | 16 | Py_ssize_t null_strings, one_strings; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 17 | #endif |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 18 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 19 | static PyBytesObject *characters[UCHAR_MAX + 1]; |
| 20 | static PyBytesObject *nullstring; |
| 21 | |
Mark Dickinson | fd24b32 | 2008-12-06 15:33:31 +0000 | [diff] [blame] | 22 | /* PyBytesObject_SIZE gives the basic size of a string; any memory allocation |
| 23 | for a string of length n should request PyBytesObject_SIZE + n bytes. |
| 24 | |
| 25 | Using PyBytesObject_SIZE instead of sizeof(PyBytesObject) saves |
| 26 | 3 bytes per string allocation on a typical system. |
| 27 | */ |
| 28 | #define PyBytesObject_SIZE (offsetof(PyBytesObject, ob_sval) + 1) |
| 29 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 30 | /* |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 31 | For PyBytes_FromString(), the parameter `str' points to a null-terminated |
| 32 | string containing exactly `size' bytes. |
| 33 | |
| 34 | For PyBytes_FromStringAndSize(), the parameter the parameter `str' is |
| 35 | either NULL or else points to a string containing at least `size' bytes. |
| 36 | For PyBytes_FromStringAndSize(), the string in the `str' parameter does |
| 37 | not have to be null-terminated. (Therefore it is safe to construct a |
| 38 | substring by calling `PyBytes_FromStringAndSize(origstring, substrlen)'.) |
| 39 | If `str' is NULL then PyBytes_FromStringAndSize() will allocate `size+1' |
| 40 | bytes (setting the last byte to the null terminating character) and you can |
| 41 | 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] | 42 | 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] | 43 | alter the data yourself, since the strings may be shared. |
| 44 | |
| 45 | The PyObject member `op->ob_size', which denotes the number of "extra |
| 46 | items" in a variable-size object, will contain the number of bytes |
Eli Bendersky | 1aef6b6 | 2011-03-24 22:32:56 +0200 | [diff] [blame] | 47 | allocated for string data, not counting the null terminating character. |
| 48 | It is therefore equal to the `size' parameter (for |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 49 | PyBytes_FromStringAndSize()) or the length of the string in the `str' |
| 50 | parameter (for PyBytes_FromString()). |
| 51 | */ |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 52 | static PyObject * |
| 53 | _PyBytes_FromSize(Py_ssize_t size, int use_calloc) |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 54 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 55 | PyBytesObject *op; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 56 | assert(size >= 0); |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 57 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | if (size == 0 && (op = nullstring) != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 59 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | null_strings++; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 61 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | Py_INCREF(op); |
| 63 | return (PyObject *)op; |
| 64 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 65 | |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 66 | if ((size_t)size > (size_t)PY_SSIZE_T_MAX - PyBytesObject_SIZE) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | PyErr_SetString(PyExc_OverflowError, |
| 68 | "byte string is too large"); |
| 69 | return NULL; |
| 70 | } |
Neal Norwitz | 3ce5d92 | 2008-08-24 07:08:55 +0000 | [diff] [blame] | 71 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | /* Inline PyObject_NewVar */ |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 73 | if (use_calloc) |
| 74 | op = (PyBytesObject *)PyObject_Calloc(1, PyBytesObject_SIZE + size); |
| 75 | else |
| 76 | op = (PyBytesObject *)PyObject_Malloc(PyBytesObject_SIZE + size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | if (op == NULL) |
| 78 | return PyErr_NoMemory(); |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 79 | (void)PyObject_INIT_VAR(op, &PyBytes_Type, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | op->ob_shash = -1; |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 81 | if (!use_calloc) |
| 82 | op->ob_sval[size] = '\0'; |
| 83 | /* empty byte string singleton */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | if (size == 0) { |
| 85 | nullstring = op; |
| 86 | Py_INCREF(op); |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 87 | } |
| 88 | return (PyObject *) op; |
| 89 | } |
| 90 | |
| 91 | PyObject * |
| 92 | PyBytes_FromStringAndSize(const char *str, Py_ssize_t size) |
| 93 | { |
| 94 | PyBytesObject *op; |
| 95 | if (size < 0) { |
| 96 | PyErr_SetString(PyExc_SystemError, |
| 97 | "Negative size passed to PyBytes_FromStringAndSize"); |
| 98 | return NULL; |
| 99 | } |
| 100 | if (size == 1 && str != NULL && |
| 101 | (op = characters[*str & UCHAR_MAX]) != NULL) |
| 102 | { |
| 103 | #ifdef COUNT_ALLOCS |
| 104 | one_strings++; |
| 105 | #endif |
| 106 | Py_INCREF(op); |
| 107 | return (PyObject *)op; |
| 108 | } |
| 109 | |
| 110 | op = (PyBytesObject *)_PyBytes_FromSize(size, 0); |
| 111 | if (op == NULL) |
| 112 | return NULL; |
| 113 | if (str == NULL) |
| 114 | return (PyObject *) op; |
| 115 | |
| 116 | Py_MEMCPY(op->ob_sval, str, size); |
| 117 | /* share short strings */ |
| 118 | if (size == 1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 | characters[*str & UCHAR_MAX] = op; |
| 120 | Py_INCREF(op); |
| 121 | } |
| 122 | return (PyObject *) op; |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 125 | PyObject * |
| 126 | PyBytes_FromString(const char *str) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 127 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 128 | size_t size; |
| 129 | PyBytesObject *op; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 130 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 131 | assert(str != NULL); |
| 132 | size = strlen(str); |
| 133 | if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) { |
| 134 | PyErr_SetString(PyExc_OverflowError, |
| 135 | "byte string is too long"); |
| 136 | return NULL; |
| 137 | } |
| 138 | if (size == 0 && (op = nullstring) != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 139 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | null_strings++; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 141 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | Py_INCREF(op); |
| 143 | return (PyObject *)op; |
| 144 | } |
| 145 | if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 146 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | one_strings++; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 148 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | Py_INCREF(op); |
| 150 | return (PyObject *)op; |
| 151 | } |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 152 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 153 | /* Inline PyObject_NewVar */ |
| 154 | op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size); |
| 155 | if (op == NULL) |
| 156 | return PyErr_NoMemory(); |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 157 | (void)PyObject_INIT_VAR(op, &PyBytes_Type, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 158 | op->ob_shash = -1; |
| 159 | Py_MEMCPY(op->ob_sval, str, size+1); |
| 160 | /* share short strings */ |
| 161 | if (size == 0) { |
| 162 | nullstring = op; |
| 163 | Py_INCREF(op); |
| 164 | } else if (size == 1) { |
| 165 | characters[*str & UCHAR_MAX] = op; |
| 166 | Py_INCREF(op); |
| 167 | } |
| 168 | return (PyObject *) op; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 169 | } |
Guido van Rossum | ebea9be | 2007-04-09 00:49:13 +0000 | [diff] [blame] | 170 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 171 | PyObject * |
| 172 | PyBytes_FromFormatV(const char *format, va_list vargs) |
| 173 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 174 | va_list count; |
| 175 | Py_ssize_t n = 0; |
| 176 | const char* f; |
| 177 | char *s; |
| 178 | PyObject* string; |
Guido van Rossum | 343e97f | 2007-04-09 00:43:24 +0000 | [diff] [blame] | 179 | |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 180 | Py_VA_COPY(count, vargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 181 | /* step 1: figure out how large a buffer we need */ |
| 182 | for (f = format; *f; f++) { |
| 183 | if (*f == '%') { |
| 184 | const char* p = f; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 185 | while (*++f && *f != '%' && !Py_ISALPHA(*f)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | ; |
Guido van Rossum | 343e97f | 2007-04-09 00:43:24 +0000 | [diff] [blame] | 187 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 188 | /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
| 189 | * they don't affect the amount of space we reserve. |
| 190 | */ |
| 191 | if ((*f == 'l' || *f == 'z') && |
| 192 | (f[1] == 'd' || f[1] == 'u')) |
| 193 | ++f; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | switch (*f) { |
| 196 | case 'c': |
Victor Stinner | c9362cf | 2013-12-13 12:14:44 +0100 | [diff] [blame] | 197 | { |
| 198 | int c = va_arg(count, int); |
| 199 | if (c < 0 || c > 255) { |
| 200 | PyErr_SetString(PyExc_OverflowError, |
| 201 | "PyBytes_FromFormatV(): %c format " |
| 202 | "expects an integer in range [0; 255]"); |
| 203 | return NULL; |
| 204 | } |
| 205 | n++; |
| 206 | break; |
| 207 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | case '%': |
| 209 | n++; |
| 210 | break; |
| 211 | case 'd': case 'u': case 'i': case 'x': |
| 212 | (void) va_arg(count, int); |
| 213 | /* 20 bytes is enough to hold a 64-bit |
| 214 | integer. Decimal takes the most space. |
| 215 | This isn't enough for octal. */ |
| 216 | n += 20; |
| 217 | break; |
| 218 | case 's': |
| 219 | s = va_arg(count, char*); |
| 220 | n += strlen(s); |
| 221 | break; |
| 222 | case 'p': |
| 223 | (void) va_arg(count, int); |
| 224 | /* maximum 64-bit pointer representation: |
| 225 | * 0xffffffffffffffff |
| 226 | * so 19 characters is enough. |
| 227 | * XXX I count 18 -- what's the extra for? |
| 228 | */ |
| 229 | n += 19; |
| 230 | break; |
| 231 | default: |
| 232 | /* if we stumble upon an unknown |
| 233 | formatting code, copy the rest of |
| 234 | the format string to the output |
| 235 | string. (we cannot just skip the |
| 236 | code, since there's no way to know |
| 237 | what's in the argument list) */ |
| 238 | n += strlen(p); |
| 239 | goto expand; |
| 240 | } |
| 241 | } else |
| 242 | n++; |
| 243 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 244 | expand: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 245 | /* step 2: fill the buffer */ |
| 246 | /* Since we've analyzed how much space we need for the worst case, |
| 247 | use sprintf directly instead of the slower PyOS_snprintf. */ |
| 248 | string = PyBytes_FromStringAndSize(NULL, n); |
| 249 | if (!string) |
| 250 | return NULL; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | s = PyBytes_AsString(string); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 254 | for (f = format; *f; f++) { |
| 255 | if (*f == '%') { |
| 256 | const char* p = f++; |
| 257 | Py_ssize_t i; |
| 258 | int longflag = 0; |
| 259 | int size_tflag = 0; |
| 260 | /* parse the width.precision part (we're only |
| 261 | interested in the precision value, if any) */ |
| 262 | n = 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 263 | while (Py_ISDIGIT(*f)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 264 | n = (n*10) + *f++ - '0'; |
| 265 | if (*f == '.') { |
| 266 | f++; |
| 267 | n = 0; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 268 | while (Py_ISDIGIT(*f)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | n = (n*10) + *f++ - '0'; |
| 270 | } |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 271 | while (*f && *f != '%' && !Py_ISALPHA(*f)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 272 | f++; |
| 273 | /* handle the long flag, but only for %ld and %lu. |
| 274 | others can be added when necessary. */ |
| 275 | if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
| 276 | longflag = 1; |
| 277 | ++f; |
| 278 | } |
| 279 | /* handle the size_t flag. */ |
| 280 | if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
| 281 | size_tflag = 1; |
| 282 | ++f; |
| 283 | } |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 285 | switch (*f) { |
| 286 | case 'c': |
Victor Stinner | c9362cf | 2013-12-13 12:14:44 +0100 | [diff] [blame] | 287 | { |
| 288 | int c = va_arg(vargs, int); |
| 289 | /* c has been checked for overflow in the first step */ |
| 290 | *s++ = (unsigned char)c; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | break; |
Victor Stinner | c9362cf | 2013-12-13 12:14:44 +0100 | [diff] [blame] | 292 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 293 | case 'd': |
| 294 | if (longflag) |
| 295 | sprintf(s, "%ld", va_arg(vargs, long)); |
| 296 | else if (size_tflag) |
| 297 | sprintf(s, "%" PY_FORMAT_SIZE_T "d", |
| 298 | va_arg(vargs, Py_ssize_t)); |
| 299 | else |
| 300 | sprintf(s, "%d", va_arg(vargs, int)); |
| 301 | s += strlen(s); |
| 302 | break; |
| 303 | case 'u': |
| 304 | if (longflag) |
| 305 | sprintf(s, "%lu", |
| 306 | va_arg(vargs, unsigned long)); |
| 307 | else if (size_tflag) |
| 308 | sprintf(s, "%" PY_FORMAT_SIZE_T "u", |
| 309 | va_arg(vargs, size_t)); |
| 310 | else |
| 311 | sprintf(s, "%u", |
| 312 | va_arg(vargs, unsigned int)); |
| 313 | s += strlen(s); |
| 314 | break; |
| 315 | case 'i': |
| 316 | sprintf(s, "%i", va_arg(vargs, int)); |
| 317 | s += strlen(s); |
| 318 | break; |
| 319 | case 'x': |
| 320 | sprintf(s, "%x", va_arg(vargs, int)); |
| 321 | s += strlen(s); |
| 322 | break; |
| 323 | case 's': |
| 324 | p = va_arg(vargs, char*); |
| 325 | i = strlen(p); |
| 326 | if (n > 0 && i > n) |
| 327 | i = n; |
| 328 | Py_MEMCPY(s, p, i); |
| 329 | s += i; |
| 330 | break; |
| 331 | case 'p': |
| 332 | sprintf(s, "%p", va_arg(vargs, void*)); |
| 333 | /* %p is ill-defined: ensure leading 0x. */ |
| 334 | if (s[1] == 'X') |
| 335 | s[1] = 'x'; |
| 336 | else if (s[1] != 'x') { |
| 337 | memmove(s+2, s, strlen(s)+1); |
| 338 | s[0] = '0'; |
| 339 | s[1] = 'x'; |
| 340 | } |
| 341 | s += strlen(s); |
| 342 | break; |
| 343 | case '%': |
| 344 | *s++ = '%'; |
| 345 | break; |
| 346 | default: |
| 347 | strcpy(s, p); |
| 348 | s += strlen(s); |
| 349 | goto end; |
| 350 | } |
| 351 | } else |
| 352 | *s++ = *f; |
| 353 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 354 | |
| 355 | end: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | _PyBytes_Resize(&string, s - PyBytes_AS_STRING(string)); |
| 357 | return string; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | PyObject * |
| 361 | PyBytes_FromFormat(const char *format, ...) |
| 362 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | PyObject* ret; |
| 364 | va_list vargs; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 365 | |
| 366 | #ifdef HAVE_STDARG_PROTOTYPES |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 367 | va_start(vargs, format); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 368 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 369 | va_start(vargs); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 370 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | ret = PyBytes_FromFormatV(format, vargs); |
| 372 | va_end(vargs); |
| 373 | return ret; |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 376 | /* Helpers for formatstring */ |
| 377 | |
| 378 | Py_LOCAL_INLINE(PyObject *) |
| 379 | getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx) |
| 380 | { |
| 381 | Py_ssize_t argidx = *p_argidx; |
| 382 | if (argidx < arglen) { |
| 383 | (*p_argidx)++; |
| 384 | if (arglen < 0) |
| 385 | return args; |
| 386 | else |
| 387 | return PyTuple_GetItem(args, argidx); |
| 388 | } |
| 389 | PyErr_SetString(PyExc_TypeError, |
| 390 | "not enough arguments for format string"); |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | /* Format codes |
| 395 | * F_LJUST '-' |
| 396 | * F_SIGN '+' |
| 397 | * F_BLANK ' ' |
| 398 | * F_ALT '#' |
| 399 | * F_ZERO '0' |
| 400 | */ |
| 401 | #define F_LJUST (1<<0) |
| 402 | #define F_SIGN (1<<1) |
| 403 | #define F_BLANK (1<<2) |
| 404 | #define F_ALT (1<<3) |
| 405 | #define F_ZERO (1<<4) |
| 406 | |
| 407 | /* Returns a new reference to a PyBytes object, or NULL on failure. */ |
| 408 | |
| 409 | static PyObject * |
| 410 | formatfloat(PyObject *v, int flags, int prec, int type) |
| 411 | { |
| 412 | char *p; |
| 413 | PyObject *result; |
| 414 | double x; |
| 415 | |
| 416 | x = PyFloat_AsDouble(v); |
| 417 | if (x == -1.0 && PyErr_Occurred()) { |
| 418 | PyErr_Format(PyExc_TypeError, "float argument required, " |
| 419 | "not %.200s", Py_TYPE(v)->tp_name); |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | if (prec < 0) |
| 424 | prec = 6; |
| 425 | |
| 426 | p = PyOS_double_to_string(x, type, prec, |
| 427 | (flags & F_ALT) ? Py_DTSF_ALT : 0, NULL); |
| 428 | |
| 429 | if (p == NULL) |
| 430 | return NULL; |
| 431 | result = PyBytes_FromStringAndSize(p, strlen(p)); |
| 432 | PyMem_Free(p); |
| 433 | return result; |
| 434 | } |
| 435 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 436 | Py_LOCAL_INLINE(int) |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 437 | byte_converter(PyObject *arg, char *p) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 438 | { |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 439 | if (PyBytes_Check(arg) && PyBytes_Size(arg) == 1) { |
| 440 | *p = PyBytes_AS_STRING(arg)[0]; |
| 441 | return 1; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 442 | } |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 443 | else if (PyByteArray_Check(arg) && PyByteArray_Size(arg) == 1) { |
| 444 | *p = PyByteArray_AS_STRING(arg)[0]; |
| 445 | return 1; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 446 | } |
| 447 | else { |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 448 | long ival = PyLong_AsLong(arg); |
| 449 | if (0 <= ival && ival <= 255) { |
| 450 | *p = (char)ival; |
| 451 | return 1; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 452 | } |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 453 | } |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 454 | PyErr_SetString(PyExc_TypeError, |
| 455 | "%c requires an integer in range(256) or a single byte"); |
| 456 | return 0; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | static PyObject * |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 460 | format_obj(PyObject *v, const char **pbuf, Py_ssize_t *plen) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 461 | { |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 462 | PyObject *func, *result; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 463 | _Py_IDENTIFIER(__bytes__); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 464 | /* is it a bytes object? */ |
| 465 | if (PyBytes_Check(v)) { |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 466 | *pbuf = PyBytes_AS_STRING(v); |
| 467 | *plen = PyBytes_GET_SIZE(v); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 468 | Py_INCREF(v); |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 469 | return v; |
| 470 | } |
| 471 | if (PyByteArray_Check(v)) { |
| 472 | *pbuf = PyByteArray_AS_STRING(v); |
| 473 | *plen = PyByteArray_GET_SIZE(v); |
| 474 | Py_INCREF(v); |
| 475 | return v; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 476 | } |
| 477 | /* does it support __bytes__? */ |
| 478 | func = _PyObject_LookupSpecial(v, &PyId___bytes__); |
| 479 | if (func != NULL) { |
| 480 | result = PyObject_CallFunctionObjArgs(func, NULL); |
| 481 | Py_DECREF(func); |
| 482 | if (result == NULL) |
| 483 | return NULL; |
| 484 | if (!PyBytes_Check(result)) { |
| 485 | PyErr_Format(PyExc_TypeError, |
| 486 | "__bytes__ returned non-bytes (type %.200s)", |
| 487 | Py_TYPE(result)->tp_name); |
| 488 | Py_DECREF(result); |
| 489 | return NULL; |
| 490 | } |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 491 | *pbuf = PyBytes_AS_STRING(result); |
| 492 | *plen = PyBytes_GET_SIZE(result); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 493 | return result; |
| 494 | } |
| 495 | PyErr_Format(PyExc_TypeError, |
| 496 | "%%b requires bytes, or an object that implements __bytes__, not '%.100s'", |
| 497 | Py_TYPE(v)->tp_name); |
| 498 | return NULL; |
| 499 | } |
| 500 | |
| 501 | /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
| 502 | |
| 503 | FORMATBUFLEN is the length of the buffer in which the ints & |
| 504 | chars are formatted. XXX This is a magic number. Each formatting |
| 505 | routine does bounds checking to ensure no overflow, but a better |
| 506 | solution may be to malloc a buffer of appropriate size for each |
| 507 | format. For now, the current solution is sufficient. |
| 508 | */ |
| 509 | #define FORMATBUFLEN (size_t)120 |
| 510 | |
| 511 | PyObject * |
| 512 | _PyBytes_Format(PyObject *format, PyObject *args) |
| 513 | { |
| 514 | char *fmt, *res; |
| 515 | Py_ssize_t arglen, argidx; |
| 516 | Py_ssize_t reslen, rescnt, fmtcnt; |
| 517 | int args_owned = 0; |
| 518 | PyObject *result; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 519 | PyObject *dict = NULL; |
| 520 | if (format == NULL || !PyBytes_Check(format) || args == NULL) { |
| 521 | PyErr_BadInternalCall(); |
| 522 | return NULL; |
| 523 | } |
| 524 | fmt = PyBytes_AS_STRING(format); |
| 525 | fmtcnt = PyBytes_GET_SIZE(format); |
| 526 | reslen = rescnt = fmtcnt + 100; |
| 527 | result = PyBytes_FromStringAndSize((char *)NULL, reslen); |
| 528 | if (result == NULL) |
| 529 | return NULL; |
| 530 | res = PyBytes_AsString(result); |
| 531 | if (PyTuple_Check(args)) { |
| 532 | arglen = PyTuple_GET_SIZE(args); |
| 533 | argidx = 0; |
| 534 | } |
| 535 | else { |
| 536 | arglen = -1; |
| 537 | argidx = -2; |
| 538 | } |
| 539 | if (Py_TYPE(args)->tp_as_mapping && Py_TYPE(args)->tp_as_mapping->mp_subscript && |
| 540 | !PyTuple_Check(args) && !PyBytes_Check(args) && !PyUnicode_Check(args) && |
| 541 | !PyByteArray_Check(args)) { |
| 542 | dict = args; |
| 543 | } |
| 544 | while (--fmtcnt >= 0) { |
| 545 | if (*fmt != '%') { |
| 546 | if (--rescnt < 0) { |
| 547 | rescnt = fmtcnt + 100; |
| 548 | reslen += rescnt; |
| 549 | if (_PyBytes_Resize(&result, reslen)) |
| 550 | return NULL; |
| 551 | res = PyBytes_AS_STRING(result) |
| 552 | + reslen - rescnt; |
| 553 | --rescnt; |
| 554 | } |
| 555 | *res++ = *fmt++; |
| 556 | } |
| 557 | else { |
| 558 | /* Got a format specifier */ |
| 559 | int flags = 0; |
| 560 | Py_ssize_t width = -1; |
| 561 | int prec = -1; |
| 562 | int c = '\0'; |
| 563 | int fill; |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 564 | PyObject *iobj; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 565 | PyObject *v = NULL; |
| 566 | PyObject *temp = NULL; |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 567 | const char *pbuf = NULL; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 568 | int sign; |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 569 | Py_ssize_t len = 0; |
| 570 | char onechar; /* For byte_converter() */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 571 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 572 | fmt++; |
| 573 | if (*fmt == '(') { |
| 574 | char *keystart; |
| 575 | Py_ssize_t keylen; |
| 576 | PyObject *key; |
| 577 | int pcount = 1; |
| 578 | |
| 579 | if (dict == NULL) { |
| 580 | PyErr_SetString(PyExc_TypeError, |
| 581 | "format requires a mapping"); |
| 582 | goto error; |
| 583 | } |
| 584 | ++fmt; |
| 585 | --fmtcnt; |
| 586 | keystart = fmt; |
| 587 | /* Skip over balanced parentheses */ |
| 588 | while (pcount > 0 && --fmtcnt >= 0) { |
| 589 | if (*fmt == ')') |
| 590 | --pcount; |
| 591 | else if (*fmt == '(') |
| 592 | ++pcount; |
| 593 | fmt++; |
| 594 | } |
| 595 | keylen = fmt - keystart - 1; |
| 596 | if (fmtcnt < 0 || pcount > 0) { |
| 597 | PyErr_SetString(PyExc_ValueError, |
| 598 | "incomplete format key"); |
| 599 | goto error; |
| 600 | } |
| 601 | key = PyBytes_FromStringAndSize(keystart, |
| 602 | keylen); |
| 603 | if (key == NULL) |
| 604 | goto error; |
| 605 | if (args_owned) { |
| 606 | Py_DECREF(args); |
| 607 | args_owned = 0; |
| 608 | } |
| 609 | args = PyObject_GetItem(dict, key); |
| 610 | Py_DECREF(key); |
| 611 | if (args == NULL) { |
| 612 | goto error; |
| 613 | } |
| 614 | args_owned = 1; |
| 615 | arglen = -1; |
| 616 | argidx = -2; |
| 617 | } |
| 618 | while (--fmtcnt >= 0) { |
| 619 | switch (c = *fmt++) { |
| 620 | case '-': flags |= F_LJUST; continue; |
| 621 | case '+': flags |= F_SIGN; continue; |
| 622 | case ' ': flags |= F_BLANK; continue; |
| 623 | case '#': flags |= F_ALT; continue; |
| 624 | case '0': flags |= F_ZERO; continue; |
| 625 | } |
| 626 | break; |
| 627 | } |
| 628 | if (c == '*') { |
| 629 | v = getnextarg(args, arglen, &argidx); |
| 630 | if (v == NULL) |
| 631 | goto error; |
| 632 | if (!PyLong_Check(v)) { |
| 633 | PyErr_SetString(PyExc_TypeError, |
| 634 | "* wants int"); |
| 635 | goto error; |
| 636 | } |
| 637 | width = PyLong_AsSsize_t(v); |
| 638 | if (width == -1 && PyErr_Occurred()) |
| 639 | goto error; |
| 640 | if (width < 0) { |
| 641 | flags |= F_LJUST; |
| 642 | width = -width; |
| 643 | } |
| 644 | if (--fmtcnt >= 0) |
| 645 | c = *fmt++; |
| 646 | } |
| 647 | else if (c >= 0 && isdigit(c)) { |
| 648 | width = c - '0'; |
| 649 | while (--fmtcnt >= 0) { |
| 650 | c = Py_CHARMASK(*fmt++); |
| 651 | if (!isdigit(c)) |
| 652 | break; |
| 653 | if (width > (PY_SSIZE_T_MAX - ((int)c - '0')) / 10) { |
| 654 | PyErr_SetString( |
| 655 | PyExc_ValueError, |
| 656 | "width too big"); |
| 657 | goto error; |
| 658 | } |
| 659 | width = width*10 + (c - '0'); |
| 660 | } |
| 661 | } |
| 662 | if (c == '.') { |
| 663 | prec = 0; |
| 664 | if (--fmtcnt >= 0) |
| 665 | c = *fmt++; |
| 666 | if (c == '*') { |
| 667 | v = getnextarg(args, arglen, &argidx); |
| 668 | if (v == NULL) |
| 669 | goto error; |
| 670 | if (!PyLong_Check(v)) { |
| 671 | PyErr_SetString( |
| 672 | PyExc_TypeError, |
| 673 | "* wants int"); |
| 674 | goto error; |
| 675 | } |
| 676 | prec = PyLong_AsSsize_t(v); |
| 677 | if (prec == -1 && PyErr_Occurred()) |
| 678 | goto error; |
| 679 | if (prec < 0) |
| 680 | prec = 0; |
| 681 | if (--fmtcnt >= 0) |
| 682 | c = *fmt++; |
| 683 | } |
| 684 | else if (c >= 0 && isdigit(c)) { |
| 685 | prec = c - '0'; |
| 686 | while (--fmtcnt >= 0) { |
| 687 | c = Py_CHARMASK(*fmt++); |
| 688 | if (!isdigit(c)) |
| 689 | break; |
| 690 | if (prec > (INT_MAX - ((int)c - '0')) / 10) { |
| 691 | PyErr_SetString( |
| 692 | PyExc_ValueError, |
| 693 | "prec too big"); |
| 694 | goto error; |
| 695 | } |
| 696 | prec = prec*10 + (c - '0'); |
| 697 | } |
| 698 | } |
| 699 | } /* prec */ |
| 700 | if (fmtcnt >= 0) { |
| 701 | if (c == 'h' || c == 'l' || c == 'L') { |
| 702 | if (--fmtcnt >= 0) |
| 703 | c = *fmt++; |
| 704 | } |
| 705 | } |
| 706 | if (fmtcnt < 0) { |
| 707 | PyErr_SetString(PyExc_ValueError, |
| 708 | "incomplete format"); |
| 709 | goto error; |
| 710 | } |
| 711 | if (c != '%') { |
| 712 | v = getnextarg(args, arglen, &argidx); |
| 713 | if (v == NULL) |
| 714 | goto error; |
| 715 | } |
| 716 | sign = 0; |
| 717 | fill = ' '; |
| 718 | switch (c) { |
| 719 | case '%': |
| 720 | pbuf = "%"; |
| 721 | len = 1; |
| 722 | break; |
| 723 | case 'a': |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 724 | temp = PyObject_ASCII(v); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 725 | if (temp == NULL) |
| 726 | goto error; |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 727 | assert(PyUnicode_IS_ASCII(temp)); |
| 728 | pbuf = (const char *)PyUnicode_1BYTE_DATA(temp); |
| 729 | len = PyUnicode_GET_LENGTH(temp); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 730 | if (prec >= 0 && len > prec) |
| 731 | len = prec; |
| 732 | break; |
| 733 | case 's': |
| 734 | // %s is only for 2/3 code; 3 only code should use %b |
| 735 | case 'b': |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 736 | temp = format_obj(v, &pbuf, &len); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 737 | if (temp == NULL) |
| 738 | goto error; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 739 | if (prec >= 0 && len > prec) |
| 740 | len = prec; |
| 741 | break; |
| 742 | case 'i': |
| 743 | case 'd': |
| 744 | case 'u': |
| 745 | case 'o': |
| 746 | case 'x': |
| 747 | case 'X': |
| 748 | if (c == 'i') |
| 749 | c = 'd'; |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 750 | iobj = NULL; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 751 | if (PyNumber_Check(v)) { |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 752 | if ((PyLong_Check(v))) { |
| 753 | iobj = v; |
| 754 | Py_INCREF(iobj); |
| 755 | } |
| 756 | else { |
| 757 | iobj = PyNumber_Long(v); |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 758 | if (iobj != NULL && !PyLong_Check(iobj)) |
| 759 | Py_CLEAR(iobj); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 760 | } |
| 761 | } |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 762 | if (iobj == NULL) { |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 763 | PyErr_Format(PyExc_TypeError, |
| 764 | "%%%c format: a number is required, " |
| 765 | "not %.200s", c, Py_TYPE(v)->tp_name); |
| 766 | goto error; |
| 767 | } |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 768 | temp = _PyUnicode_FormatLong(iobj, flags & F_ALT, prec, c); |
| 769 | Py_DECREF(iobj); |
| 770 | if (!temp) |
| 771 | goto error; |
| 772 | assert(PyUnicode_IS_ASCII(temp)); |
| 773 | pbuf = (const char *)PyUnicode_1BYTE_DATA(temp); |
| 774 | len = PyUnicode_GET_LENGTH(temp); |
| 775 | sign = 1; |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 776 | if (flags & F_ZERO) |
| 777 | fill = '0'; |
| 778 | break; |
| 779 | case 'e': |
| 780 | case 'E': |
| 781 | case 'f': |
| 782 | case 'F': |
| 783 | case 'g': |
| 784 | case 'G': |
| 785 | temp = formatfloat(v, flags, prec, c); |
| 786 | if (temp == NULL) |
| 787 | goto error; |
| 788 | pbuf = PyBytes_AS_STRING(temp); |
| 789 | len = PyBytes_GET_SIZE(temp); |
| 790 | sign = 1; |
| 791 | if (flags & F_ZERO) |
| 792 | fill = '0'; |
| 793 | break; |
| 794 | case 'c': |
Serhiy Storchaka | ea5ce5a | 2015-02-10 23:23:12 +0200 | [diff] [blame^] | 795 | pbuf = &onechar; |
| 796 | len = byte_converter(v, &onechar); |
| 797 | if (!len) |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 798 | goto error; |
| 799 | break; |
| 800 | default: |
| 801 | PyErr_Format(PyExc_ValueError, |
| 802 | "unsupported format character '%c' (0x%x) " |
| 803 | "at index %zd", |
| 804 | c, c, |
| 805 | (Py_ssize_t)(fmt - 1 - |
| 806 | PyBytes_AsString(format))); |
| 807 | goto error; |
| 808 | } |
| 809 | if (sign) { |
| 810 | if (*pbuf == '-' || *pbuf == '+') { |
| 811 | sign = *pbuf++; |
| 812 | len--; |
| 813 | } |
| 814 | else if (flags & F_SIGN) |
| 815 | sign = '+'; |
| 816 | else if (flags & F_BLANK) |
| 817 | sign = ' '; |
| 818 | else |
| 819 | sign = 0; |
| 820 | } |
| 821 | if (width < len) |
| 822 | width = len; |
| 823 | if (rescnt - (sign != 0) < width) { |
| 824 | reslen -= rescnt; |
| 825 | rescnt = width + fmtcnt + 100; |
| 826 | reslen += rescnt; |
| 827 | if (reslen < 0) { |
| 828 | Py_DECREF(result); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 829 | Py_XDECREF(temp); |
| 830 | return PyErr_NoMemory(); |
| 831 | } |
| 832 | if (_PyBytes_Resize(&result, reslen)) { |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 833 | Py_XDECREF(temp); |
| 834 | return NULL; |
| 835 | } |
| 836 | res = PyBytes_AS_STRING(result) |
| 837 | + reslen - rescnt; |
| 838 | } |
| 839 | if (sign) { |
| 840 | if (fill != ' ') |
| 841 | *res++ = sign; |
| 842 | rescnt--; |
| 843 | if (width > len) |
| 844 | width--; |
| 845 | } |
| 846 | if ((flags & F_ALT) && (c == 'x' || c == 'X')) { |
| 847 | assert(pbuf[0] == '0'); |
| 848 | assert(pbuf[1] == c); |
| 849 | if (fill != ' ') { |
| 850 | *res++ = *pbuf++; |
| 851 | *res++ = *pbuf++; |
| 852 | } |
| 853 | rescnt -= 2; |
| 854 | width -= 2; |
| 855 | if (width < 0) |
| 856 | width = 0; |
| 857 | len -= 2; |
| 858 | } |
| 859 | if (width > len && !(flags & F_LJUST)) { |
| 860 | do { |
| 861 | --rescnt; |
| 862 | *res++ = fill; |
| 863 | } while (--width > len); |
| 864 | } |
| 865 | if (fill == ' ') { |
| 866 | if (sign) |
| 867 | *res++ = sign; |
| 868 | if ((flags & F_ALT) && |
| 869 | (c == 'x' || c == 'X')) { |
| 870 | assert(pbuf[0] == '0'); |
| 871 | assert(pbuf[1] == c); |
| 872 | *res++ = *pbuf++; |
| 873 | *res++ = *pbuf++; |
| 874 | } |
| 875 | } |
| 876 | Py_MEMCPY(res, pbuf, len); |
| 877 | res += len; |
| 878 | rescnt -= len; |
| 879 | while (--width >= len) { |
| 880 | --rescnt; |
| 881 | *res++ = ' '; |
| 882 | } |
| 883 | if (dict && (argidx < arglen) && c != '%') { |
| 884 | PyErr_SetString(PyExc_TypeError, |
| 885 | "not all arguments converted during bytes formatting"); |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 886 | Py_XDECREF(temp); |
| 887 | goto error; |
| 888 | } |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 889 | Py_XDECREF(temp); |
| 890 | } /* '%' */ |
| 891 | } /* until end */ |
| 892 | if (argidx < arglen && !dict) { |
| 893 | PyErr_SetString(PyExc_TypeError, |
| 894 | "not all arguments converted during bytes formatting"); |
| 895 | goto error; |
| 896 | } |
| 897 | if (args_owned) { |
| 898 | Py_DECREF(args); |
| 899 | } |
| 900 | if (_PyBytes_Resize(&result, reslen - rescnt)) |
| 901 | return NULL; |
| 902 | return result; |
| 903 | |
| 904 | error: |
| 905 | Py_DECREF(result); |
| 906 | if (args_owned) { |
| 907 | Py_DECREF(args); |
| 908 | } |
| 909 | return NULL; |
| 910 | } |
| 911 | |
| 912 | /* =-= */ |
| 913 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 914 | static void |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 915 | bytes_dealloc(PyObject *op) |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 916 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 917 | Py_TYPE(op)->tp_free(op); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 918 | } |
| 919 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 920 | /* Unescape a backslash-escaped string. If unicode is non-zero, |
| 921 | the string is a u-literal. If recode_encoding is non-zero, |
| 922 | the string is UTF-8 encoded and should be re-encoded in the |
| 923 | specified encoding. */ |
| 924 | |
| 925 | PyObject *PyBytes_DecodeEscape(const char *s, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 926 | Py_ssize_t len, |
| 927 | const char *errors, |
| 928 | Py_ssize_t unicode, |
| 929 | const char *recode_encoding) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 930 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 931 | int c; |
| 932 | char *p, *buf; |
| 933 | const char *end; |
| 934 | PyObject *v; |
| 935 | Py_ssize_t newlen = recode_encoding ? 4*len:len; |
| 936 | v = PyBytes_FromStringAndSize((char *)NULL, newlen); |
| 937 | if (v == NULL) |
| 938 | return NULL; |
| 939 | p = buf = PyBytes_AsString(v); |
| 940 | end = s + len; |
| 941 | while (s < end) { |
| 942 | if (*s != '\\') { |
| 943 | non_esc: |
| 944 | if (recode_encoding && (*s & 0x80)) { |
| 945 | PyObject *u, *w; |
| 946 | char *r; |
| 947 | const char* t; |
| 948 | Py_ssize_t rn; |
| 949 | t = s; |
| 950 | /* Decode non-ASCII bytes as UTF-8. */ |
| 951 | while (t < end && (*t & 0x80)) t++; |
| 952 | u = PyUnicode_DecodeUTF8(s, t - s, errors); |
| 953 | if(!u) goto failed; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 954 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 955 | /* Recode them in target encoding. */ |
| 956 | w = PyUnicode_AsEncodedString( |
| 957 | u, recode_encoding, errors); |
| 958 | Py_DECREF(u); |
| 959 | if (!w) goto failed; |
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 | /* Append bytes to output buffer. */ |
| 962 | assert(PyBytes_Check(w)); |
| 963 | r = PyBytes_AS_STRING(w); |
| 964 | rn = PyBytes_GET_SIZE(w); |
| 965 | Py_MEMCPY(p, r, rn); |
| 966 | p += rn; |
| 967 | Py_DECREF(w); |
| 968 | s = t; |
| 969 | } else { |
| 970 | *p++ = *s++; |
| 971 | } |
| 972 | continue; |
| 973 | } |
| 974 | s++; |
| 975 | if (s==end) { |
| 976 | PyErr_SetString(PyExc_ValueError, |
| 977 | "Trailing \\ in string"); |
| 978 | goto failed; |
| 979 | } |
| 980 | switch (*s++) { |
| 981 | /* XXX This assumes ASCII! */ |
| 982 | case '\n': break; |
| 983 | case '\\': *p++ = '\\'; break; |
| 984 | case '\'': *p++ = '\''; break; |
| 985 | case '\"': *p++ = '\"'; break; |
| 986 | case 'b': *p++ = '\b'; break; |
| 987 | case 'f': *p++ = '\014'; break; /* FF */ |
| 988 | case 't': *p++ = '\t'; break; |
| 989 | case 'n': *p++ = '\n'; break; |
| 990 | case 'r': *p++ = '\r'; break; |
| 991 | case 'v': *p++ = '\013'; break; /* VT */ |
| 992 | case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
| 993 | case '0': case '1': case '2': case '3': |
| 994 | case '4': case '5': case '6': case '7': |
| 995 | c = s[-1] - '0'; |
| 996 | if (s < end && '0' <= *s && *s <= '7') { |
| 997 | c = (c<<3) + *s++ - '0'; |
| 998 | if (s < end && '0' <= *s && *s <= '7') |
| 999 | c = (c<<3) + *s++ - '0'; |
| 1000 | } |
| 1001 | *p++ = c; |
| 1002 | break; |
| 1003 | case 'x': |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 1004 | if (s+1 < end && Py_ISXDIGIT(s[0]) && Py_ISXDIGIT(s[1])) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | unsigned int x = 0; |
| 1006 | c = Py_CHARMASK(*s); |
| 1007 | s++; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 1008 | if (Py_ISDIGIT(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | x = c - '0'; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 1010 | else if (Py_ISLOWER(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1011 | x = 10 + c - 'a'; |
| 1012 | else |
| 1013 | x = 10 + c - 'A'; |
| 1014 | x = x << 4; |
| 1015 | c = Py_CHARMASK(*s); |
| 1016 | s++; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 1017 | if (Py_ISDIGIT(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1018 | x += c - '0'; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 1019 | else if (Py_ISLOWER(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | x += 10 + c - 'a'; |
| 1021 | else |
| 1022 | x += 10 + c - 'A'; |
| 1023 | *p++ = x; |
| 1024 | break; |
| 1025 | } |
| 1026 | if (!errors || strcmp(errors, "strict") == 0) { |
Serhiy Storchaka | 5e61f14 | 2013-02-10 17:36:00 +0200 | [diff] [blame] | 1027 | PyErr_Format(PyExc_ValueError, |
| 1028 | "invalid \\x escape at position %d", |
Serhiy Storchaka | 801d955 | 2013-02-10 17:42:01 +0200 | [diff] [blame] | 1029 | s - 2 - (end - len)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1030 | goto failed; |
| 1031 | } |
| 1032 | if (strcmp(errors, "replace") == 0) { |
| 1033 | *p++ = '?'; |
| 1034 | } else if (strcmp(errors, "ignore") == 0) |
| 1035 | /* do nothing */; |
| 1036 | else { |
| 1037 | PyErr_Format(PyExc_ValueError, |
| 1038 | "decoding error; unknown " |
| 1039 | "error handling code: %.400s", |
| 1040 | errors); |
| 1041 | goto failed; |
| 1042 | } |
Serhiy Storchaka | ace3ad3 | 2013-01-25 23:31:43 +0200 | [diff] [blame] | 1043 | /* skip \x */ |
| 1044 | if (s < end && Py_ISXDIGIT(s[0])) |
| 1045 | s++; /* and a hexdigit */ |
| 1046 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1047 | default: |
| 1048 | *p++ = '\\'; |
| 1049 | s--; |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 1050 | goto non_esc; /* an arbitrary number of unescaped |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | UTF-8 bytes may follow. */ |
| 1052 | } |
| 1053 | } |
| 1054 | if (p-buf < newlen) |
| 1055 | _PyBytes_Resize(&v, p - buf); |
| 1056 | return v; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1057 | failed: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1058 | Py_DECREF(v); |
| 1059 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | /* -------------------------------------------------------------------- */ |
| 1063 | /* object api */ |
| 1064 | |
| 1065 | Py_ssize_t |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1066 | PyBytes_Size(PyObject *op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1067 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1068 | if (!PyBytes_Check(op)) { |
| 1069 | PyErr_Format(PyExc_TypeError, |
| 1070 | "expected bytes, %.200s found", Py_TYPE(op)->tp_name); |
| 1071 | return -1; |
| 1072 | } |
| 1073 | return Py_SIZE(op); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | char * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1077 | PyBytes_AsString(PyObject *op) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1078 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | if (!PyBytes_Check(op)) { |
| 1080 | PyErr_Format(PyExc_TypeError, |
| 1081 | "expected bytes, %.200s found", Py_TYPE(op)->tp_name); |
| 1082 | return NULL; |
| 1083 | } |
| 1084 | return ((PyBytesObject *)op)->ob_sval; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | int |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1088 | PyBytes_AsStringAndSize(PyObject *obj, |
| 1089 | char **s, |
| 1090 | Py_ssize_t *len) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1091 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | if (s == NULL) { |
| 1093 | PyErr_BadInternalCall(); |
| 1094 | return -1; |
| 1095 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1096 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | if (!PyBytes_Check(obj)) { |
| 1098 | PyErr_Format(PyExc_TypeError, |
| 1099 | "expected bytes, %.200s found", Py_TYPE(obj)->tp_name); |
| 1100 | return -1; |
| 1101 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1102 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1103 | *s = PyBytes_AS_STRING(obj); |
| 1104 | if (len != NULL) |
| 1105 | *len = PyBytes_GET_SIZE(obj); |
| 1106 | else if (strlen(*s) != (size_t)PyBytes_GET_SIZE(obj)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1107 | PyErr_SetString(PyExc_ValueError, |
| 1108 | "embedded null byte"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1109 | return -1; |
| 1110 | } |
| 1111 | return 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1112 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1113 | |
| 1114 | /* -------------------------------------------------------------------- */ |
| 1115 | /* Methods */ |
| 1116 | |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1117 | #include "stringlib/stringdefs.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1118 | |
| 1119 | #include "stringlib/fastsearch.h" |
| 1120 | #include "stringlib/count.h" |
| 1121 | #include "stringlib/find.h" |
Antoine Pitrou | cfc22b4 | 2012-10-16 21:07:23 +0200 | [diff] [blame] | 1122 | #include "stringlib/join.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1123 | #include "stringlib/partition.h" |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1124 | #include "stringlib/split.h" |
Gregory P. Smith | 60d241f | 2007-10-16 06:31:30 +0000 | [diff] [blame] | 1125 | #include "stringlib/ctype.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1126 | |
Eric Smith | 0f78bff | 2009-11-30 01:01:42 +0000 | [diff] [blame] | 1127 | #include "stringlib/transmogrify.h" |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1128 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1129 | PyObject * |
| 1130 | PyBytes_Repr(PyObject *obj, int smartquotes) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1131 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1132 | PyBytesObject* op = (PyBytesObject*) obj; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1133 | Py_ssize_t i, length = Py_SIZE(op); |
Benjamin Peterson | d48bc94 | 2014-09-29 19:12:26 -0400 | [diff] [blame] | 1134 | Py_ssize_t newsize, squotes, dquotes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1135 | PyObject *v; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1136 | unsigned char quote, *s, *p; |
| 1137 | |
| 1138 | /* Compute size of output string */ |
| 1139 | squotes = dquotes = 0; |
| 1140 | newsize = 3; /* b'' */ |
| 1141 | s = (unsigned char*)op->ob_sval; |
| 1142 | for (i = 0; i < length; i++) { |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 1143 | Py_ssize_t incr = 1; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1144 | switch(s[i]) { |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 1145 | case '\'': squotes++; break; |
| 1146 | case '"': dquotes++; break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1147 | case '\\': case '\t': case '\n': case '\r': |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 1148 | incr = 2; break; /* \C */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1149 | default: |
| 1150 | if (s[i] < ' ' || s[i] >= 0x7f) |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 1151 | incr = 4; /* \xHH */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1152 | } |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 1153 | if (newsize > PY_SSIZE_T_MAX - incr) |
| 1154 | goto overflow; |
| 1155 | newsize += incr; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1156 | } |
| 1157 | quote = '\''; |
| 1158 | if (smartquotes && squotes && !dquotes) |
| 1159 | quote = '"'; |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 1160 | if (squotes && quote == '\'') { |
| 1161 | if (newsize > PY_SSIZE_T_MAX - squotes) |
| 1162 | goto overflow; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1163 | newsize += squotes; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1164 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1165 | |
| 1166 | v = PyUnicode_New(newsize, 127); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1167 | if (v == NULL) { |
| 1168 | return NULL; |
| 1169 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1170 | p = PyUnicode_1BYTE_DATA(v); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1171 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1172 | *p++ = 'b', *p++ = quote; |
| 1173 | for (i = 0; i < length; i++) { |
| 1174 | unsigned char c = op->ob_sval[i]; |
| 1175 | if (c == quote || c == '\\') |
| 1176 | *p++ = '\\', *p++ = c; |
| 1177 | else if (c == '\t') |
| 1178 | *p++ = '\\', *p++ = 't'; |
| 1179 | else if (c == '\n') |
| 1180 | *p++ = '\\', *p++ = 'n'; |
| 1181 | else if (c == '\r') |
| 1182 | *p++ = '\\', *p++ = 'r'; |
| 1183 | else if (c < ' ' || c >= 0x7f) { |
| 1184 | *p++ = '\\'; |
| 1185 | *p++ = 'x'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 1186 | *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
| 1187 | *p++ = Py_hexdigits[c & 0xf]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1188 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1189 | else |
| 1190 | *p++ = c; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1191 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1192 | *p++ = quote; |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 1193 | assert(_PyUnicode_CheckConsistency(v, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1194 | return v; |
Benjamin Peterson | 42ff105 | 2014-09-29 19:01:18 -0400 | [diff] [blame] | 1195 | |
| 1196 | overflow: |
| 1197 | PyErr_SetString(PyExc_OverflowError, |
| 1198 | "bytes object is too large to make repr"); |
| 1199 | return NULL; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1202 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1203 | bytes_repr(PyObject *op) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1204 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1205 | return PyBytes_Repr(op, 1); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1208 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1209 | bytes_str(PyObject *op) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1210 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1211 | if (Py_BytesWarningFlag) { |
| 1212 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 1213 | "str() on a bytes instance", 1)) |
| 1214 | return NULL; |
| 1215 | } |
| 1216 | return bytes_repr(op); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1219 | static Py_ssize_t |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1220 | bytes_length(PyBytesObject *a) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1221 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1222 | return Py_SIZE(a); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1223 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1224 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1225 | /* This is also used by PyBytes_Concat() */ |
| 1226 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1227 | bytes_concat(PyObject *a, PyObject *b) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1228 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1229 | Py_ssize_t size; |
| 1230 | Py_buffer va, vb; |
| 1231 | PyObject *result = NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1232 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1233 | va.len = -1; |
| 1234 | vb.len = -1; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1235 | if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 || |
| 1236 | PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1237 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 1238 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
| 1239 | goto done; |
| 1240 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1242 | /* Optimize end cases */ |
| 1243 | if (va.len == 0 && PyBytes_CheckExact(b)) { |
| 1244 | result = b; |
| 1245 | Py_INCREF(result); |
| 1246 | goto done; |
| 1247 | } |
| 1248 | if (vb.len == 0 && PyBytes_CheckExact(a)) { |
| 1249 | result = a; |
| 1250 | Py_INCREF(result); |
| 1251 | goto done; |
| 1252 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1253 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1254 | size = va.len + vb.len; |
| 1255 | if (size < 0) { |
| 1256 | PyErr_NoMemory(); |
| 1257 | goto done; |
| 1258 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1259 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1260 | result = PyBytes_FromStringAndSize(NULL, size); |
| 1261 | if (result != NULL) { |
| 1262 | memcpy(PyBytes_AS_STRING(result), va.buf, va.len); |
| 1263 | memcpy(PyBytes_AS_STRING(result) + va.len, vb.buf, vb.len); |
| 1264 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1265 | |
| 1266 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1267 | if (va.len != -1) |
| 1268 | PyBuffer_Release(&va); |
| 1269 | if (vb.len != -1) |
| 1270 | PyBuffer_Release(&vb); |
| 1271 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1272 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1273 | |
| 1274 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1275 | bytes_repeat(PyBytesObject *a, Py_ssize_t n) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1276 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1277 | Py_ssize_t i; |
| 1278 | Py_ssize_t j; |
| 1279 | Py_ssize_t size; |
| 1280 | PyBytesObject *op; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | size_t nbytes; |
| 1282 | if (n < 0) |
| 1283 | n = 0; |
| 1284 | /* watch out for overflows: the size can overflow int, |
| 1285 | * and the # of bytes needed can overflow size_t |
| 1286 | */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1287 | if (n > 0 && Py_SIZE(a) > PY_SSIZE_T_MAX / n) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 | PyErr_SetString(PyExc_OverflowError, |
| 1289 | "repeated bytes are too long"); |
| 1290 | return NULL; |
| 1291 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 1292 | size = Py_SIZE(a) * n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1293 | if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) { |
| 1294 | Py_INCREF(a); |
| 1295 | return (PyObject *)a; |
| 1296 | } |
| 1297 | nbytes = (size_t)size; |
| 1298 | if (nbytes + PyBytesObject_SIZE <= nbytes) { |
| 1299 | PyErr_SetString(PyExc_OverflowError, |
| 1300 | "repeated bytes are too long"); |
| 1301 | return NULL; |
| 1302 | } |
| 1303 | op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + nbytes); |
| 1304 | if (op == NULL) |
| 1305 | return PyErr_NoMemory(); |
Christian Heimes | d3afe78 | 2013-12-04 09:27:47 +0100 | [diff] [blame] | 1306 | (void)PyObject_INIT_VAR(op, &PyBytes_Type, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1307 | op->ob_shash = -1; |
| 1308 | op->ob_sval[size] = '\0'; |
| 1309 | if (Py_SIZE(a) == 1 && n > 0) { |
| 1310 | memset(op->ob_sval, a->ob_sval[0] , n); |
| 1311 | return (PyObject *) op; |
| 1312 | } |
| 1313 | i = 0; |
| 1314 | if (i < size) { |
| 1315 | Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a)); |
| 1316 | i = Py_SIZE(a); |
| 1317 | } |
| 1318 | while (i < size) { |
| 1319 | j = (i <= size-i) ? i : size-i; |
| 1320 | Py_MEMCPY(op->ob_sval+i, op->ob_sval, j); |
| 1321 | i += j; |
| 1322 | } |
| 1323 | return (PyObject *) op; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1326 | static int |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1327 | bytes_contains(PyObject *self, PyObject *arg) |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1328 | { |
| 1329 | Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
| 1330 | if (ival == -1 && PyErr_Occurred()) { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1331 | Py_buffer varg; |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 1332 | Py_ssize_t pos; |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1333 | PyErr_Clear(); |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1334 | if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0) |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1335 | return -1; |
| 1336 | pos = stringlib_find(PyBytes_AS_STRING(self), Py_SIZE(self), |
| 1337 | varg.buf, varg.len, 0); |
| 1338 | PyBuffer_Release(&varg); |
| 1339 | return pos >= 0; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1340 | } |
| 1341 | if (ival < 0 || ival >= 256) { |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 1342 | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
| 1343 | return -1; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1344 | } |
| 1345 | |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 1346 | 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] | 1347 | } |
| 1348 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1349 | static PyObject * |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 1350 | bytes_item(PyBytesObject *a, Py_ssize_t i) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1351 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1352 | if (i < 0 || i >= Py_SIZE(a)) { |
| 1353 | PyErr_SetString(PyExc_IndexError, "index out of range"); |
| 1354 | return NULL; |
| 1355 | } |
| 1356 | return PyLong_FromLong((unsigned char)a->ob_sval[i]); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1359 | Py_LOCAL(int) |
| 1360 | bytes_compare_eq(PyBytesObject *a, PyBytesObject *b) |
| 1361 | { |
| 1362 | int cmp; |
| 1363 | Py_ssize_t len; |
| 1364 | |
| 1365 | len = Py_SIZE(a); |
| 1366 | if (Py_SIZE(b) != len) |
| 1367 | return 0; |
| 1368 | |
| 1369 | if (a->ob_sval[0] != b->ob_sval[0]) |
| 1370 | return 0; |
| 1371 | |
| 1372 | cmp = memcmp(a->ob_sval, b->ob_sval, len); |
| 1373 | return (cmp == 0); |
| 1374 | } |
| 1375 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1376 | static PyObject* |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1377 | bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1378 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1379 | int c; |
| 1380 | Py_ssize_t len_a, len_b; |
| 1381 | Py_ssize_t min_len; |
| 1382 | PyObject *result; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1384 | /* Make sure both arguments are strings. */ |
| 1385 | if (!(PyBytes_Check(a) && PyBytes_Check(b))) { |
| 1386 | if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE) && |
| 1387 | (PyObject_IsInstance((PyObject*)a, |
| 1388 | (PyObject*)&PyUnicode_Type) || |
| 1389 | PyObject_IsInstance((PyObject*)b, |
| 1390 | (PyObject*)&PyUnicode_Type))) { |
| 1391 | if (PyErr_WarnEx(PyExc_BytesWarning, |
| 1392 | "Comparison between bytes and string", 1)) |
| 1393 | return NULL; |
| 1394 | } |
| 1395 | result = Py_NotImplemented; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | } |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1397 | else if (a == b) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1398 | switch (op) { |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 1399 | case Py_EQ: |
| 1400 | case Py_LE: |
| 1401 | case Py_GE: |
| 1402 | /* a string is equal to itself */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1403 | result = Py_True; |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1404 | break; |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 1405 | case Py_NE: |
| 1406 | case Py_LT: |
| 1407 | case Py_GT: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | result = Py_False; |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1409 | break; |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 1410 | default: |
| 1411 | PyErr_BadArgument(); |
| 1412 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1413 | } |
| 1414 | } |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1415 | else if (op == Py_EQ || op == Py_NE) { |
| 1416 | int eq = bytes_compare_eq(a, b); |
| 1417 | eq ^= (op == Py_NE); |
| 1418 | result = eq ? Py_True : Py_False; |
| 1419 | } |
| 1420 | else { |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 1421 | len_a = Py_SIZE(a); |
| 1422 | len_b = Py_SIZE(b); |
| 1423 | min_len = Py_MIN(len_a, len_b); |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1424 | if (min_len > 0) { |
| 1425 | c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 1426 | if (c == 0) |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1427 | c = memcmp(a->ob_sval, b->ob_sval, min_len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1428 | } |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1429 | else |
| 1430 | c = 0; |
| 1431 | if (c == 0) |
| 1432 | c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
| 1433 | switch (op) { |
| 1434 | case Py_LT: c = c < 0; break; |
| 1435 | case Py_LE: c = c <= 0; break; |
| 1436 | case Py_GT: c = c > 0; break; |
| 1437 | case Py_GE: c = c >= 0; break; |
| 1438 | default: |
Victor Stinner | fd9e44d | 2013-11-04 11:23:05 +0100 | [diff] [blame] | 1439 | PyErr_BadArgument(); |
| 1440 | return NULL; |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1441 | } |
| 1442 | result = c ? Py_True : Py_False; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1443 | } |
Victor Stinner | c8bc537 | 2013-11-04 11:08:10 +0100 | [diff] [blame] | 1444 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1445 | Py_INCREF(result); |
| 1446 | return result; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 1449 | static Py_hash_t |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1450 | bytes_hash(PyBytesObject *a) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1451 | { |
Antoine Pitrou | ce4a9da | 2011-11-21 20:46:33 +0100 | [diff] [blame] | 1452 | if (a->ob_shash == -1) { |
| 1453 | /* Can't fail */ |
Christian Heimes | 985ecdc | 2013-11-20 11:46:18 +0100 | [diff] [blame] | 1454 | a->ob_shash = _Py_HashBytes(a->ob_sval, Py_SIZE(a)); |
Antoine Pitrou | ce4a9da | 2011-11-21 20:46:33 +0100 | [diff] [blame] | 1455 | } |
| 1456 | return a->ob_shash; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1459 | static PyObject* |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1460 | bytes_subscript(PyBytesObject* self, PyObject* item) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1461 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1462 | if (PyIndex_Check(item)) { |
| 1463 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 1464 | if (i == -1 && PyErr_Occurred()) |
| 1465 | return NULL; |
| 1466 | if (i < 0) |
| 1467 | i += PyBytes_GET_SIZE(self); |
| 1468 | if (i < 0 || i >= PyBytes_GET_SIZE(self)) { |
| 1469 | PyErr_SetString(PyExc_IndexError, |
| 1470 | "index out of range"); |
| 1471 | return NULL; |
| 1472 | } |
| 1473 | return PyLong_FromLong((unsigned char)self->ob_sval[i]); |
| 1474 | } |
| 1475 | else if (PySlice_Check(item)) { |
| 1476 | Py_ssize_t start, stop, step, slicelength, cur, i; |
| 1477 | char* source_buf; |
| 1478 | char* result_buf; |
| 1479 | PyObject* result; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1480 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1481 | if (PySlice_GetIndicesEx(item, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1482 | PyBytes_GET_SIZE(self), |
| 1483 | &start, &stop, &step, &slicelength) < 0) { |
| 1484 | return NULL; |
| 1485 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1487 | if (slicelength <= 0) { |
| 1488 | return PyBytes_FromStringAndSize("", 0); |
| 1489 | } |
| 1490 | else if (start == 0 && step == 1 && |
| 1491 | slicelength == PyBytes_GET_SIZE(self) && |
| 1492 | PyBytes_CheckExact(self)) { |
| 1493 | Py_INCREF(self); |
| 1494 | return (PyObject *)self; |
| 1495 | } |
| 1496 | else if (step == 1) { |
| 1497 | return PyBytes_FromStringAndSize( |
| 1498 | PyBytes_AS_STRING(self) + start, |
| 1499 | slicelength); |
| 1500 | } |
| 1501 | else { |
| 1502 | source_buf = PyBytes_AS_STRING(self); |
| 1503 | result = PyBytes_FromStringAndSize(NULL, slicelength); |
| 1504 | if (result == NULL) |
| 1505 | return NULL; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1507 | result_buf = PyBytes_AS_STRING(result); |
| 1508 | for (cur = start, i = 0; i < slicelength; |
| 1509 | cur += step, i++) { |
| 1510 | result_buf[i] = source_buf[cur]; |
| 1511 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | return result; |
| 1514 | } |
| 1515 | } |
| 1516 | else { |
| 1517 | PyErr_Format(PyExc_TypeError, |
Terry Jan Reedy | ffff144 | 2014-08-02 01:30:37 -0400 | [diff] [blame] | 1518 | "byte indices must be integers or slices, not %.200s", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1519 | Py_TYPE(item)->tp_name); |
| 1520 | return NULL; |
| 1521 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | static int |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1525 | bytes_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1526 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1527 | return PyBuffer_FillInfo(view, (PyObject*)self, (void *)self->ob_sval, Py_SIZE(self), |
| 1528 | 1, flags); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1531 | static PySequenceMethods bytes_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1532 | (lenfunc)bytes_length, /*sq_length*/ |
| 1533 | (binaryfunc)bytes_concat, /*sq_concat*/ |
| 1534 | (ssizeargfunc)bytes_repeat, /*sq_repeat*/ |
| 1535 | (ssizeargfunc)bytes_item, /*sq_item*/ |
| 1536 | 0, /*sq_slice*/ |
| 1537 | 0, /*sq_ass_item*/ |
| 1538 | 0, /*sq_ass_slice*/ |
| 1539 | (objobjproc)bytes_contains /*sq_contains*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1540 | }; |
| 1541 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1542 | static PyMappingMethods bytes_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1543 | (lenfunc)bytes_length, |
| 1544 | (binaryfunc)bytes_subscript, |
| 1545 | 0, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1546 | }; |
| 1547 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1548 | static PyBufferProcs bytes_as_buffer = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1549 | (getbufferproc)bytes_buffer_getbuffer, |
| 1550 | NULL, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1551 | }; |
| 1552 | |
| 1553 | |
| 1554 | #define LEFTSTRIP 0 |
| 1555 | #define RIGHTSTRIP 1 |
| 1556 | #define BOTHSTRIP 2 |
| 1557 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1558 | /*[clinic input] |
| 1559 | bytes.split |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1560 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1561 | sep: object = None |
| 1562 | The delimiter according which to split the bytes. |
| 1563 | None (the default value) means split on ASCII whitespace characters |
| 1564 | (space, tab, return, newline, formfeed, vertical tab). |
| 1565 | maxsplit: Py_ssize_t = -1 |
| 1566 | Maximum number of splits to do. |
| 1567 | -1 (the default value) means no limit. |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1568 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1569 | Return a list of the sections in the bytes, using sep as the delimiter. |
| 1570 | [clinic start generated code]*/ |
| 1571 | |
| 1572 | PyDoc_STRVAR(bytes_split__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1573 | "split($self, /, sep=None, maxsplit=-1)\n" |
| 1574 | "--\n" |
| 1575 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1576 | "Return a list of the sections in the bytes, using sep as the delimiter.\n" |
| 1577 | "\n" |
| 1578 | " sep\n" |
| 1579 | " The delimiter according which to split the bytes.\n" |
| 1580 | " None (the default value) means split on ASCII whitespace characters\n" |
| 1581 | " (space, tab, return, newline, formfeed, vertical tab).\n" |
| 1582 | " maxsplit\n" |
| 1583 | " Maximum number of splits to do.\n" |
| 1584 | " -1 (the default value) means no limit."); |
| 1585 | |
| 1586 | #define BYTES_SPLIT_METHODDEF \ |
| 1587 | {"split", (PyCFunction)bytes_split, METH_VARARGS|METH_KEYWORDS, bytes_split__doc__}, |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1588 | |
| 1589 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1590 | bytes_split_impl(PyBytesObject*self, PyObject *sep, Py_ssize_t maxsplit); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1591 | |
| 1592 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1593 | bytes_split(PyBytesObject*self, PyObject *args, PyObject *kwargs) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1594 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1595 | PyObject *return_value = NULL; |
| 1596 | static char *_keywords[] = {"sep", "maxsplit", NULL}; |
| 1597 | PyObject *sep = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1598 | Py_ssize_t maxsplit = -1; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1599 | |
| 1600 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 1601 | "|On:split", _keywords, |
| 1602 | &sep, &maxsplit)) |
| 1603 | goto exit; |
| 1604 | return_value = bytes_split_impl(self, sep, maxsplit); |
| 1605 | |
| 1606 | exit: |
| 1607 | return return_value; |
| 1608 | } |
| 1609 | |
| 1610 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1611 | bytes_split_impl(PyBytesObject*self, PyObject *sep, Py_ssize_t maxsplit) |
| 1612 | /*[clinic end generated code: output=c80a47afdd505975 input=8b809b39074abbfa]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1613 | { |
| 1614 | Py_ssize_t len = PyBytes_GET_SIZE(self), n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1615 | const char *s = PyBytes_AS_STRING(self), *sub; |
| 1616 | Py_buffer vsub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1617 | PyObject *list; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1618 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1619 | if (maxsplit < 0) |
| 1620 | maxsplit = PY_SSIZE_T_MAX; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1621 | if (sep == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1622 | return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1623 | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1624 | return NULL; |
| 1625 | sub = vsub.buf; |
| 1626 | n = vsub.len; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 1627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1628 | list = stringlib_split((PyObject*) self, s, len, sub, n, maxsplit); |
| 1629 | PyBuffer_Release(&vsub); |
| 1630 | return list; |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1633 | /*[clinic input] |
| 1634 | bytes.partition |
| 1635 | |
| 1636 | self: self(type="PyBytesObject *") |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1637 | sep: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1638 | / |
| 1639 | |
| 1640 | Partition the bytes into three parts using the given separator. |
| 1641 | |
| 1642 | This will search for the separator sep in the bytes. If the separator is found, |
| 1643 | returns a 3-tuple containing the part before the separator, the separator |
| 1644 | itself, and the part after it. |
| 1645 | |
| 1646 | If the separator is not found, returns a 3-tuple containing the original bytes |
| 1647 | object and two empty bytes objects. |
| 1648 | [clinic start generated code]*/ |
| 1649 | |
| 1650 | PyDoc_STRVAR(bytes_partition__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1651 | "partition($self, sep, /)\n" |
| 1652 | "--\n" |
| 1653 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1654 | "Partition the bytes into three parts using the given separator.\n" |
| 1655 | "\n" |
| 1656 | "This will search for the separator sep in the bytes. If the separator is found,\n" |
| 1657 | "returns a 3-tuple containing the part before the separator, the separator\n" |
| 1658 | "itself, and the part after it.\n" |
| 1659 | "\n" |
| 1660 | "If the separator is not found, returns a 3-tuple containing the original bytes\n" |
| 1661 | "object and two empty bytes objects."); |
| 1662 | |
| 1663 | #define BYTES_PARTITION_METHODDEF \ |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1664 | {"partition", (PyCFunction)bytes_partition, METH_VARARGS, bytes_partition__doc__}, |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1665 | |
| 1666 | static PyObject * |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1667 | bytes_partition_impl(PyBytesObject *self, Py_buffer *sep); |
| 1668 | |
| 1669 | static PyObject * |
| 1670 | bytes_partition(PyBytesObject *self, PyObject *args) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1671 | { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1672 | PyObject *return_value = NULL; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1673 | Py_buffer sep = {NULL, NULL}; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1674 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1675 | if (!PyArg_ParseTuple(args, |
| 1676 | "y*:partition", |
| 1677 | &sep)) |
| 1678 | goto exit; |
| 1679 | return_value = bytes_partition_impl(self, &sep); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1680 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1681 | exit: |
| 1682 | /* Cleanup for sep */ |
| 1683 | if (sep.obj) |
| 1684 | PyBuffer_Release(&sep); |
| 1685 | |
| 1686 | return return_value; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1689 | static PyObject * |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1690 | bytes_partition_impl(PyBytesObject *self, Py_buffer *sep) |
| 1691 | /*[clinic end generated code: output=3006727cfbf83aa4 input=bc855dc63ca949de]*/ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1692 | { |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1693 | return stringlib_partition( |
| 1694 | (PyObject*) self, |
| 1695 | PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1696 | sep->obj, (const char *)sep->buf, sep->len |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1697 | ); |
| 1698 | } |
| 1699 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1700 | /*[clinic input] |
| 1701 | bytes.rpartition |
| 1702 | |
| 1703 | self: self(type="PyBytesObject *") |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1704 | sep: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1705 | / |
| 1706 | |
| 1707 | Partition the bytes into three parts using the given separator. |
| 1708 | |
| 1709 | This will search for the separator sep in the bytes, starting and the end. If |
| 1710 | the separator is found, returns a 3-tuple containing the part before the |
| 1711 | separator, the separator itself, and the part after it. |
| 1712 | |
| 1713 | If the separator is not found, returns a 3-tuple containing two empty bytes |
| 1714 | objects and the original bytes object. |
| 1715 | [clinic start generated code]*/ |
| 1716 | |
| 1717 | PyDoc_STRVAR(bytes_rpartition__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1718 | "rpartition($self, sep, /)\n" |
| 1719 | "--\n" |
| 1720 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1721 | "Partition the bytes into three parts using the given separator.\n" |
| 1722 | "\n" |
| 1723 | "This will search for the separator sep in the bytes, starting and the end. If\n" |
| 1724 | "the separator is found, returns a 3-tuple containing the part before the\n" |
| 1725 | "separator, the separator itself, and the part after it.\n" |
| 1726 | "\n" |
| 1727 | "If the separator is not found, returns a 3-tuple containing two empty bytes\n" |
| 1728 | "objects and the original bytes object."); |
| 1729 | |
| 1730 | #define BYTES_RPARTITION_METHODDEF \ |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1731 | {"rpartition", (PyCFunction)bytes_rpartition, METH_VARARGS, bytes_rpartition__doc__}, |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1732 | |
| 1733 | static PyObject * |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1734 | bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep); |
| 1735 | |
| 1736 | static PyObject * |
| 1737 | bytes_rpartition(PyBytesObject *self, PyObject *args) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1738 | { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1739 | PyObject *return_value = NULL; |
| 1740 | Py_buffer sep = {NULL, NULL}; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1741 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1742 | if (!PyArg_ParseTuple(args, |
| 1743 | "y*:rpartition", |
| 1744 | &sep)) |
| 1745 | goto exit; |
| 1746 | return_value = bytes_rpartition_impl(self, &sep); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1747 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1748 | exit: |
| 1749 | /* Cleanup for sep */ |
| 1750 | if (sep.obj) |
| 1751 | PyBuffer_Release(&sep); |
| 1752 | |
| 1753 | return return_value; |
| 1754 | } |
| 1755 | |
| 1756 | static PyObject * |
| 1757 | bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep) |
| 1758 | /*[clinic end generated code: output=57b169dc47fa90e8 input=6588fff262a9170e]*/ |
| 1759 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1760 | return stringlib_rpartition( |
| 1761 | (PyObject*) self, |
| 1762 | PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1763 | sep->obj, (const char *)sep->buf, sep->len |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1764 | ); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1767 | /*[clinic input] |
| 1768 | bytes.rsplit = bytes.split |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1769 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1770 | Return a list of the sections in the bytes, using sep as the delimiter. |
| 1771 | |
| 1772 | Splitting is done starting at the end of the bytes and working to the front. |
| 1773 | [clinic start generated code]*/ |
| 1774 | |
| 1775 | PyDoc_STRVAR(bytes_rsplit__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1776 | "rsplit($self, /, sep=None, maxsplit=-1)\n" |
| 1777 | "--\n" |
| 1778 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1779 | "Return a list of the sections in the bytes, using sep as the delimiter.\n" |
| 1780 | "\n" |
| 1781 | " sep\n" |
| 1782 | " The delimiter according which to split the bytes.\n" |
| 1783 | " None (the default value) means split on ASCII whitespace characters\n" |
| 1784 | " (space, tab, return, newline, formfeed, vertical tab).\n" |
| 1785 | " maxsplit\n" |
| 1786 | " Maximum number of splits to do.\n" |
| 1787 | " -1 (the default value) means no limit.\n" |
| 1788 | "\n" |
| 1789 | "Splitting is done starting at the end of the bytes and working to the front."); |
| 1790 | |
| 1791 | #define BYTES_RSPLIT_METHODDEF \ |
| 1792 | {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS|METH_KEYWORDS, bytes_rsplit__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1793 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1794 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1795 | bytes_rsplit_impl(PyBytesObject*self, PyObject *sep, Py_ssize_t maxsplit); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1796 | |
| 1797 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1798 | bytes_rsplit(PyBytesObject*self, PyObject *args, PyObject *kwargs) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1799 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1800 | PyObject *return_value = NULL; |
| 1801 | static char *_keywords[] = {"sep", "maxsplit", NULL}; |
| 1802 | PyObject *sep = Py_None; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1803 | Py_ssize_t maxsplit = -1; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1804 | |
| 1805 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 1806 | "|On:rsplit", _keywords, |
| 1807 | &sep, &maxsplit)) |
| 1808 | goto exit; |
| 1809 | return_value = bytes_rsplit_impl(self, sep, maxsplit); |
| 1810 | |
| 1811 | exit: |
| 1812 | return return_value; |
| 1813 | } |
| 1814 | |
| 1815 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1816 | bytes_rsplit_impl(PyBytesObject*self, PyObject *sep, Py_ssize_t maxsplit) |
| 1817 | /*[clinic end generated code: output=f86feddedbd7b26d input=0f86c9f28f7d7b7b]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1818 | { |
| 1819 | Py_ssize_t len = PyBytes_GET_SIZE(self), n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1820 | const char *s = PyBytes_AS_STRING(self), *sub; |
| 1821 | Py_buffer vsub; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1822 | PyObject *list; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1823 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1824 | if (maxsplit < 0) |
| 1825 | maxsplit = PY_SSIZE_T_MAX; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1826 | if (sep == Py_None) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1827 | return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 1828 | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1829 | return NULL; |
| 1830 | sub = vsub.buf; |
| 1831 | n = vsub.len; |
Guido van Rossum | 8f95067 | 2007-09-10 16:53:45 +0000 | [diff] [blame] | 1832 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1833 | list = stringlib_rsplit((PyObject*) self, s, len, sub, n, maxsplit); |
| 1834 | PyBuffer_Release(&vsub); |
| 1835 | return list; |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1838 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1839 | /*[clinic input] |
| 1840 | bytes.join |
| 1841 | |
| 1842 | iterable_of_bytes: object |
| 1843 | / |
| 1844 | |
| 1845 | Concatenate any number of bytes objects. |
| 1846 | |
| 1847 | The bytes whose method is called is inserted in between each pair. |
| 1848 | |
| 1849 | The result is returned as a new bytes object. |
| 1850 | |
| 1851 | Example: b'.'.join([b'ab', b'pq', b'rs']) -> b'ab.pq.rs'. |
| 1852 | [clinic start generated code]*/ |
| 1853 | |
| 1854 | PyDoc_STRVAR(bytes_join__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1855 | "join($self, iterable_of_bytes, /)\n" |
| 1856 | "--\n" |
| 1857 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 1858 | "Concatenate any number of bytes objects.\n" |
| 1859 | "\n" |
| 1860 | "The bytes whose method is called is inserted in between each pair.\n" |
| 1861 | "\n" |
| 1862 | "The result is returned as a new bytes object.\n" |
| 1863 | "\n" |
| 1864 | "Example: b\'.\'.join([b\'ab\', b\'pq\', b\'rs\']) -> b\'ab.pq.rs\'."); |
| 1865 | |
| 1866 | #define BYTES_JOIN_METHODDEF \ |
| 1867 | {"join", (PyCFunction)bytes_join, METH_O, bytes_join__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1868 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1869 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1870 | bytes_join(PyBytesObject*self, PyObject *iterable_of_bytes) |
| 1871 | /*[clinic end generated code: output=e541a14a8da97908 input=7fe377b95bd549d2]*/ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1872 | { |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1873 | return stringlib_bytes_join((PyObject*)self, iterable_of_bytes); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1876 | PyObject * |
| 1877 | _PyBytes_Join(PyObject *sep, PyObject *x) |
| 1878 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1879 | assert(sep != NULL && PyBytes_Check(sep)); |
| 1880 | assert(x != NULL); |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 1881 | return bytes_join((PyBytesObject*)sep, x); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1884 | /* helper macro to fixup start/end slice values */ |
| 1885 | #define ADJUST_INDICES(start, end, len) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1886 | if (end > len) \ |
| 1887 | end = len; \ |
| 1888 | else if (end < 0) { \ |
| 1889 | end += len; \ |
| 1890 | if (end < 0) \ |
| 1891 | end = 0; \ |
| 1892 | } \ |
| 1893 | if (start < 0) { \ |
| 1894 | start += len; \ |
| 1895 | if (start < 0) \ |
| 1896 | start = 0; \ |
| 1897 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1898 | |
| 1899 | Py_LOCAL_INLINE(Py_ssize_t) |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1900 | bytes_find_internal(PyBytesObject *self, PyObject *args, int dir) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1901 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1902 | PyObject *subobj; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1903 | char byte; |
| 1904 | Py_buffer subbuf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1905 | const char *sub; |
| 1906 | Py_ssize_t sub_len; |
| 1907 | Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1908 | Py_ssize_t res; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1909 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1910 | if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex", |
| 1911 | args, &subobj, &byte, &start, &end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1912 | return -2; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1913 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1914 | if (subobj) { |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 1915 | if (PyObject_GetBuffer(subobj, &subbuf, PyBUF_SIMPLE) != 0) |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1916 | return -2; |
| 1917 | |
| 1918 | sub = subbuf.buf; |
| 1919 | sub_len = subbuf.len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1920 | } |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1921 | else { |
| 1922 | sub = &byte; |
| 1923 | sub_len = 1; |
| 1924 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1926 | if (dir > 0) |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1927 | res = stringlib_find_slice( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1928 | PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
| 1929 | sub, sub_len, start, end); |
| 1930 | else |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1931 | res = stringlib_rfind_slice( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1932 | PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
| 1933 | sub, sub_len, start, end); |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 1934 | |
| 1935 | if (subobj) |
| 1936 | PyBuffer_Release(&subbuf); |
| 1937 | |
| 1938 | return res; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | |
| 1942 | PyDoc_STRVAR(find__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1943 | "B.find(sub[, start[, end]]) -> int\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1944 | \n\ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 1945 | Return the lowest index in B where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 1946 | such that sub is contained within B[start:end]. Optional\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1947 | arguments start and end are interpreted as in slice notation.\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1948 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1949 | Return -1 on failure."); |
| 1950 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1951 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1952 | bytes_find(PyBytesObject *self, PyObject *args) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1953 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1954 | Py_ssize_t result = bytes_find_internal(self, args, +1); |
| 1955 | if (result == -2) |
| 1956 | return NULL; |
| 1957 | return PyLong_FromSsize_t(result); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1958 | } |
| 1959 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1960 | |
| 1961 | PyDoc_STRVAR(index__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1962 | "B.index(sub[, start[, end]]) -> int\n\ |
Alexandre Vassalotti | 09121e8 | 2007-12-04 05:51:13 +0000 | [diff] [blame] | 1963 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1964 | Like B.find() but raise ValueError when the substring is not found."); |
| 1965 | |
Alexandre Vassalotti | 09121e8 | 2007-12-04 05:51:13 +0000 | [diff] [blame] | 1966 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1967 | bytes_index(PyBytesObject *self, PyObject *args) |
Alexandre Vassalotti | 09121e8 | 2007-12-04 05:51:13 +0000 | [diff] [blame] | 1968 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 | Py_ssize_t result = bytes_find_internal(self, args, +1); |
| 1970 | if (result == -2) |
| 1971 | return NULL; |
| 1972 | if (result == -1) { |
| 1973 | PyErr_SetString(PyExc_ValueError, |
| 1974 | "substring not found"); |
| 1975 | return NULL; |
| 1976 | } |
| 1977 | return PyLong_FromSsize_t(result); |
Alexandre Vassalotti | 09121e8 | 2007-12-04 05:51:13 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1980 | |
| 1981 | PyDoc_STRVAR(rfind__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 1982 | "B.rfind(sub[, start[, end]]) -> int\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1983 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1984 | Return the highest index in B where substring sub is found,\n\ |
Senthil Kumaran | 53516a8 | 2011-07-27 23:33:54 +0800 | [diff] [blame] | 1985 | such that sub is contained within B[start:end]. Optional\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1986 | arguments start and end are interpreted as in slice notation.\n\ |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1987 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 1988 | Return -1 on failure."); |
| 1989 | |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1990 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 1991 | bytes_rfind(PyBytesObject *self, PyObject *args) |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1992 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1993 | Py_ssize_t result = bytes_find_internal(self, args, -1); |
| 1994 | if (result == -2) |
| 1995 | return NULL; |
| 1996 | return PyLong_FromSsize_t(result); |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 1999 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2000 | PyDoc_STRVAR(rindex__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2001 | "B.rindex(sub[, start[, end]]) -> int\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2002 | \n\ |
| 2003 | Like B.rfind() but raise ValueError when the substring is not found."); |
| 2004 | |
| 2005 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2006 | bytes_rindex(PyBytesObject *self, PyObject *args) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2007 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2008 | Py_ssize_t result = bytes_find_internal(self, args, -1); |
| 2009 | if (result == -2) |
| 2010 | return NULL; |
| 2011 | if (result == -1) { |
| 2012 | PyErr_SetString(PyExc_ValueError, |
| 2013 | "substring not found"); |
| 2014 | return NULL; |
| 2015 | } |
| 2016 | return PyLong_FromSsize_t(result); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2019 | |
| 2020 | Py_LOCAL_INLINE(PyObject *) |
| 2021 | do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2022 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2023 | Py_buffer vsep; |
| 2024 | char *s = PyBytes_AS_STRING(self); |
| 2025 | Py_ssize_t len = PyBytes_GET_SIZE(self); |
| 2026 | char *sep; |
| 2027 | Py_ssize_t seplen; |
| 2028 | Py_ssize_t i, j; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2029 | |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2030 | if (PyObject_GetBuffer(sepobj, &vsep, PyBUF_SIMPLE) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2031 | return NULL; |
| 2032 | sep = vsep.buf; |
| 2033 | seplen = vsep.len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2035 | i = 0; |
| 2036 | if (striptype != RIGHTSTRIP) { |
| 2037 | while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) { |
| 2038 | i++; |
| 2039 | } |
| 2040 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2041 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2042 | j = len; |
| 2043 | if (striptype != LEFTSTRIP) { |
| 2044 | do { |
| 2045 | j--; |
| 2046 | } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen)); |
| 2047 | j++; |
| 2048 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2050 | PyBuffer_Release(&vsep); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2052 | if (i == 0 && j == len && PyBytes_CheckExact(self)) { |
| 2053 | Py_INCREF(self); |
| 2054 | return (PyObject*)self; |
| 2055 | } |
| 2056 | else |
| 2057 | return PyBytes_FromStringAndSize(s+i, j-i); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2060 | |
| 2061 | Py_LOCAL_INLINE(PyObject *) |
| 2062 | do_strip(PyBytesObject *self, int striptype) |
| 2063 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2064 | char *s = PyBytes_AS_STRING(self); |
| 2065 | Py_ssize_t len = PyBytes_GET_SIZE(self), i, j; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2066 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2067 | i = 0; |
| 2068 | if (striptype != RIGHTSTRIP) { |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2069 | while (i < len && Py_ISSPACE(s[i])) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2070 | i++; |
| 2071 | } |
| 2072 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2073 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2074 | j = len; |
| 2075 | if (striptype != LEFTSTRIP) { |
| 2076 | do { |
| 2077 | j--; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 2078 | } while (j >= i && Py_ISSPACE(s[j])); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2079 | j++; |
| 2080 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2082 | if (i == 0 && j == len && PyBytes_CheckExact(self)) { |
| 2083 | Py_INCREF(self); |
| 2084 | return (PyObject*)self; |
| 2085 | } |
| 2086 | else |
| 2087 | return PyBytes_FromStringAndSize(s+i, j-i); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | |
| 2091 | Py_LOCAL_INLINE(PyObject *) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2092 | do_argstrip(PyBytesObject *self, int striptype, PyObject *bytes) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2093 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2094 | if (bytes != NULL && bytes != Py_None) { |
| 2095 | return do_xstrip(self, striptype, bytes); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2096 | } |
| 2097 | return do_strip(self, striptype); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2100 | /*[clinic input] |
| 2101 | bytes.strip |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2102 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2103 | self: self(type="PyBytesObject *") |
| 2104 | bytes: object = None |
| 2105 | / |
| 2106 | |
| 2107 | Strip leading and trailing bytes contained in the argument. |
| 2108 | |
| 2109 | If the argument is omitted or None, strip leading and trailing ASCII whitespace. |
| 2110 | [clinic start generated code]*/ |
| 2111 | |
| 2112 | PyDoc_STRVAR(bytes_strip__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2113 | "strip($self, bytes=None, /)\n" |
| 2114 | "--\n" |
| 2115 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2116 | "Strip leading and trailing bytes contained in the argument.\n" |
| 2117 | "\n" |
| 2118 | "If the argument is omitted or None, strip leading and trailing ASCII whitespace."); |
| 2119 | |
| 2120 | #define BYTES_STRIP_METHODDEF \ |
| 2121 | {"strip", (PyCFunction)bytes_strip, METH_VARARGS, bytes_strip__doc__}, |
| 2122 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2123 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2124 | bytes_strip_impl(PyBytesObject *self, PyObject *bytes); |
| 2125 | |
| 2126 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2127 | bytes_strip(PyBytesObject *self, PyObject *args) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2128 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2129 | PyObject *return_value = NULL; |
| 2130 | PyObject *bytes = Py_None; |
| 2131 | |
| 2132 | if (!PyArg_UnpackTuple(args, "strip", |
| 2133 | 0, 1, |
| 2134 | &bytes)) |
| 2135 | goto exit; |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2136 | return_value = bytes_strip_impl(self, bytes); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2137 | |
| 2138 | exit: |
| 2139 | return return_value; |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2142 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2143 | bytes_strip_impl(PyBytesObject *self, PyObject *bytes) |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2144 | /*[clinic end generated code: output=c8234a599ba5ec35 input=37daa5fad1395d95]*/ |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2145 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2146 | return do_argstrip(self, BOTHSTRIP, bytes); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2147 | } |
| 2148 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2149 | /*[clinic input] |
| 2150 | bytes.lstrip |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2151 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2152 | self: self(type="PyBytesObject *") |
| 2153 | bytes: object = None |
| 2154 | / |
| 2155 | |
| 2156 | Strip leading bytes contained in the argument. |
| 2157 | |
| 2158 | If the argument is omitted or None, strip leading ASCII whitespace. |
| 2159 | [clinic start generated code]*/ |
| 2160 | |
| 2161 | PyDoc_STRVAR(bytes_lstrip__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2162 | "lstrip($self, bytes=None, /)\n" |
| 2163 | "--\n" |
| 2164 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2165 | "Strip leading bytes contained in the argument.\n" |
| 2166 | "\n" |
| 2167 | "If the argument is omitted or None, strip leading ASCII whitespace."); |
| 2168 | |
| 2169 | #define BYTES_LSTRIP_METHODDEF \ |
| 2170 | {"lstrip", (PyCFunction)bytes_lstrip, METH_VARARGS, bytes_lstrip__doc__}, |
| 2171 | |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2172 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2173 | bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes); |
| 2174 | |
| 2175 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2176 | bytes_lstrip(PyBytesObject *self, PyObject *args) |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2177 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2178 | PyObject *return_value = NULL; |
| 2179 | PyObject *bytes = Py_None; |
| 2180 | |
| 2181 | if (!PyArg_UnpackTuple(args, "lstrip", |
| 2182 | 0, 1, |
| 2183 | &bytes)) |
| 2184 | goto exit; |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2185 | return_value = bytes_lstrip_impl(self, bytes); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2186 | |
| 2187 | exit: |
| 2188 | return return_value; |
| 2189 | } |
| 2190 | |
| 2191 | static PyObject * |
| 2192 | bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes) |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2193 | /*[clinic end generated code: output=529e8511ab6f1115 input=88811b09dfbc2988]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2194 | { |
| 2195 | return do_argstrip(self, LEFTSTRIP, bytes); |
| 2196 | } |
| 2197 | |
| 2198 | /*[clinic input] |
| 2199 | bytes.rstrip |
| 2200 | |
| 2201 | self: self(type="PyBytesObject *") |
| 2202 | bytes: object = None |
| 2203 | / |
| 2204 | |
| 2205 | Strip trailing bytes contained in the argument. |
| 2206 | |
| 2207 | If the argument is omitted or None, strip trailing ASCII whitespace. |
| 2208 | [clinic start generated code]*/ |
| 2209 | |
| 2210 | PyDoc_STRVAR(bytes_rstrip__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2211 | "rstrip($self, bytes=None, /)\n" |
| 2212 | "--\n" |
| 2213 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2214 | "Strip trailing bytes contained in the argument.\n" |
| 2215 | "\n" |
| 2216 | "If the argument is omitted or None, strip trailing ASCII whitespace."); |
| 2217 | |
| 2218 | #define BYTES_RSTRIP_METHODDEF \ |
| 2219 | {"rstrip", (PyCFunction)bytes_rstrip, METH_VARARGS, bytes_rstrip__doc__}, |
| 2220 | |
| 2221 | static PyObject * |
| 2222 | bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes); |
| 2223 | |
| 2224 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2225 | bytes_rstrip(PyBytesObject *self, PyObject *args) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2226 | { |
| 2227 | PyObject *return_value = NULL; |
| 2228 | PyObject *bytes = Py_None; |
| 2229 | |
| 2230 | if (!PyArg_UnpackTuple(args, "rstrip", |
| 2231 | 0, 1, |
| 2232 | &bytes)) |
| 2233 | goto exit; |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2234 | return_value = bytes_rstrip_impl(self, bytes); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2235 | |
| 2236 | exit: |
| 2237 | return return_value; |
| 2238 | } |
| 2239 | |
| 2240 | static PyObject * |
| 2241 | bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes) |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2242 | /*[clinic end generated code: output=e98730bd133e6593 input=8f93c9cd361f0140]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2243 | { |
| 2244 | return do_argstrip(self, RIGHTSTRIP, bytes); |
Guido van Rossum | ad7d8d1 | 2007-04-13 01:39:34 +0000 | [diff] [blame] | 2245 | } |
Neal Norwitz | 6968b05 | 2007-02-27 19:02:19 +0000 | [diff] [blame] | 2246 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2247 | |
| 2248 | PyDoc_STRVAR(count__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 2249 | "B.count(sub[, start[, end]]) -> int\n\ |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 2250 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2251 | Return the number of non-overlapping occurrences of substring sub in\n\ |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2252 | string B[start:end]. Optional arguments start and end are interpreted\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2253 | as in slice notation."); |
| 2254 | |
| 2255 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 2256 | bytes_count(PyBytesObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2257 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2258 | PyObject *sub_obj; |
| 2259 | const char *str = PyBytes_AS_STRING(self), *sub; |
| 2260 | Py_ssize_t sub_len; |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 2261 | char byte; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2262 | Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2263 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 2264 | Py_buffer vsub; |
| 2265 | PyObject *count_obj; |
| 2266 | |
| 2267 | if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte, |
| 2268 | &start, &end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2269 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2270 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 2271 | if (sub_obj) { |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2272 | if (PyObject_GetBuffer(sub_obj, &vsub, PyBUF_SIMPLE) != 0) |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 2273 | return NULL; |
| 2274 | |
| 2275 | sub = vsub.buf; |
| 2276 | sub_len = vsub.len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2277 | } |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 2278 | else { |
| 2279 | sub = &byte; |
| 2280 | sub_len = 1; |
| 2281 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | ADJUST_INDICES(start, end, PyBytes_GET_SIZE(self)); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2284 | |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 2285 | count_obj = PyLong_FromSsize_t( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX) |
| 2287 | ); |
Antoine Pitrou | ac65d96 | 2011-10-20 23:54:17 +0200 | [diff] [blame] | 2288 | |
| 2289 | if (sub_obj) |
| 2290 | PyBuffer_Release(&vsub); |
| 2291 | |
| 2292 | return count_obj; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2293 | } |
| 2294 | |
| 2295 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2296 | /*[clinic input] |
| 2297 | bytes.translate |
| 2298 | |
| 2299 | self: self(type="PyBytesObject *") |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2300 | table: object |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2301 | Translation table, which must be a bytes object of length 256. |
| 2302 | [ |
| 2303 | deletechars: object |
| 2304 | ] |
| 2305 | / |
| 2306 | |
| 2307 | Return a copy with each character mapped by the given translation table. |
| 2308 | |
| 2309 | All characters occurring in the optional argument deletechars are removed. |
| 2310 | The remaining characters are mapped through the given translation table. |
| 2311 | [clinic start generated code]*/ |
| 2312 | |
| 2313 | PyDoc_STRVAR(bytes_translate__doc__, |
| 2314 | "translate(table, [deletechars])\n" |
| 2315 | "Return a copy with each character mapped by the given translation table.\n" |
| 2316 | "\n" |
| 2317 | " table\n" |
| 2318 | " Translation table, which must be a bytes object of length 256.\n" |
| 2319 | "\n" |
| 2320 | "All characters occurring in the optional argument deletechars are removed.\n" |
| 2321 | "The remaining characters are mapped through the given translation table."); |
| 2322 | |
| 2323 | #define BYTES_TRANSLATE_METHODDEF \ |
| 2324 | {"translate", (PyCFunction)bytes_translate, METH_VARARGS, bytes_translate__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2325 | |
| 2326 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2327 | bytes_translate_impl(PyBytesObject *self, PyObject *table, int group_right_1, PyObject *deletechars); |
| 2328 | |
| 2329 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2330 | bytes_translate(PyBytesObject *self, PyObject *args) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2331 | { |
| 2332 | PyObject *return_value = NULL; |
| 2333 | PyObject *table; |
| 2334 | int group_right_1 = 0; |
| 2335 | PyObject *deletechars = NULL; |
| 2336 | |
| 2337 | switch (PyTuple_GET_SIZE(args)) { |
| 2338 | case 1: |
| 2339 | if (!PyArg_ParseTuple(args, "O:translate", &table)) |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2340 | goto exit; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2341 | break; |
| 2342 | case 2: |
| 2343 | if (!PyArg_ParseTuple(args, "OO:translate", &table, &deletechars)) |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2344 | goto exit; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2345 | group_right_1 = 1; |
| 2346 | break; |
| 2347 | default: |
| 2348 | PyErr_SetString(PyExc_TypeError, "bytes.translate requires 1 to 2 arguments"); |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2349 | goto exit; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2350 | } |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2351 | return_value = bytes_translate_impl(self, table, group_right_1, deletechars); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2352 | |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2353 | exit: |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2354 | return return_value; |
| 2355 | } |
| 2356 | |
| 2357 | static PyObject * |
| 2358 | bytes_translate_impl(PyBytesObject *self, PyObject *table, int group_right_1, PyObject *deletechars) |
Larry Hastings | dfbeb16 | 2014-10-13 10:39:41 +0100 | [diff] [blame] | 2359 | /*[clinic end generated code: output=f0f29a57f41df5d8 input=d8fa5519d7cc4be7]*/ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2360 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2361 | char *input, *output; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2362 | Py_buffer table_view = {NULL, NULL}; |
| 2363 | Py_buffer del_table_view = {NULL, NULL}; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2364 | const char *table_chars; |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 2365 | Py_ssize_t i, c, changed = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2366 | PyObject *input_obj = (PyObject*)self; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2367 | const char *output_start, *del_table_chars=NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2368 | Py_ssize_t inlen, tablen, dellen = 0; |
| 2369 | PyObject *result; |
| 2370 | int trans_table[256]; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2371 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2372 | if (PyBytes_Check(table)) { |
| 2373 | table_chars = PyBytes_AS_STRING(table); |
| 2374 | tablen = PyBytes_GET_SIZE(table); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2375 | } |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2376 | else if (table == Py_None) { |
| 2377 | table_chars = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2378 | tablen = 256; |
| 2379 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2380 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2381 | if (PyObject_GetBuffer(table, &table_view, PyBUF_SIMPLE) != 0) |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2382 | return NULL; |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2383 | table_chars = table_view.buf; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2384 | tablen = table_view.len; |
| 2385 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | if (tablen != 256) { |
| 2388 | PyErr_SetString(PyExc_ValueError, |
| 2389 | "translation table must be 256 characters long"); |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2390 | PyBuffer_Release(&table_view); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2391 | return NULL; |
| 2392 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2393 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2394 | if (deletechars != NULL) { |
| 2395 | if (PyBytes_Check(deletechars)) { |
| 2396 | del_table_chars = PyBytes_AS_STRING(deletechars); |
| 2397 | dellen = PyBytes_GET_SIZE(deletechars); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2398 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2399 | else { |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2400 | if (PyObject_GetBuffer(deletechars, &del_table_view, PyBUF_SIMPLE) != 0) { |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2401 | PyBuffer_Release(&table_view); |
| 2402 | return NULL; |
| 2403 | } |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2404 | del_table_chars = del_table_view.buf; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2405 | dellen = del_table_view.len; |
| 2406 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2407 | } |
| 2408 | else { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2409 | del_table_chars = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2410 | dellen = 0; |
| 2411 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2413 | inlen = PyBytes_GET_SIZE(input_obj); |
| 2414 | result = PyBytes_FromStringAndSize((char *)NULL, inlen); |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2415 | if (result == NULL) { |
| 2416 | PyBuffer_Release(&del_table_view); |
| 2417 | PyBuffer_Release(&table_view); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2418 | return NULL; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2419 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2420 | output_start = output = PyBytes_AsString(result); |
| 2421 | input = PyBytes_AS_STRING(input_obj); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2422 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2423 | if (dellen == 0 && table_chars != NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2424 | /* If no deletions are required, use faster code */ |
| 2425 | for (i = inlen; --i >= 0; ) { |
| 2426 | c = Py_CHARMASK(*input++); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2427 | if (Py_CHARMASK((*output++ = table_chars[c])) != c) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2428 | changed = 1; |
| 2429 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2430 | if (!changed && PyBytes_CheckExact(input_obj)) { |
| 2431 | Py_INCREF(input_obj); |
| 2432 | Py_DECREF(result); |
| 2433 | result = input_obj; |
| 2434 | } |
| 2435 | PyBuffer_Release(&del_table_view); |
| 2436 | PyBuffer_Release(&table_view); |
| 2437 | return result; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2438 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2439 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2440 | if (table_chars == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2441 | for (i = 0; i < 256; i++) |
| 2442 | trans_table[i] = Py_CHARMASK(i); |
| 2443 | } else { |
| 2444 | for (i = 0; i < 256; i++) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2445 | trans_table[i] = Py_CHARMASK(table_chars[i]); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2446 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2447 | PyBuffer_Release(&table_view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2448 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2449 | for (i = 0; i < dellen; i++) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2450 | trans_table[(int) Py_CHARMASK(del_table_chars[i])] = -1; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 2451 | PyBuffer_Release(&del_table_view); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2453 | for (i = inlen; --i >= 0; ) { |
| 2454 | c = Py_CHARMASK(*input++); |
| 2455 | if (trans_table[c] != -1) |
| 2456 | if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
| 2457 | continue; |
| 2458 | changed = 1; |
| 2459 | } |
| 2460 | if (!changed && PyBytes_CheckExact(input_obj)) { |
| 2461 | Py_DECREF(result); |
| 2462 | Py_INCREF(input_obj); |
| 2463 | return input_obj; |
| 2464 | } |
| 2465 | /* Fix the size of the resulting string */ |
| 2466 | if (inlen > 0) |
| 2467 | _PyBytes_Resize(&result, output - output_start); |
| 2468 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2472 | /*[clinic input] |
| 2473 | |
| 2474 | @staticmethod |
| 2475 | bytes.maketrans |
| 2476 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2477 | frm: Py_buffer |
| 2478 | to: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2479 | / |
| 2480 | |
| 2481 | Return a translation table useable for the bytes or bytearray translate method. |
| 2482 | |
| 2483 | The returned table will be one where each byte in frm is mapped to the byte at |
| 2484 | the same position in to. |
| 2485 | |
| 2486 | The bytes objects frm and to must be of the same length. |
| 2487 | [clinic start generated code]*/ |
| 2488 | |
| 2489 | PyDoc_STRVAR(bytes_maketrans__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 2490 | "maketrans(frm, to, /)\n" |
| 2491 | "--\n" |
| 2492 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2493 | "Return a translation table useable for the bytes or bytearray translate method.\n" |
| 2494 | "\n" |
| 2495 | "The returned table will be one where each byte in frm is mapped to the byte at\n" |
| 2496 | "the same position in to.\n" |
| 2497 | "\n" |
| 2498 | "The bytes objects frm and to must be of the same length."); |
| 2499 | |
| 2500 | #define BYTES_MAKETRANS_METHODDEF \ |
| 2501 | {"maketrans", (PyCFunction)bytes_maketrans, METH_VARARGS|METH_STATIC, bytes_maketrans__doc__}, |
| 2502 | |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 2503 | static PyObject * |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2504 | bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2505 | |
| 2506 | static PyObject * |
| 2507 | bytes_maketrans(void *null, PyObject *args) |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 2508 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2509 | PyObject *return_value = NULL; |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2510 | Py_buffer frm = {NULL, NULL}; |
| 2511 | Py_buffer to = {NULL, NULL}; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2512 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2513 | if (!PyArg_ParseTuple(args, |
| 2514 | "y*y*:maketrans", |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2515 | &frm, &to)) |
| 2516 | goto exit; |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2517 | return_value = bytes_maketrans_impl(&frm, &to); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2518 | |
| 2519 | exit: |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2520 | /* Cleanup for frm */ |
| 2521 | if (frm.obj) |
| 2522 | PyBuffer_Release(&frm); |
| 2523 | /* Cleanup for to */ |
| 2524 | if (to.obj) |
| 2525 | PyBuffer_Release(&to); |
| 2526 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2527 | return return_value; |
| 2528 | } |
| 2529 | |
| 2530 | static PyObject * |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 2531 | bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to) |
| 2532 | /*[clinic end generated code: output=7df47390c476ac60 input=de7a8fc5632bb8f1]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 2533 | { |
| 2534 | return _Py_bytes_maketrans(frm, to); |
Georg Brandl | abc3877 | 2009-04-12 15:51:51 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2537 | /* find and count characters and substrings */ |
| 2538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2539 | #define findchar(target, target_len, c) \ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2540 | ((char *)memchr((const void *)(target), c, target_len)) |
| 2541 | |
| 2542 | /* String ops must return a string. */ |
| 2543 | /* If the object is subclass of string, create a copy */ |
| 2544 | Py_LOCAL(PyBytesObject *) |
| 2545 | return_self(PyBytesObject *self) |
| 2546 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2547 | if (PyBytes_CheckExact(self)) { |
| 2548 | Py_INCREF(self); |
| 2549 | return self; |
| 2550 | } |
| 2551 | return (PyBytesObject *)PyBytes_FromStringAndSize( |
| 2552 | PyBytes_AS_STRING(self), |
| 2553 | PyBytes_GET_SIZE(self)); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | Py_LOCAL_INLINE(Py_ssize_t) |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 2557 | 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] | 2558 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2559 | Py_ssize_t count=0; |
| 2560 | const char *start=target; |
| 2561 | const char *end=target+target_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2562 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2563 | while ( (start=findchar(start, end-start, c)) != NULL ) { |
| 2564 | count++; |
| 2565 | if (count >= maxcount) |
| 2566 | break; |
| 2567 | start += 1; |
| 2568 | } |
| 2569 | return count; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2572 | |
| 2573 | /* Algorithms for different cases of string replacement */ |
| 2574 | |
| 2575 | /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
| 2576 | Py_LOCAL(PyBytesObject *) |
| 2577 | replace_interleave(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2578 | const char *to_s, Py_ssize_t to_len, |
| 2579 | Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2580 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2581 | char *self_s, *result_s; |
| 2582 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2583 | Py_ssize_t count, i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2584 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2585 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2586 | self_len = PyBytes_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2587 | |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2588 | /* 1 at the end plus 1 after every character; |
| 2589 | count = min(maxcount, self_len + 1) */ |
| 2590 | if (maxcount <= self_len) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2591 | count = maxcount; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2592 | else |
| 2593 | /* Can't overflow: self_len + 1 <= maxcount <= PY_SSIZE_T_MAX. */ |
| 2594 | count = self_len + 1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2596 | /* Check for overflow */ |
| 2597 | /* result_len = count * to_len + self_len; */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2598 | assert(count > 0); |
| 2599 | if (to_len > (PY_SSIZE_T_MAX - self_len) / count) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2600 | PyErr_SetString(PyExc_OverflowError, |
| 2601 | "replacement bytes are too long"); |
| 2602 | return NULL; |
| 2603 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2604 | result_len = count * to_len + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2606 | if (! (result = (PyBytesObject *) |
| 2607 | PyBytes_FromStringAndSize(NULL, result_len)) ) |
| 2608 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2609 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2610 | self_s = PyBytes_AS_STRING(self); |
| 2611 | result_s = PyBytes_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2613 | /* TODO: special case single character, which doesn't need memcpy */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2614 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2615 | /* Lay the first one down (guaranteed this will occur) */ |
| 2616 | Py_MEMCPY(result_s, to_s, to_len); |
| 2617 | result_s += to_len; |
| 2618 | count -= 1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2620 | for (i=0; i<count; i++) { |
| 2621 | *result_s++ = *self_s++; |
| 2622 | Py_MEMCPY(result_s, to_s, to_len); |
| 2623 | result_s += to_len; |
| 2624 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2626 | /* Copy the rest of the original string */ |
| 2627 | Py_MEMCPY(result_s, self_s, self_len-i); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2629 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | /* Special case for deleting a single character */ |
| 2633 | /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
| 2634 | Py_LOCAL(PyBytesObject *) |
| 2635 | replace_delete_single_character(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2636 | char from_c, Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2637 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2638 | char *self_s, *result_s; |
| 2639 | char *start, *next, *end; |
| 2640 | Py_ssize_t self_len, result_len; |
| 2641 | Py_ssize_t count; |
| 2642 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2643 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2644 | self_len = PyBytes_GET_SIZE(self); |
| 2645 | self_s = PyBytes_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2647 | count = countchar(self_s, self_len, from_c, maxcount); |
| 2648 | if (count == 0) { |
| 2649 | return return_self(self); |
| 2650 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2651 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2652 | result_len = self_len - count; /* from_len == 1 */ |
| 2653 | assert(result_len>=0); |
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 ( (result = (PyBytesObject *) |
| 2656 | PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
| 2657 | return NULL; |
| 2658 | result_s = PyBytes_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2660 | start = self_s; |
| 2661 | end = self_s + self_len; |
| 2662 | while (count-- > 0) { |
| 2663 | next = findchar(start, end-start, from_c); |
| 2664 | if (next == NULL) |
| 2665 | break; |
| 2666 | Py_MEMCPY(result_s, start, next-start); |
| 2667 | result_s += (next-start); |
| 2668 | start = next+1; |
| 2669 | } |
| 2670 | Py_MEMCPY(result_s, start, end-start); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2671 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2672 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
| 2676 | |
| 2677 | Py_LOCAL(PyBytesObject *) |
| 2678 | replace_delete_substring(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | const char *from_s, Py_ssize_t from_len, |
| 2680 | Py_ssize_t maxcount) { |
| 2681 | char *self_s, *result_s; |
| 2682 | char *start, *next, *end; |
| 2683 | Py_ssize_t self_len, result_len; |
| 2684 | Py_ssize_t count, offset; |
| 2685 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2686 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2687 | self_len = PyBytes_GET_SIZE(self); |
| 2688 | self_s = PyBytes_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2690 | count = stringlib_count(self_s, self_len, |
| 2691 | from_s, from_len, |
| 2692 | maxcount); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2693 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2694 | if (count == 0) { |
| 2695 | /* no matches */ |
| 2696 | return return_self(self); |
| 2697 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2698 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2699 | result_len = self_len - (count * from_len); |
| 2700 | assert (result_len>=0); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2702 | if ( (result = (PyBytesObject *) |
| 2703 | PyBytes_FromStringAndSize(NULL, result_len)) == NULL ) |
| 2704 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2706 | result_s = PyBytes_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2707 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2708 | start = self_s; |
| 2709 | end = self_s + self_len; |
| 2710 | while (count-- > 0) { |
| 2711 | offset = stringlib_find(start, end-start, |
| 2712 | from_s, from_len, |
| 2713 | 0); |
| 2714 | if (offset == -1) |
| 2715 | break; |
| 2716 | next = start + offset; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2717 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2718 | Py_MEMCPY(result_s, start, next-start); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2720 | result_s += (next-start); |
| 2721 | start = next+from_len; |
| 2722 | } |
| 2723 | Py_MEMCPY(result_s, start, end-start); |
| 2724 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
| 2728 | Py_LOCAL(PyBytesObject *) |
| 2729 | replace_single_character_in_place(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2730 | char from_c, char to_c, |
| 2731 | Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2732 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2733 | char *self_s, *result_s, *start, *end, *next; |
| 2734 | Py_ssize_t self_len; |
| 2735 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2736 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2737 | /* The result string will be the same size */ |
| 2738 | self_s = PyBytes_AS_STRING(self); |
| 2739 | self_len = PyBytes_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2741 | next = findchar(self_s, self_len, from_c); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2743 | if (next == NULL) { |
| 2744 | /* No matches; return the original string */ |
| 2745 | return return_self(self); |
| 2746 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2747 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2748 | /* Need to make a new string */ |
| 2749 | result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len); |
| 2750 | if (result == NULL) |
| 2751 | return NULL; |
| 2752 | result_s = PyBytes_AS_STRING(result); |
| 2753 | Py_MEMCPY(result_s, self_s, self_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2754 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2755 | /* change everything in-place, starting with this one */ |
| 2756 | start = result_s + (next-self_s); |
| 2757 | *start = to_c; |
| 2758 | start++; |
| 2759 | end = result_s + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | while (--maxcount > 0) { |
| 2762 | next = findchar(start, end-start, from_c); |
| 2763 | if (next == NULL) |
| 2764 | break; |
| 2765 | *next = to_c; |
| 2766 | start = next+1; |
| 2767 | } |
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 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
| 2772 | /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
| 2773 | Py_LOCAL(PyBytesObject *) |
| 2774 | replace_substring_in_place(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2775 | const char *from_s, Py_ssize_t from_len, |
| 2776 | const char *to_s, Py_ssize_t to_len, |
| 2777 | Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2778 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2779 | char *result_s, *start, *end; |
| 2780 | char *self_s; |
| 2781 | Py_ssize_t self_len, offset; |
| 2782 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2783 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2784 | /* The result string will be the same size */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2785 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | self_s = PyBytes_AS_STRING(self); |
| 2787 | self_len = PyBytes_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2789 | offset = stringlib_find(self_s, self_len, |
| 2790 | from_s, from_len, |
| 2791 | 0); |
| 2792 | if (offset == -1) { |
| 2793 | /* No matches; return the original string */ |
| 2794 | return return_self(self); |
| 2795 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2797 | /* Need to make a new string */ |
| 2798 | result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len); |
| 2799 | if (result == NULL) |
| 2800 | return NULL; |
| 2801 | result_s = PyBytes_AS_STRING(result); |
| 2802 | Py_MEMCPY(result_s, self_s, self_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2804 | /* change everything in-place, starting with this one */ |
| 2805 | start = result_s + offset; |
| 2806 | Py_MEMCPY(start, to_s, from_len); |
| 2807 | start += from_len; |
| 2808 | end = result_s + self_len; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2809 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2810 | while ( --maxcount > 0) { |
| 2811 | offset = stringlib_find(start, end-start, |
| 2812 | from_s, from_len, |
| 2813 | 0); |
| 2814 | if (offset==-1) |
| 2815 | break; |
| 2816 | Py_MEMCPY(start+offset, to_s, from_len); |
| 2817 | start += offset+from_len; |
| 2818 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2820 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
| 2824 | Py_LOCAL(PyBytesObject *) |
| 2825 | replace_single_character(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2826 | char from_c, |
| 2827 | const char *to_s, Py_ssize_t to_len, |
| 2828 | Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2829 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2830 | char *self_s, *result_s; |
| 2831 | char *start, *next, *end; |
| 2832 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2833 | Py_ssize_t count; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2834 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2836 | self_s = PyBytes_AS_STRING(self); |
| 2837 | self_len = PyBytes_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2839 | count = countchar(self_s, self_len, from_c, maxcount); |
| 2840 | if (count == 0) { |
| 2841 | /* no matches, return unchanged */ |
| 2842 | return return_self(self); |
| 2843 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2845 | /* use the difference between current and new, hence the "-1" */ |
| 2846 | /* result_len = self_len + count * (to_len-1) */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2847 | assert(count > 0); |
| 2848 | if (to_len - 1 > (PY_SSIZE_T_MAX - self_len) / count) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2849 | PyErr_SetString(PyExc_OverflowError, |
| 2850 | "replacement bytes are too long"); |
| 2851 | return NULL; |
| 2852 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2853 | result_len = self_len + count * (to_len - 1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2854 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2855 | if ( (result = (PyBytesObject *) |
| 2856 | PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
| 2857 | return NULL; |
| 2858 | result_s = PyBytes_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2860 | start = self_s; |
| 2861 | end = self_s + self_len; |
| 2862 | while (count-- > 0) { |
| 2863 | next = findchar(start, end-start, from_c); |
| 2864 | if (next == NULL) |
| 2865 | break; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2866 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2867 | if (next == start) { |
| 2868 | /* replace with the 'to' */ |
| 2869 | Py_MEMCPY(result_s, to_s, to_len); |
| 2870 | result_s += to_len; |
| 2871 | start += 1; |
| 2872 | } else { |
| 2873 | /* copy the unchanged old then the 'to' */ |
| 2874 | Py_MEMCPY(result_s, start, next-start); |
| 2875 | result_s += (next-start); |
| 2876 | Py_MEMCPY(result_s, to_s, to_len); |
| 2877 | result_s += to_len; |
| 2878 | start = next+1; |
| 2879 | } |
| 2880 | } |
| 2881 | /* Copy the remainder of the remaining string */ |
| 2882 | Py_MEMCPY(result_s, start, end-start); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2883 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2884 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
| 2887 | /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
| 2888 | Py_LOCAL(PyBytesObject *) |
| 2889 | replace_substring(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2890 | const char *from_s, Py_ssize_t from_len, |
| 2891 | const char *to_s, Py_ssize_t to_len, |
| 2892 | Py_ssize_t maxcount) { |
| 2893 | char *self_s, *result_s; |
| 2894 | char *start, *next, *end; |
| 2895 | Py_ssize_t self_len, result_len; |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2896 | Py_ssize_t count, offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2897 | PyBytesObject *result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2899 | self_s = PyBytes_AS_STRING(self); |
| 2900 | self_len = PyBytes_GET_SIZE(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2901 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2902 | count = stringlib_count(self_s, self_len, |
| 2903 | from_s, from_len, |
| 2904 | maxcount); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 2905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2906 | if (count == 0) { |
| 2907 | /* no matches, return unchanged */ |
| 2908 | return return_self(self); |
| 2909 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2910 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2911 | /* Check for overflow */ |
| 2912 | /* result_len = self_len + count * (to_len-from_len) */ |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2913 | assert(count > 0); |
| 2914 | if (to_len - from_len > (PY_SSIZE_T_MAX - self_len) / count) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2915 | PyErr_SetString(PyExc_OverflowError, |
| 2916 | "replacement bytes are too long"); |
| 2917 | return NULL; |
| 2918 | } |
Mark Dickinson | cf940c7 | 2010-08-10 18:35:01 +0000 | [diff] [blame] | 2919 | result_len = self_len + count * (to_len-from_len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | if ( (result = (PyBytesObject *) |
| 2922 | PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
| 2923 | return NULL; |
| 2924 | result_s = PyBytes_AS_STRING(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2926 | start = self_s; |
| 2927 | end = self_s + self_len; |
| 2928 | while (count-- > 0) { |
| 2929 | offset = stringlib_find(start, end-start, |
| 2930 | from_s, from_len, |
| 2931 | 0); |
| 2932 | if (offset == -1) |
| 2933 | break; |
| 2934 | next = start+offset; |
| 2935 | if (next == start) { |
| 2936 | /* replace with the 'to' */ |
| 2937 | Py_MEMCPY(result_s, to_s, to_len); |
| 2938 | result_s += to_len; |
| 2939 | start += from_len; |
| 2940 | } else { |
| 2941 | /* copy the unchanged old then the 'to' */ |
| 2942 | Py_MEMCPY(result_s, start, next-start); |
| 2943 | result_s += (next-start); |
| 2944 | Py_MEMCPY(result_s, to_s, to_len); |
| 2945 | result_s += to_len; |
| 2946 | start = next+from_len; |
| 2947 | } |
| 2948 | } |
| 2949 | /* Copy the remainder of the remaining string */ |
| 2950 | Py_MEMCPY(result_s, start, end-start); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2952 | return result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
| 2955 | |
| 2956 | Py_LOCAL(PyBytesObject *) |
| 2957 | replace(PyBytesObject *self, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2958 | const char *from_s, Py_ssize_t from_len, |
| 2959 | const char *to_s, Py_ssize_t to_len, |
| 2960 | Py_ssize_t maxcount) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2961 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2962 | if (maxcount < 0) { |
| 2963 | maxcount = PY_SSIZE_T_MAX; |
| 2964 | } else if (maxcount == 0 || PyBytes_GET_SIZE(self) == 0) { |
| 2965 | /* nothing to do; return the original string */ |
| 2966 | return return_self(self); |
| 2967 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2968 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2969 | if (maxcount == 0 || |
| 2970 | (from_len == 0 && to_len == 0)) { |
| 2971 | /* nothing to do; return the original string */ |
| 2972 | return return_self(self); |
| 2973 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2974 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2975 | /* Handle zero-length special cases */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2976 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2977 | if (from_len == 0) { |
| 2978 | /* insert the 'to' string everywhere. */ |
| 2979 | /* >>> "Python".replace("", ".") */ |
| 2980 | /* '.P.y.t.h.o.n.' */ |
| 2981 | return replace_interleave(self, to_s, to_len, maxcount); |
| 2982 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2984 | /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
| 2985 | /* point for an empty self string to generate a non-empty string */ |
| 2986 | /* Special case so the remaining code always gets a non-empty string */ |
| 2987 | if (PyBytes_GET_SIZE(self) == 0) { |
| 2988 | return return_self(self); |
| 2989 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 2990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2991 | if (to_len == 0) { |
| 2992 | /* delete all occurrences of 'from' string */ |
| 2993 | if (from_len == 1) { |
| 2994 | return replace_delete_single_character( |
| 2995 | self, from_s[0], maxcount); |
| 2996 | } else { |
| 2997 | return replace_delete_substring(self, from_s, |
| 2998 | from_len, maxcount); |
| 2999 | } |
| 3000 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3001 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3002 | /* Handle special case where both strings have the same length */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3004 | if (from_len == to_len) { |
| 3005 | if (from_len == 1) { |
| 3006 | return replace_single_character_in_place( |
| 3007 | self, |
| 3008 | from_s[0], |
| 3009 | to_s[0], |
| 3010 | maxcount); |
| 3011 | } else { |
| 3012 | return replace_substring_in_place( |
| 3013 | self, from_s, from_len, to_s, to_len, |
| 3014 | maxcount); |
| 3015 | } |
| 3016 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3018 | /* Otherwise use the more generic algorithms */ |
| 3019 | if (from_len == 1) { |
| 3020 | return replace_single_character(self, from_s[0], |
| 3021 | to_s, to_len, maxcount); |
| 3022 | } else { |
| 3023 | /* len('from')>=2, len('to')>=1 */ |
| 3024 | return replace_substring(self, from_s, from_len, to_s, to_len, |
| 3025 | maxcount); |
| 3026 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3029 | |
| 3030 | /*[clinic input] |
| 3031 | bytes.replace |
| 3032 | |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 3033 | old: Py_buffer |
| 3034 | new: Py_buffer |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3035 | count: Py_ssize_t = -1 |
| 3036 | Maximum number of occurrences to replace. |
| 3037 | -1 (the default value) means replace all occurrences. |
| 3038 | / |
| 3039 | |
| 3040 | Return a copy with all occurrences of substring old replaced by new. |
| 3041 | |
| 3042 | If the optional argument count is given, only the first count occurrences are |
| 3043 | replaced. |
| 3044 | [clinic start generated code]*/ |
| 3045 | |
| 3046 | PyDoc_STRVAR(bytes_replace__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3047 | "replace($self, old, new, count=-1, /)\n" |
| 3048 | "--\n" |
| 3049 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3050 | "Return a copy with all occurrences of substring old replaced by new.\n" |
| 3051 | "\n" |
| 3052 | " count\n" |
| 3053 | " Maximum number of occurrences to replace.\n" |
| 3054 | " -1 (the default value) means replace all occurrences.\n" |
| 3055 | "\n" |
| 3056 | "If the optional argument count is given, only the first count occurrences are\n" |
| 3057 | "replaced."); |
| 3058 | |
| 3059 | #define BYTES_REPLACE_METHODDEF \ |
| 3060 | {"replace", (PyCFunction)bytes_replace, METH_VARARGS, bytes_replace__doc__}, |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3061 | |
| 3062 | static PyObject * |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 3063 | bytes_replace_impl(PyBytesObject*self, Py_buffer *old, Py_buffer *new, Py_ssize_t count); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3064 | |
| 3065 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3066 | bytes_replace(PyBytesObject*self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3067 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3068 | PyObject *return_value = NULL; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 3069 | Py_buffer old = {NULL, NULL}; |
| 3070 | Py_buffer new = {NULL, NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3071 | Py_ssize_t count = -1; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3072 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3073 | if (!PyArg_ParseTuple(args, |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 3074 | "y*y*|n:replace", |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3075 | &old, &new, &count)) |
| 3076 | goto exit; |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 3077 | return_value = bytes_replace_impl(self, &old, &new, count); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3078 | |
| 3079 | exit: |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 3080 | /* Cleanup for old */ |
| 3081 | if (old.obj) |
| 3082 | PyBuffer_Release(&old); |
| 3083 | /* Cleanup for new */ |
| 3084 | if (new.obj) |
| 3085 | PyBuffer_Release(&new); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3086 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3087 | return return_value; |
| 3088 | } |
| 3089 | |
| 3090 | static PyObject * |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 3091 | bytes_replace_impl(PyBytesObject*self, Py_buffer *old, Py_buffer *new, Py_ssize_t count) |
| 3092 | /*[clinic end generated code: output=f07bd9ecf29ee8d8 input=b2fbbf0bf04de8e5]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3093 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3094 | return (PyObject *)replace((PyBytesObject *) self, |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 3095 | (const char *)old->buf, old->len, |
| 3096 | (const char *)new->buf, new->len, count); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | /** End DALKE **/ |
| 3100 | |
| 3101 | /* Matches the end (direction >= 0) or start (direction < 0) of self |
| 3102 | * against substr, using the start and end arguments. Returns |
| 3103 | * -1 on error, 0 if not found and 1 if found. |
| 3104 | */ |
| 3105 | Py_LOCAL(int) |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 3106 | _bytes_tailmatch(PyBytesObject *self, PyObject *substr, Py_ssize_t start, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3107 | Py_ssize_t end, int direction) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3108 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3109 | Py_ssize_t len = PyBytes_GET_SIZE(self); |
| 3110 | Py_ssize_t slen; |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 3111 | Py_buffer sub_view = {NULL, NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3112 | const char* sub; |
| 3113 | const char* str; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3115 | if (PyBytes_Check(substr)) { |
| 3116 | sub = PyBytes_AS_STRING(substr); |
| 3117 | slen = PyBytes_GET_SIZE(substr); |
| 3118 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 3119 | else { |
| 3120 | if (PyObject_GetBuffer(substr, &sub_view, PyBUF_SIMPLE) != 0) |
| 3121 | return -1; |
| 3122 | sub = sub_view.buf; |
| 3123 | slen = sub_view.len; |
| 3124 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3125 | str = PyBytes_AS_STRING(self); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3127 | ADJUST_INDICES(start, end, len); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3129 | if (direction < 0) { |
| 3130 | /* startswith */ |
| 3131 | if (start+slen > len) |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 3132 | goto notfound; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3133 | } else { |
| 3134 | /* endswith */ |
| 3135 | if (end-start < slen || start > len) |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 3136 | goto notfound; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3138 | if (end-slen > start) |
| 3139 | start = end - slen; |
| 3140 | } |
Serhiy Storchaka | 4fdb684 | 2015-02-03 01:21:08 +0200 | [diff] [blame] | 3141 | if (end-start < slen) |
| 3142 | goto notfound; |
| 3143 | if (memcmp(str+start, sub, slen) != 0) |
| 3144 | goto notfound; |
| 3145 | |
| 3146 | PyBuffer_Release(&sub_view); |
| 3147 | return 1; |
| 3148 | |
| 3149 | notfound: |
| 3150 | PyBuffer_Release(&sub_view); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3151 | return 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
| 3154 | |
| 3155 | PyDoc_STRVAR(startswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 3156 | "B.startswith(prefix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3157 | \n\ |
| 3158 | Return True if B starts with the specified prefix, False otherwise.\n\ |
| 3159 | With optional start, test B beginning at that position.\n\ |
| 3160 | With optional end, stop comparing B at that position.\n\ |
Benjamin Peterson | 4116f36 | 2008-05-27 00:36:20 +0000 | [diff] [blame] | 3161 | prefix can also be a tuple of bytes to try."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3162 | |
| 3163 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 3164 | bytes_startswith(PyBytesObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3166 | Py_ssize_t start = 0; |
| 3167 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 3168 | PyObject *subobj; |
| 3169 | int result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3170 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 3171 | if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3172 | return NULL; |
| 3173 | if (PyTuple_Check(subobj)) { |
| 3174 | Py_ssize_t i; |
| 3175 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 3176 | result = _bytes_tailmatch(self, |
| 3177 | PyTuple_GET_ITEM(subobj, i), |
| 3178 | start, end, -1); |
| 3179 | if (result == -1) |
| 3180 | return NULL; |
| 3181 | else if (result) { |
| 3182 | Py_RETURN_TRUE; |
| 3183 | } |
| 3184 | } |
| 3185 | Py_RETURN_FALSE; |
| 3186 | } |
| 3187 | result = _bytes_tailmatch(self, subobj, start, end, -1); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 3188 | if (result == -1) { |
| 3189 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 3190 | PyErr_Format(PyExc_TypeError, "startswith first arg must be bytes " |
| 3191 | "or a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 3193 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3194 | else |
| 3195 | return PyBool_FromLong(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | |
| 3199 | PyDoc_STRVAR(endswith__doc__, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 3200 | "B.endswith(suffix[, start[, end]]) -> bool\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3201 | \n\ |
| 3202 | Return True if B ends with the specified suffix, False otherwise.\n\ |
| 3203 | With optional start, test B beginning at that position.\n\ |
| 3204 | With optional end, stop comparing B at that position.\n\ |
Benjamin Peterson | 4116f36 | 2008-05-27 00:36:20 +0000 | [diff] [blame] | 3205 | suffix can also be a tuple of bytes to try."); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3206 | |
| 3207 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 3208 | bytes_endswith(PyBytesObject *self, PyObject *args) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3209 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3210 | Py_ssize_t start = 0; |
| 3211 | Py_ssize_t end = PY_SSIZE_T_MAX; |
| 3212 | PyObject *subobj; |
| 3213 | int result; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3214 | |
Jesus Cea | ac45150 | 2011-04-20 17:09:23 +0200 | [diff] [blame] | 3215 | if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3216 | return NULL; |
| 3217 | if (PyTuple_Check(subobj)) { |
| 3218 | Py_ssize_t i; |
| 3219 | for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
| 3220 | result = _bytes_tailmatch(self, |
| 3221 | PyTuple_GET_ITEM(subobj, i), |
| 3222 | start, end, +1); |
| 3223 | if (result == -1) |
| 3224 | return NULL; |
| 3225 | else if (result) { |
| 3226 | Py_RETURN_TRUE; |
| 3227 | } |
| 3228 | } |
| 3229 | Py_RETURN_FALSE; |
| 3230 | } |
| 3231 | result = _bytes_tailmatch(self, subobj, start, end, +1); |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 3232 | if (result == -1) { |
| 3233 | if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 3234 | PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or " |
| 3235 | "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3236 | return NULL; |
Ezio Melotti | ba42fd5 | 2011-04-26 06:09:45 +0300 | [diff] [blame] | 3237 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3238 | else |
| 3239 | return PyBool_FromLong(result); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
| 3242 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3243 | /*[clinic input] |
| 3244 | bytes.decode |
| 3245 | |
| 3246 | encoding: str(c_default="NULL") = 'utf-8' |
| 3247 | The encoding with which to decode the bytes. |
| 3248 | errors: str(c_default="NULL") = 'strict' |
| 3249 | The error handling scheme to use for the handling of decoding errors. |
| 3250 | The default is 'strict' meaning that decoding errors raise a |
| 3251 | UnicodeDecodeError. Other possible values are 'ignore' and 'replace' |
| 3252 | as well as any other name registered with codecs.register_error that |
| 3253 | can handle UnicodeDecodeErrors. |
| 3254 | |
| 3255 | Decode the bytes using the codec registered for encoding. |
| 3256 | [clinic start generated code]*/ |
| 3257 | |
| 3258 | PyDoc_STRVAR(bytes_decode__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3259 | "decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n" |
| 3260 | "--\n" |
| 3261 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3262 | "Decode the bytes using the codec registered for encoding.\n" |
| 3263 | "\n" |
| 3264 | " encoding\n" |
| 3265 | " The encoding with which to decode the bytes.\n" |
| 3266 | " errors\n" |
| 3267 | " The error handling scheme to use for the handling of decoding errors.\n" |
| 3268 | " The default is \'strict\' meaning that decoding errors raise a\n" |
| 3269 | " UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n" |
| 3270 | " as well as any other name registered with codecs.register_error that\n" |
| 3271 | " can handle UnicodeDecodeErrors."); |
| 3272 | |
| 3273 | #define BYTES_DECODE_METHODDEF \ |
| 3274 | {"decode", (PyCFunction)bytes_decode, METH_VARARGS|METH_KEYWORDS, bytes_decode__doc__}, |
| 3275 | |
| 3276 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3277 | bytes_decode_impl(PyBytesObject*self, const char *encoding, const char *errors); |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 3278 | |
| 3279 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3280 | bytes_decode(PyBytesObject*self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6f1fdc | 2007-04-12 22:49:52 +0000 | [diff] [blame] | 3281 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3282 | PyObject *return_value = NULL; |
| 3283 | static char *_keywords[] = {"encoding", "errors", NULL}; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3284 | const char *encoding = NULL; |
| 3285 | const char *errors = NULL; |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 3286 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3287 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3288 | "|ss:decode", _keywords, |
| 3289 | &encoding, &errors)) |
| 3290 | goto exit; |
| 3291 | return_value = bytes_decode_impl(self, encoding, errors); |
| 3292 | |
| 3293 | exit: |
| 3294 | return return_value; |
| 3295 | } |
| 3296 | |
| 3297 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3298 | bytes_decode_impl(PyBytesObject*self, const char *encoding, const char *errors) |
| 3299 | /*[clinic end generated code: output=61a80290bbfce696 input=958174769d2a40ca]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3300 | { |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3301 | return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors); |
Guido van Rossum | d624f18 | 2006-04-24 13:47:05 +0000 | [diff] [blame] | 3302 | } |
| 3303 | |
Guido van Rossum | 2018831 | 2006-05-05 15:15:40 +0000 | [diff] [blame] | 3304 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3305 | /*[clinic input] |
| 3306 | bytes.splitlines |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 3307 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3308 | keepends: int(py_default="False") = 0 |
| 3309 | |
| 3310 | Return a list of the lines in the bytes, breaking at line boundaries. |
| 3311 | |
| 3312 | Line breaks are not included in the resulting list unless keepends is given and |
| 3313 | true. |
| 3314 | [clinic start generated code]*/ |
| 3315 | |
| 3316 | PyDoc_STRVAR(bytes_splitlines__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3317 | "splitlines($self, /, keepends=False)\n" |
| 3318 | "--\n" |
| 3319 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3320 | "Return a list of the lines in the bytes, breaking at line boundaries.\n" |
| 3321 | "\n" |
| 3322 | "Line breaks are not included in the resulting list unless keepends is given and\n" |
| 3323 | "true."); |
| 3324 | |
| 3325 | #define BYTES_SPLITLINES_METHODDEF \ |
| 3326 | {"splitlines", (PyCFunction)bytes_splitlines, METH_VARARGS|METH_KEYWORDS, bytes_splitlines__doc__}, |
| 3327 | |
| 3328 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3329 | bytes_splitlines_impl(PyBytesObject*self, int keepends); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3330 | |
| 3331 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3332 | bytes_splitlines(PyBytesObject*self, PyObject *args, PyObject *kwargs) |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 3333 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3334 | PyObject *return_value = NULL; |
| 3335 | static char *_keywords[] = {"keepends", NULL}; |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 3336 | int keepends = 0; |
| 3337 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3338 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3339 | "|i:splitlines", _keywords, |
| 3340 | &keepends)) |
| 3341 | goto exit; |
| 3342 | return_value = bytes_splitlines_impl(self, keepends); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 3343 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3344 | exit: |
| 3345 | return return_value; |
| 3346 | } |
| 3347 | |
| 3348 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3349 | bytes_splitlines_impl(PyBytesObject*self, int keepends) |
| 3350 | /*[clinic end generated code: output=79da057d05d126de input=ddb93e3351080c8c]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3351 | { |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 3352 | return stringlib_splitlines( |
Antoine Pitrou | d118856 | 2010-06-09 16:38:55 +0000 | [diff] [blame] | 3353 | (PyObject*) self, PyBytes_AS_STRING(self), |
| 3354 | PyBytes_GET_SIZE(self), keepends |
| 3355 | ); |
Antoine Pitrou | f2c5484 | 2010-01-13 08:07:53 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 3358 | static int |
Victor Stinner | 6430fd5 | 2011-09-29 04:02:13 +0200 | [diff] [blame] | 3359 | hex_digit_to_int(Py_UCS4 c) |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 3360 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3361 | if (c >= 128) |
| 3362 | return -1; |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3363 | if (Py_ISDIGIT(c)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3364 | return c - '0'; |
| 3365 | else { |
David Malcolm | 9696088 | 2010-11-05 17:23:41 +0000 | [diff] [blame] | 3366 | if (Py_ISUPPER(c)) |
| 3367 | c = Py_TOLOWER(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3368 | if (c >= 'a' && c <= 'f') |
| 3369 | return c - 'a' + 10; |
| 3370 | } |
| 3371 | return -1; |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3374 | /*[clinic input] |
| 3375 | @classmethod |
| 3376 | bytes.fromhex |
| 3377 | |
| 3378 | string: unicode |
| 3379 | / |
| 3380 | |
| 3381 | Create a bytes object from a string of hexadecimal numbers. |
| 3382 | |
| 3383 | Spaces between two numbers are accepted. |
| 3384 | Example: bytes.fromhex('B9 01EF') -> b'\\xb9\\x01\\xef'. |
| 3385 | [clinic start generated code]*/ |
| 3386 | |
| 3387 | PyDoc_STRVAR(bytes_fromhex__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3388 | "fromhex($type, string, /)\n" |
| 3389 | "--\n" |
| 3390 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3391 | "Create a bytes object from a string of hexadecimal numbers.\n" |
| 3392 | "\n" |
| 3393 | "Spaces between two numbers are accepted.\n" |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3394 | "Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'."); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3395 | |
| 3396 | #define BYTES_FROMHEX_METHODDEF \ |
| 3397 | {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS, bytes_fromhex__doc__}, |
| 3398 | |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 3399 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3400 | bytes_fromhex_impl(PyTypeObject *type, PyObject *string); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3401 | |
| 3402 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3403 | bytes_fromhex(PyTypeObject *type, PyObject *args) |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 3404 | { |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3405 | PyObject *return_value = NULL; |
| 3406 | PyObject *string; |
| 3407 | |
| 3408 | if (!PyArg_ParseTuple(args, |
| 3409 | "U:fromhex", |
| 3410 | &string)) |
| 3411 | goto exit; |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3412 | return_value = bytes_fromhex_impl(type, string); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3413 | |
| 3414 | exit: |
| 3415 | return return_value; |
| 3416 | } |
| 3417 | |
| 3418 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3419 | bytes_fromhex_impl(PyTypeObject *type, PyObject *string) |
| 3420 | /*[clinic end generated code: output=09e6cbef56cbbb65 input=bf4d1c361670acd3]*/ |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3421 | { |
| 3422 | PyObject *newstring; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3423 | char *buf; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3424 | Py_ssize_t hexlen, byteslen, i, j; |
| 3425 | int top, bot; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3426 | void *data; |
| 3427 | unsigned int kind; |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 3428 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3429 | assert(PyUnicode_Check(string)); |
| 3430 | if (PyUnicode_READY(string)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3431 | return NULL; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3432 | kind = PyUnicode_KIND(string); |
| 3433 | data = PyUnicode_DATA(string); |
| 3434 | hexlen = PyUnicode_GET_LENGTH(string); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3435 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3436 | byteslen = hexlen/2; /* This overestimates if there are spaces */ |
| 3437 | newstring = PyBytes_FromStringAndSize(NULL, byteslen); |
| 3438 | if (!newstring) |
| 3439 | return NULL; |
| 3440 | buf = PyBytes_AS_STRING(newstring); |
| 3441 | for (i = j = 0; i < hexlen; i += 2) { |
| 3442 | /* skip over spaces in the input */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3443 | while (PyUnicode_READ(kind, data, i) == ' ') |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3444 | i++; |
| 3445 | if (i >= hexlen) |
| 3446 | break; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3447 | top = hex_digit_to_int(PyUnicode_READ(kind, data, i)); |
| 3448 | bot = hex_digit_to_int(PyUnicode_READ(kind, data, i+1)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3449 | if (top == -1 || bot == -1) { |
| 3450 | PyErr_Format(PyExc_ValueError, |
| 3451 | "non-hexadecimal number found in " |
| 3452 | "fromhex() arg at position %zd", i); |
| 3453 | goto error; |
| 3454 | } |
| 3455 | buf[j++] = (top << 4) + bot; |
| 3456 | } |
| 3457 | if (j != byteslen && _PyBytes_Resize(&newstring, j) < 0) |
| 3458 | goto error; |
| 3459 | return newstring; |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 3460 | |
| 3461 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3462 | Py_XDECREF(newstring); |
| 3463 | return NULL; |
Georg Brandl | 0b9b9e0 | 2007-02-27 08:40:54 +0000 | [diff] [blame] | 3464 | } |
| 3465 | |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3466 | /*[clinic input] |
| 3467 | bytes.__sizeof__ as bytes_sizeof |
| 3468 | |
| 3469 | self: self(type="PyBytesObject *") |
| 3470 | |
| 3471 | Returns the size of the bytes object in memory, in bytes. |
| 3472 | [clinic start generated code]*/ |
| 3473 | |
| 3474 | PyDoc_STRVAR(bytes_sizeof__doc__, |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3475 | "__sizeof__($self, /)\n" |
| 3476 | "--\n" |
| 3477 | "\n" |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3478 | "Returns the size of the bytes object in memory, in bytes."); |
| 3479 | |
| 3480 | #define BYTES_SIZEOF_METHODDEF \ |
| 3481 | {"__sizeof__", (PyCFunction)bytes_sizeof, METH_NOARGS, bytes_sizeof__doc__}, |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3482 | |
| 3483 | static PyObject * |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3484 | bytes_sizeof_impl(PyBytesObject *self); |
| 3485 | |
| 3486 | static PyObject * |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3487 | bytes_sizeof(PyBytesObject *self, PyObject *Py_UNUSED(ignored)) |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3488 | { |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3489 | return bytes_sizeof_impl(self); |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3490 | } |
| 3491 | |
| 3492 | static PyObject * |
| 3493 | bytes_sizeof_impl(PyBytesObject *self) |
Martin v. Löwis | 0efea32 | 2014-07-27 17:29:17 +0200 | [diff] [blame] | 3494 | /*[clinic end generated code: output=44933279343f24ae input=bee4c64bb42078ed]*/ |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3495 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3496 | Py_ssize_t res; |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3497 | res = PyBytesObject_SIZE + Py_SIZE(self) * Py_TYPE(self)->tp_itemsize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3498 | return PyLong_FromSsize_t(res); |
Martin v. Löwis | 00709aa | 2008-06-04 14:18:43 +0000 | [diff] [blame] | 3499 | } |
| 3500 | |
Guido van Rossum | 0dd32e2 | 2007-04-11 05:40:58 +0000 | [diff] [blame] | 3501 | |
| 3502 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 3503 | bytes_getnewargs(PyBytesObject *v) |
Guido van Rossum | 0dd32e2 | 2007-04-11 05:40:58 +0000 | [diff] [blame] | 3504 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3505 | return Py_BuildValue("(y#)", v->ob_sval, Py_SIZE(v)); |
Guido van Rossum | 0dd32e2 | 2007-04-11 05:40:58 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3508 | |
| 3509 | static PyMethodDef |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 3510 | bytes_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3511 | {"__getnewargs__", (PyCFunction)bytes_getnewargs, METH_NOARGS}, |
| 3512 | {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
| 3513 | _Py_capitalize__doc__}, |
| 3514 | {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, |
| 3515 | {"count", (PyCFunction)bytes_count, METH_VARARGS, count__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3516 | BYTES_DECODE_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3517 | {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS, |
| 3518 | endswith__doc__}, |
Ezio Melotti | 745d54d | 2013-11-16 19:10:57 +0200 | [diff] [blame] | 3519 | {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3520 | expandtabs__doc__}, |
| 3521 | {"find", (PyCFunction)bytes_find, METH_VARARGS, find__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3522 | BYTES_FROMHEX_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3523 | {"index", (PyCFunction)bytes_index, METH_VARARGS, index__doc__}, |
| 3524 | {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
| 3525 | _Py_isalnum__doc__}, |
| 3526 | {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
| 3527 | _Py_isalpha__doc__}, |
| 3528 | {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
| 3529 | _Py_isdigit__doc__}, |
| 3530 | {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
| 3531 | _Py_islower__doc__}, |
| 3532 | {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
| 3533 | _Py_isspace__doc__}, |
| 3534 | {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
| 3535 | _Py_istitle__doc__}, |
| 3536 | {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
| 3537 | _Py_isupper__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3538 | BYTES_JOIN_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3539 | {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, |
| 3540 | {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3541 | BYTES_LSTRIP_METHODDEF |
| 3542 | BYTES_MAKETRANS_METHODDEF |
| 3543 | BYTES_PARTITION_METHODDEF |
| 3544 | BYTES_REPLACE_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3545 | {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, rfind__doc__}, |
| 3546 | {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, rindex__doc__}, |
| 3547 | {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3548 | BYTES_RPARTITION_METHODDEF |
| 3549 | BYTES_RSPLIT_METHODDEF |
| 3550 | BYTES_RSTRIP_METHODDEF |
| 3551 | BYTES_SPLIT_METHODDEF |
| 3552 | BYTES_SPLITLINES_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3553 | {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS, |
| 3554 | startswith__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3555 | BYTES_STRIP_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3556 | {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
| 3557 | _Py_swapcase__doc__}, |
| 3558 | {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3559 | BYTES_TRANSLATE_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3560 | {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
| 3561 | {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, |
Martin v. Löwis | 7252a6e | 2014-07-27 16:25:09 +0200 | [diff] [blame] | 3562 | BYTES_SIZEOF_METHODDEF |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3563 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3564 | }; |
| 3565 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3566 | static PyObject * |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 3567 | bytes_mod(PyObject *v, PyObject *w) |
| 3568 | { |
| 3569 | if (!PyBytes_Check(v)) |
| 3570 | Py_RETURN_NOTIMPLEMENTED; |
| 3571 | return _PyBytes_Format(v, w); |
| 3572 | } |
| 3573 | |
| 3574 | static PyNumberMethods bytes_as_number = { |
| 3575 | 0, /*nb_add*/ |
| 3576 | 0, /*nb_subtract*/ |
| 3577 | 0, /*nb_multiply*/ |
| 3578 | bytes_mod, /*nb_remainder*/ |
| 3579 | }; |
| 3580 | |
| 3581 | static PyObject * |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3582 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 3583 | |
| 3584 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 3585 | bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3586 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3587 | PyObject *x = NULL; |
| 3588 | const char *encoding = NULL; |
| 3589 | const char *errors = NULL; |
| 3590 | PyObject *new = NULL; |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 3591 | PyObject *func; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3592 | Py_ssize_t size; |
| 3593 | static char *kwlist[] = {"source", "encoding", "errors", 0}; |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 3594 | _Py_IDENTIFIER(__bytes__); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3596 | if (type != &PyBytes_Type) |
| 3597 | return str_subtype_new(type, args, kwds); |
| 3598 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytes", kwlist, &x, |
| 3599 | &encoding, &errors)) |
| 3600 | return NULL; |
| 3601 | if (x == NULL) { |
| 3602 | if (encoding != NULL || errors != NULL) { |
| 3603 | PyErr_SetString(PyExc_TypeError, |
| 3604 | "encoding or errors without sequence " |
| 3605 | "argument"); |
| 3606 | return NULL; |
| 3607 | } |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3608 | return PyBytes_FromStringAndSize(NULL, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3609 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3611 | if (PyUnicode_Check(x)) { |
| 3612 | /* Encode via the codec registry */ |
| 3613 | if (encoding == NULL) { |
| 3614 | PyErr_SetString(PyExc_TypeError, |
| 3615 | "string argument without an encoding"); |
| 3616 | return NULL; |
| 3617 | } |
| 3618 | new = PyUnicode_AsEncodedString(x, encoding, errors); |
| 3619 | if (new == NULL) |
| 3620 | return NULL; |
| 3621 | assert(PyBytes_Check(new)); |
| 3622 | return new; |
| 3623 | } |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 3624 | |
Serhiy Storchaka | 83cf99d | 2014-12-02 09:24:06 +0200 | [diff] [blame] | 3625 | /* If it's not unicode, there can't be encoding or errors */ |
| 3626 | if (encoding != NULL || errors != NULL) { |
| 3627 | PyErr_SetString(PyExc_TypeError, |
| 3628 | "encoding or errors without a string argument"); |
| 3629 | return NULL; |
| 3630 | } |
| 3631 | |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 3632 | /* We'd like to call PyObject_Bytes here, but we need to check for an |
| 3633 | integer argument before deferring to PyBytes_FromObject, something |
| 3634 | PyObject_Bytes doesn't do. */ |
| 3635 | func = _PyObject_LookupSpecial(x, &PyId___bytes__); |
| 3636 | if (func != NULL) { |
| 3637 | new = PyObject_CallFunctionObjArgs(func, NULL); |
| 3638 | Py_DECREF(func); |
| 3639 | if (new == NULL) |
| 3640 | return NULL; |
| 3641 | if (!PyBytes_Check(new)) { |
| 3642 | PyErr_Format(PyExc_TypeError, |
| 3643 | "__bytes__ returned non-bytes (type %.200s)", |
| 3644 | Py_TYPE(new)->tp_name); |
| 3645 | Py_DECREF(new); |
| 3646 | return NULL; |
| 3647 | } |
| 3648 | return new; |
| 3649 | } |
| 3650 | else if (PyErr_Occurred()) |
| 3651 | return NULL; |
| 3652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3653 | /* Is it an integer? */ |
| 3654 | size = PyNumber_AsSsize_t(x, PyExc_OverflowError); |
| 3655 | if (size == -1 && PyErr_Occurred()) { |
| 3656 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
| 3657 | return NULL; |
| 3658 | PyErr_Clear(); |
| 3659 | } |
| 3660 | else if (size < 0) { |
| 3661 | PyErr_SetString(PyExc_ValueError, "negative count"); |
| 3662 | return NULL; |
| 3663 | } |
| 3664 | else { |
Victor Stinner | db067af | 2014-05-02 22:31:14 +0200 | [diff] [blame] | 3665 | new = _PyBytes_FromSize(size, 1); |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 3666 | if (new == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3667 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3668 | return new; |
| 3669 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3670 | |
Benjamin Peterson | 5ff3f73 | 2012-12-19 15:27:41 -0600 | [diff] [blame] | 3671 | return PyBytes_FromObject(x); |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
| 3674 | PyObject * |
| 3675 | PyBytes_FromObject(PyObject *x) |
| 3676 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3677 | PyObject *new, *it; |
| 3678 | Py_ssize_t i, size; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3680 | if (x == NULL) { |
| 3681 | PyErr_BadInternalCall(); |
| 3682 | return NULL; |
| 3683 | } |
Larry Hastings | ca28e99 | 2012-05-24 22:58:30 -0700 | [diff] [blame] | 3684 | |
| 3685 | if (PyBytes_CheckExact(x)) { |
| 3686 | Py_INCREF(x); |
| 3687 | return x; |
| 3688 | } |
| 3689 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3690 | /* Use the modern buffer interface */ |
| 3691 | if (PyObject_CheckBuffer(x)) { |
| 3692 | Py_buffer view; |
| 3693 | if (PyObject_GetBuffer(x, &view, PyBUF_FULL_RO) < 0) |
| 3694 | return NULL; |
| 3695 | new = PyBytes_FromStringAndSize(NULL, view.len); |
| 3696 | if (!new) |
| 3697 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3698 | if (PyBuffer_ToContiguous(((PyBytesObject *)new)->ob_sval, |
| 3699 | &view, view.len, 'C') < 0) |
| 3700 | goto fail; |
| 3701 | PyBuffer_Release(&view); |
| 3702 | return new; |
| 3703 | fail: |
| 3704 | Py_XDECREF(new); |
| 3705 | PyBuffer_Release(&view); |
| 3706 | return NULL; |
| 3707 | } |
| 3708 | if (PyUnicode_Check(x)) { |
| 3709 | PyErr_SetString(PyExc_TypeError, |
| 3710 | "cannot convert unicode object to bytes"); |
| 3711 | return NULL; |
| 3712 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3713 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3714 | if (PyList_CheckExact(x)) { |
| 3715 | new = PyBytes_FromStringAndSize(NULL, Py_SIZE(x)); |
| 3716 | if (new == NULL) |
| 3717 | return NULL; |
| 3718 | for (i = 0; i < Py_SIZE(x); i++) { |
| 3719 | Py_ssize_t value = PyNumber_AsSsize_t( |
| 3720 | PyList_GET_ITEM(x, i), PyExc_ValueError); |
| 3721 | if (value == -1 && PyErr_Occurred()) { |
| 3722 | Py_DECREF(new); |
| 3723 | return NULL; |
| 3724 | } |
| 3725 | if (value < 0 || value >= 256) { |
| 3726 | PyErr_SetString(PyExc_ValueError, |
| 3727 | "bytes must be in range(0, 256)"); |
| 3728 | Py_DECREF(new); |
| 3729 | return NULL; |
| 3730 | } |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 3731 | ((PyBytesObject *)new)->ob_sval[i] = (char) value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3732 | } |
| 3733 | return new; |
| 3734 | } |
| 3735 | if (PyTuple_CheckExact(x)) { |
| 3736 | new = PyBytes_FromStringAndSize(NULL, Py_SIZE(x)); |
| 3737 | if (new == NULL) |
| 3738 | return NULL; |
| 3739 | for (i = 0; i < Py_SIZE(x); i++) { |
| 3740 | Py_ssize_t value = PyNumber_AsSsize_t( |
| 3741 | PyTuple_GET_ITEM(x, i), PyExc_ValueError); |
| 3742 | if (value == -1 && PyErr_Occurred()) { |
| 3743 | Py_DECREF(new); |
| 3744 | return NULL; |
| 3745 | } |
| 3746 | if (value < 0 || value >= 256) { |
| 3747 | PyErr_SetString(PyExc_ValueError, |
| 3748 | "bytes must be in range(0, 256)"); |
| 3749 | Py_DECREF(new); |
| 3750 | return NULL; |
| 3751 | } |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 3752 | ((PyBytesObject *)new)->ob_sval[i] = (char) value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3753 | } |
| 3754 | return new; |
| 3755 | } |
Alexandre Vassalotti | a5c565a | 2010-01-09 22:14:46 +0000 | [diff] [blame] | 3756 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3757 | /* For iterator version, create a string object and resize as needed */ |
Armin Ronacher | aa9a79d | 2012-10-06 14:03:24 +0200 | [diff] [blame] | 3758 | size = PyObject_LengthHint(x, 64); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3759 | if (size == -1 && PyErr_Occurred()) |
| 3760 | return NULL; |
| 3761 | /* Allocate an extra byte to prevent PyBytes_FromStringAndSize() from |
| 3762 | returning a shared empty bytes string. This required because we |
| 3763 | want to call _PyBytes_Resize() the returned object, which we can |
| 3764 | only do on bytes objects with refcount == 1. */ |
Victor Stinner | 88d146b | 2014-08-17 21:12:18 +0200 | [diff] [blame] | 3765 | if (size == 0) |
| 3766 | size = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3767 | new = PyBytes_FromStringAndSize(NULL, size); |
| 3768 | if (new == NULL) |
| 3769 | return NULL; |
Victor Stinner | 88d146b | 2014-08-17 21:12:18 +0200 | [diff] [blame] | 3770 | assert(Py_REFCNT(new) == 1); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3772 | /* Get the iterator */ |
| 3773 | it = PyObject_GetIter(x); |
| 3774 | if (it == NULL) |
| 3775 | goto error; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3776 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3777 | /* Run the iterator to exhaustion */ |
| 3778 | for (i = 0; ; i++) { |
| 3779 | PyObject *item; |
| 3780 | Py_ssize_t value; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3782 | /* Get the next item */ |
| 3783 | item = PyIter_Next(it); |
| 3784 | if (item == NULL) { |
| 3785 | if (PyErr_Occurred()) |
| 3786 | goto error; |
| 3787 | break; |
| 3788 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3790 | /* Interpret it as an int (__index__) */ |
| 3791 | value = PyNumber_AsSsize_t(item, PyExc_ValueError); |
| 3792 | Py_DECREF(item); |
| 3793 | if (value == -1 && PyErr_Occurred()) |
| 3794 | goto error; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3796 | /* Range check */ |
| 3797 | if (value < 0 || value >= 256) { |
| 3798 | PyErr_SetString(PyExc_ValueError, |
| 3799 | "bytes must be in range(0, 256)"); |
| 3800 | goto error; |
| 3801 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3802 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3803 | /* Append the byte */ |
| 3804 | if (i >= size) { |
| 3805 | size = 2 * size + 1; |
| 3806 | if (_PyBytes_Resize(&new, size) < 0) |
| 3807 | goto error; |
| 3808 | } |
Antoine Pitrou | 0010d37 | 2010-08-15 17:12:55 +0000 | [diff] [blame] | 3809 | ((PyBytesObject *)new)->ob_sval[i] = (char) value; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3810 | } |
| 3811 | _PyBytes_Resize(&new, i); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3813 | /* Clean up and return success */ |
| 3814 | Py_DECREF(it); |
| 3815 | return new; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3816 | |
| 3817 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3818 | Py_XDECREF(it); |
Victor Stinner | 986e224 | 2013-10-29 03:14:22 +0100 | [diff] [blame] | 3819 | Py_XDECREF(new); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3820 | return NULL; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3821 | } |
| 3822 | |
| 3823 | static PyObject * |
| 3824 | str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3825 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3826 | PyObject *tmp, *pnew; |
| 3827 | Py_ssize_t n; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3829 | assert(PyType_IsSubtype(type, &PyBytes_Type)); |
| 3830 | tmp = bytes_new(&PyBytes_Type, args, kwds); |
| 3831 | if (tmp == NULL) |
| 3832 | return NULL; |
| 3833 | assert(PyBytes_CheckExact(tmp)); |
| 3834 | n = PyBytes_GET_SIZE(tmp); |
| 3835 | pnew = type->tp_alloc(type, n); |
| 3836 | if (pnew != NULL) { |
| 3837 | Py_MEMCPY(PyBytes_AS_STRING(pnew), |
| 3838 | PyBytes_AS_STRING(tmp), n+1); |
| 3839 | ((PyBytesObject *)pnew)->ob_shash = |
| 3840 | ((PyBytesObject *)tmp)->ob_shash; |
| 3841 | } |
| 3842 | Py_DECREF(tmp); |
| 3843 | return pnew; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3844 | } |
| 3845 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 3846 | PyDoc_STRVAR(bytes_doc, |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 3847 | "bytes(iterable_of_ints) -> bytes\n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3848 | bytes(string, encoding[, errors]) -> bytes\n\ |
Georg Brandl | 17cb8a8 | 2008-05-30 08:20:09 +0000 | [diff] [blame] | 3849 | bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 3850 | bytes(int) -> bytes object of size given by the parameter initialized with null bytes\n\ |
| 3851 | bytes() -> empty bytes object\n\ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3852 | \n\ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3853 | Construct an immutable array of bytes from:\n\ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3854 | - an iterable yielding integers in range(256)\n\ |
| 3855 | - a text string encoded using the specified encoding\n\ |
Victor Stinner | bb2e9c4 | 2011-12-17 23:18:07 +0100 | [diff] [blame] | 3856 | - any object implementing the buffer API.\n\ |
| 3857 | - an integer"); |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 3858 | |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 3859 | static PyObject *bytes_iter(PyObject *seq); |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3860 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3861 | PyTypeObject PyBytes_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3862 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 3863 | "bytes", |
| 3864 | PyBytesObject_SIZE, |
| 3865 | sizeof(char), |
| 3866 | bytes_dealloc, /* tp_dealloc */ |
| 3867 | 0, /* tp_print */ |
| 3868 | 0, /* tp_getattr */ |
| 3869 | 0, /* tp_setattr */ |
| 3870 | 0, /* tp_reserved */ |
| 3871 | (reprfunc)bytes_repr, /* tp_repr */ |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 3872 | &bytes_as_number, /* tp_as_number */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3873 | &bytes_as_sequence, /* tp_as_sequence */ |
| 3874 | &bytes_as_mapping, /* tp_as_mapping */ |
| 3875 | (hashfunc)bytes_hash, /* tp_hash */ |
| 3876 | 0, /* tp_call */ |
| 3877 | bytes_str, /* tp_str */ |
| 3878 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 3879 | 0, /* tp_setattro */ |
| 3880 | &bytes_as_buffer, /* tp_as_buffer */ |
| 3881 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 3882 | Py_TPFLAGS_BYTES_SUBCLASS, /* tp_flags */ |
| 3883 | bytes_doc, /* tp_doc */ |
| 3884 | 0, /* tp_traverse */ |
| 3885 | 0, /* tp_clear */ |
| 3886 | (richcmpfunc)bytes_richcompare, /* tp_richcompare */ |
| 3887 | 0, /* tp_weaklistoffset */ |
| 3888 | bytes_iter, /* tp_iter */ |
| 3889 | 0, /* tp_iternext */ |
| 3890 | bytes_methods, /* tp_methods */ |
| 3891 | 0, /* tp_members */ |
| 3892 | 0, /* tp_getset */ |
| 3893 | &PyBaseObject_Type, /* tp_base */ |
| 3894 | 0, /* tp_dict */ |
| 3895 | 0, /* tp_descr_get */ |
| 3896 | 0, /* tp_descr_set */ |
| 3897 | 0, /* tp_dictoffset */ |
| 3898 | 0, /* tp_init */ |
| 3899 | 0, /* tp_alloc */ |
| 3900 | bytes_new, /* tp_new */ |
| 3901 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 3902 | }; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 3903 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3904 | void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3905 | PyBytes_Concat(PyObject **pv, PyObject *w) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3906 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3907 | assert(pv != NULL); |
| 3908 | if (*pv == NULL) |
| 3909 | return; |
| 3910 | if (w == NULL) { |
Serhiy Storchaka | f458a03 | 2013-02-02 18:45:22 +0200 | [diff] [blame] | 3911 | Py_CLEAR(*pv); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3912 | return; |
| 3913 | } |
Antoine Pitrou | 161d695 | 2014-05-01 14:36:20 +0200 | [diff] [blame] | 3914 | |
| 3915 | if (Py_REFCNT(*pv) == 1 && PyBytes_CheckExact(*pv)) { |
| 3916 | /* Only one reference, so we can resize in place */ |
Zachary Ware | bca9694 | 2014-05-06 11:42:37 -0500 | [diff] [blame] | 3917 | Py_ssize_t oldsize; |
Antoine Pitrou | 161d695 | 2014-05-01 14:36:20 +0200 | [diff] [blame] | 3918 | Py_buffer wb; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 3919 | |
Antoine Pitrou | 161d695 | 2014-05-01 14:36:20 +0200 | [diff] [blame] | 3920 | wb.len = -1; |
Serhiy Storchaka | 3dd3e26 | 2015-02-03 01:25:42 +0200 | [diff] [blame] | 3921 | if (PyObject_GetBuffer(w, &wb, PyBUF_SIMPLE) != 0) { |
Antoine Pitrou | 161d695 | 2014-05-01 14:36:20 +0200 | [diff] [blame] | 3922 | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
| 3923 | Py_TYPE(w)->tp_name, Py_TYPE(*pv)->tp_name); |
| 3924 | Py_CLEAR(*pv); |
| 3925 | return; |
| 3926 | } |
| 3927 | |
| 3928 | oldsize = PyBytes_GET_SIZE(*pv); |
| 3929 | if (oldsize > PY_SSIZE_T_MAX - wb.len) { |
| 3930 | PyErr_NoMemory(); |
| 3931 | goto error; |
| 3932 | } |
| 3933 | if (_PyBytes_Resize(pv, oldsize + wb.len) < 0) |
| 3934 | goto error; |
| 3935 | |
| 3936 | memcpy(PyBytes_AS_STRING(*pv) + oldsize, wb.buf, wb.len); |
| 3937 | PyBuffer_Release(&wb); |
| 3938 | return; |
| 3939 | |
| 3940 | error: |
| 3941 | PyBuffer_Release(&wb); |
| 3942 | Py_CLEAR(*pv); |
| 3943 | return; |
| 3944 | } |
| 3945 | |
| 3946 | else { |
| 3947 | /* Multiple references, need to create new object */ |
| 3948 | PyObject *v; |
| 3949 | v = bytes_concat(*pv, w); |
| 3950 | Py_DECREF(*pv); |
| 3951 | *pv = v; |
| 3952 | } |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
| 3955 | void |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3956 | PyBytes_ConcatAndDel(PyObject **pv, PyObject *w) |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3957 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3958 | PyBytes_Concat(pv, w); |
| 3959 | Py_XDECREF(w); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3960 | } |
| 3961 | |
| 3962 | |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 3963 | /* The following function breaks the notion that bytes are immutable: |
| 3964 | it changes the size of a bytes object. We get away with this only if there |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3965 | is only one module referencing the object. You can also think of it |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 3966 | as creating a new bytes object and destroying the old one, only |
| 3967 | more efficiently. In any case, don't use this if the bytes object may |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3968 | already be known to some other part of the code... |
Ethan Furman | b95b561 | 2015-01-23 20:05:18 -0800 | [diff] [blame] | 3969 | Note that if there's not enough memory to resize the bytes object, the |
| 3970 | original bytes object at *pv is deallocated, *pv is set to NULL, an "out of |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3971 | memory" exception is set, and -1 is returned. Else (on success) 0 is |
| 3972 | returned, and the value in *pv may or may not be the same as on input. |
| 3973 | As always, an extra byte is allocated for a trailing \0 byte (newsize |
| 3974 | does *not* include that), and a trailing \0 byte is stored. |
| 3975 | */ |
| 3976 | |
| 3977 | int |
| 3978 | _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize) |
| 3979 | { |
Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 3980 | PyObject *v; |
| 3981 | PyBytesObject *sv; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3982 | v = *pv; |
| 3983 | if (!PyBytes_Check(v) || Py_REFCNT(v) != 1 || newsize < 0) { |
| 3984 | *pv = 0; |
| 3985 | Py_DECREF(v); |
| 3986 | PyErr_BadInternalCall(); |
| 3987 | return -1; |
| 3988 | } |
| 3989 | /* XXX UNREF/NEWREF interface should be more symmetrical */ |
| 3990 | _Py_DEC_REFTOTAL; |
| 3991 | _Py_ForgetReference(v); |
| 3992 | *pv = (PyObject *) |
Serhiy Storchaka | 20b39b2 | 2014-09-28 11:27:24 +0300 | [diff] [blame] | 3993 | PyObject_REALLOC(v, PyBytesObject_SIZE + newsize); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3994 | if (*pv == NULL) { |
| 3995 | PyObject_Del(v); |
| 3996 | PyErr_NoMemory(); |
| 3997 | return -1; |
| 3998 | } |
| 3999 | _Py_NewReference(*pv); |
| 4000 | sv = (PyBytesObject *) *pv; |
| 4001 | Py_SIZE(sv) = newsize; |
| 4002 | sv->ob_sval[newsize] = '\0'; |
| 4003 | sv->ob_shash = -1; /* invalidate cached hash value */ |
| 4004 | return 0; |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 4005 | } |
| 4006 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 4007 | void |
| 4008 | PyBytes_Fini(void) |
| 4009 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4010 | int i; |
Serhiy Storchaka | f458a03 | 2013-02-02 18:45:22 +0200 | [diff] [blame] | 4011 | for (i = 0; i < UCHAR_MAX + 1; i++) |
| 4012 | Py_CLEAR(characters[i]); |
| 4013 | Py_CLEAR(nullstring); |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
Benjamin Peterson | 4116f36 | 2008-05-27 00:36:20 +0000 | [diff] [blame] | 4016 | /*********************** Bytes Iterator ****************************/ |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4017 | |
| 4018 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4019 | PyObject_HEAD |
| 4020 | Py_ssize_t it_index; |
| 4021 | PyBytesObject *it_seq; /* Set to NULL when iterator is exhausted */ |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 4022 | } striterobject; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4023 | |
| 4024 | static void |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 4025 | striter_dealloc(striterobject *it) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4026 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4027 | _PyObject_GC_UNTRACK(it); |
| 4028 | Py_XDECREF(it->it_seq); |
| 4029 | PyObject_GC_Del(it); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
| 4032 | static int |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 4033 | striter_traverse(striterobject *it, visitproc visit, void *arg) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4034 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4035 | Py_VISIT(it->it_seq); |
| 4036 | return 0; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4037 | } |
| 4038 | |
| 4039 | static PyObject * |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 4040 | striter_next(striterobject *it) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4041 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4042 | PyBytesObject *seq; |
| 4043 | PyObject *item; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4044 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4045 | assert(it != NULL); |
| 4046 | seq = it->it_seq; |
| 4047 | if (seq == NULL) |
| 4048 | return NULL; |
| 4049 | assert(PyBytes_Check(seq)); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4050 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4051 | if (it->it_index < PyBytes_GET_SIZE(seq)) { |
| 4052 | item = PyLong_FromLong( |
| 4053 | (unsigned char)seq->ob_sval[it->it_index]); |
| 4054 | if (item != NULL) |
| 4055 | ++it->it_index; |
| 4056 | return item; |
| 4057 | } |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4058 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4059 | Py_DECREF(seq); |
| 4060 | it->it_seq = NULL; |
| 4061 | return NULL; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4062 | } |
| 4063 | |
| 4064 | static PyObject * |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 4065 | striter_len(striterobject *it) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4066 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4067 | Py_ssize_t len = 0; |
| 4068 | if (it->it_seq) |
| 4069 | len = PyBytes_GET_SIZE(it->it_seq) - it->it_index; |
| 4070 | return PyLong_FromSsize_t(len); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4071 | } |
| 4072 | |
| 4073 | PyDoc_STRVAR(length_hint_doc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4074 | "Private method returning an estimate of len(list(it))."); |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4075 | |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4076 | static PyObject * |
| 4077 | striter_reduce(striterobject *it) |
| 4078 | { |
| 4079 | if (it->it_seq != NULL) { |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 4080 | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4081 | it->it_seq, it->it_index); |
| 4082 | } else { |
| 4083 | PyObject *u = PyUnicode_FromUnicode(NULL, 0); |
| 4084 | if (u == NULL) |
| 4085 | return NULL; |
Antoine Pitrou | a701388 | 2012-04-05 00:04:20 +0200 | [diff] [blame] | 4086 | return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u); |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4087 | } |
| 4088 | } |
| 4089 | |
| 4090 | PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); |
| 4091 | |
| 4092 | static PyObject * |
| 4093 | striter_setstate(striterobject *it, PyObject *state) |
| 4094 | { |
| 4095 | Py_ssize_t index = PyLong_AsSsize_t(state); |
| 4096 | if (index == -1 && PyErr_Occurred()) |
| 4097 | return NULL; |
Kristján Valur Jónsson | 25dded0 | 2014-03-05 13:47:57 +0000 | [diff] [blame] | 4098 | if (it->it_seq != NULL) { |
| 4099 | if (index < 0) |
| 4100 | index = 0; |
| 4101 | else if (index > PyBytes_GET_SIZE(it->it_seq)) |
| 4102 | index = PyBytes_GET_SIZE(it->it_seq); /* iterator exhausted */ |
| 4103 | it->it_index = index; |
| 4104 | } |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4105 | Py_RETURN_NONE; |
| 4106 | } |
| 4107 | |
| 4108 | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
| 4109 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 4110 | static PyMethodDef striter_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4111 | {"__length_hint__", (PyCFunction)striter_len, METH_NOARGS, |
| 4112 | length_hint_doc}, |
Kristján Valur Jónsson | 31668b8 | 2012-04-03 10:49:41 +0000 | [diff] [blame] | 4113 | {"__reduce__", (PyCFunction)striter_reduce, METH_NOARGS, |
| 4114 | reduce_doc}, |
| 4115 | {"__setstate__", (PyCFunction)striter_setstate, METH_O, |
| 4116 | setstate_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4117 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4118 | }; |
| 4119 | |
Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 4120 | PyTypeObject PyBytesIter_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4121 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 4122 | "bytes_iterator", /* tp_name */ |
| 4123 | sizeof(striterobject), /* tp_basicsize */ |
| 4124 | 0, /* tp_itemsize */ |
| 4125 | /* methods */ |
| 4126 | (destructor)striter_dealloc, /* tp_dealloc */ |
| 4127 | 0, /* tp_print */ |
| 4128 | 0, /* tp_getattr */ |
| 4129 | 0, /* tp_setattr */ |
| 4130 | 0, /* tp_reserved */ |
| 4131 | 0, /* tp_repr */ |
| 4132 | 0, /* tp_as_number */ |
| 4133 | 0, /* tp_as_sequence */ |
| 4134 | 0, /* tp_as_mapping */ |
| 4135 | 0, /* tp_hash */ |
| 4136 | 0, /* tp_call */ |
| 4137 | 0, /* tp_str */ |
| 4138 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4139 | 0, /* tp_setattro */ |
| 4140 | 0, /* tp_as_buffer */ |
| 4141 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 4142 | 0, /* tp_doc */ |
| 4143 | (traverseproc)striter_traverse, /* tp_traverse */ |
| 4144 | 0, /* tp_clear */ |
| 4145 | 0, /* tp_richcompare */ |
| 4146 | 0, /* tp_weaklistoffset */ |
| 4147 | PyObject_SelfIter, /* tp_iter */ |
| 4148 | (iternextfunc)striter_next, /* tp_iternext */ |
| 4149 | striter_methods, /* tp_methods */ |
| 4150 | 0, |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4151 | }; |
| 4152 | |
| 4153 | static PyObject * |
Benjamin Peterson | 80688ef | 2009-04-18 15:17:02 +0000 | [diff] [blame] | 4154 | bytes_iter(PyObject *seq) |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4155 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4156 | striterobject *it; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4157 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4158 | if (!PyBytes_Check(seq)) { |
| 4159 | PyErr_BadInternalCall(); |
| 4160 | return NULL; |
| 4161 | } |
| 4162 | it = PyObject_GC_New(striterobject, &PyBytesIter_Type); |
| 4163 | if (it == NULL) |
| 4164 | return NULL; |
| 4165 | it->it_index = 0; |
| 4166 | Py_INCREF(seq); |
| 4167 | it->it_seq = (PyBytesObject *)seq; |
| 4168 | _PyObject_GC_TRACK(it); |
| 4169 | return (PyObject *)it; |
Guido van Rossum | a5d2d55 | 2007-10-26 17:39:48 +0000 | [diff] [blame] | 4170 | } |