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