Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
| 5 | PyDoc_STRVAR(bytes_split__doc__, |
| 6 | "split($self, /, sep=None, maxsplit=-1)\n" |
| 7 | "--\n" |
| 8 | "\n" |
| 9 | "Return a list of the sections in the bytes, using sep as the delimiter.\n" |
| 10 | "\n" |
| 11 | " sep\n" |
| 12 | " The delimiter according which to split the bytes.\n" |
| 13 | " None (the default value) means split on ASCII whitespace characters\n" |
| 14 | " (space, tab, return, newline, formfeed, vertical tab).\n" |
| 15 | " maxsplit\n" |
| 16 | " Maximum number of splits to do.\n" |
| 17 | " -1 (the default value) means no limit."); |
| 18 | |
| 19 | #define BYTES_SPLIT_METHODDEF \ |
| 20 | {"split", (PyCFunction)bytes_split, METH_VARARGS|METH_KEYWORDS, bytes_split__doc__}, |
| 21 | |
| 22 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 23 | bytes_split_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 24 | |
| 25 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 26 | bytes_split(PyBytesObject *self, PyObject *args, PyObject *kwargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 27 | { |
| 28 | PyObject *return_value = NULL; |
| 29 | static char *_keywords[] = {"sep", "maxsplit", NULL}; |
| 30 | PyObject *sep = Py_None; |
| 31 | Py_ssize_t maxsplit = -1; |
| 32 | |
Serhiy Storchaka | 247789c | 2015-04-24 00:40:51 +0300 | [diff] [blame] | 33 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|On:split", _keywords, |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 34 | &sep, &maxsplit)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 35 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 36 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 37 | return_value = bytes_split_impl(self, sep, maxsplit); |
| 38 | |
| 39 | exit: |
| 40 | return return_value; |
| 41 | } |
| 42 | |
| 43 | PyDoc_STRVAR(bytes_partition__doc__, |
| 44 | "partition($self, sep, /)\n" |
| 45 | "--\n" |
| 46 | "\n" |
| 47 | "Partition the bytes into three parts using the given separator.\n" |
| 48 | "\n" |
| 49 | "This will search for the separator sep in the bytes. If the separator is found,\n" |
| 50 | "returns a 3-tuple containing the part before the separator, the separator\n" |
| 51 | "itself, and the part after it.\n" |
| 52 | "\n" |
| 53 | "If the separator is not found, returns a 3-tuple containing the original bytes\n" |
| 54 | "object and two empty bytes objects."); |
| 55 | |
| 56 | #define BYTES_PARTITION_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 57 | {"partition", (PyCFunction)bytes_partition, METH_O, bytes_partition__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 58 | |
| 59 | static PyObject * |
| 60 | bytes_partition_impl(PyBytesObject *self, Py_buffer *sep); |
| 61 | |
| 62 | static PyObject * |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 63 | bytes_partition(PyBytesObject *self, PyObject *arg) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 64 | { |
| 65 | PyObject *return_value = NULL; |
| 66 | Py_buffer sep = {NULL, NULL}; |
| 67 | |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 68 | if (!PyArg_Parse(arg, "y*:partition", &sep)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 69 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 70 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 71 | return_value = bytes_partition_impl(self, &sep); |
| 72 | |
| 73 | exit: |
| 74 | /* Cleanup for sep */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 75 | if (sep.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 76 | PyBuffer_Release(&sep); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 77 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 78 | |
| 79 | return return_value; |
| 80 | } |
| 81 | |
| 82 | PyDoc_STRVAR(bytes_rpartition__doc__, |
| 83 | "rpartition($self, sep, /)\n" |
| 84 | "--\n" |
| 85 | "\n" |
| 86 | "Partition the bytes into three parts using the given separator.\n" |
| 87 | "\n" |
| 88 | "This will search for the separator sep in the bytes, starting and the end. If\n" |
| 89 | "the separator is found, returns a 3-tuple containing the part before the\n" |
| 90 | "separator, the separator itself, and the part after it.\n" |
| 91 | "\n" |
| 92 | "If the separator is not found, returns a 3-tuple containing two empty bytes\n" |
| 93 | "objects and the original bytes object."); |
| 94 | |
| 95 | #define BYTES_RPARTITION_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 96 | {"rpartition", (PyCFunction)bytes_rpartition, METH_O, bytes_rpartition__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 97 | |
| 98 | static PyObject * |
| 99 | bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep); |
| 100 | |
| 101 | static PyObject * |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 102 | bytes_rpartition(PyBytesObject *self, PyObject *arg) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 103 | { |
| 104 | PyObject *return_value = NULL; |
| 105 | Py_buffer sep = {NULL, NULL}; |
| 106 | |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 107 | if (!PyArg_Parse(arg, "y*:rpartition", &sep)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 108 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 109 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 110 | return_value = bytes_rpartition_impl(self, &sep); |
| 111 | |
| 112 | exit: |
| 113 | /* Cleanup for sep */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 114 | if (sep.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 115 | PyBuffer_Release(&sep); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 116 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 117 | |
| 118 | return return_value; |
| 119 | } |
| 120 | |
| 121 | PyDoc_STRVAR(bytes_rsplit__doc__, |
| 122 | "rsplit($self, /, sep=None, maxsplit=-1)\n" |
| 123 | "--\n" |
| 124 | "\n" |
| 125 | "Return a list of the sections in the bytes, using sep as the delimiter.\n" |
| 126 | "\n" |
| 127 | " sep\n" |
| 128 | " The delimiter according which to split the bytes.\n" |
| 129 | " None (the default value) means split on ASCII whitespace characters\n" |
| 130 | " (space, tab, return, newline, formfeed, vertical tab).\n" |
| 131 | " maxsplit\n" |
| 132 | " Maximum number of splits to do.\n" |
| 133 | " -1 (the default value) means no limit.\n" |
| 134 | "\n" |
| 135 | "Splitting is done starting at the end of the bytes and working to the front."); |
| 136 | |
| 137 | #define BYTES_RSPLIT_METHODDEF \ |
| 138 | {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS|METH_KEYWORDS, bytes_rsplit__doc__}, |
| 139 | |
| 140 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 141 | bytes_rsplit_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 142 | |
| 143 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 144 | bytes_rsplit(PyBytesObject *self, PyObject *args, PyObject *kwargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 145 | { |
| 146 | PyObject *return_value = NULL; |
| 147 | static char *_keywords[] = {"sep", "maxsplit", NULL}; |
| 148 | PyObject *sep = Py_None; |
| 149 | Py_ssize_t maxsplit = -1; |
| 150 | |
Serhiy Storchaka | 247789c | 2015-04-24 00:40:51 +0300 | [diff] [blame] | 151 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|On:rsplit", _keywords, |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 152 | &sep, &maxsplit)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 153 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 154 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 155 | return_value = bytes_rsplit_impl(self, sep, maxsplit); |
| 156 | |
| 157 | exit: |
| 158 | return return_value; |
| 159 | } |
| 160 | |
| 161 | PyDoc_STRVAR(bytes_join__doc__, |
| 162 | "join($self, iterable_of_bytes, /)\n" |
| 163 | "--\n" |
| 164 | "\n" |
| 165 | "Concatenate any number of bytes objects.\n" |
| 166 | "\n" |
| 167 | "The bytes whose method is called is inserted in between each pair.\n" |
| 168 | "\n" |
| 169 | "The result is returned as a new bytes object.\n" |
| 170 | "\n" |
| 171 | "Example: b\'.\'.join([b\'ab\', b\'pq\', b\'rs\']) -> b\'ab.pq.rs\'."); |
| 172 | |
| 173 | #define BYTES_JOIN_METHODDEF \ |
| 174 | {"join", (PyCFunction)bytes_join, METH_O, bytes_join__doc__}, |
| 175 | |
| 176 | PyDoc_STRVAR(bytes_strip__doc__, |
| 177 | "strip($self, bytes=None, /)\n" |
| 178 | "--\n" |
| 179 | "\n" |
| 180 | "Strip leading and trailing bytes contained in the argument.\n" |
| 181 | "\n" |
| 182 | "If the argument is omitted or None, strip leading and trailing ASCII whitespace."); |
| 183 | |
| 184 | #define BYTES_STRIP_METHODDEF \ |
| 185 | {"strip", (PyCFunction)bytes_strip, METH_VARARGS, bytes_strip__doc__}, |
| 186 | |
| 187 | static PyObject * |
| 188 | bytes_strip_impl(PyBytesObject *self, PyObject *bytes); |
| 189 | |
| 190 | static PyObject * |
| 191 | bytes_strip(PyBytesObject *self, PyObject *args) |
| 192 | { |
| 193 | PyObject *return_value = NULL; |
| 194 | PyObject *bytes = Py_None; |
| 195 | |
| 196 | if (!PyArg_UnpackTuple(args, "strip", |
| 197 | 0, 1, |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 198 | &bytes)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 199 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 200 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 201 | return_value = bytes_strip_impl(self, bytes); |
| 202 | |
| 203 | exit: |
| 204 | return return_value; |
| 205 | } |
| 206 | |
| 207 | PyDoc_STRVAR(bytes_lstrip__doc__, |
| 208 | "lstrip($self, bytes=None, /)\n" |
| 209 | "--\n" |
| 210 | "\n" |
| 211 | "Strip leading bytes contained in the argument.\n" |
| 212 | "\n" |
| 213 | "If the argument is omitted or None, strip leading ASCII whitespace."); |
| 214 | |
| 215 | #define BYTES_LSTRIP_METHODDEF \ |
| 216 | {"lstrip", (PyCFunction)bytes_lstrip, METH_VARARGS, bytes_lstrip__doc__}, |
| 217 | |
| 218 | static PyObject * |
| 219 | bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes); |
| 220 | |
| 221 | static PyObject * |
| 222 | bytes_lstrip(PyBytesObject *self, PyObject *args) |
| 223 | { |
| 224 | PyObject *return_value = NULL; |
| 225 | PyObject *bytes = Py_None; |
| 226 | |
| 227 | if (!PyArg_UnpackTuple(args, "lstrip", |
| 228 | 0, 1, |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 229 | &bytes)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 230 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 231 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 232 | return_value = bytes_lstrip_impl(self, bytes); |
| 233 | |
| 234 | exit: |
| 235 | return return_value; |
| 236 | } |
| 237 | |
| 238 | PyDoc_STRVAR(bytes_rstrip__doc__, |
| 239 | "rstrip($self, bytes=None, /)\n" |
| 240 | "--\n" |
| 241 | "\n" |
| 242 | "Strip trailing bytes contained in the argument.\n" |
| 243 | "\n" |
| 244 | "If the argument is omitted or None, strip trailing ASCII whitespace."); |
| 245 | |
| 246 | #define BYTES_RSTRIP_METHODDEF \ |
| 247 | {"rstrip", (PyCFunction)bytes_rstrip, METH_VARARGS, bytes_rstrip__doc__}, |
| 248 | |
| 249 | static PyObject * |
| 250 | bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes); |
| 251 | |
| 252 | static PyObject * |
| 253 | bytes_rstrip(PyBytesObject *self, PyObject *args) |
| 254 | { |
| 255 | PyObject *return_value = NULL; |
| 256 | PyObject *bytes = Py_None; |
| 257 | |
| 258 | if (!PyArg_UnpackTuple(args, "rstrip", |
| 259 | 0, 1, |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 260 | &bytes)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 261 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 262 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 263 | return_value = bytes_rstrip_impl(self, bytes); |
| 264 | |
| 265 | exit: |
| 266 | return return_value; |
| 267 | } |
| 268 | |
| 269 | PyDoc_STRVAR(bytes_translate__doc__, |
| 270 | "translate(table, [deletechars])\n" |
| 271 | "Return a copy with each character mapped by the given translation table.\n" |
| 272 | "\n" |
| 273 | " table\n" |
| 274 | " Translation table, which must be a bytes object of length 256.\n" |
| 275 | "\n" |
| 276 | "All characters occurring in the optional argument deletechars are removed.\n" |
| 277 | "The remaining characters are mapped through the given translation table."); |
| 278 | |
| 279 | #define BYTES_TRANSLATE_METHODDEF \ |
| 280 | {"translate", (PyCFunction)bytes_translate, METH_VARARGS, bytes_translate__doc__}, |
| 281 | |
| 282 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 283 | bytes_translate_impl(PyBytesObject *self, PyObject *table, int group_right_1, |
| 284 | PyObject *deletechars); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 285 | |
| 286 | static PyObject * |
| 287 | bytes_translate(PyBytesObject *self, PyObject *args) |
| 288 | { |
| 289 | PyObject *return_value = NULL; |
| 290 | PyObject *table; |
| 291 | int group_right_1 = 0; |
| 292 | PyObject *deletechars = NULL; |
| 293 | |
| 294 | switch (PyTuple_GET_SIZE(args)) { |
| 295 | case 1: |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 296 | if (!PyArg_ParseTuple(args, "O:translate", &table)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 297 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 298 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 299 | break; |
| 300 | case 2: |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 301 | if (!PyArg_ParseTuple(args, "OO:translate", &table, &deletechars)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 302 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 303 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 304 | group_right_1 = 1; |
| 305 | break; |
| 306 | default: |
| 307 | PyErr_SetString(PyExc_TypeError, "bytes.translate requires 1 to 2 arguments"); |
| 308 | goto exit; |
| 309 | } |
| 310 | return_value = bytes_translate_impl(self, table, group_right_1, deletechars); |
| 311 | |
| 312 | exit: |
| 313 | return return_value; |
| 314 | } |
| 315 | |
| 316 | PyDoc_STRVAR(bytes_maketrans__doc__, |
| 317 | "maketrans(frm, to, /)\n" |
| 318 | "--\n" |
| 319 | "\n" |
| 320 | "Return a translation table useable for the bytes or bytearray translate method.\n" |
| 321 | "\n" |
| 322 | "The returned table will be one where each byte in frm is mapped to the byte at\n" |
| 323 | "the same position in to.\n" |
| 324 | "\n" |
| 325 | "The bytes objects frm and to must be of the same length."); |
| 326 | |
| 327 | #define BYTES_MAKETRANS_METHODDEF \ |
| 328 | {"maketrans", (PyCFunction)bytes_maketrans, METH_VARARGS|METH_STATIC, bytes_maketrans__doc__}, |
| 329 | |
| 330 | static PyObject * |
| 331 | bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to); |
| 332 | |
| 333 | static PyObject * |
| 334 | bytes_maketrans(void *null, PyObject *args) |
| 335 | { |
| 336 | PyObject *return_value = NULL; |
| 337 | Py_buffer frm = {NULL, NULL}; |
| 338 | Py_buffer to = {NULL, NULL}; |
| 339 | |
Serhiy Storchaka | 247789c | 2015-04-24 00:40:51 +0300 | [diff] [blame] | 340 | if (!PyArg_ParseTuple(args, "y*y*:maketrans", |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 341 | &frm, &to)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 342 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 343 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 344 | return_value = bytes_maketrans_impl(&frm, &to); |
| 345 | |
| 346 | exit: |
| 347 | /* Cleanup for frm */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 348 | if (frm.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 349 | PyBuffer_Release(&frm); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 350 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 351 | /* Cleanup for to */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 352 | if (to.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 353 | PyBuffer_Release(&to); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 354 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 355 | |
| 356 | return return_value; |
| 357 | } |
| 358 | |
| 359 | PyDoc_STRVAR(bytes_replace__doc__, |
| 360 | "replace($self, old, new, count=-1, /)\n" |
| 361 | "--\n" |
| 362 | "\n" |
| 363 | "Return a copy with all occurrences of substring old replaced by new.\n" |
| 364 | "\n" |
| 365 | " count\n" |
| 366 | " Maximum number of occurrences to replace.\n" |
| 367 | " -1 (the default value) means replace all occurrences.\n" |
| 368 | "\n" |
| 369 | "If the optional argument count is given, only the first count occurrences are\n" |
| 370 | "replaced."); |
| 371 | |
| 372 | #define BYTES_REPLACE_METHODDEF \ |
| 373 | {"replace", (PyCFunction)bytes_replace, METH_VARARGS, bytes_replace__doc__}, |
| 374 | |
| 375 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 376 | bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 377 | Py_ssize_t count); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 378 | |
| 379 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 380 | bytes_replace(PyBytesObject *self, PyObject *args) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 381 | { |
| 382 | PyObject *return_value = NULL; |
| 383 | Py_buffer old = {NULL, NULL}; |
| 384 | Py_buffer new = {NULL, NULL}; |
| 385 | Py_ssize_t count = -1; |
| 386 | |
Serhiy Storchaka | 247789c | 2015-04-24 00:40:51 +0300 | [diff] [blame] | 387 | if (!PyArg_ParseTuple(args, "y*y*|n:replace", |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 388 | &old, &new, &count)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 389 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 390 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 391 | return_value = bytes_replace_impl(self, &old, &new, count); |
| 392 | |
| 393 | exit: |
| 394 | /* Cleanup for old */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 395 | if (old.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 396 | PyBuffer_Release(&old); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 397 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 398 | /* Cleanup for new */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 399 | if (new.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 400 | PyBuffer_Release(&new); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 401 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 402 | |
| 403 | return return_value; |
| 404 | } |
| 405 | |
| 406 | PyDoc_STRVAR(bytes_decode__doc__, |
| 407 | "decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n" |
| 408 | "--\n" |
| 409 | "\n" |
| 410 | "Decode the bytes using the codec registered for encoding.\n" |
| 411 | "\n" |
| 412 | " encoding\n" |
| 413 | " The encoding with which to decode the bytes.\n" |
| 414 | " errors\n" |
| 415 | " The error handling scheme to use for the handling of decoding errors.\n" |
| 416 | " The default is \'strict\' meaning that decoding errors raise a\n" |
| 417 | " UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n" |
| 418 | " as well as any other name registered with codecs.register_error that\n" |
| 419 | " can handle UnicodeDecodeErrors."); |
| 420 | |
| 421 | #define BYTES_DECODE_METHODDEF \ |
| 422 | {"decode", (PyCFunction)bytes_decode, METH_VARARGS|METH_KEYWORDS, bytes_decode__doc__}, |
| 423 | |
| 424 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 425 | bytes_decode_impl(PyBytesObject *self, const char *encoding, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 426 | const char *errors); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 427 | |
| 428 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 429 | bytes_decode(PyBytesObject *self, PyObject *args, PyObject *kwargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 430 | { |
| 431 | PyObject *return_value = NULL; |
| 432 | static char *_keywords[] = {"encoding", "errors", NULL}; |
| 433 | const char *encoding = NULL; |
| 434 | const char *errors = NULL; |
| 435 | |
Serhiy Storchaka | 247789c | 2015-04-24 00:40:51 +0300 | [diff] [blame] | 436 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", _keywords, |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 437 | &encoding, &errors)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 438 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 439 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 440 | return_value = bytes_decode_impl(self, encoding, errors); |
| 441 | |
| 442 | exit: |
| 443 | return return_value; |
| 444 | } |
| 445 | |
| 446 | PyDoc_STRVAR(bytes_splitlines__doc__, |
| 447 | "splitlines($self, /, keepends=False)\n" |
| 448 | "--\n" |
| 449 | "\n" |
| 450 | "Return a list of the lines in the bytes, breaking at line boundaries.\n" |
| 451 | "\n" |
| 452 | "Line breaks are not included in the resulting list unless keepends is given and\n" |
| 453 | "true."); |
| 454 | |
| 455 | #define BYTES_SPLITLINES_METHODDEF \ |
| 456 | {"splitlines", (PyCFunction)bytes_splitlines, METH_VARARGS|METH_KEYWORDS, bytes_splitlines__doc__}, |
| 457 | |
| 458 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 459 | bytes_splitlines_impl(PyBytesObject *self, int keepends); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 460 | |
| 461 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 462 | bytes_splitlines(PyBytesObject *self, PyObject *args, PyObject *kwargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 463 | { |
| 464 | PyObject *return_value = NULL; |
| 465 | static char *_keywords[] = {"keepends", NULL}; |
| 466 | int keepends = 0; |
| 467 | |
Serhiy Storchaka | 247789c | 2015-04-24 00:40:51 +0300 | [diff] [blame] | 468 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i:splitlines", _keywords, |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 469 | &keepends)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 470 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 471 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 472 | return_value = bytes_splitlines_impl(self, keepends); |
| 473 | |
| 474 | exit: |
| 475 | return return_value; |
| 476 | } |
| 477 | |
| 478 | PyDoc_STRVAR(bytes_fromhex__doc__, |
| 479 | "fromhex($type, string, /)\n" |
| 480 | "--\n" |
| 481 | "\n" |
| 482 | "Create a bytes object from a string of hexadecimal numbers.\n" |
| 483 | "\n" |
| 484 | "Spaces between two numbers are accepted.\n" |
| 485 | "Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'."); |
| 486 | |
| 487 | #define BYTES_FROMHEX_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 488 | {"fromhex", (PyCFunction)bytes_fromhex, METH_O|METH_CLASS, bytes_fromhex__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 489 | |
| 490 | static PyObject * |
| 491 | bytes_fromhex_impl(PyTypeObject *type, PyObject *string); |
| 492 | |
| 493 | static PyObject * |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 494 | bytes_fromhex(PyTypeObject *type, PyObject *arg) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 495 | { |
| 496 | PyObject *return_value = NULL; |
| 497 | PyObject *string; |
| 498 | |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 499 | if (!PyArg_Parse(arg, "U:fromhex", &string)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 500 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 501 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 502 | return_value = bytes_fromhex_impl(type, string); |
| 503 | |
| 504 | exit: |
| 505 | return return_value; |
| 506 | } |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 507 | /*[clinic end generated code: output=6fe884a74e7d49cf input=a9049054013a1b77]*/ |