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