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