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