annotate Objects/bytesobject.c @ 42669:2c51be9904de

Code review of the new buffer protocol. Mostly add questions that should be answered with the comments removed. There are many places that require checks when doing arithmetic for memory sizes when allocating memory. Otherwise, overflow is possible with a subsequent crash. Fix SF #1777057 which was a result of not initializing the new BufferError properly. Had to update the test for exceptions for BufferError too.
author Neal Norwitz <nnorwitz@gmail.com>
date Sun, 19 Aug 2007 04:23:20 +0000
parents 85c22b4d4455
children 4b7036dc76d5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Bytes object implementation */
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 /* XXX TO DO: optimizations */
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 #define PY_SSIZE_T_CLEAN
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 #include "Python.h"
38115
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
7 #include "structmember.h"
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
9 /* The nullbytes are used by the stringlib during partition.
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
10 * If partition is removed from bytes, nullbytes and its helper
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
11 * Init/Fini should also be removed.
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
12 */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
13 static PyBytesObject *nullbytes = NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
14
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
15 void
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
16 PyBytes_Fini(void)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
17 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
18 Py_CLEAR(nullbytes);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
19 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
20
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
21 int
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
22 PyBytes_Init(void)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
23 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
24 nullbytes = PyObject_New(PyBytesObject, &PyBytes_Type);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
25 if (nullbytes == NULL)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
26 return 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
27 nullbytes->ob_bytes = NULL;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
28 Py_Size(nullbytes) = nullbytes->ob_alloc = 0;
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
29 nullbytes->ob_exports = 0;
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
30 return 1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
31 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
32
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
33 /* end nullbytes support */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
34
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
35 /* Helpers */
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
36
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
37 static int
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
38 _getbytevalue(PyObject* arg, int *value)
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
39 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
40 PyObject *intarg = PyNumber_Int(arg);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
41 if (! intarg)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
42 return 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
43 *value = PyInt_AsLong(intarg);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
44 Py_DECREF(intarg);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
45 if (*value < 0 || *value >= 256) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
46 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
47 return 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
48 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
49 return 1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
50 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
51
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
52 static int
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
53 bytes_getbuffer(PyBytesObject *obj, PyBuffer *view, int flags)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
54 {
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
55 int ret;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
56 void *ptr;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
57 if (view == NULL) {
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
58 obj->ob_exports++;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
59 return 0;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
60 }
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
61 if (obj->ob_bytes == NULL)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
62 ptr = "";
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
63 else
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
64 ptr = obj->ob_bytes;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
65 ret = PyBuffer_FillInfo(view, ptr, Py_Size(obj), 0, flags);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
66 if (ret >= 0) {
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
67 obj->ob_exports++;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
68 }
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
69 return ret;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
70 }
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
71
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
72 static void
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
73 bytes_releasebuffer(PyBytesObject *obj, PyBuffer *view)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
74 {
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
75 obj->ob_exports--;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
76 }
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
77
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
78 Py_ssize_t
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
79 _getbuffer(PyObject *obj, PyBuffer *view)
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
80 {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
81 PyBufferProcs *buffer = Py_Type(obj)->tp_as_buffer;
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
82
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
83 if (buffer == NULL ||
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
84 PyUnicode_Check(obj) ||
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
85 buffer->bf_getbuffer == NULL) return -1;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
86
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
87 if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
88 return -1;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
89 return view->len;
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
90 }
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
91
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 /* Direct API functions */
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
93
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 PyObject *
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
95 PyBytes_FromObject(PyObject *input)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
96 {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
97 return PyObject_CallFunctionObjArgs((PyObject *)&PyBytes_Type,
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
98 input, NULL);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
99 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
100
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
101 PyObject *
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
102 PyBytes_FromStringAndSize(const char *bytes, Py_ssize_t size)
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 {
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 PyBytesObject *new;
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41450
diff changeset
105 int alloc;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
106
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
107 assert(size >= 0);
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 new = PyObject_New(PyBytesObject, &PyBytes_Type);
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 if (new == NULL)
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
111 return NULL;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
112
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41450
diff changeset
113 if (size == 0) {
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
114 new->ob_bytes = NULL;
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41450
diff changeset
115 alloc = 0;
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41450
diff changeset
116 }
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
117 else {
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41450
diff changeset
118 alloc = size + 1;
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41450
diff changeset
119 new->ob_bytes = PyMem_Malloc(alloc);
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
120 if (new->ob_bytes == NULL) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
121 Py_DECREF(new);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
122 return NULL;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
123 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
124 if (bytes != NULL)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
125 memcpy(new->ob_bytes, bytes, size);
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41450
diff changeset
126 new->ob_bytes[size] = '\0'; /* Trailing null byte */
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 }
42335
c8b850b5a066 Fix merge breakage.
Martin v. Löwis <martin@v.loewis.de>
parents: 42334
diff changeset
128 Py_Size(new) = size;
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41450
diff changeset
129 new->ob_alloc = alloc;
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
130 new->ob_exports = 0;
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
131
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 return (PyObject *)new;
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
134
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 Py_ssize_t
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 PyBytes_Size(PyObject *self)
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 {
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 assert(self != NULL);
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 assert(PyBytes_Check(self));
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
140
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
141 return PyBytes_GET_SIZE(self);
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
143
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 char *
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 PyBytes_AsString(PyObject *self)
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 {
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 assert(self != NULL);
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 assert(PyBytes_Check(self));
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
149
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
150 return PyBytes_AS_STRING(self);
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 int
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 PyBytes_Resize(PyObject *self, Py_ssize_t size)
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 {
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 void *sval;
38115
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
157 Py_ssize_t alloc = ((PyBytesObject *)self)->ob_alloc;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
158
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 assert(self != NULL);
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 assert(PyBytes_Check(self));
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 assert(size >= 0);
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
162
38115
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
163 if (size < alloc / 2) {
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
164 /* Major downsize; resize down to exact size */
41597
cb3c92062ec4 Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents: 41595
diff changeset
165 alloc = size + 1;
38115
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
166 }
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41450
diff changeset
167 else if (size < alloc) {
38115
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
168 /* Within allocated size; quick exit */
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
169 Py_Size(self) = size;
41597
cb3c92062ec4 Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents: 41595
diff changeset
170 ((PyBytesObject *)self)->ob_bytes[size] = '\0'; /* Trailing null byte */
38115
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
171 return 0;
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
172 }
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
173 else if (size <= alloc * 1.125) {
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
174 /* Moderate upsize; overallocate similar to list_resize() */
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
175 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
176 }
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
177 else {
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
178 /* Major upsize; resize up to exact size */
41597
cb3c92062ec4 Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents: 41595
diff changeset
179 alloc = size + 1;
38115
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
180 }
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
181
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
182 if (((PyBytesObject *)self)->ob_exports > 0) {
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
183 /*
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
184 fprintf(stderr, "%d: %s", ((PyBytesObject *)self)->ob_exports, ((PyBytesObject *)self)->ob_bytes);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
185 */
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
186 PyErr_SetString(PyExc_BufferError,
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
187 "Existing exports of data: object cannot be re-sized");
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
188 return -1;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
189 }
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
190
38115
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
191 sval = PyMem_Realloc(((PyBytesObject *)self)->ob_bytes, alloc);
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 if (sval == NULL) {
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
193 PyErr_NoMemory();
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
194 return -1;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
197 ((PyBytesObject *)self)->ob_bytes = sval;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
198 Py_Size(self) = size;
38115
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
199 ((PyBytesObject *)self)->ob_alloc = alloc;
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41450
diff changeset
200 ((PyBytesObject *)self)->ob_bytes[size] = '\0'; /* Trailing null byte */
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41450
diff changeset
201
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 return 0;
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
205 PyObject *
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
206 PyBytes_Concat(PyObject *a, PyObject *b)
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
207 {
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
208 Py_ssize_t size;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
209 PyBuffer va, vb;
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
210 PyBytesObject *result;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
211
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
212 va.len = -1;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
213 vb.len = -1;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
214 if (_getbuffer(a, &va) < 0 ||
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
215 _getbuffer(b, &vb) < 0) {
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
216 if (va.len != -1)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
217 PyObject_ReleaseBuffer(a, &va);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
218 if (vb.len != -1)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
219 PyObject_ReleaseBuffer(b, &vb);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
220 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
221 Py_Type(a)->tp_name, Py_Type(b)->tp_name);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
222 return NULL;
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
223 }
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
224
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
225 size = va.len + vb.len;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
226 if (size < 0) {
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
227 PyObject_ReleaseBuffer(a, &va);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
228 PyObject_ReleaseBuffer(b, &vb);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
229 return PyErr_NoMemory();
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
230 }
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
231
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
232 result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, size);
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
233 if (result != NULL) {
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
234 memcpy(result->ob_bytes, va.buf, va.len);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
235 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
236 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
237
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
238 PyObject_ReleaseBuffer(a, &va);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
239 PyObject_ReleaseBuffer(b, &vb);
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
240 return (PyObject *)result;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
241 }
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
242
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 /* Functions stuffed into the type object */
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
244
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 static Py_ssize_t
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 bytes_length(PyBytesObject *self)
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
248 return Py_Size(self);
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
250
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 static PyObject *
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
252 bytes_concat(PyBytesObject *self, PyObject *other)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
253 {
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
254 return PyBytes_Concat((PyObject *)self, other);
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
255 }
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
256
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
257 static PyObject *
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
258 bytes_iconcat(PyBytesObject *self, PyObject *other)
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
259 {
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
260 Py_ssize_t mysize;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
261 Py_ssize_t size;
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
262 PyBuffer vo;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
263
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
264 if (_getbuffer(other, &vo) < 0) {
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
265 PyErr_Format(PyExc_TypeError,
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
266 "can't concat bytes to %.100s", Py_Type(self)->tp_name);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
267 return NULL;
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
268 }
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
269
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
270 mysize = Py_Size(self);
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
271 size = mysize + vo.len;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
272 if (size < 0) {
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
273 PyObject_ReleaseBuffer(other, &vo);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
274 return PyErr_NoMemory();
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
275 }
41597
cb3c92062ec4 Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents: 41595
diff changeset
276 if (size < self->ob_alloc) {
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
277 Py_Size(self) = size;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
278 self->ob_bytes[Py_Size(self)] = '\0'; /* Trailing null byte */
41597
cb3c92062ec4 Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents: 41595
diff changeset
279 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
280 else if (PyBytes_Resize((PyObject *)self, size) < 0) {
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
281 PyObject_ReleaseBuffer(other, &vo);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
282 return NULL;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
283 }
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
284 memcpy(self->ob_bytes + mysize, vo.buf, vo.len);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
285 PyObject_ReleaseBuffer(other, &vo);
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
286 Py_INCREF(self);
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
287 return (PyObject *)self;
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
288 }
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
289
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
290 static PyObject *
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
291 bytes_repeat(PyBytesObject *self, Py_ssize_t count)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
292 {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
293 PyBytesObject *result;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
294 Py_ssize_t mysize;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
295 Py_ssize_t size;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
296
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
297 if (count < 0)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
298 count = 0;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
299 mysize = Py_Size(self);
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
300 size = mysize * count;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
301 if (count != 0 && size / count != mysize)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
302 return PyErr_NoMemory();
41595
2c54763c4504 More coding by random modification.
Guido van Rossum <guido@python.org>
parents: 41450
diff changeset
303 result = (PyBytesObject *)PyBytes_FromStringAndSize(NULL, size);
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
304 if (result != NULL && size != 0) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
305 if (mysize == 1)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
306 memset(result->ob_bytes, self->ob_bytes[0], size);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
307 else {
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
308 Py_ssize_t i;
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
309 for (i = 0; i < count; i++)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
310 memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
311 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
312 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
313 return (PyObject *)result;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
314 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
315
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
316 static PyObject *
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
317 bytes_irepeat(PyBytesObject *self, Py_ssize_t count)
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
318 {
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
319 Py_ssize_t mysize;
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
320 Py_ssize_t size;
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
321
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
322 if (count < 0)
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
323 count = 0;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
324 mysize = Py_Size(self);
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
325 size = mysize * count;
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
326 if (count != 0 && size / count != mysize)
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
327 return PyErr_NoMemory();
41597
cb3c92062ec4 Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents: 41595
diff changeset
328 if (size < self->ob_alloc) {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
329 Py_Size(self) = size;
42335
c8b850b5a066 Fix merge breakage.
Martin v. Löwis <martin@v.loewis.de>
parents: 42334
diff changeset
330 self->ob_bytes[Py_Size(self)] = '\0'; /* Trailing null byte */
41597
cb3c92062ec4 Add trailing null bytes to a few more places.
Guido van Rossum <guido@python.org>
parents: 41595
diff changeset
331 }
38115
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
332 else if (PyBytes_Resize((PyObject *)self, size) < 0)
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
333 return NULL;
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
334
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
335 if (mysize == 1)
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
336 memset(self->ob_bytes, self->ob_bytes[0], size);
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
337 else {
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
338 Py_ssize_t i;
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
339 for (i = 1; i < count; i++)
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
340 memcpy(self->ob_bytes + i*mysize, self->ob_bytes, mysize);
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
341 }
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
342
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
343 Py_INCREF(self);
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
344 return (PyObject *)self;
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
345 }
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
346
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
347 static int
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
348 bytes_substring(PyBytesObject *self, PyBytesObject *other)
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
349 {
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
350 Py_ssize_t i;
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
351
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
352 if (Py_Size(other) == 1) {
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
353 return memchr(self->ob_bytes, other->ob_bytes[0],
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
354 Py_Size(self)) != NULL;
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
355 }
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
356 if (Py_Size(other) == 0)
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
357 return 1; /* Edge case */
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
358 for (i = 0; i + Py_Size(other) <= Py_Size(self); i++) {
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
359 /* XXX Yeah, yeah, lots of optimizations possible... */
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
360 if (memcmp(self->ob_bytes + i, other->ob_bytes, Py_Size(other)) == 0)
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
361 return 1;
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
362 }
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
363 return 0;
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
364 }
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
365
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
366 static int
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
367 bytes_contains(PyBytesObject *self, PyObject *value)
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
368 {
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
369 Py_ssize_t ival;
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
370
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
371 if (PyBytes_Check(value))
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
372 return bytes_substring(self, (PyBytesObject *)value);
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
373
39631
8d71f9a6c1c3 Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents: 39610
diff changeset
374 ival = PyNumber_AsSsize_t(value, PyExc_ValueError);
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
375 if (ival == -1 && PyErr_Occurred())
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
376 return -1;
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
377 if (ival < 0 || ival >= 256) {
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
378 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
379 return -1;
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
380 }
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
381
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
382 return memchr(self->ob_bytes, ival, Py_Size(self)) != NULL;
38017
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
383 }
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
384
0b8e782b8484 Implement bytes += bytes, bytes *= int, int in bytes, bytes in bytes.
Guido van Rossum <guido@python.org>
parents: 37960
diff changeset
385 static PyObject *
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 bytes_getitem(PyBytesObject *self, Py_ssize_t i)
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 {
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 if (i < 0)
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
389 i += Py_Size(self);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
390 if (i < 0 || i >= Py_Size(self)) {
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
391 PyErr_SetString(PyExc_IndexError, "bytes index out of range");
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
392 return NULL;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
393 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
394 return PyInt_FromLong((unsigned char)(self->ob_bytes[i]));
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
395 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
396
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
397 static PyObject *
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
398 bytes_subscript(PyBytesObject *self, PyObject *item)
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
399 {
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
400 if (PyIndex_Check(item)) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
401 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
402
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
403 if (i == -1 && PyErr_Occurred())
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
404 return NULL;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
405
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
406 if (i < 0)
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
407 i += PyBytes_GET_SIZE(self);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
408
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
409 if (i < 0 || i >= Py_Size(self)) {
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
410 PyErr_SetString(PyExc_IndexError, "bytes index out of range");
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
411 return NULL;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
412 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
413 return PyInt_FromLong((unsigned char)(self->ob_bytes[i]));
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
414 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
415 else if (PySlice_Check(item)) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
416 Py_ssize_t start, stop, step, slicelength, cur, i;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
417 if (PySlice_GetIndicesEx((PySliceObject *)item,
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
418 PyBytes_GET_SIZE(self),
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
419 &start, &stop, &step, &slicelength) < 0) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
420 return NULL;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
421 }
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
422
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
423 if (slicelength <= 0)
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
424 return PyBytes_FromStringAndSize("", 0);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
425 else if (step == 1) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
426 return PyBytes_FromStringAndSize(self->ob_bytes + start,
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
427 slicelength);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
428 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
429 else {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
430 char *source_buf = PyBytes_AS_STRING(self);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
431 char *result_buf = (char *)PyMem_Malloc(slicelength);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
432 PyObject *result;
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
433
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
434 if (result_buf == NULL)
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
435 return PyErr_NoMemory();
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
436
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
437 for (cur = start, i = 0; i < slicelength;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
438 cur += step, i++) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
439 result_buf[i] = source_buf[cur];
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
440 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
441 result = PyBytes_FromStringAndSize(result_buf, slicelength);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
442 PyMem_Free(result_buf);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
443 return result;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
444 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
445 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
446 else {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
447 PyErr_SetString(PyExc_TypeError, "bytes indices must be integers");
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
448 return NULL;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
449 }
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
450 }
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
451
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
452 static int
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
453 bytes_setslice(PyBytesObject *self, Py_ssize_t lo, Py_ssize_t hi,
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
454 PyObject *values)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
455 {
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
456 Py_ssize_t avail, needed;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
457 void *bytes;
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
458 PyBuffer vbytes;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
459 int res = 0;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
460
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
461 vbytes.len = -1;
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
462 if (values == (PyObject *)self) {
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
463 /* Make a copy and call this function recursively */
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
464 int err;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
465 values = PyBytes_FromObject(values);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
466 if (values == NULL)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
467 return -1;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
468 err = bytes_setslice(self, lo, hi, values);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
469 Py_DECREF(values);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
470 return err;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
471 }
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
472 if (values == NULL) {
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
473 /* del b[lo:hi] */
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
474 bytes = NULL;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
475 needed = 0;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
476 }
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
477 else {
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
478 if (_getbuffer(values, &vbytes) < 0) {
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
479 PyErr_Format(PyExc_TypeError,
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
480 "can't set bytes slice from %.100s",
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
481 Py_Type(values)->tp_name);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
482 return -1;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
483 }
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
484 needed = vbytes.len;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
485 bytes = vbytes.buf;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
486 }
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
487
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
488 if (lo < 0)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
489 lo = 0;
39643
3dd61ffd049e Fix buglet in slice assignment of bytesobjects: assigning to b[3:0] ('stop'
Thomas Wouters <thomas@python.org>
parents: 39631
diff changeset
490 if (hi < lo)
3dd61ffd049e Fix buglet in slice assignment of bytesobjects: assigning to b[3:0] ('stop'
Thomas Wouters <thomas@python.org>
parents: 39631
diff changeset
491 hi = lo;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
492 if (hi > Py_Size(self))
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
493 hi = Py_Size(self);
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
494
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
495 avail = hi - lo;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
496 if (avail < 0)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
497 lo = hi = avail = 0;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
498
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
499 if (avail != needed) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
500 if (avail > needed) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
501 /*
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
502 0 lo hi old_size
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
503 | |<----avail----->|<-----tomove------>|
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
504 | |<-needed->|<-----tomove------>|
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
505 0 lo new_hi new_size
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
506 */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
507 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi,
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
508 Py_Size(self) - hi);
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
509 }
42669
2c51be9904de Code review of the new buffer protocol. Mostly add questions that should
Neal Norwitz <nnorwitz@gmail.com>
parents: 42665
diff changeset
510 /* XXX(nnorwitz): need to verify this can't overflow! */
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
511 if (PyBytes_Resize((PyObject *)self,
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
512 Py_Size(self) + needed - avail) < 0) {
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
513 res = -1;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
514 goto finish;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
515 }
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
516 if (avail < needed) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
517 /*
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
518 0 lo hi old_size
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
519 | |<-avail->|<-----tomove------>|
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
520 | |<----needed---->|<-----tomove------>|
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
521 0 lo new_hi new_size
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
522 */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
523 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi,
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
524 Py_Size(self) - lo - needed);
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
525 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
526 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
527
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
528 if (needed > 0)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
529 memcpy(self->ob_bytes + lo, bytes, needed);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
530
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
531
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
532 finish:
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
533 if (vbytes.len != -1)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
534 PyObject_ReleaseBuffer(values, &vbytes);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
535 return res;
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
536 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
537
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
538 static int
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
539 bytes_setitem(PyBytesObject *self, Py_ssize_t i, PyObject *value)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
540 {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
541 Py_ssize_t ival;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
542
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
543 if (i < 0)
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
544 i += Py_Size(self);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
545
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
546 if (i < 0 || i >= Py_Size(self)) {
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
547 PyErr_SetString(PyExc_IndexError, "bytes index out of range");
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
548 return -1;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
549 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
550
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
551 if (value == NULL)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
552 return bytes_setslice(self, i, i+1, NULL);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
553
39631
8d71f9a6c1c3 Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents: 39610
diff changeset
554 ival = PyNumber_AsSsize_t(value, PyExc_ValueError);
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
555 if (ival == -1 && PyErr_Occurred())
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
556 return -1;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
557
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
558 if (ival < 0 || ival >= 256) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
559 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
560 return -1;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
561 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
562
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
563 self->ob_bytes[i] = ival;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
564 return 0;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
566
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 static int
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
568 bytes_ass_subscript(PyBytesObject *self, PyObject *item, PyObject *values)
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
569 {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
570 Py_ssize_t start, stop, step, slicelen, needed;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
571 char *bytes;
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
572
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
573 if (PyIndex_Check(item)) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
574 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
575
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
576 if (i == -1 && PyErr_Occurred())
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
577 return -1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
578
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
579 if (i < 0)
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
580 i += PyBytes_GET_SIZE(self);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
581
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
582 if (i < 0 || i >= Py_Size(self)) {
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
583 PyErr_SetString(PyExc_IndexError, "bytes index out of range");
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
584 return -1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
585 }
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
586
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
587 if (values == NULL) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
588 /* Fall through to slice assignment */
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
589 start = i;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
590 stop = i + 1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
591 step = 1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
592 slicelen = 1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
593 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
594 else {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
595 Py_ssize_t ival = PyNumber_AsSsize_t(values, PyExc_ValueError);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
596 if (ival == -1 && PyErr_Occurred())
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
597 return -1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
598 if (ival < 0 || ival >= 256) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
599 PyErr_SetString(PyExc_ValueError,
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
600 "byte must be in range(0, 256)");
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
601 return -1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
602 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
603 self->ob_bytes[i] = (char)ival;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
604 return 0;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
605 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
606 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
607 else if (PySlice_Check(item)) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
608 if (PySlice_GetIndicesEx((PySliceObject *)item,
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
609 PyBytes_GET_SIZE(self),
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
610 &start, &stop, &step, &slicelen) < 0) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
611 return -1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
612 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
613 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
614 else {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
615 PyErr_SetString(PyExc_TypeError, "bytes indices must be integer");
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
616 return -1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
617 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
618
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
619 if (values == NULL) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
620 bytes = NULL;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
621 needed = 0;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
622 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
623 else if (values == (PyObject *)self || !PyBytes_Check(values)) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
624 /* Make a copy an call this function recursively */
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
625 int err;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
626 values = PyBytes_FromObject(values);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
627 if (values == NULL)
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
628 return -1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
629 err = bytes_ass_subscript(self, item, values);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
630 Py_DECREF(values);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
631 return err;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
632 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
633 else {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
634 assert(PyBytes_Check(values));
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
635 bytes = ((PyBytesObject *)values)->ob_bytes;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
636 needed = Py_Size(values);
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
637 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
638 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
639 if ((step < 0 && start < stop) ||
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
640 (step > 0 && start > stop))
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
641 stop = start;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
642 if (step == 1) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
643 if (slicelen != needed) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
644 if (slicelen > needed) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
645 /*
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
646 0 start stop old_size
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
647 | |<---slicelen--->|<-----tomove------>|
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
648 | |<-needed->|<-----tomove------>|
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
649 0 lo new_hi new_size
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
650 */
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
651 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop,
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
652 Py_Size(self) - stop);
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
653 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
654 if (PyBytes_Resize((PyObject *)self,
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
655 Py_Size(self) + needed - slicelen) < 0)
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
656 return -1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
657 if (slicelen < needed) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
658 /*
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
659 0 lo hi old_size
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
660 | |<-avail->|<-----tomove------>|
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
661 | |<----needed---->|<-----tomove------>|
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
662 0 lo new_hi new_size
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
663 */
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
664 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop,
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
665 Py_Size(self) - start - needed);
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
666 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
667 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
668
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
669 if (needed > 0)
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
670 memcpy(self->ob_bytes + start, bytes, needed);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
671
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
672 return 0;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
673 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
674 else {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
675 if (needed == 0) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
676 /* Delete slice */
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
677 Py_ssize_t cur, i;
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
678
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
679 if (step < 0) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
680 stop = start + 1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
681 start = stop + step * (slicelen - 1) - 1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
682 step = -step;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
683 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
684 for (cur = start, i = 0;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
685 i < slicelen; cur += step, i++) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
686 Py_ssize_t lim = step - 1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
687
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
688 if (cur + step >= PyBytes_GET_SIZE(self))
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
689 lim = PyBytes_GET_SIZE(self) - cur - 1;
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
690
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
691 memmove(self->ob_bytes + cur - i,
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
692 self->ob_bytes + cur + 1, lim);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
693 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
694 /* Move the tail of the bytes, in one chunk */
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
695 cur = start + slicelen*step;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
696 if (cur < PyBytes_GET_SIZE(self)) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
697 memmove(self->ob_bytes + cur - slicelen,
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
698 self->ob_bytes + cur,
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
699 PyBytes_GET_SIZE(self) - cur);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
700 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
701 if (PyBytes_Resize((PyObject *)self,
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
702 PyBytes_GET_SIZE(self) - slicelen) < 0)
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
703 return -1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
704
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
705 return 0;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
706 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
707 else {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
708 /* Assign slice */
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
709 Py_ssize_t cur, i;
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
710
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
711 if (needed != slicelen) {
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
712 PyErr_Format(PyExc_ValueError,
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
713 "attempt to assign bytes of size %zd "
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
714 "to extended slice of size %zd",
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
715 needed, slicelen);
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
716 return -1;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
717 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
718 for (cur = start, i = 0; i < slicelen; cur += step, i++)
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
719 self->ob_bytes[cur] = bytes[i];
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
720 return 0;
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
721 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
722 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
723 }
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
724
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
725 static int
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 bytes_init(PyBytesObject *self, PyObject *args, PyObject *kwds)
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 {
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
728 static char *kwlist[] = {"source", "encoding", "errors", 0};
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
729 PyObject *arg = NULL;
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
730 const char *encoding = NULL;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
731 const char *errors = NULL;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
732 Py_ssize_t count;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
733 PyObject *it;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
734 PyObject *(*iternext)(PyObject *);
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
735
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
736 if (Py_Size(self) != 0) {
38115
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
737 /* Empty previous contents (yes, do this first of all!) */
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
738 if (PyBytes_Resize((PyObject *)self, 0) < 0)
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
739 return -1;
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
740 }
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
741
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
742 /* Parse arguments */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
743 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytes", kwlist,
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
744 &arg, &encoding, &errors))
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
745 return -1;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
746
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
747 /* Make a quick exit if no first argument */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
748 if (arg == NULL) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
749 if (encoding != NULL || errors != NULL) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
750 PyErr_SetString(PyExc_TypeError,
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
751 "encoding or errors without sequence argument");
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
752 return -1;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
753 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
754 return 0;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
755 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
756
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
757 if (PyUnicode_Check(arg)) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
758 /* Encode via the codec registry */
41598
ca448c607b14 Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents: 41597
diff changeset
759 PyObject *encoded, *new;
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
760 if (encoding == NULL)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
761 encoding = PyUnicode_GetDefaultEncoding();
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
762 encoded = PyCodec_Encode(arg, encoding, errors);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
763 if (encoded == NULL)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
764 return -1;
41598
ca448c607b14 Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents: 41597
diff changeset
765 if (!PyBytes_Check(encoded) && !PyString_Check(encoded)) {
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
766 PyErr_Format(PyExc_TypeError,
41598
ca448c607b14 Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents: 41597
diff changeset
767 "encoder did not return a str8 or bytes object (type=%.400s)",
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
768 Py_Type(encoded)->tp_name);
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
769 Py_DECREF(encoded);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
770 return -1;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
771 }
41598
ca448c607b14 Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents: 41597
diff changeset
772 new = bytes_iconcat(self, encoded);
ca448c607b14 Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents: 41597
diff changeset
773 Py_DECREF(encoded);
ca448c607b14 Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents: 41597
diff changeset
774 if (new == NULL)
ca448c607b14 Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents: 41597
diff changeset
775 return -1;
ca448c607b14 Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents: 41597
diff changeset
776 Py_DECREF(new);
ca448c607b14 Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents: 41597
diff changeset
777 return 0;
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
778 }
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
779
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
780 /* If it's not unicode, there can't be encoding or errors */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
781 if (encoding != NULL || errors != NULL) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
782 PyErr_SetString(PyExc_TypeError,
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
783 "encoding or errors without a string argument");
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
784 return -1;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
785 }
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
786
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
787 /* Is it an int? */
39631
8d71f9a6c1c3 Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents: 39610
diff changeset
788 count = PyNumber_AsSsize_t(arg, PyExc_ValueError);
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
789 if (count == -1 && PyErr_Occurred())
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
790 PyErr_Clear();
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
791 else {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
792 if (count < 0) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
793 PyErr_SetString(PyExc_ValueError, "negative count");
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
794 return -1;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
795 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
796 if (count > 0) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
797 if (PyBytes_Resize((PyObject *)self, count))
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
798 return -1;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
799 memset(self->ob_bytes, 0, count);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
800 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
801 return 0;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
802 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
803
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
804 /* Use the modern buffer interface */
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
805 if (PyObject_CheckBuffer(arg)) {
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
806 Py_ssize_t size;
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
807 PyBuffer view;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
808 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
809 return -1;
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
810 size = view.len;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
811 if (PyBytes_Resize((PyObject *)self, size) < 0) goto fail;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
812 if (PyBuffer_ToContiguous(self->ob_bytes, &view, size, 'C') < 0)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
813 goto fail;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
814 PyObject_ReleaseBuffer(arg, &view);
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
815 return 0;
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
816 fail:
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
817 PyObject_ReleaseBuffer(arg, &view);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
818 return -1;
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
819 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
820
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
821 /* XXX Optimize this if the arguments is a list, tuple */
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
822
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
823 /* Get the iterator */
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
824 it = PyObject_GetIter(arg);
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
825 if (it == NULL)
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
826 return -1;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
827 iternext = *Py_Type(it)->tp_iternext;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
828
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
829 /* Run the iterator to exhaustion */
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
830 for (;;) {
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
831 PyObject *item;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
832 Py_ssize_t value;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
833
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
834 /* Get the next item */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
835 item = iternext(it);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
836 if (item == NULL) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
837 if (PyErr_Occurred()) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
838 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
839 goto error;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
840 PyErr_Clear();
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
841 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
842 break;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
843 }
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
844
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
845 /* Interpret it as an int (__index__) */
39631
8d71f9a6c1c3 Make bytesobject raise ValueError instead of TypeError again (thanks, Nick)
Thomas Wouters <thomas@python.org>
parents: 39610
diff changeset
846 value = PyNumber_AsSsize_t(item, PyExc_ValueError);
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
847 Py_DECREF(item);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
848 if (value == -1 && PyErr_Occurred())
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
849 goto error;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
850
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
851 /* Range check */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
852 if (value < 0 || value >= 256) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
853 PyErr_SetString(PyExc_ValueError,
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
854 "bytes must be in range(0, 256)");
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
855 goto error;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
856 }
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
857
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
858 /* Append the byte */
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
859 if (Py_Size(self) < self->ob_alloc)
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
860 Py_Size(self)++;
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
861 else if (PyBytes_Resize((PyObject *)self, Py_Size(self)+1) < 0)
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
862 goto error;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
863 self->ob_bytes[Py_Size(self)-1] = value;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
864 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
865
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
866 /* Clean up and return success */
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
867 Py_DECREF(it);
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
868 return 0;
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
869
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
870 error:
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
871 /* Error handling when it != NULL */
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
872 Py_DECREF(it);
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
873 return -1;
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
874 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
875
40898
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
876 /* Mostly copied from string_repr, but without the
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
877 "smart quote" functionality. */
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
878 static PyObject *
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
879 bytes_repr(PyBytesObject *self)
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
880 {
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41668
diff changeset
881 static const char *hexdigits = "0123456789abcdef";
42335
c8b850b5a066 Fix merge breakage.
Martin v. Löwis <martin@v.loewis.de>
parents: 42334
diff changeset
882 size_t newsize = 3 + 4 * Py_Size(self);
40898
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
883 PyObject *v;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
884 if (newsize > PY_SSIZE_T_MAX || newsize / 4 != Py_Size(self)) {
40898
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
885 PyErr_SetString(PyExc_OverflowError,
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
886 "bytes object is too large to make repr");
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
887 return NULL;
40898
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
888 }
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41668
diff changeset
889 v = PyUnicode_FromUnicode(NULL, newsize);
40898
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
890 if (v == NULL) {
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
891 return NULL;
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
892 }
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
893 else {
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
894 register Py_ssize_t i;
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41668
diff changeset
895 register Py_UNICODE c;
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41668
diff changeset
896 register Py_UNICODE *p;
40898
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
897 int quote = '\'';
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
898
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41668
diff changeset
899 p = PyUnicode_AS_UNICODE(v);
40898
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
900 *p++ = 'b';
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
901 *p++ = quote;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
902 for (i = 0; i < Py_Size(self); i++) {
40898
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
903 /* There's at least enough room for a hex escape
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
904 and a closing quote. */
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41668
diff changeset
905 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5);
40898
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
906 c = self->ob_bytes[i];
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
907 if (c == quote || c == '\\')
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
908 *p++ = '\\', *p++ = c;
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
909 else if (c == '\t')
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
910 *p++ = '\\', *p++ = 't';
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
911 else if (c == '\n')
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
912 *p++ = '\\', *p++ = 'n';
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
913 else if (c == '\r')
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
914 *p++ = '\\', *p++ = 'r';
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
915 else if (c == 0)
41648
bc6ad63cd5f1 repr(b"\0") should return b"\x00", not the (unusual) b"\0".
Guido van Rossum <guido@python.org>
parents: 41598
diff changeset
916 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
40898
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
917 else if (c < ' ' || c >= 0x7f) {
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41668
diff changeset
918 *p++ = '\\';
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41668
diff changeset
919 *p++ = 'x';
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41668
diff changeset
920 *p++ = hexdigits[(c & 0xf0) >> 4];
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41668
diff changeset
921 *p++ = hexdigits[c & 0xf];
40898
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
922 }
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
923 else
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
924 *p++ = c;
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
925 }
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41668
diff changeset
926 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1);
40898
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
927 *p++ = quote;
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
928 *p = '\0';
41769
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41668
diff changeset
929 if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) {
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41668
diff changeset
930 Py_DECREF(v);
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41668
diff changeset
931 return NULL;
b29d71d43c80 Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents: 41668
diff changeset
932 }
40898
b58a4374ac21 Make bytes_repr return a string containing a b"" literal.
Georg Brandl <georg@python.org>
parents: 40472
diff changeset
933 return v;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
934 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
935 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
936
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
937 static PyObject *
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
938 bytes_str(PyBytesObject *self)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
939 {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
940 return PyString_FromStringAndSize(self->ob_bytes, Py_Size(self));
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
941 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
942
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
943 static PyObject *
41411
339015d98b78 For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents: 40982
diff changeset
944 bytes_richcompare(PyObject *self, PyObject *other, int op)
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
945 {
41411
339015d98b78 For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents: 40982
diff changeset
946 Py_ssize_t self_size, other_size;
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
947 PyBuffer self_bytes, other_bytes;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
948 PyObject *res;
41411
339015d98b78 For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents: 40982
diff changeset
949 Py_ssize_t minsize;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
950 int cmp;
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
951
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
952 /* Bytes can be compared to anything that supports the (binary) buffer
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
953 API. Except Unicode. */
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
954
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
955 self_size = _getbuffer(self, &self_bytes);
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
956 if (self_size < 0) {
41412
522b3f35d0eb Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents: 41411
diff changeset
957 Py_INCREF(Py_NotImplemented);
522b3f35d0eb Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents: 41411
diff changeset
958 return Py_NotImplemented;
522b3f35d0eb Bytes should never equal unicode.
Guido van Rossum <guido@python.org>
parents: 41411
diff changeset
959 }
41411
339015d98b78 For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents: 40982
diff changeset
960
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
961 other_size = _getbuffer(other, &other_bytes);
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
962 if (other_size < 0) {
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
963 PyObject_ReleaseBuffer(self, &self_bytes);
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
964 Py_INCREF(Py_NotImplemented);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
965 return Py_NotImplemented;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
966 }
41411
339015d98b78 For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents: 40982
diff changeset
967
339015d98b78 For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents: 40982
diff changeset
968 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
969 /* Shortcut: if the lengths differ, the objects differ */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
970 cmp = (op == Py_NE);
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
971 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
972 else {
41411
339015d98b78 For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents: 40982
diff changeset
973 minsize = self_size;
339015d98b78 For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents: 40982
diff changeset
974 if (other_size < minsize)
339015d98b78 For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents: 40982
diff changeset
975 minsize = other_size;
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
976
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
977 cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize);
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
978 /* In ISO C, memcmp() guarantees to use unsigned bytes! */
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
979
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
980 if (cmp == 0) {
41411
339015d98b78 For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents: 40982
diff changeset
981 if (self_size < other_size)
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
982 cmp = -1;
41411
339015d98b78 For Make Benefit Glorious Nation of Backwards Compatibility,
Guido van Rossum <guido@python.org>
parents: 40982
diff changeset
983 else if (self_size > other_size)
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
984 cmp = 1;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
985 }
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
986
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
987 switch (op) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
988 case Py_LT: cmp = cmp < 0; break;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
989 case Py_LE: cmp = cmp <= 0; break;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
990 case Py_EQ: cmp = cmp == 0; break;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
991 case Py_NE: cmp = cmp != 0; break;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
992 case Py_GT: cmp = cmp > 0; break;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
993 case Py_GE: cmp = cmp >= 0; break;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
994 }
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
995 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
996
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
997 res = cmp ? Py_True : Py_False;
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
998 PyObject_ReleaseBuffer(self, &self_bytes);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
999 PyObject_ReleaseBuffer(other, &other_bytes);
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1000 Py_INCREF(res);
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1001 return res;
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1002 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1003
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1004 static void
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1005 bytes_dealloc(PyBytesObject *self)
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1006 {
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
1007 if (self->ob_bytes != 0) {
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
1008 PyMem_Free(self->ob_bytes);
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1009 }
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
1010 Py_Type(self)->tp_free((PyObject *)self);
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1011 }
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1012
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1013
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1014 /* -------------------------------------------------------------------- */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1015 /* Methods */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1016
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1017 #define STRINGLIB_CHAR char
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1018 #define STRINGLIB_CMP memcmp
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1019 #define STRINGLIB_LEN PyBytes_GET_SIZE
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1020 #define STRINGLIB_NEW PyBytes_FromStringAndSize
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1021 #define STRINGLIB_EMPTY nullbytes
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1022
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1023 #include "stringlib/fastsearch.h"
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1024 #include "stringlib/count.h"
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1025 #include "stringlib/find.h"
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1026 #include "stringlib/partition.h"
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1027
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1028
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1029 /* The following Py_LOCAL_INLINE and Py_LOCAL functions
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1030 were copied from the old char* style string object. */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1031
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1032 Py_LOCAL_INLINE(void)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1033 _adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1034 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1035 if (*end > len)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1036 *end = len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1037 else if (*end < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1038 *end += len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1039 if (*end < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1040 *end = 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1041 if (*start < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1042 *start += len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1043 if (*start < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1044 *start = 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1045 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1046
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1047
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1048 Py_LOCAL_INLINE(Py_ssize_t)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1049 bytes_find_internal(PyBytesObject *self, PyObject *args, int dir)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1050 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1051 PyObject *subobj;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1052 const char *sub;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1053 Py_ssize_t sub_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1054 Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1055
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1056 if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", &subobj,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1057 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1058 return -2;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1059 if (PyBytes_Check(subobj)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1060 sub = PyBytes_AS_STRING(subobj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1061 sub_len = PyBytes_GET_SIZE(subobj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1062 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
1063 /* XXX --> use the modern buffer interface */
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1064 else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1065 /* XXX - the "expected a character buffer object" is pretty
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1066 confusing for a non-expert. remap to something else ? */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1067 return -2;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1068
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1069 if (dir > 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1070 return stringlib_find_slice(
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1071 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1072 sub, sub_len, start, end);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1073 else
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1074 return stringlib_rfind_slice(
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1075 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1076 sub, sub_len, start, end);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1077 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1078
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1079
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1080 PyDoc_STRVAR(find__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1081 "B.find(sub [,start [,end]]) -> int\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1082 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1083 Return the lowest index in B where subsection sub is found,\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1084 such that sub is contained within s[start,end]. Optional\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1085 arguments start and end are interpreted as in slice notation.\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1086 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1087 Return -1 on failure.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1088
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1089 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1090 bytes_find(PyBytesObject *self, PyObject *args)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1091 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1092 Py_ssize_t result = bytes_find_internal(self, args, +1);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1093 if (result == -2)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1094 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1095 return PyInt_FromSsize_t(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1096 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1097
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1098 PyDoc_STRVAR(count__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1099 "B.count(sub[, start[, end]]) -> int\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1100 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1101 Return the number of non-overlapping occurrences of subsection sub in\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1102 bytes B[start:end]. Optional arguments start and end are interpreted\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1103 as in slice notation.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1104
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1105 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1106 bytes_count(PyBytesObject *self, PyObject *args)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1107 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1108 PyObject *sub_obj;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1109 const char *str = PyBytes_AS_STRING(self), *sub;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1110 Py_ssize_t sub_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1111 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1112
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1113 if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1114 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1115 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1116
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1117 if (PyBytes_Check(sub_obj)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1118 sub = PyBytes_AS_STRING(sub_obj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1119 sub_len = PyBytes_GET_SIZE(sub_obj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1120 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
1121 /* XXX --> use the modern buffer interface */
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1122 else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1123 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1124
42013
cd125fe83051 Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents: 41769
diff changeset
1125 _adjust_indices(&start, &end, PyBytes_GET_SIZE(self));
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1126
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1127 return PyInt_FromSsize_t(
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1128 stringlib_count(str + start, end - start, sub, sub_len)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1129 );
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1130 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1131
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1132
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1133 PyDoc_STRVAR(index__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1134 "B.index(sub [,start [,end]]) -> int\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1135 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1136 Like B.find() but raise ValueError when the subsection is not found.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1137
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1138 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1139 bytes_index(PyBytesObject *self, PyObject *args)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1140 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1141 Py_ssize_t result = bytes_find_internal(self, args, +1);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1142 if (result == -2)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1143 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1144 if (result == -1) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1145 PyErr_SetString(PyExc_ValueError,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1146 "subsection not found");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1147 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1148 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1149 return PyInt_FromSsize_t(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1150 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1151
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1152
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1153 PyDoc_STRVAR(rfind__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1154 "B.rfind(sub [,start [,end]]) -> int\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1155 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1156 Return the highest index in B where subsection sub is found,\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1157 such that sub is contained within s[start,end]. Optional\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1158 arguments start and end are interpreted as in slice notation.\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1159 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1160 Return -1 on failure.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1161
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1162 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1163 bytes_rfind(PyBytesObject *self, PyObject *args)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1164 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1165 Py_ssize_t result = bytes_find_internal(self, args, -1);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1166 if (result == -2)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1167 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1168 return PyInt_FromSsize_t(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1169 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1170
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1171
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1172 PyDoc_STRVAR(rindex__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1173 "B.rindex(sub [,start [,end]]) -> int\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1174 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1175 Like B.rfind() but raise ValueError when the subsection is not found.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1176
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1177 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1178 bytes_rindex(PyBytesObject *self, PyObject *args)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1179 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1180 Py_ssize_t result = bytes_find_internal(self, args, -1);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1181 if (result == -2)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1182 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1183 if (result == -1) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1184 PyErr_SetString(PyExc_ValueError,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1185 "subsection not found");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1186 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1187 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1188 return PyInt_FromSsize_t(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1189 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1190
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1191
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1192 /* Matches the end (direction >= 0) or start (direction < 0) of self
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1193 * against substr, using the start and end arguments. Returns
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1194 * -1 on error, 0 if not found and 1 if found.
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1195 */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1196 Py_LOCAL(int)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1197 _bytes_tailmatch(PyBytesObject *self, PyObject *substr, Py_ssize_t start,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1198 Py_ssize_t end, int direction)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1199 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1200 Py_ssize_t len = PyBytes_GET_SIZE(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1201 Py_ssize_t slen;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1202 const char* sub;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1203 const char* str;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1204
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1205 if (PyBytes_Check(substr)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1206 sub = PyBytes_AS_STRING(substr);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1207 slen = PyBytes_GET_SIZE(substr);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1208 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
1209 /* XXX --> Use the modern buffer interface */
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1210 else if (PyObject_AsCharBuffer(substr, &sub, &slen))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1211 return -1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1212 str = PyBytes_AS_STRING(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1213
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1214 _adjust_indices(&start, &end, len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1215
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1216 if (direction < 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1217 /* startswith */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1218 if (start+slen > len)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1219 return 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1220 } else {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1221 /* endswith */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1222 if (end-start < slen || start > len)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1223 return 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1224
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1225 if (end-slen > start)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1226 start = end - slen;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1227 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1228 if (end-start >= slen)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1229 return ! memcmp(str+start, sub, slen);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1230 return 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1231 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1232
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1233
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1234 PyDoc_STRVAR(startswith__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1235 "B.startswith(prefix[, start[, end]]) -> bool\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1236 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1237 Return True if B starts with the specified prefix, False otherwise.\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1238 With optional start, test B beginning at that position.\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1239 With optional end, stop comparing B at that position.\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1240 prefix can also be a tuple of strings to try.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1241
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1242 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1243 bytes_startswith(PyBytesObject *self, PyObject *args)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1244 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1245 Py_ssize_t start = 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1246 Py_ssize_t end = PY_SSIZE_T_MAX;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1247 PyObject *subobj;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1248 int result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1249
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1250 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1251 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1252 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1253 if (PyTuple_Check(subobj)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1254 Py_ssize_t i;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1255 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1256 result = _bytes_tailmatch(self,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1257 PyTuple_GET_ITEM(subobj, i),
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1258 start, end, -1);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1259 if (result == -1)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1260 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1261 else if (result) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1262 Py_RETURN_TRUE;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1263 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1264 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1265 Py_RETURN_FALSE;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1266 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1267 result = _bytes_tailmatch(self, subobj, start, end, -1);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1268 if (result == -1)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1269 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1270 else
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1271 return PyBool_FromLong(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1272 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1273
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1274 PyDoc_STRVAR(endswith__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1275 "B.endswith(suffix[, start[, end]]) -> bool\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1276 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1277 Return True if B ends with the specified suffix, False otherwise.\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1278 With optional start, test B beginning at that position.\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1279 With optional end, stop comparing B at that position.\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1280 suffix can also be a tuple of strings to try.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1281
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1282 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1283 bytes_endswith(PyBytesObject *self, PyObject *args)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1284 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1285 Py_ssize_t start = 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1286 Py_ssize_t end = PY_SSIZE_T_MAX;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1287 PyObject *subobj;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1288 int result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1289
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1290 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1291 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1292 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1293 if (PyTuple_Check(subobj)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1294 Py_ssize_t i;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1295 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1296 result = _bytes_tailmatch(self,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1297 PyTuple_GET_ITEM(subobj, i),
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1298 start, end, +1);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1299 if (result == -1)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1300 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1301 else if (result) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1302 Py_RETURN_TRUE;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1303 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1304 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1305 Py_RETURN_FALSE;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1306 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1307 result = _bytes_tailmatch(self, subobj, start, end, +1);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1308 if (result == -1)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1309 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1310 else
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1311 return PyBool_FromLong(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1312 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1313
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1314
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1315
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1316 PyDoc_STRVAR(translate__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1317 "B.translate(table [,deletechars]) -> bytes\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1318 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1319 Return a copy of the bytes B, where all characters occurring\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1320 in the optional argument deletechars are removed, and the\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1321 remaining characters have been mapped through the given\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1322 translation table, which must be a bytes of length 256.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1323
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1324 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1325 bytes_translate(PyBytesObject *self, PyObject *args)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1326 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1327 register char *input, *output;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1328 register const char *table;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1329 register Py_ssize_t i, c, changed = 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1330 PyObject *input_obj = (PyObject*)self;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1331 const char *table1, *output_start, *del_table=NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1332 Py_ssize_t inlen, tablen, dellen = 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1333 PyObject *result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1334 int trans_table[256];
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1335 PyObject *tableobj, *delobj = NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1336
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1337 if (!PyArg_UnpackTuple(args, "translate", 1, 2,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1338 &tableobj, &delobj))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1339 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1340
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1341 if (PyBytes_Check(tableobj)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1342 table1 = PyBytes_AS_STRING(tableobj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1343 tablen = PyBytes_GET_SIZE(tableobj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1344 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
1345 /* XXX -> Use the modern buffer interface */
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1346 else if (PyObject_AsCharBuffer(tableobj, &table1, &tablen))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1347 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1348
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1349 if (tablen != 256) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1350 PyErr_SetString(PyExc_ValueError,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1351 "translation table must be 256 characters long");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1352 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1353 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1354
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1355 if (delobj != NULL) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1356 if (PyBytes_Check(delobj)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1357 del_table = PyBytes_AS_STRING(delobj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1358 dellen = PyBytes_GET_SIZE(delobj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1359 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
1360 /* XXX -> use the modern buffer interface */
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1361 else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1362 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1363 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1364 else {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1365 del_table = NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1366 dellen = 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1367 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1368
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1369 table = table1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1370 inlen = PyBytes_GET_SIZE(input_obj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1371 result = PyBytes_FromStringAndSize((char *)NULL, inlen);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1372 if (result == NULL)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1373 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1374 output_start = output = PyBytes_AsString(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1375 input = PyBytes_AS_STRING(input_obj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1376
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1377 if (dellen == 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1378 /* If no deletions are required, use faster code */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1379 for (i = inlen; --i >= 0; ) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1380 c = Py_CHARMASK(*input++);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1381 if (Py_CHARMASK((*output++ = table[c])) != c)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1382 changed = 1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1383 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1384 if (changed || !PyBytes_CheckExact(input_obj))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1385 return result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1386 Py_DECREF(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1387 Py_INCREF(input_obj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1388 return input_obj;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1389 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1390
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1391 for (i = 0; i < 256; i++)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1392 trans_table[i] = Py_CHARMASK(table[i]);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1393
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1394 for (i = 0; i < dellen; i++)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1395 trans_table[(int) Py_CHARMASK(del_table[i])] = -1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1396
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1397 for (i = inlen; --i >= 0; ) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1398 c = Py_CHARMASK(*input++);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1399 if (trans_table[c] != -1)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1400 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1401 continue;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1402 changed = 1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1403 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1404 if (!changed && PyBytes_CheckExact(input_obj)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1405 Py_DECREF(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1406 Py_INCREF(input_obj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1407 return input_obj;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1408 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1409 /* Fix the size of the resulting string */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1410 if (inlen > 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1411 PyBytes_Resize(result, output - output_start);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1412 return result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1413 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1414
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1415
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1416 #define FORWARD 1
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1417 #define REVERSE -1
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1418
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1419 /* find and count characters and substrings */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1420
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1421 #define findchar(target, target_len, c) \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1422 ((char *)memchr((const void *)(target), c, target_len))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1423
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1424 /* Don't call if length < 2 */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1425 #define Py_STRING_MATCH(target, offset, pattern, length) \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1426 (target[offset] == pattern[0] && \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1427 target[offset+length-1] == pattern[length-1] && \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1428 !memcmp(target+offset+1, pattern+1, length-2) )
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1429
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1430
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1431 /* Bytes ops must return a string. */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1432 /* If the object is subclass of bytes, create a copy */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1433 Py_LOCAL(PyBytesObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1434 return_self(PyBytesObject *self)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1435 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1436 if (PyBytes_CheckExact(self)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1437 Py_INCREF(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1438 return (PyBytesObject *)self;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1439 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1440 return (PyBytesObject *)PyBytes_FromStringAndSize(
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1441 PyBytes_AS_STRING(self),
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1442 PyBytes_GET_SIZE(self));
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1443 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1444
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1445 Py_LOCAL_INLINE(Py_ssize_t)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1446 countchar(const char *target, int target_len, char c, Py_ssize_t maxcount)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1447 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1448 Py_ssize_t count=0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1449 const char *start=target;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1450 const char *end=target+target_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1451
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1452 while ( (start=findchar(start, end-start, c)) != NULL ) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1453 count++;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1454 if (count >= maxcount)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1455 break;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1456 start += 1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1457 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1458 return count;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1459 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1460
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1461 Py_LOCAL(Py_ssize_t)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1462 findstring(const char *target, Py_ssize_t target_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1463 const char *pattern, Py_ssize_t pattern_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1464 Py_ssize_t start,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1465 Py_ssize_t end,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1466 int direction)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1467 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1468 if (start < 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1469 start += target_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1470 if (start < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1471 start = 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1472 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1473 if (end > target_len) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1474 end = target_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1475 } else if (end < 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1476 end += target_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1477 if (end < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1478 end = 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1479 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1480
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1481 /* zero-length substrings always match at the first attempt */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1482 if (pattern_len == 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1483 return (direction > 0) ? start : end;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1484
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1485 end -= pattern_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1486
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1487 if (direction < 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1488 for (; end >= start; end--)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1489 if (Py_STRING_MATCH(target, end, pattern, pattern_len))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1490 return end;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1491 } else {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1492 for (; start <= end; start++)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1493 if (Py_STRING_MATCH(target, start, pattern, pattern_len))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1494 return start;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1495 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1496 return -1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1497 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1498
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1499 Py_LOCAL_INLINE(Py_ssize_t)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1500 countstring(const char *target, Py_ssize_t target_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1501 const char *pattern, Py_ssize_t pattern_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1502 Py_ssize_t start,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1503 Py_ssize_t end,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1504 int direction, Py_ssize_t maxcount)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1505 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1506 Py_ssize_t count=0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1507
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1508 if (start < 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1509 start += target_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1510 if (start < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1511 start = 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1512 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1513 if (end > target_len) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1514 end = target_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1515 } else if (end < 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1516 end += target_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1517 if (end < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1518 end = 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1519 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1520
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1521 /* zero-length substrings match everywhere */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1522 if (pattern_len == 0 || maxcount == 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1523 if (target_len+1 < maxcount)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1524 return target_len+1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1525 return maxcount;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1526 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1527
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1528 end -= pattern_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1529 if (direction < 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1530 for (; (end >= start); end--)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1531 if (Py_STRING_MATCH(target, end, pattern, pattern_len)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1532 count++;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1533 if (--maxcount <= 0) break;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1534 end -= pattern_len-1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1535 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1536 } else {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1537 for (; (start <= end); start++)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1538 if (Py_STRING_MATCH(target, start, pattern, pattern_len)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1539 count++;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1540 if (--maxcount <= 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1541 break;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1542 start += pattern_len-1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1543 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1544 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1545 return count;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1546 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1547
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1548
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1549 /* Algorithms for different cases of string replacement */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1550
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1551 /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1552 Py_LOCAL(PyBytesObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1553 replace_interleave(PyBytesObject *self,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1554 const char *to_s, Py_ssize_t to_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1555 Py_ssize_t maxcount)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1556 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1557 char *self_s, *result_s;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1558 Py_ssize_t self_len, result_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1559 Py_ssize_t count, i, product;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1560 PyBytesObject *result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1561
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1562 self_len = PyBytes_GET_SIZE(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1563
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1564 /* 1 at the end plus 1 after every character */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1565 count = self_len+1;
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
1566 if (maxcount < count)
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1567 count = maxcount;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1568
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1569 /* Check for overflow */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1570 /* result_len = count * to_len + self_len; */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1571 product = count * to_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1572 if (product / to_len != count) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1573 PyErr_SetString(PyExc_OverflowError,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1574 "replace string is too long");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1575 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1576 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1577 result_len = product + self_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1578 if (result_len < 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1579 PyErr_SetString(PyExc_OverflowError,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1580 "replace string is too long");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1581 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1582 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1583
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1584 if (! (result = (PyBytesObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1585 PyBytes_FromStringAndSize(NULL, result_len)) )
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1586 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1587
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1588 self_s = PyBytes_AS_STRING(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1589 result_s = PyBytes_AS_STRING(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1590
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1591 /* TODO: special case single character, which doesn't need memcpy */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1592
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1593 /* Lay the first one down (guaranteed this will occur) */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1594 Py_MEMCPY(result_s, to_s, to_len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1595 result_s += to_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1596 count -= 1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1597
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1598 for (i=0; i<count; i++) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1599 *result_s++ = *self_s++;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1600 Py_MEMCPY(result_s, to_s, to_len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1601 result_s += to_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1602 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1603
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1604 /* Copy the rest of the original string */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1605 Py_MEMCPY(result_s, self_s, self_len-i);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1606
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1607 return result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1608 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1609
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1610 /* Special case for deleting a single character */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1611 /* len(self)>=1, len(from)==1, to="", maxcount>=1 */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1612 Py_LOCAL(PyBytesObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1613 replace_delete_single_character(PyBytesObject *self,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1614 char from_c, Py_ssize_t maxcount)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1615 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1616 char *self_s, *result_s;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1617 char *start, *next, *end;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1618 Py_ssize_t self_len, result_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1619 Py_ssize_t count;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1620 PyBytesObject *result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1621
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1622 self_len = PyBytes_GET_SIZE(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1623 self_s = PyBytes_AS_STRING(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1624
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1625 count = countchar(self_s, self_len, from_c, maxcount);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1626 if (count == 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1627 return return_self(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1628 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1629
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1630 result_len = self_len - count; /* from_len == 1 */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1631 assert(result_len>=0);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1632
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1633 if ( (result = (PyBytesObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1634 PyBytes_FromStringAndSize(NULL, result_len)) == NULL)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1635 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1636 result_s = PyBytes_AS_STRING(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1637
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1638 start = self_s;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1639 end = self_s + self_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1640 while (count-- > 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1641 next = findchar(start, end-start, from_c);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1642 if (next == NULL)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1643 break;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1644 Py_MEMCPY(result_s, start, next-start);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1645 result_s += (next-start);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1646 start = next+1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1647 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1648 Py_MEMCPY(result_s, start, end-start);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1649
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1650 return result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1651 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1652
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1653 /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1654
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1655 Py_LOCAL(PyBytesObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1656 replace_delete_substring(PyBytesObject *self,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1657 const char *from_s, Py_ssize_t from_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1658 Py_ssize_t maxcount)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1659 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1660 char *self_s, *result_s;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1661 char *start, *next, *end;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1662 Py_ssize_t self_len, result_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1663 Py_ssize_t count, offset;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1664 PyBytesObject *result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1665
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1666 self_len = PyBytes_GET_SIZE(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1667 self_s = PyBytes_AS_STRING(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1668
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1669 count = countstring(self_s, self_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1670 from_s, from_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1671 0, self_len, 1,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1672 maxcount);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1673
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1674 if (count == 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1675 /* no matches */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1676 return return_self(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1677 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1678
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1679 result_len = self_len - (count * from_len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1680 assert (result_len>=0);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1681
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1682 if ( (result = (PyBytesObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1683 PyBytes_FromStringAndSize(NULL, result_len)) == NULL )
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1684 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1685
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1686 result_s = PyBytes_AS_STRING(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1687
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1688 start = self_s;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1689 end = self_s + self_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1690 while (count-- > 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1691 offset = findstring(start, end-start,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1692 from_s, from_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1693 0, end-start, FORWARD);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1694 if (offset == -1)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1695 break;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1696 next = start + offset;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1697
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1698 Py_MEMCPY(result_s, start, next-start);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1699
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1700 result_s += (next-start);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1701 start = next+from_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1702 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1703 Py_MEMCPY(result_s, start, end-start);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1704 return result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1705 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1706
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1707 /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1708 Py_LOCAL(PyBytesObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1709 replace_single_character_in_place(PyBytesObject *self,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1710 char from_c, char to_c,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1711 Py_ssize_t maxcount)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1712 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1713 char *self_s, *result_s, *start, *end, *next;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1714 Py_ssize_t self_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1715 PyBytesObject *result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1716
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1717 /* The result string will be the same size */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1718 self_s = PyBytes_AS_STRING(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1719 self_len = PyBytes_GET_SIZE(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1720
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1721 next = findchar(self_s, self_len, from_c);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1722
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1723 if (next == NULL) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1724 /* No matches; return the original bytes */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1725 return return_self(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1726 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1727
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1728 /* Need to make a new bytes */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1729 result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1730 if (result == NULL)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1731 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1732 result_s = PyBytes_AS_STRING(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1733 Py_MEMCPY(result_s, self_s, self_len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1734
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1735 /* change everything in-place, starting with this one */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1736 start = result_s + (next-self_s);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1737 *start = to_c;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1738 start++;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1739 end = result_s + self_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1740
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1741 while (--maxcount > 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1742 next = findchar(start, end-start, from_c);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1743 if (next == NULL)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1744 break;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1745 *next = to_c;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1746 start = next+1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1747 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1748
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1749 return result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1750 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1751
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1752 /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1753 Py_LOCAL(PyBytesObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1754 replace_substring_in_place(PyBytesObject *self,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1755 const char *from_s, Py_ssize_t from_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1756 const char *to_s, Py_ssize_t to_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1757 Py_ssize_t maxcount)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1758 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1759 char *result_s, *start, *end;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1760 char *self_s;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1761 Py_ssize_t self_len, offset;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1762 PyBytesObject *result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1763
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1764 /* The result bytes will be the same size */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1765
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1766 self_s = PyBytes_AS_STRING(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1767 self_len = PyBytes_GET_SIZE(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1768
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1769 offset = findstring(self_s, self_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1770 from_s, from_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1771 0, self_len, FORWARD);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1772 if (offset == -1) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1773 /* No matches; return the original bytes */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1774 return return_self(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1775 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1776
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1777 /* Need to make a new bytes */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1778 result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1779 if (result == NULL)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1780 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1781 result_s = PyBytes_AS_STRING(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1782 Py_MEMCPY(result_s, self_s, self_len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1783
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1784 /* change everything in-place, starting with this one */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1785 start = result_s + offset;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1786 Py_MEMCPY(start, to_s, from_len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1787 start += from_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1788 end = result_s + self_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1789
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1790 while ( --maxcount > 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1791 offset = findstring(start, end-start,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1792 from_s, from_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1793 0, end-start, FORWARD);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1794 if (offset==-1)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1795 break;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1796 Py_MEMCPY(start+offset, to_s, from_len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1797 start += offset+from_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1798 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1799
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1800 return result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1801 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1802
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1803 /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1804 Py_LOCAL(PyBytesObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1805 replace_single_character(PyBytesObject *self,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1806 char from_c,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1807 const char *to_s, Py_ssize_t to_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1808 Py_ssize_t maxcount)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1809 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1810 char *self_s, *result_s;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1811 char *start, *next, *end;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1812 Py_ssize_t self_len, result_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1813 Py_ssize_t count, product;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1814 PyBytesObject *result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1815
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1816 self_s = PyBytes_AS_STRING(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1817 self_len = PyBytes_GET_SIZE(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1818
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1819 count = countchar(self_s, self_len, from_c, maxcount);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1820 if (count == 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1821 /* no matches, return unchanged */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1822 return return_self(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1823 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1824
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1825 /* use the difference between current and new, hence the "-1" */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1826 /* result_len = self_len + count * (to_len-1) */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1827 product = count * (to_len-1);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1828 if (product / (to_len-1) != count) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1829 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1830 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1831 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1832 result_len = self_len + product;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1833 if (result_len < 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1834 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1835 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1836 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1837
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1838 if ( (result = (PyBytesObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1839 PyBytes_FromStringAndSize(NULL, result_len)) == NULL)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1840 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1841 result_s = PyBytes_AS_STRING(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1842
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1843 start = self_s;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1844 end = self_s + self_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1845 while (count-- > 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1846 next = findchar(start, end-start, from_c);
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
1847 if (next == NULL)
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1848 break;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1849
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1850 if (next == start) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1851 /* replace with the 'to' */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1852 Py_MEMCPY(result_s, to_s, to_len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1853 result_s += to_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1854 start += 1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1855 } else {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1856 /* copy the unchanged old then the 'to' */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1857 Py_MEMCPY(result_s, start, next-start);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1858 result_s += (next-start);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1859 Py_MEMCPY(result_s, to_s, to_len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1860 result_s += to_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1861 start = next+1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1862 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1863 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1864 /* Copy the remainder of the remaining bytes */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1865 Py_MEMCPY(result_s, start, end-start);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1866
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1867 return result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1868 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1869
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1870 /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1871 Py_LOCAL(PyBytesObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1872 replace_substring(PyBytesObject *self,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1873 const char *from_s, Py_ssize_t from_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1874 const char *to_s, Py_ssize_t to_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1875 Py_ssize_t maxcount)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1876 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1877 char *self_s, *result_s;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1878 char *start, *next, *end;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1879 Py_ssize_t self_len, result_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1880 Py_ssize_t count, offset, product;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1881 PyBytesObject *result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1882
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1883 self_s = PyBytes_AS_STRING(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1884 self_len = PyBytes_GET_SIZE(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1885
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1886 count = countstring(self_s, self_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1887 from_s, from_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1888 0, self_len, FORWARD, maxcount);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1889 if (count == 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1890 /* no matches, return unchanged */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1891 return return_self(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1892 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1893
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1894 /* Check for overflow */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1895 /* result_len = self_len + count * (to_len-from_len) */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1896 product = count * (to_len-from_len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1897 if (product / (to_len-from_len) != count) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1898 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1899 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1900 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1901 result_len = self_len + product;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1902 if (result_len < 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1903 PyErr_SetString(PyExc_OverflowError, "replace bytes is too long");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1904 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1905 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1906
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1907 if ( (result = (PyBytesObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1908 PyBytes_FromStringAndSize(NULL, result_len)) == NULL)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1909 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1910 result_s = PyBytes_AS_STRING(result);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1911
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1912 start = self_s;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1913 end = self_s + self_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1914 while (count-- > 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1915 offset = findstring(start, end-start,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1916 from_s, from_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1917 0, end-start, FORWARD);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1918 if (offset == -1)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1919 break;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1920 next = start+offset;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1921 if (next == start) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1922 /* replace with the 'to' */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1923 Py_MEMCPY(result_s, to_s, to_len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1924 result_s += to_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1925 start += from_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1926 } else {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1927 /* copy the unchanged old then the 'to' */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1928 Py_MEMCPY(result_s, start, next-start);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1929 result_s += (next-start);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1930 Py_MEMCPY(result_s, to_s, to_len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1931 result_s += to_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1932 start = next+from_len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1933 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1934 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1935 /* Copy the remainder of the remaining bytes */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1936 Py_MEMCPY(result_s, start, end-start);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1937
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1938 return result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1939 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1940
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1941
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1942 Py_LOCAL(PyBytesObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1943 replace(PyBytesObject *self,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1944 const char *from_s, Py_ssize_t from_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1945 const char *to_s, Py_ssize_t to_len,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1946 Py_ssize_t maxcount)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1947 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1948 if (maxcount < 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1949 maxcount = PY_SSIZE_T_MAX;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1950 } else if (maxcount == 0 || PyBytes_GET_SIZE(self) == 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1951 /* nothing to do; return the original bytes */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1952 return return_self(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1953 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1954
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1955 if (maxcount == 0 ||
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1956 (from_len == 0 && to_len == 0)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1957 /* nothing to do; return the original bytes */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1958 return return_self(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1959 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1960
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1961 /* Handle zero-length special cases */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1962
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1963 if (from_len == 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1964 /* insert the 'to' bytes everywhere. */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1965 /* >>> "Python".replace("", ".") */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1966 /* '.P.y.t.h.o.n.' */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1967 return replace_interleave(self, to_s, to_len, maxcount);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1968 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1969
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1970 /* Except for "".replace("", "A") == "A" there is no way beyond this */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1971 /* point for an empty self bytes to generate a non-empty bytes */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1972 /* Special case so the remaining code always gets a non-empty bytes */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1973 if (PyBytes_GET_SIZE(self) == 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1974 return return_self(self);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1975 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1976
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1977 if (to_len == 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1978 /* delete all occurances of 'from' bytes */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1979 if (from_len == 1) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1980 return replace_delete_single_character(
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1981 self, from_s[0], maxcount);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1982 } else {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1983 return replace_delete_substring(self, from_s, from_len, maxcount);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1984 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1985 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1986
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1987 /* Handle special case where both bytes have the same length */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1988
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1989 if (from_len == to_len) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1990 if (from_len == 1) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1991 return replace_single_character_in_place(
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1992 self,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1993 from_s[0],
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1994 to_s[0],
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1995 maxcount);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1996 } else {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1997 return replace_substring_in_place(
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1998 self, from_s, from_len, to_s, to_len, maxcount);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
1999 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2000 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2001
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2002 /* Otherwise use the more generic algorithms */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2003 if (from_len == 1) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2004 return replace_single_character(self, from_s[0],
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2005 to_s, to_len, maxcount);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2006 } else {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2007 /* len('from')>=2, len('to')>=1 */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2008 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2009 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2010 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2011
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2012 PyDoc_STRVAR(replace__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2013 "B.replace (old, new[, count]) -> bytes\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2014 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2015 Return a copy of bytes B with all occurrences of subsection\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2016 old replaced by new. If the optional argument count is\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2017 given, only the first count occurrences are replaced.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2018
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2019 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2020 bytes_replace(PyBytesObject *self, PyObject *args)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2021 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2022 Py_ssize_t count = -1;
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2023 PyObject *from, *to, *res;
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2024 const char *from_s, *to_s;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2025 Py_ssize_t from_len, to_len;
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2026 int relfrom=0, relto=0;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2027 PyBuffer vfrom, vto;
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2028
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2029 if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2030 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2031
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2032 if (PyBytes_Check(from)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2033 from_s = PyBytes_AS_STRING(from);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2034 from_len = PyBytes_GET_SIZE(from);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2035 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2036 else {
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2037 if (PyObject_GetBuffer(from, &vfrom, PyBUF_CHARACTER) < 0)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2038 return NULL;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2039 from_s = vfrom.buf;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2040 from_len = vfrom.len;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2041 relfrom = 1;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2042 }
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2043
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2044 if (PyBytes_Check(to)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2045 to_s = PyBytes_AS_STRING(to);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2046 to_len = PyBytes_GET_SIZE(to);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2047 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2048 else {
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2049 if (PyObject_GetBuffer(to, &vto, PyBUF_CHARACTER) < 0) {
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2050 if (relfrom)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2051 PyObject_ReleaseBuffer(from, &vfrom);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2052 return NULL;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2053 }
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2054 to_s = vto.buf;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2055 to_len = vto.len;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2056 relto = 1;
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2057 }
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2058
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2059 res = (PyObject *)replace((PyBytesObject *) self,
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2060 from_s, from_len,
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2061 to_s, to_len, count);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2062
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2063 if (relfrom)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2064 PyObject_ReleaseBuffer(from, &vfrom);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2065 if (relto)
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2066 PyObject_ReleaseBuffer(to, &vto);
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2067 return res;
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2068 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2069
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2070
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2071 /* Overallocate the initial list to reduce the number of reallocs for small
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2072 split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2073 resizes, to sizes 4, 8, then 16. Most observed string splits are for human
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2074 text (roughly 11 words per line) and field delimited data (usually 1-10
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2075 fields). For large strings the split algorithms are bandwidth limited
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2076 so increasing the preallocation likely will not improve things.*/
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2077
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2078 #define MAX_PREALLOC 12
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2079
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2080 /* 5 splits gives 6 elements */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2081 #define PREALLOC_SIZE(maxsplit) \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2082 (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2083
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2084 #define SPLIT_APPEND(data, left, right) \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2085 str = PyBytes_FromStringAndSize((data) + (left), \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2086 (right) - (left)); \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2087 if (str == NULL) \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2088 goto onError; \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2089 if (PyList_Append(list, str)) { \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2090 Py_DECREF(str); \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2091 goto onError; \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2092 } \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2093 else \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2094 Py_DECREF(str);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2095
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2096 #define SPLIT_ADD(data, left, right) { \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2097 str = PyBytes_FromStringAndSize((data) + (left), \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2098 (right) - (left)); \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2099 if (str == NULL) \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2100 goto onError; \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2101 if (count < MAX_PREALLOC) { \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2102 PyList_SET_ITEM(list, count, str); \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2103 } else { \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2104 if (PyList_Append(list, str)) { \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2105 Py_DECREF(str); \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2106 goto onError; \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2107 } \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2108 else \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2109 Py_DECREF(str); \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2110 } \
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2111 count++; }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2112
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2113 /* Always force the list to the expected size. */
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2114 #define FIX_PREALLOC_SIZE(list) Py_Size(list) = count
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2115
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2116
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2117 Py_LOCAL_INLINE(PyObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2118 split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2119 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2120 register Py_ssize_t i, j, count=0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2121 PyObject *str;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2122 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2123
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2124 if (list == NULL)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2125 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2126
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2127 i = j = 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2128 while ((j < len) && (maxcount-- > 0)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2129 for(; j<len; j++) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2130 /* I found that using memchr makes no difference */
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2131 if (s[j] == ch) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2132 SPLIT_ADD(s, i, j);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2133 i = j = j + 1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2134 break;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2135 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2136 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2137 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2138 if (i <= len) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2139 SPLIT_ADD(s, i, len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2140 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2141 FIX_PREALLOC_SIZE(list);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2142 return list;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2143
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2144 onError:
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2145 Py_DECREF(list);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2146 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2147 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2148
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2149 PyDoc_STRVAR(split__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2150 "B.split(sep [,maxsplit]) -> list of bytes\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2151 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2152 Return a list of the bytes in the string B, using sep as the\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2153 delimiter. If maxsplit is given, at most maxsplit\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2154 splits are done.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2155
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2156 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2157 bytes_split(PyBytesObject *self, PyObject *args)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2158 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2159 Py_ssize_t len = PyBytes_GET_SIZE(self), n, i, j;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2160 Py_ssize_t maxsplit = -1, count=0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2161 const char *s = PyBytes_AS_STRING(self), *sub;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2162 PyObject *list, *str, *subobj;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2163 #ifdef USE_FAST
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2164 Py_ssize_t pos;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2165 #endif
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2166
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2167 if (!PyArg_ParseTuple(args, "O|n:split", &subobj, &maxsplit))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2168 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2169 if (maxsplit < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2170 maxsplit = PY_SSIZE_T_MAX;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2171 if (PyBytes_Check(subobj)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2172 sub = PyBytes_AS_STRING(subobj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2173 n = PyBytes_GET_SIZE(subobj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2174 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2175 /* XXX -> use the modern buffer interface */
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2176 else if (PyObject_AsCharBuffer(subobj, &sub, &n))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2177 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2178
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2179 if (n == 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2180 PyErr_SetString(PyExc_ValueError, "empty separator");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2181 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2182 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2183 else if (n == 1)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2184 return split_char(s, len, sub[0], maxsplit);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2185
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2186 list = PyList_New(PREALLOC_SIZE(maxsplit));
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2187 if (list == NULL)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2188 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2189
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2190 #ifdef USE_FAST
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2191 i = j = 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2192 while (maxsplit-- > 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2193 pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2194 if (pos < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2195 break;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2196 j = i+pos;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2197 SPLIT_ADD(s, i, j);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2198 i = j + n;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2199 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2200 #else
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2201 i = j = 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2202 while ((j+n <= len) && (maxsplit-- > 0)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2203 for (; j+n <= len; j++) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2204 if (Py_STRING_MATCH(s, j, sub, n)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2205 SPLIT_ADD(s, i, j);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2206 i = j = j + n;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2207 break;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2208 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2209 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2210 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2211 #endif
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2212 SPLIT_ADD(s, i, len);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2213 FIX_PREALLOC_SIZE(list);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2214 return list;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2215
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2216 onError:
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2217 Py_DECREF(list);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2218 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2219 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2220
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2221 PyDoc_STRVAR(partition__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2222 "B.partition(sep) -> (head, sep, tail)\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2223 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2224 Searches for the separator sep in B, and returns the part before it,\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2225 the separator itself, and the part after it. If the separator is not\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2226 found, returns B and two empty bytes.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2227
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2228 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2229 bytes_partition(PyBytesObject *self, PyObject *sep_obj)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2230 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2231 PyObject *bytesep, *result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2232
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2233 bytesep = PyBytes_FromObject(sep_obj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2234 if (! bytesep)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2235 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2236
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2237 result = stringlib_partition(
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2238 (PyObject*) self,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2239 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
2240 bytesep,
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2241 PyBytes_AS_STRING(bytesep), PyBytes_GET_SIZE(bytesep)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2242 );
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2243
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2244 Py_DECREF(bytesep);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2245 return result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2246 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2247
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2248 PyDoc_STRVAR(rpartition__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2249 "B.rpartition(sep) -> (tail, sep, head)\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2250 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2251 Searches for the separator sep in B, starting at the end of B, and returns\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2252 the part before it, the separator itself, and the part after it. If the\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2253 separator is not found, returns two empty bytes and B.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2254
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2255 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2256 bytes_rpartition(PyBytesObject *self, PyObject *sep_obj)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2257 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2258 PyObject *bytesep, *result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2259
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2260 bytesep = PyBytes_FromObject(sep_obj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2261 if (! bytesep)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2262 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2263
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2264 result = stringlib_rpartition(
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2265 (PyObject*) self,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2266 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self),
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
2267 bytesep,
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2268 PyBytes_AS_STRING(bytesep), PyBytes_GET_SIZE(bytesep)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2269 );
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2270
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2271 Py_DECREF(bytesep);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2272 return result;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2273 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2274
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2275 Py_LOCAL_INLINE(PyObject *)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2276 rsplit_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2277 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2278 register Py_ssize_t i, j, count=0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2279 PyObject *str;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2280 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2281
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2282 if (list == NULL)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2283 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2284
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2285 i = j = len - 1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2286 while ((i >= 0) && (maxcount-- > 0)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2287 for (; i >= 0; i--) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2288 if (s[i] == ch) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2289 SPLIT_ADD(s, i + 1, j + 1);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2290 j = i = i - 1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2291 break;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2292 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2293 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2294 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2295 if (j >= -1) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2296 SPLIT_ADD(s, 0, j + 1);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2297 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2298 FIX_PREALLOC_SIZE(list);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2299 if (PyList_Reverse(list) < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2300 goto onError;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2301
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2302 return list;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2303
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2304 onError:
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2305 Py_DECREF(list);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2306 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2307 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2308
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2309 PyDoc_STRVAR(rsplit__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2310 "B.rsplit(sep [,maxsplit]) -> list of bytes\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2311 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2312 Return a list of the sections in the byte B, using sep as the\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2313 delimiter, starting at the end of the bytes and working\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2314 to the front. If maxsplit is given, at most maxsplit splits are\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2315 done.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2316
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2317 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2318 bytes_rsplit(PyBytesObject *self, PyObject *args)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2319 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2320 Py_ssize_t len = PyBytes_GET_SIZE(self), n, i, j;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2321 Py_ssize_t maxsplit = -1, count=0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2322 const char *s = PyBytes_AS_STRING(self), *sub;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2323 PyObject *list, *str, *subobj;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2324
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2325 if (!PyArg_ParseTuple(args, "O|n:rsplit", &subobj, &maxsplit))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2326 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2327 if (maxsplit < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2328 maxsplit = PY_SSIZE_T_MAX;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2329 if (PyBytes_Check(subobj)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2330 sub = PyBytes_AS_STRING(subobj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2331 n = PyBytes_GET_SIZE(subobj);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2332 }
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2333 /* XXX -> Use the modern buffer interface */
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2334 else if (PyObject_AsCharBuffer(subobj, &sub, &n))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2335 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2336
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2337 if (n == 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2338 PyErr_SetString(PyExc_ValueError, "empty separator");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2339 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2340 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2341 else if (n == 1)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2342 return rsplit_char(s, len, sub[0], maxsplit);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2343
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2344 list = PyList_New(PREALLOC_SIZE(maxsplit));
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2345 if (list == NULL)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2346 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2347
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2348 j = len;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2349 i = j - n;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2350
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2351 while ( (i >= 0) && (maxsplit-- > 0) ) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2352 for (; i>=0; i--) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2353 if (Py_STRING_MATCH(s, i, sub, n)) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2354 SPLIT_ADD(s, i + n, j);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2355 j = i;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2356 i -= n;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2357 break;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2358 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2359 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2360 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2361 SPLIT_ADD(s, 0, j);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2362 FIX_PREALLOC_SIZE(list);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2363 if (PyList_Reverse(list) < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2364 goto onError;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2365 return list;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2366
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2367 onError:
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2368 Py_DECREF(list);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2369 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2370 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2371
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2372 PyDoc_STRVAR(extend__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2373 "B.extend(iterable int) -> None\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2374 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2375 Append all the elements from the iterator or sequence to the\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2376 end of the bytes.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2377 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2378 bytes_extend(PyBytesObject *self, PyObject *arg)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2379 {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2380 if (bytes_setslice(self, Py_Size(self), Py_Size(self), arg) == -1)
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2381 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2382 Py_RETURN_NONE;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2383 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2384
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2385
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2386 PyDoc_STRVAR(reverse__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2387 "B.reverse() -> None\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2388 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2389 Reverse the order of the values in bytes in place.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2390 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2391 bytes_reverse(PyBytesObject *self, PyObject *unused)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2392 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2393 char swap, *head, *tail;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2394 Py_ssize_t i, j, n = Py_Size(self);
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2395
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2396 j = n / 2;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2397 head = self->ob_bytes;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2398 tail = head + n - 1;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2399 for (i = 0; i < j; i++) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2400 swap = *head;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2401 *head++ = *tail;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2402 *tail-- = swap;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2403 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2404
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2405 Py_RETURN_NONE;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2406 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2407
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2408 PyDoc_STRVAR(insert__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2409 "B.insert(index, int) -> None\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2410 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2411 Insert a single item into the bytes before the given index.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2412 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2413 bytes_insert(PyBytesObject *self, PyObject *args)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2414 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2415 int value;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2416 Py_ssize_t where, n = Py_Size(self);
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2417
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2418 if (!PyArg_ParseTuple(args, "ni:insert", &where, &value))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2419 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2420
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2421 if (n == PY_SSIZE_T_MAX) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2422 PyErr_SetString(PyExc_OverflowError,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2423 "cannot add more objects to bytes");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2424 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2425 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2426 if (value < 0 || value >= 256) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2427 PyErr_SetString(PyExc_ValueError,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2428 "byte must be in range(0, 256)");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2429 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2430 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2431 if (PyBytes_Resize((PyObject *)self, n + 1) < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2432 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2433
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2434 if (where < 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2435 where += n;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2436 if (where < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2437 where = 0;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2438 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2439 if (where > n)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2440 where = n;
40982
431b3238593e Fix off-by-one bug in memmove() call in bytes_insert().
Guido van Rossum <guido@python.org>
parents: 40978
diff changeset
2441 memmove(self->ob_bytes + where + 1, self->ob_bytes + where, n - where);
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2442 self->ob_bytes[where] = value;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2443
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2444 Py_RETURN_NONE;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2445 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2446
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2447 PyDoc_STRVAR(append__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2448 "B.append(int) -> None\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2449 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2450 Append a single item to the end of the bytes.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2451 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2452 bytes_append(PyBytesObject *self, PyObject *arg)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2453 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2454 int value;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2455 Py_ssize_t n = Py_Size(self);
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2456
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2457 if (! _getbytevalue(arg, &value))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2458 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2459 if (n == PY_SSIZE_T_MAX) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2460 PyErr_SetString(PyExc_OverflowError,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2461 "cannot add more objects to bytes");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2462 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2463 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2464 if (PyBytes_Resize((PyObject *)self, n + 1) < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2465 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2466
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2467 self->ob_bytes[n] = value;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2468
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2469 Py_RETURN_NONE;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2470 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2471
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2472 PyDoc_STRVAR(pop__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2473 "B.pop([index]) -> int\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2474 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2475 Remove and return a single item from the bytes. If no index\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2476 argument is give, will pop the last value.");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2477 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2478 bytes_pop(PyBytesObject *self, PyObject *args)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2479 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2480 int value;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2481 Py_ssize_t where = -1, n = Py_Size(self);
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2482
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2483 if (!PyArg_ParseTuple(args, "|n:pop", &where))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2484 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2485
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2486 if (n == 0) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2487 PyErr_SetString(PyExc_OverflowError,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2488 "cannot pop an empty bytes");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2489 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2490 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2491 if (where < 0)
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2492 where += Py_Size(self);
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2493 if (where < 0 || where >= Py_Size(self)) {
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2494 PyErr_SetString(PyExc_IndexError, "pop index out of range");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2495 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2496 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2497
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2498 value = self->ob_bytes[where];
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2499 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2500 if (PyBytes_Resize((PyObject *)self, n - 1) < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2501 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2502
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2503 return PyInt_FromLong(value);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2504 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2505
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2506 PyDoc_STRVAR(remove__doc__,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2507 "B.remove(int) -> None\n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2508 \n\
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2509 Remove the first occurance of a value in bytes");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2510 static PyObject *
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2511 bytes_remove(PyBytesObject *self, PyObject *arg)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2512 {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2513 int value;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2514 Py_ssize_t where, n = Py_Size(self);
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2515
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2516 if (! _getbytevalue(arg, &value))
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2517 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2518
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2519 for (where = 0; where < n; where++) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2520 if (self->ob_bytes[where] == value)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2521 break;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2522 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2523 if (where == n) {
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2524 PyErr_SetString(PyExc_ValueError, "value not found in bytes");
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2525 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2526 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2527
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2528 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where);
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2529 if (PyBytes_Resize((PyObject *)self, n - 1) < 0)
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2530 return NULL;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2531
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2532 Py_RETURN_NONE;
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2533 }
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2534
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2535 /* XXX These two helpers could be optimized if argsize == 1 */
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2536
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2537 Py_ssize_t
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2538 lstrip_helper(unsigned char *myptr, Py_ssize_t mysize,
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2539 void *argptr, Py_ssize_t argsize)
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2540 {
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2541 Py_ssize_t i = 0;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2542 while (i < mysize && memchr(argptr, myptr[i], argsize))
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2543 i++;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2544 return i;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2545 }
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2546
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2547 Py_ssize_t
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2548 rstrip_helper(unsigned char *myptr, Py_ssize_t mysize,
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2549 void *argptr, Py_ssize_t argsize)
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2550 {
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2551 Py_ssize_t i = mysize - 1;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2552 while (i >= 0 && memchr(argptr, myptr[i], argsize))
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2553 i--;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2554 return i + 1;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2555 }
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2556
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2557 PyDoc_STRVAR(strip__doc__,
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2558 "B.strip(bytes) -> bytes\n\
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2559 \n\
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2560 Strip leading and trailing bytes contained in the argument.");
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2561 static PyObject *
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2562 bytes_strip(PyBytesObject *self, PyObject *arg)
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2563 {
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2564 Py_ssize_t left, right, mysize, argsize;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2565 void *myptr, *argptr;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2566 if (arg == NULL || !PyBytes_Check(arg)) {
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2567 PyErr_SetString(PyExc_TypeError, "strip() requires a bytes argument");
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2568 return NULL;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2569 }
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2570 myptr = self->ob_bytes;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2571 mysize = Py_Size(self);
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2572 argptr = ((PyBytesObject *)arg)->ob_bytes;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2573 argsize = Py_Size(arg);
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2574 left = lstrip_helper(myptr, mysize, argptr, argsize);
42499
96098abc8f8f Fix core dump in an endcase of b.strip() that I missed.
Guido van Rossum <guido@python.org>
parents: 42457
diff changeset
2575 if (left == mysize)
96098abc8f8f Fix core dump in an endcase of b.strip() that I missed.
Guido van Rossum <guido@python.org>
parents: 42457
diff changeset
2576 right = left;
96098abc8f8f Fix core dump in an endcase of b.strip() that I missed.
Guido van Rossum <guido@python.org>
parents: 42457
diff changeset
2577 else
96098abc8f8f Fix core dump in an endcase of b.strip() that I missed.
Guido van Rossum <guido@python.org>
parents: 42457
diff changeset
2578 right = rstrip_helper(myptr, mysize, argptr, argsize);
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2579 return PyBytes_FromStringAndSize(self->ob_bytes + left, right - left);
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2580 }
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2581
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2582 PyDoc_STRVAR(lstrip__doc__,
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2583 "B.lstrip(bytes) -> bytes\n\
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2584 \n\
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2585 Strip leading bytes contained in the argument.");
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2586 static PyObject *
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2587 bytes_lstrip(PyBytesObject *self, PyObject *arg)
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2588 {
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2589 Py_ssize_t left, right, mysize, argsize;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2590 void *myptr, *argptr;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2591 if (arg == NULL || !PyBytes_Check(arg)) {
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2592 PyErr_SetString(PyExc_TypeError, "strip() requires a bytes argument");
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2593 return NULL;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2594 }
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2595 myptr = self->ob_bytes;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2596 mysize = Py_Size(self);
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2597 argptr = ((PyBytesObject *)arg)->ob_bytes;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2598 argsize = Py_Size(arg);
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2599 left = lstrip_helper(myptr, mysize, argptr, argsize);
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2600 right = mysize;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2601 return PyBytes_FromStringAndSize(self->ob_bytes + left, right - left);
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2602 }
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2603
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2604 PyDoc_STRVAR(rstrip__doc__,
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2605 "B.rstrip(bytes) -> bytes\n\
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2606 \n\
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2607 Strip trailing bytes contained in the argument.");
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2608 static PyObject *
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2609 bytes_rstrip(PyBytesObject *self, PyObject *arg)
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2610 {
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2611 Py_ssize_t left, right, mysize, argsize;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2612 void *myptr, *argptr;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2613 if (arg == NULL || !PyBytes_Check(arg)) {
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2614 PyErr_SetString(PyExc_TypeError, "strip() requires a bytes argument");
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2615 return NULL;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2616 }
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2617 myptr = self->ob_bytes;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2618 mysize = Py_Size(self);
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2619 argptr = ((PyBytesObject *)arg)->ob_bytes;
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2620 argsize = Py_Size(arg);
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2621 left = 0;
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2622 right = rstrip_helper(myptr, mysize, argptr, argsize);
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2623 return PyBytes_FromStringAndSize(self->ob_bytes + left, right - left);
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2624 }
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2625
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2626 PyDoc_STRVAR(decode_doc,
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2627 "B.decode([encoding[,errors]]) -> unicode obect.\n\
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2628 \n\
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2629 Decodes B using the codec registered for encoding. encoding defaults\n\
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2630 to the default encoding. errors may be given to set a different error\n\
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2631 handling scheme. Default is 'strict' meaning that encoding errors raise\n\
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2632 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2633 as well as any other name registerd with codecs.register_error that is\n\
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2634 able to handle UnicodeDecodeErrors.");
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2635
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2636 static PyObject *
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2637 bytes_decode(PyObject *self, PyObject *args)
41448
458c8db2e946 Clean up trailing whitespace.
Guido van Rossum <guido@python.org>
parents: 41426
diff changeset
2638 {
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2639 const char *encoding = NULL;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2640 const char *errors = NULL;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2641
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2642 if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors))
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2643 return NULL;
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2644 if (encoding == NULL)
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2645 encoding = PyUnicode_GetDefaultEncoding();
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2646 return PyCodec_Decode(self, encoding, errors);
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2647 }
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2648
38115
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
2649 PyDoc_STRVAR(alloc_doc,
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
2650 "B.__alloc__() -> int\n\
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
2651 \n\
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
2652 Returns the number of bytes actually allocated.");
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
2653
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
2654 static PyObject *
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
2655 bytes_alloc(PyBytesObject *self)
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
2656 {
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
2657 return PyInt_FromSsize_t(self->ob_alloc);
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
2658 }
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
2659
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2660 PyDoc_STRVAR(join_doc,
41664
a363cea32b9b I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents: 41648
diff changeset
2661 "B.join(iterable_of_bytes) -> bytes\n\
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2662 \n\
41664
a363cea32b9b I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents: 41648
diff changeset
2663 Concatenates any number of bytes objects, with B in between each pair.\n\
a363cea32b9b I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents: 41648
diff changeset
2664 Example: b'.'.join([b'ab', b'pq', b'rs']) -> b'ab.pq.rs'.");
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2665
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2666 static PyObject *
41664
a363cea32b9b I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents: 41648
diff changeset
2667 bytes_join(PyBytesObject *self, PyObject *it)
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2668 {
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2669 PyObject *seq;
42335
c8b850b5a066 Fix merge breakage.
Martin v. Löwis <martin@v.loewis.de>
parents: 42334
diff changeset
2670 Py_ssize_t mysize = Py_Size(self);
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2671 Py_ssize_t i;
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2672 Py_ssize_t n;
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2673 PyObject **items;
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2674 Py_ssize_t totalsize = 0;
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2675 PyObject *result;
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2676 char *dest;
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2677
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2678 seq = PySequence_Fast(it, "can only join an iterable");
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2679 if (seq == NULL)
40968
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2680 return NULL;
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2681 n = PySequence_Fast_GET_SIZE(seq);
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2682 items = PySequence_Fast_ITEMS(seq);
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2683
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2684 /* Compute the total size, and check that they are all bytes */
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2685 for (i = 0; i < n; i++) {
40968
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2686 PyObject *obj = items[i];
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2687 if (!PyBytes_Check(obj)) {
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2688 PyErr_Format(PyExc_TypeError,
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2689 "can only join an iterable of bytes "
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2690 "(item %ld has type '%.100s')",
39222
b0cdcbcc5f45 Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents: 38117
diff changeset
2691 /* XXX %ld isn't right on Win64 */
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2692 (long)i, Py_Type(obj)->tp_name);
40968
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2693 goto error;
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2694 }
41664
a363cea32b9b I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents: 41648
diff changeset
2695 if (i > 0)
a363cea32b9b I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents: 41648
diff changeset
2696 totalsize += mysize;
40968
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2697 totalsize += PyBytes_GET_SIZE(obj);
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2698 if (totalsize < 0) {
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2699 PyErr_NoMemory();
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2700 goto error;
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2701 }
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2702 }
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2703
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2704 /* Allocate the result, and copy the bytes */
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2705 result = PyBytes_FromStringAndSize(NULL, totalsize);
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2706 if (result == NULL)
40968
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2707 goto error;
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2708 dest = PyBytes_AS_STRING(result);
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2709 for (i = 0; i < n; i++) {
40968
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2710 PyObject *obj = items[i];
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2711 Py_ssize_t size = PyBytes_GET_SIZE(obj);
41664
a363cea32b9b I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents: 41648
diff changeset
2712 if (i > 0) {
a363cea32b9b I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents: 41648
diff changeset
2713 memcpy(dest, self->ob_bytes, mysize);
a363cea32b9b I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents: 41648
diff changeset
2714 dest += mysize;
a363cea32b9b I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents: 41648
diff changeset
2715 }
40968
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2716 memcpy(dest, PyBytes_AS_STRING(obj), size);
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2717 dest += size;
38117
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2718 }
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2719
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2720 /* Done */
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2721 Py_DECREF(seq);
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2722 return result;
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2723
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2724 /* Error handling */
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2725 error:
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2726 Py_DECREF(seq);
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2727 return NULL;
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2728 }
57bd7c7d91e3 Adding bytes.join() -- a class methods that concatenates an iterable of bytes.
Guido van Rossum <guido@python.org>
parents: 38115
diff changeset
2729
40967
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2730 PyDoc_STRVAR(fromhex_doc,
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2731 "bytes.fromhex(string) -> bytes\n\
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2732 \n\
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2733 Create a bytes object from a string of hexadecimal numbers.\n\
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2734 Spaces between two numbers are accepted. Example:\n\
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2735 bytes.fromhex('10 2030') -> bytes([0x10, 0x20, 0x30]).");
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2736
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2737 static int
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2738 hex_digit_to_int(int c)
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2739 {
40968
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2740 if (isdigit(c))
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2741 return c - '0';
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2742 else {
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2743 if (isupper(c))
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2744 c = tolower(c);
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2745 if (c >= 'a' && c <= 'f')
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2746 return c - 'a' + 10;
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2747 }
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2748 return -1;
40967
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2749 }
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2750
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2751 static PyObject *
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2752 bytes_fromhex(PyObject *cls, PyObject *args)
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2753 {
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2754 PyObject *newbytes;
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2755 char *hex, *buf;
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2756 Py_ssize_t len, byteslen, i, j;
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2757 int top, bot;
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2758
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2759 if (!PyArg_ParseTuple(args, "s#:fromhex", &hex, &len))
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2760 return NULL;
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2761
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2762 byteslen = len / 2; /* max length if there are no spaces */
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2763
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2764 newbytes = PyBytes_FromStringAndSize(NULL, byteslen);
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2765 if (!newbytes)
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2766 return NULL;
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2767 buf = PyBytes_AS_STRING(newbytes);
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2768
41598
ca448c607b14 Make all of test_bytes pass (except pickling, which is too badly busted).
Guido van Rossum <guido@python.org>
parents: 41597
diff changeset
2769 for (i = j = 0; i < len; i += 2) {
40967
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2770 /* skip over spaces in the input */
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2771 while (Py_CHARMASK(hex[i]) == ' ')
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2772 i++;
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2773 if (i >= len)
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2774 break;
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2775 top = hex_digit_to_int(Py_CHARMASK(hex[i]));
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2776 bot = hex_digit_to_int(Py_CHARMASK(hex[i+1]));
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2777 if (top == -1 || bot == -1) {
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2778 PyErr_Format(PyExc_ValueError,
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2779 "non-hexadecimal number string '%c%c' found in "
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2780 "fromhex() arg at position %zd",
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2781 hex[i], hex[i+1], i);
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2782 goto error;
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2783 }
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2784 buf[j++] = (top << 4) + bot;
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2785 }
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2786 if (PyBytes_Resize(newbytes, j) < 0)
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2787 goto error;
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2788 return newbytes;
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2789
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2790 error:
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2791 Py_DECREF(newbytes);
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2792 return NULL;
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2793 }
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2794
41426
4561f6da3b7d Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents: 41412
diff changeset
2795 PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
4561f6da3b7d Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents: 41412
diff changeset
2796
4561f6da3b7d Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents: 41412
diff changeset
2797 static PyObject *
4561f6da3b7d Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents: 41412
diff changeset
2798 bytes_reduce(PyBytesObject *self)
4561f6da3b7d Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents: 41412
diff changeset
2799 {
42457
080f2aa18015 Change PyUnicode_FromString[AndSize] to expect UTF-8.
Martin v. Löwis <martin@v.loewis.de>
parents: 42335
diff changeset
2800 PyObject *latin1;
080f2aa18015 Change PyUnicode_FromString[AndSize] to expect UTF-8.
Martin v. Löwis <martin@v.loewis.de>
parents: 42335
diff changeset
2801 if (self->ob_bytes)
080f2aa18015 Change PyUnicode_FromString[AndSize] to expect UTF-8.
Martin v. Löwis <martin@v.loewis.de>
parents: 42335
diff changeset
2802 latin1 = PyUnicode_DecodeLatin1(self->ob_bytes,
080f2aa18015 Change PyUnicode_FromString[AndSize] to expect UTF-8.
Martin v. Löwis <martin@v.loewis.de>
parents: 42335
diff changeset
2803 Py_Size(self), NULL);
080f2aa18015 Change PyUnicode_FromString[AndSize] to expect UTF-8.
Martin v. Löwis <martin@v.loewis.de>
parents: 42335
diff changeset
2804 else
080f2aa18015 Change PyUnicode_FromString[AndSize] to expect UTF-8.
Martin v. Löwis <martin@v.loewis.de>
parents: 42335
diff changeset
2805 latin1 = PyUnicode_FromString("");
080f2aa18015 Change PyUnicode_FromString[AndSize] to expect UTF-8.
Martin v. Löwis <martin@v.loewis.de>
parents: 42335
diff changeset
2806 return Py_BuildValue("(O(Ns))", Py_Type(self), latin1, "latin-1");
41426
4561f6da3b7d Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents: 41412
diff changeset
2807 }
4561f6da3b7d Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents: 41412
diff changeset
2808
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2809 static PySequenceMethods bytes_as_sequence = {
40967
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2810 (lenfunc)bytes_length, /* sq_length */
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2811 (binaryfunc)bytes_concat, /* sq_concat */
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2812 (ssizeargfunc)bytes_repeat, /* sq_repeat */
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2813 (ssizeargfunc)bytes_getitem, /* sq_item */
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2814 0, /* sq_slice */
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2815 (ssizeobjargproc)bytes_setitem, /* sq_ass_item */
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2816 0, /* sq_ass_slice */
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2817 (objobjproc)bytes_contains, /* sq_contains */
40967
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2818 (binaryfunc)bytes_iconcat, /* sq_inplace_concat */
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2819 (ssizeargfunc)bytes_irepeat, /* sq_inplace_repeat */
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2820 };
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2821
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2822 static PyMappingMethods bytes_as_mapping = {
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2823 (lenfunc)bytes_length,
40472
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
2824 (binaryfunc)bytes_subscript,
9548ca0e6560 Implement extended slicing in bytes objects.
Thomas Wouters <thomas@python.org>
parents: 39643
diff changeset
2825 (objobjargproc)bytes_ass_subscript,
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2826 };
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2827
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2828 static PyBufferProcs bytes_as_buffer = {
42665
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2829 (getbufferproc)bytes_getbuffer,
85c22b4d4455 Merged in py3k-buffer branch to main line. All objects now use the buffer protocol in PEP 3118.
Travis E. Oliphant <oliphant@enthought.com>
parents: 42499
diff changeset
2830 (releasebufferproc)bytes_releasebuffer,
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2831 };
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2832
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2833 static PyMethodDef
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2834 bytes_methods[] = {
40978
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2835 {"find", (PyCFunction)bytes_find, METH_VARARGS, find__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2836 {"count", (PyCFunction)bytes_count, METH_VARARGS, count__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2837 {"index", (PyCFunction)bytes_index, METH_VARARGS, index__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2838 {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, rfind__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2839 {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, rindex__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2840 {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS, endswith__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2841 {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS,
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2842 startswith__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2843 {"replace", (PyCFunction)bytes_replace, METH_VARARGS, replace__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2844 {"translate", (PyCFunction)bytes_translate, METH_VARARGS, translate__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2845 {"partition", (PyCFunction)bytes_partition, METH_O, partition__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2846 {"rpartition", (PyCFunction)bytes_rpartition, METH_O, rpartition__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2847 {"split", (PyCFunction)bytes_split, METH_VARARGS, split__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2848 {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS, rsplit__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2849 {"extend", (PyCFunction)bytes_extend, METH_O, extend__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2850 {"insert", (PyCFunction)bytes_insert, METH_VARARGS, insert__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2851 {"append", (PyCFunction)bytes_append, METH_O, append__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2852 {"reverse", (PyCFunction)bytes_reverse, METH_NOARGS, reverse__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2853 {"pop", (PyCFunction)bytes_pop, METH_VARARGS, pop__doc__},
61fee1853ca8 SF patch #1669633, add methods for bytes from Pete Shinners.
Neal Norwitz <nnorwitz@gmail.com>
parents: 40968
diff changeset
2854 {"remove", (PyCFunction)bytes_remove, METH_O, remove__doc__},
41450
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2855 {"strip", (PyCFunction)bytes_strip, METH_O, strip__doc__},
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2856 {"lstrip", (PyCFunction)bytes_lstrip, METH_O, lstrip__doc__},
d1c3dd184e38 Rough and dirty job -- allow concatenation of bytes and arbitrary
Guido van Rossum <guido@python.org>
parents: 41448
diff changeset
2857 {"rstrip", (PyCFunction)bytes_rstrip, METH_O, rstrip__doc__},
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2858 {"decode", (PyCFunction)bytes_decode, METH_VARARGS, decode_doc},
38115
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
2859 {"__alloc__", (PyCFunction)bytes_alloc, METH_NOARGS, alloc_doc},
41426
4561f6da3b7d Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents: 41412
diff changeset
2860 {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS,
4561f6da3b7d Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents: 41412
diff changeset
2861 fromhex_doc},
41664
a363cea32b9b I don't know how come bytes.join() was a class method, but that's clearly
Guido van Rossum <guido@python.org>
parents: 41648
diff changeset
2862 {"join", (PyCFunction)bytes_join, METH_O, join_doc},
41426
4561f6da3b7d Real pickling for bytes.
Guido van Rossum <guido@python.org>
parents: 41412
diff changeset
2863 {"__reduce__", (PyCFunction)bytes_reduce, METH_NOARGS, reduce_doc},
38115
a6a1359aab84 Optimizations for bytes reallocation.
Guido van Rossum <guido@python.org>
parents: 38017
diff changeset
2864 {NULL}
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2865 };
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2866
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2867 PyDoc_STRVAR(bytes_doc,
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2868 "bytes([iterable]) -> new array of bytes.\n\
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2869 \n\
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2870 If an argument is given it must be an iterable yielding ints in range(256).");
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2871
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2872 PyTypeObject PyBytes_Type = {
42334
e70cdce0e8b2 Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents: 42319
diff changeset
2873 PyVarObject_HEAD_INIT(&PyType_Type, 0)
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2874 "bytes",
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2875 sizeof(PyBytesObject),
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2876 0,
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2877 (destructor)bytes_dealloc, /* tp_dealloc */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2878 0, /* tp_print */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2879 0, /* tp_getattr */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2880 0, /* tp_setattr */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2881 0, /* tp_compare */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2882 (reprfunc)bytes_repr, /* tp_repr */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2883 0, /* tp_as_number */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2884 &bytes_as_sequence, /* tp_as_sequence */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2885 &bytes_as_mapping, /* tp_as_mapping */
40968
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2886 0, /* tp_hash */
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2887 0, /* tp_call */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2888 (reprfunc)bytes_str, /* tp_str */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2889 PyObject_GenericGetAttr, /* tp_getattro */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2890 0, /* tp_setattro */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2891 &bytes_as_buffer, /* tp_as_buffer */
40968
2d9d10702ec4 Remove a few stray tabs from bytesobject.c.
Georg Brandl <georg@python.org>
parents: 40967
diff changeset
2892 /* bytes is 'final' or 'sealed' */
40967
a9e6c73e0fa5 Implement bytes.fromhex(), with tests.
Georg Brandl <georg@python.org>
parents: 40898
diff changeset
2893 Py_TPFLAGS_DEFAULT, /* tp_flags */
37960
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2894 bytes_doc, /* tp_doc */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2895 0, /* tp_traverse */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2896 0, /* tp_clear */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2897 (richcmpfunc)bytes_richcompare, /* tp_richcompare */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2898 0, /* tp_weaklistoffset */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2899 0, /* tp_iter */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2900 0, /* tp_iternext */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2901 bytes_methods, /* tp_methods */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2902 0, /* tp_members */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2903 0, /* tp_getset */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2904 0, /* tp_base */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2905 0, /* tp_dict */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2906 0, /* tp_descr_get */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2907 0, /* tp_descr_set */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2908 0, /* tp_dictoffset */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2909 (initproc)bytes_init, /* tp_init */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2910 PyType_GenericAlloc, /* tp_alloc */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2911 PyType_GenericNew, /* tp_new */
e0b7e34b5971 Added much functionality to the bytes type.
Guido van Rossum <guido@python.org>
parents: 37930
diff changeset
2912 PyObject_Del, /* tp_free */
37927
a7f3765ea464 Here is a bytes type. It's very minimal but it's a start.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2913 };