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