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