Mercurial > cpython-lru_cache-weakref
annotate Objects/bytesobject.c @ 58029:d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
| author | Alexandre Vassalotti <alexandre@peadrop.com> |
|---|---|
| date | Thu, 31 Dec 2009 03:56:09 +0000 |
| parents | f37f6b9b224b |
| children | 7e2af4e57039 |
| rev | line source |
|---|---|
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
1 /* bytes object implementation */ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
3 #define PY_SSIZE_T_CLEAN |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
4 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
5 #include "Python.h" |
| 6 | 6 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
7 #include "bytes_methods.h" |
|
50044
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
8 #include <stddef.h> |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
9 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
10 static Py_ssize_t |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
11 _getbuffer(PyObject *obj, Py_buffer *view) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
12 { |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
13 PyBufferProcs *buffer = Py_TYPE(obj)->tp_as_buffer; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
14 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
15 if (buffer == NULL || buffer->bf_getbuffer == NULL) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
16 { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
17 PyErr_Format(PyExc_TypeError, |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
18 "Type %.100s doesn't support the buffer API", |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
19 Py_TYPE(obj)->tp_name); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
20 return -1; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
21 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
22 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
23 if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
24 return -1; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
25 return view->len; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
26 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
27 |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
28 #ifdef COUNT_ALLOCS |
|
50735
27db82a5cda9
Merged revisions 68381 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50322
diff
changeset
|
29 Py_ssize_t null_strings, one_strings; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
30 #endif |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
31 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
32 static PyBytesObject *characters[UCHAR_MAX + 1]; |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
33 static PyBytesObject *nullstring; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
34 |
|
50044
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
35 /* PyBytesObject_SIZE gives the basic size of a string; any memory allocation |
|
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
36 for a string of length n should request PyBytesObject_SIZE + n bytes. |
|
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
37 |
|
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
38 Using PyBytesObject_SIZE instead of sizeof(PyBytesObject) saves |
|
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
39 3 bytes per string allocation on a typical system. |
|
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
40 */ |
|
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
41 #define PyBytesObject_SIZE (offsetof(PyBytesObject, ob_sval) + 1) |
|
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
42 |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
43 /* |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
44 For both PyBytes_FromString() and PyBytes_FromStringAndSize(), the |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
45 parameter `size' denotes number of characters to allocate, not counting any |
|
21737
30a817038af0
Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents:
21327
diff
changeset
|
46 null terminating character. |
|
30a817038af0
Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents:
21327
diff
changeset
|
47 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
48 For PyBytes_FromString(), the parameter `str' points to a null-terminated |
|
21737
30a817038af0
Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents:
21327
diff
changeset
|
49 string containing exactly `size' bytes. |
|
30a817038af0
Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents:
21327
diff
changeset
|
50 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
51 For PyBytes_FromStringAndSize(), the parameter the parameter `str' is |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
52 either NULL or else points to a string containing at least `size' bytes. |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
53 For PyBytes_FromStringAndSize(), the string in the `str' parameter does |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
54 not have to be null-terminated. (Therefore it is safe to construct a |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
55 substring by calling `PyBytes_FromStringAndSize(origstring, substrlen)'.) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
56 If `str' is NULL then PyBytes_FromStringAndSize() will allocate `size+1' |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
57 bytes (setting the last byte to the null terminating character) and you can |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
58 fill in the data yourself. If `str' is non-NULL then the resulting |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
59 PyString object must be treated as immutable and you must not fill in nor |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
60 alter the data yourself, since the strings may be shared. |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
61 |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
62 The PyObject member `op->ob_size', which denotes the number of "extra |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
63 items" in a variable-size object, will contain the number of bytes |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
64 allocated for string data, not counting the null terminating character. It |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
65 is therefore equal to the equal to the `size' parameter (for |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
66 PyBytes_FromStringAndSize()) or the length of the string in the `str' |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
67 parameter (for PyBytes_FromString()). |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
68 */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
69 PyObject * |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
70 PyBytes_FromStringAndSize(const char *str, Py_ssize_t size) |
| 6 | 71 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
72 register PyBytesObject *op; |
|
46430
47d9bbe10897
Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45052
diff
changeset
|
73 if (size < 0) { |
|
47d9bbe10897
Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45052
diff
changeset
|
74 PyErr_SetString(PyExc_SystemError, |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
75 "Negative size passed to PyBytes_FromStringAndSize"); |
|
46430
47d9bbe10897
Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45052
diff
changeset
|
76 return NULL; |
|
47d9bbe10897
Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45052
diff
changeset
|
77 } |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
78 if (size == 0 && (op = nullstring) != NULL) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
79 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
80 null_strings++; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
81 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
82 Py_INCREF(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
83 return (PyObject *)op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
84 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
85 if (size == 1 && str != NULL && |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
86 (op = characters[*str & UCHAR_MAX]) != NULL) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
87 { |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
88 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
89 one_strings++; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
90 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
91 Py_INCREF(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
92 return (PyObject *)op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
93 } |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
94 |
|
50044
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
95 if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) { |
|
48850
afb9808b0f30
Closes release blocker #3627.
Neal Norwitz <nnorwitz@gmail.com>
parents:
48833
diff
changeset
|
96 PyErr_SetString(PyExc_OverflowError, |
|
afb9808b0f30
Closes release blocker #3627.
Neal Norwitz <nnorwitz@gmail.com>
parents:
48833
diff
changeset
|
97 "byte string is too large"); |
|
afb9808b0f30
Closes release blocker #3627.
Neal Norwitz <nnorwitz@gmail.com>
parents:
48833
diff
changeset
|
98 return NULL; |
|
afb9808b0f30
Closes release blocker #3627.
Neal Norwitz <nnorwitz@gmail.com>
parents:
48833
diff
changeset
|
99 } |
|
afb9808b0f30
Closes release blocker #3627.
Neal Norwitz <nnorwitz@gmail.com>
parents:
48833
diff
changeset
|
100 |
|
24898
a869d2de536f
Call me anal, but there was a particular phrase that was speading to
Guido van Rossum <guido@python.org>
parents:
24872
diff
changeset
|
101 /* Inline PyObject_NewVar */ |
|
50044
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
102 op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size); |
| 27 | 103 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
104 return PyErr_NoMemory(); |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
105 PyObject_INIT_VAR(op, &PyBytes_Type, size); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
106 op->ob_shash = -1; |
| 27 | 107 if (str != NULL) |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
108 Py_MEMCPY(op->ob_sval, str, size); |
| 27 | 109 op->ob_sval[size] = '\0'; |
|
22689
614167df2096
Eliminate DONT_SHARE_SHORT_STRINGS.
Tim Peters <tim.peters@gmail.com>
parents:
22651
diff
changeset
|
110 /* share short strings */ |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
111 if (size == 0) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
112 nullstring = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
113 Py_INCREF(op); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
114 } else if (size == 1 && str != NULL) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
115 characters[*str & UCHAR_MAX] = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
116 Py_INCREF(op); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
117 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
118 return (PyObject *) op; |
| 6 | 119 } |
| 120 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
121 PyObject * |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
122 PyBytes_FromString(const char *str) |
| 6 | 123 { |
|
21263
2f2abeeefd87
PyString_FromString: this requires its argument be non-NULL, but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21254
diff
changeset
|
124 register size_t size; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
125 register PyBytesObject *op; |
|
21263
2f2abeeefd87
PyString_FromString: this requires its argument be non-NULL, but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21254
diff
changeset
|
126 |
|
2f2abeeefd87
PyString_FromString: this requires its argument be non-NULL, but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21254
diff
changeset
|
127 assert(str != NULL); |
|
2f2abeeefd87
PyString_FromString: this requires its argument be non-NULL, but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21254
diff
changeset
|
128 size = strlen(str); |
|
50044
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
129 if (size > PY_SSIZE_T_MAX - PyBytesObject_SIZE) { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
130 PyErr_SetString(PyExc_OverflowError, |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
131 "byte string is too long"); |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
132 return NULL; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
133 } |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
134 if (size == 0 && (op = nullstring) != NULL) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
135 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
136 null_strings++; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
137 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
138 Py_INCREF(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
139 return (PyObject *)op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
140 } |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
141 if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
142 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
143 one_strings++; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
144 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
145 Py_INCREF(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
146 return (PyObject *)op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
147 } |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
148 |
|
24898
a869d2de536f
Call me anal, but there was a particular phrase that was speading to
Guido van Rossum <guido@python.org>
parents:
24872
diff
changeset
|
149 /* Inline PyObject_NewVar */ |
|
50044
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
150 op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + size); |
| 27 | 151 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
152 return PyErr_NoMemory(); |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
153 PyObject_INIT_VAR(op, &PyBytes_Type, size); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
154 op->ob_shash = -1; |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
155 Py_MEMCPY(op->ob_sval, str, size+1); |
|
22689
614167df2096
Eliminate DONT_SHARE_SHORT_STRINGS.
Tim Peters <tim.peters@gmail.com>
parents:
22651
diff
changeset
|
156 /* share short strings */ |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
157 if (size == 0) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
158 nullstring = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
159 Py_INCREF(op); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
160 } else if (size == 1) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
161 characters[*str & UCHAR_MAX] = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
162 Py_INCREF(op); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
163 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
164 return (PyObject *) op; |
| 6 | 165 } |
| 166 | |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
167 PyObject * |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
168 PyBytes_FromFormatV(const char *format, va_list vargs) |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
169 { |
|
20221
abaf9ea2e560
SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents:
20140
diff
changeset
|
170 va_list count; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
171 Py_ssize_t n = 0; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
172 const char* f; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
173 char *s; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
174 PyObject* string; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
175 |
|
20221
abaf9ea2e560
SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents:
20140
diff
changeset
|
176 #ifdef VA_LIST_IS_ARRAY |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
177 Py_MEMCPY(count, vargs, sizeof(va_list)); |
|
20221
abaf9ea2e560
SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents:
20140
diff
changeset
|
178 #else |
|
24400
81ca6ca5c307
Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
179 #ifdef __va_copy |
|
81ca6ca5c307
Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
180 __va_copy(count, vargs); |
|
81ca6ca5c307
Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
181 #else |
|
20221
abaf9ea2e560
SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents:
20140
diff
changeset
|
182 count = vargs; |
|
abaf9ea2e560
SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents:
20140
diff
changeset
|
183 #endif |
|
24400
81ca6ca5c307
Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
184 #endif |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
185 /* step 1: figure out how large a buffer we need */ |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
186 for (f = format; *f; f++) { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
187 if (*f == '%') { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
188 const char* p = f; |
|
43503
98f6180b9b33
Replace all (locale-dependent) uses of isupper(), tolower(), etc., by
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
189 while (*++f && *f != '%' && !ISALPHA(*f)) |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
190 ; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
191 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
192 /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
193 * they don't affect the amount of space we reserve. |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
194 */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
195 if ((*f == 'l' || *f == 'z') && |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
196 (f[1] == 'd' || f[1] == 'u')) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
197 ++f; |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
198 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
199 switch (*f) { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
200 case 'c': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
201 (void)va_arg(count, int); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
202 /* fall through... */ |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
203 case '%': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
204 n++; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
205 break; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
206 case 'd': case 'u': case 'i': case 'x': |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
207 (void) va_arg(count, int); |
|
21147
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
208 /* 20 bytes is enough to hold a 64-bit |
|
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
209 integer. Decimal takes the most space. |
|
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
210 This isn't enough for octal. */ |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
211 n += 20; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
212 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
213 case 's': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
214 s = va_arg(count, char*); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
215 n += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
216 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
217 case 'p': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
218 (void) va_arg(count, int); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
219 /* maximum 64-bit pointer representation: |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
220 * 0xffffffffffffffff |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
221 * so 19 characters is enough. |
|
21147
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
222 * XXX I count 18 -- what's the extra for? |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
223 */ |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
224 n += 19; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
225 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
226 default: |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
227 /* if we stumble upon an unknown |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
228 formatting code, copy the rest of |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
229 the format string to the output |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
230 string. (we cannot just skip the |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
231 code, since there's no way to know |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
232 what's in the argument list) */ |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
233 n += strlen(p); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
234 goto expand; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
235 } |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
236 } else |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
237 n++; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
238 } |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
239 expand: |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
240 /* step 2: fill the buffer */ |
|
21147
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
241 /* Since we've analyzed how much space we need for the worst case, |
|
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
242 use sprintf directly instead of the slower PyOS_snprintf. */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
243 string = PyBytes_FromStringAndSize(NULL, n); |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
244 if (!string) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
245 return NULL; |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
246 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
247 s = PyBytes_AsString(string); |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
248 |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
249 for (f = format; *f; f++) { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
250 if (*f == '%') { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
251 const char* p = f++; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
252 Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
253 int longflag = 0; |
|
36510
0f3894bb2ada
Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
254 int size_tflag = 0; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
255 /* parse the width.precision part (we're only |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
256 interested in the precision value, if any) */ |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
257 n = 0; |
|
43503
98f6180b9b33
Replace all (locale-dependent) uses of isupper(), tolower(), etc., by
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
258 while (ISDIGIT(*f)) |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
259 n = (n*10) + *f++ - '0'; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
260 if (*f == '.') { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
261 f++; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
262 n = 0; |
|
43503
98f6180b9b33
Replace all (locale-dependent) uses of isupper(), tolower(), etc., by
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
263 while (ISDIGIT(*f)) |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
264 n = (n*10) + *f++ - '0'; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
265 } |
|
43503
98f6180b9b33
Replace all (locale-dependent) uses of isupper(), tolower(), etc., by
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
266 while (*f && *f != '%' && !ISALPHA(*f)) |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
267 f++; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
268 /* handle the long flag, but only for %ld and %lu. |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
269 others can be added when necessary. */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
270 if (*f == 'l' && (f[1] == 'd' || f[1] == 'u')) { |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
271 longflag = 1; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
272 ++f; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
273 } |
|
36510
0f3894bb2ada
Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
274 /* handle the size_t flag. */ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
275 if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
|
36510
0f3894bb2ada
Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
276 size_tflag = 1; |
|
0f3894bb2ada
Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
277 ++f; |
|
0f3894bb2ada
Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
278 } |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
279 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
280 switch (*f) { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
281 case 'c': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
282 *s++ = va_arg(vargs, int); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
283 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
284 case 'd': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
285 if (longflag) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
286 sprintf(s, "%ld", va_arg(vargs, long)); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
287 else if (size_tflag) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
288 sprintf(s, "%" PY_FORMAT_SIZE_T "d", |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
289 va_arg(vargs, Py_ssize_t)); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
290 else |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
291 sprintf(s, "%d", va_arg(vargs, int)); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
292 s += strlen(s); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
293 break; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
294 case 'u': |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
295 if (longflag) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
296 sprintf(s, "%lu", |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
297 va_arg(vargs, unsigned long)); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
298 else if (size_tflag) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
299 sprintf(s, "%" PY_FORMAT_SIZE_T "u", |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
300 va_arg(vargs, size_t)); |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
301 else |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
302 sprintf(s, "%u", |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
303 va_arg(vargs, unsigned int)); |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
304 s += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
305 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
306 case 'i': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
307 sprintf(s, "%i", va_arg(vargs, int)); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
308 s += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
309 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
310 case 'x': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
311 sprintf(s, "%x", va_arg(vargs, int)); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
312 s += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
313 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
314 case 's': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
315 p = va_arg(vargs, char*); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
316 i = strlen(p); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
317 if (n > 0 && i > n) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
318 i = n; |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
319 Py_MEMCPY(s, p, i); |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
320 s += i; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
321 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
322 case 'p': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
323 sprintf(s, "%p", va_arg(vargs, void*)); |
|
19486
5c859d2cb37b
PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents:
19475
diff
changeset
|
324 /* %p is ill-defined: ensure leading 0x. */ |
|
5c859d2cb37b
PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents:
19475
diff
changeset
|
325 if (s[1] == 'X') |
|
5c859d2cb37b
PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents:
19475
diff
changeset
|
326 s[1] = 'x'; |
|
5c859d2cb37b
PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents:
19475
diff
changeset
|
327 else if (s[1] != 'x') { |
|
5c859d2cb37b
PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents:
19475
diff
changeset
|
328 memmove(s+2, s, strlen(s)+1); |
|
5c859d2cb37b
PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents:
19475
diff
changeset
|
329 s[0] = '0'; |
|
5c859d2cb37b
PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents:
19475
diff
changeset
|
330 s[1] = 'x'; |
|
5c859d2cb37b
PyString_FromFormatV: Massage platform %p output to match what gcc does,
Tim Peters <tim.peters@gmail.com>
parents:
19475
diff
changeset
|
331 } |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
332 s += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
333 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
334 case '%': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
335 *s++ = '%'; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
336 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
337 default: |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
338 strcpy(s, p); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
339 s += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
340 goto end; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
341 } |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
342 } else |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
343 *s++ = *f; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
344 } |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
345 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
346 end: |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
347 _PyBytes_Resize(&string, s - PyBytes_AS_STRING(string)); |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
348 return string; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
349 } |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
350 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
351 PyObject * |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
352 PyBytes_FromFormat(const char *format, ...) |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
353 { |
|
19489
3e74318a1b76
Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents:
19486
diff
changeset
|
354 PyObject* ret; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
355 va_list vargs; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
356 |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
357 #ifdef HAVE_STDARG_PROTOTYPES |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
358 va_start(vargs, format); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
359 #else |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
360 va_start(vargs); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
361 #endif |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
362 ret = PyBytes_FromFormatV(format, vargs); |
|
19489
3e74318a1b76
Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents:
19486
diff
changeset
|
363 va_end(vargs); |
|
3e74318a1b76
Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents:
19486
diff
changeset
|
364 return ret; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
365 } |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
366 |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
367 static void |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
368 bytes_dealloc(PyObject *op) |
| 750 | 369 { |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
370 Py_TYPE(op)->tp_free(op); |
| 750 | 371 } |
| 372 | |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
373 /* Unescape a backslash-escaped string. If unicode is non-zero, |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
374 the string is a u-literal. If recode_encoding is non-zero, |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
375 the string is UTF-8 encoded and should be re-encoded in the |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
376 specified encoding. */ |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
377 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
378 PyObject *PyBytes_DecodeEscape(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
379 Py_ssize_t len, |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
380 const char *errors, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
381 Py_ssize_t unicode, |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
382 const char *recode_encoding) |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
383 { |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
384 int c; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
385 char *p, *buf; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
386 const char *end; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
387 PyObject *v; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
388 Py_ssize_t newlen = recode_encoding ? 4*len:len; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
389 v = PyBytes_FromStringAndSize((char *)NULL, newlen); |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
390 if (v == NULL) |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
391 return NULL; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
392 p = buf = PyBytes_AsString(v); |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
393 end = s + len; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
394 while (s < end) { |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
395 if (*s != '\\') { |
|
25173
f4e9f09df4f2
Fix escaping of non-ASCII characters.
Martin v. Löwis <martin@v.loewis.de>
parents:
25100
diff
changeset
|
396 non_esc: |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
397 if (recode_encoding && (*s & 0x80)) { |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
398 PyObject *u, *w; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
399 char *r; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
400 const char* t; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
401 Py_ssize_t rn; |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
402 t = s; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
403 /* Decode non-ASCII bytes as UTF-8. */ |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
404 while (t < end && (*t & 0x80)) t++; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
405 u = PyUnicode_DecodeUTF8(s, t - s, errors); |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
406 if(!u) goto failed; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
407 |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
408 /* Recode them in target encoding. */ |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
409 w = PyUnicode_AsEncodedString( |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
410 u, recode_encoding, errors); |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
411 Py_DECREF(u); |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
412 if (!w) goto failed; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
413 |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
414 /* Append bytes to output buffer. */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
415 assert(PyBytes_Check(w)); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
416 r = PyBytes_AS_STRING(w); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
417 rn = PyBytes_GET_SIZE(w); |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
418 Py_MEMCPY(p, r, rn); |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
419 p += rn; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
420 Py_DECREF(w); |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
421 s = t; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
422 } else { |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
423 *p++ = *s++; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
424 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
425 continue; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
426 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
427 s++; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
428 if (s==end) { |
|
24809
c75cdc471b22
Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents:
24808
diff
changeset
|
429 PyErr_SetString(PyExc_ValueError, |
|
c75cdc471b22
Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents:
24808
diff
changeset
|
430 "Trailing \\ in string"); |
|
c75cdc471b22
Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents:
24808
diff
changeset
|
431 goto failed; |
|
c75cdc471b22
Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents:
24808
diff
changeset
|
432 } |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
433 switch (*s++) { |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
434 /* XXX This assumes ASCII! */ |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
435 case '\n': break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
436 case '\\': *p++ = '\\'; break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
437 case '\'': *p++ = '\''; break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
438 case '\"': *p++ = '\"'; break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
439 case 'b': *p++ = '\b'; break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
440 case 'f': *p++ = '\014'; break; /* FF */ |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
441 case 't': *p++ = '\t'; break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
442 case 'n': *p++ = '\n'; break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
443 case 'r': *p++ = '\r'; break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
444 case 'v': *p++ = '\013'; break; /* VT */ |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
445 case 'a': *p++ = '\007'; break; /* BEL, not classic C */ |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
446 case '0': case '1': case '2': case '3': |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
447 case '4': case '5': case '6': case '7': |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
448 c = s[-1] - '0'; |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
449 if (s < end && '0' <= *s && *s <= '7') { |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
450 c = (c<<3) + *s++ - '0'; |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
451 if (s < end && '0' <= *s && *s <= '7') |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
452 c = (c<<3) + *s++ - '0'; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
453 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
454 *p++ = c; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
455 break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
456 case 'x': |
|
43734
3e5edf19d5f0
Merged revisions 58221-58741 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43693
diff
changeset
|
457 if (s+1 < end && ISXDIGIT(s[0]) && ISXDIGIT(s[1])) { |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
458 unsigned int x = 0; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
459 c = Py_CHARMASK(*s); |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
460 s++; |
|
43503
98f6180b9b33
Replace all (locale-dependent) uses of isupper(), tolower(), etc., by
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
461 if (ISDIGIT(c)) |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
462 x = c - '0'; |
|
43503
98f6180b9b33
Replace all (locale-dependent) uses of isupper(), tolower(), etc., by
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
463 else if (ISLOWER(c)) |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
464 x = 10 + c - 'a'; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
465 else |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
466 x = 10 + c - 'A'; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
467 x = x << 4; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
468 c = Py_CHARMASK(*s); |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
469 s++; |
|
43503
98f6180b9b33
Replace all (locale-dependent) uses of isupper(), tolower(), etc., by
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
470 if (ISDIGIT(c)) |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
471 x += c - '0'; |
|
43503
98f6180b9b33
Replace all (locale-dependent) uses of isupper(), tolower(), etc., by
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
472 else if (ISLOWER(c)) |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
473 x += 10 + c - 'a'; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
474 else |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
475 x += 10 + c - 'A'; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
476 *p++ = x; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
477 break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
478 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
479 if (!errors || strcmp(errors, "strict") == 0) { |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
480 PyErr_SetString(PyExc_ValueError, |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
481 "invalid \\x escape"); |
|
24809
c75cdc471b22
Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents:
24808
diff
changeset
|
482 goto failed; |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
483 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
484 if (strcmp(errors, "replace") == 0) { |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
485 *p++ = '?'; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
486 } else if (strcmp(errors, "ignore") == 0) |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
487 /* do nothing */; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
488 else { |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
489 PyErr_Format(PyExc_ValueError, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
490 "decoding error; unknown " |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
491 "error handling code: %.400s", |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
492 errors); |
|
24809
c75cdc471b22
Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents:
24808
diff
changeset
|
493 goto failed; |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
494 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
495 default: |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
496 *p++ = '\\'; |
|
25173
f4e9f09df4f2
Fix escaping of non-ASCII characters.
Martin v. Löwis <martin@v.loewis.de>
parents:
25100
diff
changeset
|
497 s--; |
|
f4e9f09df4f2
Fix escaping of non-ASCII characters.
Martin v. Löwis <martin@v.loewis.de>
parents:
25100
diff
changeset
|
498 goto non_esc; /* an arbitry number of unescaped |
|
f4e9f09df4f2
Fix escaping of non-ASCII characters.
Martin v. Löwis <martin@v.loewis.de>
parents:
25100
diff
changeset
|
499 UTF-8 bytes may follow. */ |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
500 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
501 } |
|
25100
bc590beadf0b
Check whether a string resize is necessary at the end
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
502 if (p-buf < newlen) |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
503 _PyBytes_Resize(&v, p - buf); |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
504 return v; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
505 failed: |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
506 Py_DECREF(v); |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
507 return NULL; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
508 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
509 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
510 /* -------------------------------------------------------------------- */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
511 /* object api */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
512 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
513 Py_ssize_t |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
514 PyBytes_Size(register PyObject *op) |
| 6 | 515 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
516 if (!PyBytes_Check(op)) { |
|
44548
05bcb790ef63
Clean up PyString_Size().
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44351
diff
changeset
|
517 PyErr_Format(PyExc_TypeError, |
|
05bcb790ef63
Clean up PyString_Size().
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44351
diff
changeset
|
518 "expected bytes, %.200s found", Py_TYPE(op)->tp_name); |
|
05bcb790ef63
Clean up PyString_Size().
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44351
diff
changeset
|
519 return -1; |
|
05bcb790ef63
Clean up PyString_Size().
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44351
diff
changeset
|
520 } |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
521 return Py_SIZE(op); |
| 6 | 522 } |
| 523 | |
|
44548
05bcb790ef63
Clean up PyString_Size().
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
44351
diff
changeset
|
524 char * |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
525 PyBytes_AsString(register PyObject *op) |
| 6 | 526 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
527 if (!PyBytes_Check(op)) { |
|
43976
8744e41daf54
Just inline a function, and discover that it can only raise an exception.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43966
diff
changeset
|
528 PyErr_Format(PyExc_TypeError, |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
529 "expected bytes, %.200s found", Py_TYPE(op)->tp_name); |
|
43976
8744e41daf54
Just inline a function, and discover that it can only raise an exception.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43966
diff
changeset
|
530 return NULL; |
|
8744e41daf54
Just inline a function, and discover that it can only raise an exception.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43966
diff
changeset
|
531 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
532 return ((PyBytesObject *)op)->ob_sval; |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
533 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
534 |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
535 int |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
536 PyBytes_AsStringAndSize(register PyObject *obj, |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
537 register char **s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
538 register Py_ssize_t *len) |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
539 { |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
540 if (s == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
541 PyErr_BadInternalCall(); |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
542 return -1; |
| 6 | 543 } |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
544 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
545 if (!PyBytes_Check(obj)) { |
|
43966
d23829a9db21
Removed blocks from several functions in unicodeobject and stringobject where a PyString function was still checking for PyUnicode or the other way around.
Christian Heimes <christian@cheimes.de>
parents:
43962
diff
changeset
|
546 PyErr_Format(PyExc_TypeError, |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
547 "expected bytes, %.200s found", Py_TYPE(obj)->tp_name); |
|
43966
d23829a9db21
Removed blocks from several functions in unicodeobject and stringobject where a PyString function was still checking for PyUnicode or the other way around.
Christian Heimes <christian@cheimes.de>
parents:
43962
diff
changeset
|
548 return -1; |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
549 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
550 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
551 *s = PyBytes_AS_STRING(obj); |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
552 if (len != NULL) |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
553 *len = PyBytes_GET_SIZE(obj); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
554 else if (strlen(*s) != (size_t)PyBytes_GET_SIZE(obj)) { |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
555 PyErr_SetString(PyExc_TypeError, |
|
43976
8744e41daf54
Just inline a function, and discover that it can only raise an exception.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43966
diff
changeset
|
556 "expected bytes with no null"); |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
557 return -1; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
558 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
559 return 0; |
| 6 | 560 } |
| 561 | |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
562 /* -------------------------------------------------------------------- */ |
| 6 | 563 /* Methods */ |
| 564 | |
|
53187
b653eab13b73
The other half of Issue #1580: use short float repr where possible.
Eric Smith <eric@trueblade.com>
parents:
53094
diff
changeset
|
565 #include "stringlib/stringdefs.h" |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
566 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
567 #include "stringlib/fastsearch.h" |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
568 #include "stringlib/count.h" |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
569 #include "stringlib/find.h" |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
570 #include "stringlib/partition.h" |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
571 #include "stringlib/ctype.h" |
|
57515
f37f6b9b224b
Issue #5748: bytesobject.c should not have its own private defines for stringlib macros. Also removed unused defines and include for localutil.h.
Eric Smith <eric@trueblade.com>
parents:
56099
diff
changeset
|
572 |
|
f37f6b9b224b
Issue #5748: bytesobject.c should not have its own private defines for stringlib macros. Also removed unused defines and include for localutil.h.
Eric Smith <eric@trueblade.com>
parents:
56099
diff
changeset
|
573 #define STRINGLIB_MUTABLE 0 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
574 #include "stringlib/transmogrify.h" |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
575 |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
576 PyObject * |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
577 PyBytes_Repr(PyObject *obj, int smartquotes) |
| 6 | 578 { |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
579 static const char *hexdigits = "0123456789abcdef"; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
580 register PyBytesObject* op = (PyBytesObject*) obj; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
581 Py_ssize_t length = Py_SIZE(op); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
582 size_t newsize = 3 + 4 * length; |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
583 PyObject *v; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
584 if (newsize > PY_SSIZE_T_MAX || (newsize-3) / 4 != length) { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
585 PyErr_SetString(PyExc_OverflowError, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
586 "bytes object is too large to make repr"); |
|
43815
85cf334b32c3
Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43777
diff
changeset
|
587 return NULL; |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
588 } |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
589 v = PyUnicode_FromUnicode(NULL, newsize); |
| 6 | 590 if (v == NULL) { |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
591 return NULL; |
| 6 | 592 } |
| 593 else { | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
594 register Py_ssize_t i; |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
595 register Py_UNICODE c; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
596 register Py_UNICODE *p = PyUnicode_AS_UNICODE(v); |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
597 int quote; |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
598 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
599 /* Figure out which quote to use; single is preferred */ |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
600 quote = '\''; |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
601 if (smartquotes) { |
|
42143
0dc62647aff0
Fix a subtle bug in PyString_Repr().
Guido van Rossum <guido@python.org>
parents:
42095
diff
changeset
|
602 char *test, *start; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
603 start = PyBytes_AS_STRING(op); |
|
42143
0dc62647aff0
Fix a subtle bug in PyString_Repr().
Guido van Rossum <guido@python.org>
parents:
42095
diff
changeset
|
604 for (test = start; test < start+length; ++test) { |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
605 if (*test == '"') { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
606 quote = '\''; /* back to single */ |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
607 goto decided; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
608 } |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
609 else if (*test == '\'') |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
610 quote = '"'; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
611 } |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
612 decided: |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
613 ; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
614 } |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
615 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
616 *p++ = 'b', *p++ = quote; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
617 for (i = 0; i < length; i++) { |
|
21147
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
618 /* There's at least enough room for a hex escape |
|
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
619 and a closing quote. */ |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
620 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5); |
| 6 | 621 c = op->ob_sval[i]; |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
622 if (c == quote || c == '\\') |
| 6 | 623 *p++ = '\\', *p++ = c; |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
624 else if (c == '\t') |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
625 *p++ = '\\', *p++ = 't'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
626 else if (c == '\n') |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
627 *p++ = '\\', *p++ = 'n'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
628 else if (c == '\r') |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
629 *p++ = '\\', *p++ = 'r'; |
| 25721 | 630 else if (c < ' ' || c >= 0x7f) { |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
631 *p++ = '\\'; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
632 *p++ = 'x'; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
633 *p++ = hexdigits[(c & 0xf0) >> 4]; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
634 *p++ = hexdigits[c & 0xf]; |
| 25721 | 635 } |
| 636 else | |
|
25643
5c6fac95a94b
Patch #479898: Use multibyte C library for printing strings if available.
Martin v. Löwis <martin@v.loewis.de>
parents:
25233
diff
changeset
|
637 *p++ = c; |
| 6 | 638 } |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
639 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1); |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
640 *p++ = quote; |
| 6 | 641 *p = '\0'; |
|
41769
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
642 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:
41749
diff
changeset
|
643 Py_DECREF(v); |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
644 return NULL; |
|
b29d71d43c80
Add functions PyUnicode_Append() and PyUnicode_AppendAndDel() that mirror
Walter Dörwald <walter@livinglogic.de>
parents:
41749
diff
changeset
|
645 } |
| 27 | 646 return v; |
| 6 | 647 } |
| 648 } | |
| 649 | |
|
17645
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
650 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
651 bytes_repr(PyObject *op) |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
652 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
653 return PyBytes_Repr(op, 1); |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
654 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
655 |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
656 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
657 bytes_str(PyObject *op) |
|
17645
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
658 { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
659 if (Py_BytesWarningFlag) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
660 if (PyErr_WarnEx(PyExc_BytesWarning, |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
661 "str() on a bytes instance", 1)) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
662 return NULL; |
|
20464
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
663 } |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
664 return bytes_repr(op); |
|
17645
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
665 } |
|
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
666 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
667 static Py_ssize_t |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
668 bytes_length(PyBytesObject *a) |
| 6 | 669 { |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
670 return Py_SIZE(a); |
| 6 | 671 } |
| 672 | |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
673 /* This is also used by PyBytes_Concat() */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
674 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
675 bytes_concat(PyObject *a, PyObject *b) |
| 6 | 676 { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
677 Py_ssize_t size; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
678 Py_buffer va, vb; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
679 PyObject *result = NULL; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
680 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
681 va.len = -1; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
682 vb.len = -1; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
683 if (_getbuffer(a, &va) < 0 || |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
684 _getbuffer(b, &vb) < 0) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
685 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
686 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
687 goto done; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
688 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
689 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
690 /* Optimize end cases */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
691 if (va.len == 0 && PyBytes_CheckExact(b)) { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
692 result = b; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
693 Py_INCREF(result); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
694 goto done; |
| 6 | 695 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
696 if (vb.len == 0 && PyBytes_CheckExact(a)) { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
697 result = a; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
698 Py_INCREF(result); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
699 goto done; |
| 6 | 700 } |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
701 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
702 size = va.len + vb.len; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
703 if (size < 0) { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
704 PyErr_NoMemory(); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
705 goto done; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
706 } |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
707 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
708 result = PyBytes_FromStringAndSize(NULL, size); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
709 if (result != NULL) { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
710 memcpy(PyBytes_AS_STRING(result), va.buf, va.len); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
711 memcpy(PyBytes_AS_STRING(result) + va.len, vb.buf, vb.len); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
712 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
713 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
714 done: |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
715 if (va.len != -1) |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
716 PyBuffer_Release(&va); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
717 if (vb.len != -1) |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
718 PyBuffer_Release(&vb); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
719 return result; |
| 6 | 720 } |
| 721 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
722 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
723 bytes_repeat(register PyBytesObject *a, register Py_ssize_t n) |
| 6 | 724 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
725 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
726 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
727 register Py_ssize_t size; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
728 register PyBytesObject *op; |
|
14492
7728b0ea9344
Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
729 size_t nbytes; |
| 6 | 730 if (n < 0) |
| 731 n = 0; | |
|
14492
7728b0ea9344
Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
732 /* watch out for overflows: the size can overflow int, |
|
7728b0ea9344
Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
733 * and the # of bytes needed can overflow size_t |
|
7728b0ea9344
Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
734 */ |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
735 size = Py_SIZE(a) * n; |
|
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
736 if (n && size / n != Py_SIZE(a)) { |
|
14492
7728b0ea9344
Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
737 PyErr_SetString(PyExc_OverflowError, |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
738 "repeated bytes are too long"); |
|
14492
7728b0ea9344
Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
739 return NULL; |
|
7728b0ea9344
Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
740 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
741 if (size == Py_SIZE(a) && PyBytes_CheckExact(a)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
742 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
743 return (PyObject *)a; |
| 6 | 744 } |
|
32423
64534fb3afcd
sizeof(char) is 1, by definition, so get rid of that expression in
Tim Peters <tim.peters@gmail.com>
parents:
32305
diff
changeset
|
745 nbytes = (size_t)size; |
|
50044
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
746 if (nbytes + PyBytesObject_SIZE <= nbytes) { |
|
14492
7728b0ea9344
Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
747 PyErr_SetString(PyExc_OverflowError, |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
748 "repeated bytes are too long"); |
|
14492
7728b0ea9344
Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
749 return NULL; |
|
7728b0ea9344
Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
750 } |
|
50044
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
751 op = (PyBytesObject *)PyObject_MALLOC(PyBytesObject_SIZE + nbytes); |
| 27 | 752 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
753 return PyErr_NoMemory(); |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
754 PyObject_INIT_VAR(op, &PyBytes_Type, size); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
755 op->ob_shash = -1; |
|
27123
1b881d5e4a90
GvR's idea to use memset() for the most common special case of repeating
Raymond Hettinger <python@rcn.com>
parents:
27101
diff
changeset
|
756 op->ob_sval[size] = '\0'; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
757 if (Py_SIZE(a) == 1 && n > 0) { |
|
27123
1b881d5e4a90
GvR's idea to use memset() for the most common special case of repeating
Raymond Hettinger <python@rcn.com>
parents:
27101
diff
changeset
|
758 memset(op->ob_sval, a->ob_sval[0] , n); |
|
1b881d5e4a90
GvR's idea to use memset() for the most common special case of repeating
Raymond Hettinger <python@rcn.com>
parents:
27101
diff
changeset
|
759 return (PyObject *) op; |
|
1b881d5e4a90
GvR's idea to use memset() for the most common special case of repeating
Raymond Hettinger <python@rcn.com>
parents:
27101
diff
changeset
|
760 } |
|
27101
6d7daa5329c6
Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents:
26825
diff
changeset
|
761 i = 0; |
|
6d7daa5329c6
Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents:
26825
diff
changeset
|
762 if (i < size) { |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
763 Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a)); |
|
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
764 i = Py_SIZE(a); |
|
27101
6d7daa5329c6
Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents:
26825
diff
changeset
|
765 } |
|
6d7daa5329c6
Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents:
26825
diff
changeset
|
766 while (i < size) { |
|
6d7daa5329c6
Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents:
26825
diff
changeset
|
767 j = (i <= size-i) ? i : size-i; |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
768 Py_MEMCPY(op->ob_sval+i, op->ob_sval, j); |
|
27101
6d7daa5329c6
Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents:
26825
diff
changeset
|
769 i += j; |
|
6d7daa5329c6
Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents:
26825
diff
changeset
|
770 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
771 return (PyObject *) op; |
| 6 | 772 } |
| 773 | |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
774 static int |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
775 bytes_contains(PyObject *self, PyObject *arg) |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
776 { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
777 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
778 if (ival == -1 && PyErr_Occurred()) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
779 Py_buffer varg; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
780 int pos; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
781 PyErr_Clear(); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
782 if (_getbuffer(arg, &varg) < 0) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
783 return -1; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
784 pos = stringlib_find(PyBytes_AS_STRING(self), Py_SIZE(self), |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
785 varg.buf, varg.len, 0); |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
786 PyBuffer_Release(&varg); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
787 return pos >= 0; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
788 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
789 if (ival < 0 || ival >= 256) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
790 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
791 return -1; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
792 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
793 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
794 return memchr(PyBytes_AS_STRING(self), ival, Py_SIZE(self)) != NULL; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
795 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
796 |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
797 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
798 bytes_item(PyBytesObject *a, register Py_ssize_t i) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
799 { |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
800 if (i < 0 || i >= Py_SIZE(a)) { |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
801 PyErr_SetString(PyExc_IndexError, "index out of range"); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
802 return NULL; |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
803 } |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
44017
diff
changeset
|
804 return PyLong_FromLong((unsigned char)a->ob_sval[i]); |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
805 } |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
806 |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
807 static PyObject* |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
808 bytes_richcompare(PyBytesObject *a, PyBytesObject *b, int op) |
| 6 | 809 { |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
810 int c; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
811 Py_ssize_t len_a, len_b; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
812 Py_ssize_t min_len; |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
813 PyObject *result; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
814 |
|
20140
225f17ec8932
Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents:
20057
diff
changeset
|
815 /* Make sure both arguments are strings. */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
816 if (!(PyBytes_Check(a) && PyBytes_Check(b))) { |
|
49538
645fe56c117a
STINNER Victor (haypo)'s patch for bug 3988, Byte warning mode and b'' != ''
Barry Warsaw <barry@python.org>
parents:
49387
diff
changeset
|
817 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE) && |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
818 (PyObject_IsInstance((PyObject*)a, |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
819 (PyObject*)&PyUnicode_Type) || |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
820 PyObject_IsInstance((PyObject*)b, |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
821 (PyObject*)&PyUnicode_Type))) { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
822 if (PyErr_WarnEx(PyExc_BytesWarning, |
| 47461 | 823 "Comparison between bytes and string", 1)) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
824 return NULL; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
825 } |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
826 result = Py_NotImplemented; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
827 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
828 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
829 if (a == b) { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
830 switch (op) { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
831 case Py_EQ:case Py_LE:case Py_GE: |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
832 result = Py_True; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
833 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
834 case Py_NE:case Py_LT:case Py_GT: |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
835 result = Py_False; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
836 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
837 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
838 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
839 if (op == Py_EQ) { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
840 /* Supporting Py_NE here as well does not save |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
841 much time, since Py_NE is rarely used. */ |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
842 if (Py_SIZE(a) == Py_SIZE(b) |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
843 && (a->ob_sval[0] == b->ob_sval[0] |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
844 && memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0)) { |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
845 result = Py_True; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
846 } else { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
847 result = Py_False; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
848 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
849 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
850 } |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
851 len_a = Py_SIZE(a); len_b = Py_SIZE(b); |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
852 min_len = (len_a < len_b) ? len_a : len_b; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
853 if (min_len > 0) { |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
854 c = Py_CHARMASK(*a->ob_sval) - Py_CHARMASK(*b->ob_sval); |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
855 if (c==0) |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
856 c = memcmp(a->ob_sval, b->ob_sval, min_len); |
|
40909
f2f0b41f270d
Merged revisions 53875-53911 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40626
diff
changeset
|
857 } else |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
858 c = 0; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
859 if (c == 0) |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
860 c = (len_a < len_b) ? -1 : (len_a > len_b) ? 1 : 0; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
861 switch (op) { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
862 case Py_LT: c = c < 0; break; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
863 case Py_LE: c = c <= 0; break; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
864 case Py_EQ: assert(0); break; /* unreachable */ |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
865 case Py_NE: c = c != 0; break; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
866 case Py_GT: c = c > 0; break; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
867 case Py_GE: c = c >= 0; break; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
868 default: |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
869 result = Py_NotImplemented; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
870 goto out; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
871 } |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
872 result = c ? Py_True : Py_False; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
873 out: |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
874 Py_INCREF(result); |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
875 return result; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
876 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
877 |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
878 static long |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
879 bytes_hash(PyBytesObject *a) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
880 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
881 register Py_ssize_t len; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
882 register unsigned char *p; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
883 register long x; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
884 |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
885 if (a->ob_shash != -1) |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
886 return a->ob_shash; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
887 len = Py_SIZE(a); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
888 p = (unsigned char *) a->ob_sval; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
889 x = *p << 7; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
890 while (--len >= 0) |
|
4283
839f72610ae1
Multiply by 1000003 instead of 3 in string hach
Guido van Rossum <guido@python.org>
parents:
3586
diff
changeset
|
891 x = (1000003*x) ^ *p++; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
892 x ^= Py_SIZE(a); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
893 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
894 x = -2; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
895 a->ob_shash = x; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
896 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
897 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
898 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
899 static PyObject* |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
900 bytes_subscript(PyBytesObject* self, PyObject* item) |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
901 { |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39544
diff
changeset
|
902 if (PyIndex_Check(item)) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39544
diff
changeset
|
903 Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
904 if (i == -1 && PyErr_Occurred()) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
905 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
906 if (i < 0) |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
907 i += PyBytes_GET_SIZE(self); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
908 if (i < 0 || i >= PyBytes_GET_SIZE(self)) { |
|
43604
b18f6092f6e0
Patch 1280, by Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
909 PyErr_SetString(PyExc_IndexError, |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
910 "index out of range"); |
|
43604
b18f6092f6e0
Patch 1280, by Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
911 return NULL; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
912 } |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
44017
diff
changeset
|
913 return PyLong_FromLong((unsigned char)self->ob_sval[i]); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
914 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
915 else if (PySlice_Check(item)) { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
916 Py_ssize_t start, stop, step, slicelength, cur, i; |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
917 char* source_buf; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
918 char* result_buf; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
919 PyObject* result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
920 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
921 if (PySlice_GetIndicesEx((PySliceObject*)item, |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
922 PyBytes_GET_SIZE(self), |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
923 &start, &stop, &step, &slicelength) < 0) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
924 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
925 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
926 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
927 if (slicelength <= 0) { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
928 return PyBytes_FromStringAndSize("", 0); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
929 } |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
42665
diff
changeset
|
930 else if (start == 0 && step == 1 && |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
931 slicelength == PyBytes_GET_SIZE(self) && |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
932 PyBytes_CheckExact(self)) { |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
42665
diff
changeset
|
933 Py_INCREF(self); |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
42665
diff
changeset
|
934 return (PyObject *)self; |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
42665
diff
changeset
|
935 } |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
42665
diff
changeset
|
936 else if (step == 1) { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
937 return PyBytes_FromStringAndSize( |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
938 PyBytes_AS_STRING(self) + start, |
|
42954
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
42665
diff
changeset
|
939 slicelength); |
|
27f0289b6835
Merge the trunk changes in. Breaks socket.ssl for now.
Thomas Wouters <thomas@python.org>
parents:
42665
diff
changeset
|
940 } |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
941 else { |
|
52768
643f44ad387f
Optimize slicing of bytes and bytearray by avoiding useless copying.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
51270
diff
changeset
|
942 source_buf = PyBytes_AS_STRING(self); |
|
643f44ad387f
Optimize slicing of bytes and bytearray by avoiding useless copying.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
51270
diff
changeset
|
943 result = PyBytes_FromStringAndSize(NULL, slicelength); |
|
643f44ad387f
Optimize slicing of bytes and bytearray by avoiding useless copying.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
51270
diff
changeset
|
944 if (result == NULL) |
|
643f44ad387f
Optimize slicing of bytes and bytearray by avoiding useless copying.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
51270
diff
changeset
|
945 return NULL; |
|
643f44ad387f
Optimize slicing of bytes and bytearray by avoiding useless copying.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
51270
diff
changeset
|
946 |
|
643f44ad387f
Optimize slicing of bytes and bytearray by avoiding useless copying.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
51270
diff
changeset
|
947 result_buf = PyBytes_AS_STRING(result); |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
948 for (cur = start, i = 0; i < slicelength; |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
949 cur += step, i++) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
950 result_buf[i] = source_buf[cur]; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
951 } |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
952 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
953 return result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
954 } |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
955 } |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
956 else { |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
957 PyErr_Format(PyExc_TypeError, |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
958 "byte indices must be integers, not %.200s", |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
959 Py_TYPE(item)->tp_name); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
960 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
961 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
962 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
963 |
|
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:
42594
diff
changeset
|
964 static int |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
965 bytes_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags) |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
966 { |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
967 return PyBuffer_FillInfo(view, (PyObject*)self, (void *)self->ob_sval, Py_SIZE(self), |
|
48623
bf50085fbcd3
#2538: bytes objects can only provide read-only buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
47496
diff
changeset
|
968 1, flags); |
|
9305
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
969 } |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
970 |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
971 static PySequenceMethods bytes_as_sequence = { |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
972 (lenfunc)bytes_length, /*sq_length*/ |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
973 (binaryfunc)bytes_concat, /*sq_concat*/ |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
974 (ssizeargfunc)bytes_repeat, /*sq_repeat*/ |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
975 (ssizeargfunc)bytes_item, /*sq_item*/ |
|
43100
4e6b4ba60c3b
Remove the simple slicing API. All slicing is now done with slice objects.
Thomas Wouters <thomas@python.org>
parents:
42954
diff
changeset
|
976 0, /*sq_slice*/ |
|
259
6321d3dc57e0
Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents:
180
diff
changeset
|
977 0, /*sq_ass_item*/ |
|
6321d3dc57e0
Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents:
180
diff
changeset
|
978 0, /*sq_ass_slice*/ |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
979 (objobjproc)bytes_contains /*sq_contains*/ |
| 6 | 980 }; |
| 981 | |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
982 static PyMappingMethods bytes_as_mapping = { |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
983 (lenfunc)bytes_length, |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
984 (binaryfunc)bytes_subscript, |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
985 0, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
986 }; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
987 |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
988 static PyBufferProcs bytes_as_buffer = { |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
989 (getbufferproc)bytes_buffer_getbuffer, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
990 NULL, |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
991 }; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
992 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
993 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
994 #define LEFTSTRIP 0 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
995 #define RIGHTSTRIP 1 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
996 #define BOTHSTRIP 2 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
997 |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
998 /* Arrays indexed by above */ |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
999 static const char *stripformat[] = {"|O:lstrip", "|O:rstrip", "|O:strip"}; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1000 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1001 #define STRIPNAME(i) (stripformat[i]+3) |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1002 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1003 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1004 /* Don't call if length < 2 */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1005 #define Py_STRING_MATCH(target, offset, pattern, length) \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1006 (target[offset] == pattern[0] && \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1007 target[offset+length-1] == pattern[length-1] && \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1008 !memcmp(target+offset+1, pattern+1, length-2) ) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1009 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1010 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1011 /* Overallocate the initial list to reduce the number of reallocs for small |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1012 split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1013 resizes, to sizes 4, 8, then 16. Most observed string splits are for human |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1014 text (roughly 11 words per line) and field delimited data (usually 1-10 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1015 fields). For large strings the split algorithms are bandwidth limited |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1016 so increasing the preallocation likely will not improve things.*/ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1017 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1018 #define MAX_PREALLOC 12 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1019 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1020 /* 5 splits gives 6 elements */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1021 #define PREALLOC_SIZE(maxsplit) \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1022 (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1023 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1024 #define SPLIT_ADD(data, left, right) { \ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1025 str = PyBytes_FromStringAndSize((data) + (left), \ |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1026 (right) - (left)); \ |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1027 if (str == NULL) \ |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1028 goto onError; \ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1029 if (count < MAX_PREALLOC) { \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1030 PyList_SET_ITEM(list, count, str); \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1031 } else { \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1032 if (PyList_Append(list, str)) { \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1033 Py_DECREF(str); \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1034 goto onError; \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1035 } \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1036 else \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1037 Py_DECREF(str); \ |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1038 } \ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1039 count++; } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1040 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1041 /* Always force the list to the expected size. */ |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
1042 #define FIX_PREALLOC_SIZE(list) Py_SIZE(list) = count |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1043 |
|
43503
98f6180b9b33
Replace all (locale-dependent) uses of isupper(), tolower(), etc., by
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
1044 #define SKIP_SPACE(s, i, len) { while (i<len && ISSPACE(s[i])) i++; } |
|
98f6180b9b33
Replace all (locale-dependent) uses of isupper(), tolower(), etc., by
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
1045 #define SKIP_NONSPACE(s, i, len) { while (i<len && !ISSPACE(s[i])) i++; } |
|
98f6180b9b33
Replace all (locale-dependent) uses of isupper(), tolower(), etc., by
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
1046 #define RSKIP_SPACE(s, i) { while (i>=0 && ISSPACE(s[i])) i--; } |
|
98f6180b9b33
Replace all (locale-dependent) uses of isupper(), tolower(), etc., by
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
1047 #define RSKIP_NONSPACE(s, i) { while (i>=0 && !ISSPACE(s[i])) i--; } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1048 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1049 Py_LOCAL_INLINE(PyObject *) |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1050 split_whitespace(PyBytesObject *self, Py_ssize_t len, Py_ssize_t maxsplit) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1051 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1052 const char *s = PyBytes_AS_STRING(self); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1053 Py_ssize_t i, j, count=0; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1054 PyObject *str; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1055 PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit)); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1056 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1057 if (list == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1058 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1059 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1060 i = j = 0; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1061 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1062 while (maxsplit-- > 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1063 SKIP_SPACE(s, i, len); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1064 if (i==len) break; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1065 j = i; i++; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1066 SKIP_NONSPACE(s, i, len); |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1067 if (j == 0 && i == len && PyBytes_CheckExact(self)) { |
|
44230
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1068 /* No whitespace in self, so just use it as list[0] */ |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1069 Py_INCREF(self); |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1070 PyList_SET_ITEM(list, 0, (PyObject *)self); |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1071 count++; |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1072 break; |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1073 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1074 SPLIT_ADD(s, j, i); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1075 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1076 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1077 if (i < len) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1078 /* Only occurs when maxsplit was reached */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1079 /* Skip any remaining whitespace and copy to end of string */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1080 SKIP_SPACE(s, i, len); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1081 if (i != len) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1082 SPLIT_ADD(s, i, len); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1083 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1084 FIX_PREALLOC_SIZE(list); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1085 return list; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1086 onError: |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1087 Py_DECREF(list); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1088 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1089 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1090 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1091 Py_LOCAL_INLINE(PyObject *) |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1092 split_char(PyBytesObject *self, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1093 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1094 const char *s = PyBytes_AS_STRING(self); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1095 register Py_ssize_t i, j, count=0; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1096 PyObject *str; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1097 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1098 |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1099 if (list == NULL) |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1100 return NULL; |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1101 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1102 i = j = 0; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1103 while ((j < len) && (maxcount-- > 0)) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1104 for(; j<len; j++) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1105 /* I found that using memchr makes no difference */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1106 if (s[j] == ch) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1107 SPLIT_ADD(s, i, j); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1108 i = j = j + 1; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1109 break; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1110 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1111 } |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1112 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1113 if (i == 0 && count == 0 && PyBytes_CheckExact(self)) { |
|
44230
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1114 /* ch not in self, so just use self as list[0] */ |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1115 Py_INCREF(self); |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1116 PyList_SET_ITEM(list, 0, (PyObject *)self); |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1117 count++; |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1118 } |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1119 else if (i <= len) { |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1120 SPLIT_ADD(s, i, len); |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1121 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1122 FIX_PREALLOC_SIZE(list); |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1123 return list; |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1124 |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1125 onError: |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1126 Py_DECREF(list); |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1127 return NULL; |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1128 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1129 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1130 PyDoc_STRVAR(split__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1131 "B.split([sep[, maxsplit]]) -> list of bytes\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1132 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1133 Return a list of the sections in B, using sep as the delimiter.\n\ |
|
47067
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46909
diff
changeset
|
1134 If sep is not specified or is None, B is split on ASCII whitespace\n\ |
|
960ade856078
Merged revisions 62998-63003,63005-63006,63009-63012,63014-63017,63019-63020,63022-63024,63026-63029,63031-63041,63043-63045,63047-63054,63056-63062 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
46909
diff
changeset
|
1135 characters (space, tab, return, newline, formfeed, vertical tab).\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1136 If maxsplit is given, at most maxsplit splits are done."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1137 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1138 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1139 bytes_split(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1140 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1141 Py_ssize_t len = PyBytes_GET_SIZE(self), n, i, j; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1142 Py_ssize_t maxsplit = -1, count=0; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1143 const char *s = PyBytes_AS_STRING(self), *sub; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1144 Py_buffer vsub; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1145 PyObject *list, *str, *subobj = Py_None; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1146 #ifdef USE_FAST |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1147 Py_ssize_t pos; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1148 #endif |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1149 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
1150 if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit)) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1151 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1152 if (maxsplit < 0) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
1153 maxsplit = PY_SSIZE_T_MAX; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1154 if (subobj == Py_None) |
|
44230
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1155 return split_whitespace(self, len, maxsplit); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1156 if (_getbuffer(subobj, &vsub) < 0) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1157 return NULL; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1158 sub = vsub.buf; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1159 n = vsub.len; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1160 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1161 if (n == 0) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1162 PyErr_SetString(PyExc_ValueError, "empty separator"); |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
1163 PyBuffer_Release(&vsub); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1164 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1165 } |
|
48833
cfd79a4c1604
#3650: fix a reference leak in bytes.split('x')
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48683
diff
changeset
|
1166 else if (n == 1) { |
|
cfd79a4c1604
#3650: fix a reference leak in bytes.split('x')
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48683
diff
changeset
|
1167 list = split_char(self, len, sub[0], maxsplit); |
|
cfd79a4c1604
#3650: fix a reference leak in bytes.split('x')
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48683
diff
changeset
|
1168 PyBuffer_Release(&vsub); |
|
cfd79a4c1604
#3650: fix a reference leak in bytes.split('x')
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48683
diff
changeset
|
1169 return list; |
|
cfd79a4c1604
#3650: fix a reference leak in bytes.split('x')
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48683
diff
changeset
|
1170 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1171 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1172 list = PyList_New(PREALLOC_SIZE(maxsplit)); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1173 if (list == NULL) { |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
1174 PyBuffer_Release(&vsub); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1175 return NULL; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1176 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1177 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1178 #ifdef USE_FAST |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1179 i = j = 0; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1180 while (maxsplit-- > 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1181 pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1182 if (pos < 0) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1183 break; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1184 j = i+pos; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1185 SPLIT_ADD(s, i, j); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1186 i = j + n; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1187 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1188 #else |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1189 i = j = 0; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1190 while ((j+n <= len) && (maxsplit-- > 0)) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1191 for (; j+n <= len; j++) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1192 if (Py_STRING_MATCH(s, j, sub, n)) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1193 SPLIT_ADD(s, i, j); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1194 i = j = j + n; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1195 break; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1196 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1197 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1198 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1199 #endif |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1200 SPLIT_ADD(s, i, len); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1201 FIX_PREALLOC_SIZE(list); |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
1202 PyBuffer_Release(&vsub); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1203 return list; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1204 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1205 onError: |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1206 Py_DECREF(list); |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
1207 PyBuffer_Release(&vsub); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1208 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1209 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1210 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1211 PyDoc_STRVAR(partition__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1212 "B.partition(sep) -> (head, sep, tail)\n\ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1213 \n\ |
|
47389
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
1214 Search for the separator sep in B, and return the part before it,\n\ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1215 the separator itself, and the part after it. If the separator is not\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1216 found, returns B and two empty bytes objects."); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1217 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1218 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1219 bytes_partition(PyBytesObject *self, PyObject *sep_obj) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1220 { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1221 const char *sep; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1222 Py_ssize_t sep_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1223 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1224 if (PyBytes_Check(sep_obj)) { |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1225 sep = PyBytes_AS_STRING(sep_obj); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1226 sep_len = PyBytes_GET_SIZE(sep_obj); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1227 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1228 else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1229 return NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1230 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1231 return stringlib_partition( |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1232 (PyObject*) self, |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1233 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1234 sep_obj, sep, sep_len |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1235 ); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1236 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1237 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1238 PyDoc_STRVAR(rpartition__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1239 "B.rpartition(sep) -> (tail, sep, head)\n\ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1240 \n\ |
|
47389
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
1241 Search for the separator sep in B, starting at the end of B,\n\ |
|
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
1242 and return the part before it, the separator itself, and the\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1243 part after it. If the separator is not found, returns two empty\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1244 bytes objects and B."); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1245 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1246 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1247 bytes_rpartition(PyBytesObject *self, PyObject *sep_obj) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1248 { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1249 const char *sep; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1250 Py_ssize_t sep_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1251 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1252 if (PyBytes_Check(sep_obj)) { |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1253 sep = PyBytes_AS_STRING(sep_obj); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1254 sep_len = PyBytes_GET_SIZE(sep_obj); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1255 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1256 else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1257 return NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1258 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1259 return stringlib_rpartition( |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1260 (PyObject*) self, |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1261 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1262 sep_obj, sep, sep_len |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1263 ); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1264 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1265 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1266 Py_LOCAL_INLINE(PyObject *) |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1267 rsplit_whitespace(PyBytesObject *self, Py_ssize_t len, Py_ssize_t maxsplit) |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1268 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1269 const char *s = PyBytes_AS_STRING(self); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1270 Py_ssize_t i, j, count=0; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1271 PyObject *str; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1272 PyObject *list = PyList_New(PREALLOC_SIZE(maxsplit)); |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1273 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1274 if (list == NULL) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1275 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1276 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1277 i = j = len-1; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
1278 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1279 while (maxsplit-- > 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1280 RSKIP_SPACE(s, i); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1281 if (i<0) break; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1282 j = i; i--; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1283 RSKIP_NONSPACE(s, i); |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1284 if (j == len-1 && i < 0 && PyBytes_CheckExact(self)) { |
|
44230
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1285 /* No whitespace in self, so just use it as list[0] */ |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1286 Py_INCREF(self); |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1287 PyList_SET_ITEM(list, 0, (PyObject *)self); |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1288 count++; |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1289 break; |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1290 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1291 SPLIT_ADD(s, i + 1, j + 1); |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1292 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1293 if (i >= 0) { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1294 /* Only occurs when maxsplit was reached. Skip any remaining |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1295 whitespace and copy to beginning of string. */ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1296 RSKIP_SPACE(s, i); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1297 if (i >= 0) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1298 SPLIT_ADD(s, 0, i + 1); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1299 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1300 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1301 FIX_PREALLOC_SIZE(list); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1302 if (PyList_Reverse(list) < 0) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1303 goto onError; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1304 return list; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1305 onError: |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1306 Py_DECREF(list); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1307 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1308 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1309 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1310 Py_LOCAL_INLINE(PyObject *) |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1311 rsplit_char(PyBytesObject *self, Py_ssize_t len, char ch, Py_ssize_t maxcount) |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1312 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1313 const char *s = PyBytes_AS_STRING(self); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1314 register Py_ssize_t i, j, count=0; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1315 PyObject *str; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1316 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1317 |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1318 if (list == NULL) |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1319 return NULL; |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1320 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1321 i = j = len - 1; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1322 while ((i >= 0) && (maxcount-- > 0)) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1323 for (; i >= 0; i--) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1324 if (s[i] == ch) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1325 SPLIT_ADD(s, i + 1, j + 1); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1326 j = i = i - 1; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1327 break; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1328 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1329 } |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1330 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1331 if (i < 0 && count == 0 && PyBytes_CheckExact(self)) { |
|
44230
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1332 /* ch not in self, so just use self as list[0] */ |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1333 Py_INCREF(self); |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1334 PyList_SET_ITEM(list, 0, (PyObject *)self); |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1335 count++; |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1336 } |
|
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1337 else if (j >= -1) { |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1338 SPLIT_ADD(s, 0, j + 1); |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1339 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1340 FIX_PREALLOC_SIZE(list); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1341 if (PyList_Reverse(list) < 0) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1342 goto onError; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1343 return list; |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1344 |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1345 onError: |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1346 Py_DECREF(list); |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1347 return NULL; |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1348 } |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1349 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1350 PyDoc_STRVAR(rsplit__doc__, |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
1351 "B.rsplit([sep[, maxsplit]]) -> list of bytes\n\ |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1352 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1353 Return a list of the sections in B, using sep as the delimiter,\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1354 starting at the end of B and working to the front.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1355 If sep is not given, B is split on ASCII whitespace characters\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1356 (space, tab, return, newline, formfeed, vertical tab).\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1357 If maxsplit is given, at most maxsplit splits are done."); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1358 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1359 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1360 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1361 bytes_rsplit(PyBytesObject *self, PyObject *args) |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1362 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1363 Py_ssize_t len = PyBytes_GET_SIZE(self), n, i, j; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1364 Py_ssize_t maxsplit = -1, count=0; |
|
44230
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1365 const char *s, *sub; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1366 Py_buffer vsub; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1367 PyObject *list, *str, *subobj = Py_None; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1368 |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
1369 if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit)) |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1370 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1371 if (maxsplit < 0) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
1372 maxsplit = PY_SSIZE_T_MAX; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1373 if (subobj == Py_None) |
|
44230
6b517ac10e8e
Merged revisions 59407-59422 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
1374 return rsplit_whitespace(self, len, maxsplit); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1375 if (_getbuffer(subobj, &vsub) < 0) |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1376 return NULL; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1377 sub = vsub.buf; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1378 n = vsub.len; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1379 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1380 if (n == 0) { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1381 PyErr_SetString(PyExc_ValueError, "empty separator"); |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
1382 PyBuffer_Release(&vsub); |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1383 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1384 } |
|
48833
cfd79a4c1604
#3650: fix a reference leak in bytes.split('x')
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48683
diff
changeset
|
1385 else if (n == 1) { |
|
cfd79a4c1604
#3650: fix a reference leak in bytes.split('x')
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48683
diff
changeset
|
1386 list = rsplit_char(self, len, sub[0], maxsplit); |
|
cfd79a4c1604
#3650: fix a reference leak in bytes.split('x')
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48683
diff
changeset
|
1387 PyBuffer_Release(&vsub); |
|
cfd79a4c1604
#3650: fix a reference leak in bytes.split('x')
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48683
diff
changeset
|
1388 return list; |
|
cfd79a4c1604
#3650: fix a reference leak in bytes.split('x')
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48683
diff
changeset
|
1389 } |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1390 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1391 list = PyList_New(PREALLOC_SIZE(maxsplit)); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1392 if (list == NULL) { |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
1393 PyBuffer_Release(&vsub); |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1394 return NULL; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1395 } |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1396 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1397 j = len; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1398 i = j - n; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1399 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1400 s = PyBytes_AS_STRING(self); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1401 while ( (i >= 0) && (maxsplit-- > 0) ) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1402 for (; i>=0; i--) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1403 if (Py_STRING_MATCH(s, i, sub, n)) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1404 SPLIT_ADD(s, i + n, j); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1405 j = i; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1406 i -= n; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1407 break; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1408 } |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1409 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1410 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1411 SPLIT_ADD(s, 0, j); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1412 FIX_PREALLOC_SIZE(list); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1413 if (PyList_Reverse(list) < 0) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1414 goto onError; |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
1415 PyBuffer_Release(&vsub); |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1416 return list; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1417 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1418 onError: |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1419 Py_DECREF(list); |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
1420 PyBuffer_Release(&vsub); |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1421 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1422 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1423 |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
1424 #undef SPLIT_ADD |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
1425 #undef MAX_PREALLOC |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
1426 #undef PREALLOC_SIZE |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
1427 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1428 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1429 PyDoc_STRVAR(join__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1430 "B.join(iterable_of_bytes) -> bytes\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1431 \n\ |
|
47389
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
1432 Concatenate any number of bytes objects, with B in between each pair.\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1433 Example: b'.'.join([b'ab', b'pq', b'rs']) -> b'ab.pq.rs'."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1434 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1435 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1436 bytes_join(PyObject *self, PyObject *orig) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1437 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1438 char *sep = PyBytes_AS_STRING(self); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1439 const Py_ssize_t seplen = PyBytes_GET_SIZE(self); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1440 PyObject *res = NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1441 char *p; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1442 Py_ssize_t seqlen = 0; |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1443 size_t sz = 0; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1444 Py_ssize_t i; |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
1445 PyObject *seq, *item; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1446 |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1447 seq = PySequence_Fast(orig, ""); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1448 if (seq == NULL) { |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1449 return NULL; |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1450 } |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1451 |
|
13576
61e4c29d53d8
replace PyXXX_Length calls with PyXXX_Size calls
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13561
diff
changeset
|
1452 seqlen = PySequence_Size(seq); |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1453 if (seqlen == 0) { |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1454 Py_DECREF(seq); |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1455 return PyBytes_FromString(""); |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1456 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1457 if (seqlen == 1) { |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1458 item = PySequence_Fast_GET_ITEM(seq, 0); |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1459 if (PyBytes_CheckExact(item)) { |
|
33246
03440272028c
SF Patch #1007087: Return new string for single subclass joins (Bug #1001011)
Raymond Hettinger <python@rcn.com>
parents:
33030
diff
changeset
|
1460 Py_INCREF(item); |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1461 Py_DECREF(seq); |
|
33246
03440272028c
SF Patch #1007087: Return new string for single subclass joins (Bug #1001011)
Raymond Hettinger <python@rcn.com>
parents:
33030
diff
changeset
|
1462 return item; |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1463 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1464 } |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1465 |
|
33246
03440272028c
SF Patch #1007087: Return new string for single subclass joins (Bug #1001011)
Raymond Hettinger <python@rcn.com>
parents:
33030
diff
changeset
|
1466 /* There are at least two things to join, or else we have a subclass |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
1467 * of the builtin types in the sequence. |
|
33246
03440272028c
SF Patch #1007087: Return new string for single subclass joins (Bug #1001011)
Raymond Hettinger <python@rcn.com>
parents:
33030
diff
changeset
|
1468 * Do a pre-pass to figure out the total amount of space we'll |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1469 * need (sz), and see whether all argument are bytes. |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1470 */ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1471 /* XXX Shouldn't we use _getbuffer() on these items instead? */ |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1472 for (i = 0; i < seqlen; i++) { |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1473 const size_t old_sz = sz; |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1474 item = PySequence_Fast_GET_ITEM(seq, i); |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1475 if (!PyBytes_Check(item) && !PyByteArray_Check(item)) { |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1476 PyErr_Format(PyExc_TypeError, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1477 "sequence item %zd: expected bytes," |
|
13544
3cf4f5aee00e
small updates to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13528
diff
changeset
|
1478 " %.80s found", |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
1479 i, Py_TYPE(item)->tp_name); |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1480 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1481 return NULL; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1482 } |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
1483 sz += Py_SIZE(item); |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1484 if (i != 0) |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1485 sz += seplen; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
1486 if (sz < old_sz || sz > PY_SSIZE_T_MAX) { |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1487 PyErr_SetString(PyExc_OverflowError, |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
1488 "join() result is too long for bytes"); |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1489 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1490 return NULL; |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1491 } |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1492 } |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1493 |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1494 /* Allocate result space. */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1495 res = PyBytes_FromStringAndSize((char*)NULL, sz); |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1496 if (res == NULL) { |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1497 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1498 return NULL; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1499 } |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1500 |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1501 /* Catenate everything. */ |
|
47300
96b34263e448
Renamed PyBytes to PyByteArray
Christian Heimes <christian@cheimes.de>
parents:
47067
diff
changeset
|
1502 /* I'm not worried about a PyByteArray item growing because there's |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1503 nowhere in this function where we release the GIL. */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1504 p = PyBytes_AS_STRING(res); |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1505 for (i = 0; i < seqlen; ++i) { |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1506 size_t n; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1507 char *q; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1508 if (i) { |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1509 Py_MEMCPY(p, sep, seplen); |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1510 p += seplen; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1511 } |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1512 item = PySequence_Fast_GET_ITEM(seq, i); |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
1513 n = Py_SIZE(item); |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1514 if (PyBytes_Check(item)) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1515 q = PyBytes_AS_STRING(item); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1516 else |
|
47300
96b34263e448
Renamed PyBytes to PyByteArray
Christian Heimes <christian@cheimes.de>
parents:
47067
diff
changeset
|
1517 q = PyByteArray_AS_STRING(item); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1518 Py_MEMCPY(p, q, n); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1519 p += n; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1520 } |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1521 |
|
13526
3686d12362b8
fix two refcount bugs in new string_join implementation:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13524
diff
changeset
|
1522 Py_DECREF(seq); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1523 return res; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1524 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1525 |
|
18130
8eea2efb34cd
Reformat decl of new _PyString_Join. Add NEWS blurb about repr() speedup.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
1526 PyObject * |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1527 _PyBytes_Join(PyObject *sep, PyObject *x) |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1528 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1529 assert(sep != NULL && PyBytes_Check(sep)); |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1530 assert(x != NULL); |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1531 return bytes_join(sep, x); |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1532 } |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1533 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1534 Py_LOCAL_INLINE(void) |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1535 bytes_adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len) |
|
23850
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1536 { |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1537 if (*end > len) |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1538 *end = len; |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1539 else if (*end < 0) |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1540 *end += len; |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1541 if (*end < 0) |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1542 *end = 0; |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1543 if (*start < 0) |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1544 *start += len; |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1545 if (*start < 0) |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1546 *start = 0; |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1547 } |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1548 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1549 Py_LOCAL_INLINE(Py_ssize_t) |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1550 bytes_find_internal(PyBytesObject *self, PyObject *args, int dir) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1551 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1552 PyObject *subobj; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1553 const char *sub; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1554 Py_ssize_t sub_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1555 Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43815
diff
changeset
|
1556 PyObject *obj_start=Py_None, *obj_end=Py_None; |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43815
diff
changeset
|
1557 |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43815
diff
changeset
|
1558 if (!PyArg_ParseTuple(args, "O|OO:find/rfind/index/rindex", &subobj, |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43815
diff
changeset
|
1559 &obj_start, &obj_end)) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1560 return -2; |
|
43897
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43815
diff
changeset
|
1561 /* To support None in "start" and "end" arguments, meaning |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43815
diff
changeset
|
1562 the same as if they were not passed. |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43815
diff
changeset
|
1563 */ |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43815
diff
changeset
|
1564 if (obj_start != Py_None) |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43815
diff
changeset
|
1565 if (!_PyEval_SliceIndex(obj_start, &start)) |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43815
diff
changeset
|
1566 return -2; |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43815
diff
changeset
|
1567 if (obj_end != Py_None) |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43815
diff
changeset
|
1568 if (!_PyEval_SliceIndex(obj_end, &end)) |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43815
diff
changeset
|
1569 return -2; |
|
41f7c13ef161
Merged revisions 59005-59040 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43815
diff
changeset
|
1570 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1571 if (PyBytes_Check(subobj)) { |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1572 sub = PyBytes_AS_STRING(subobj); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1573 sub_len = PyBytes_GET_SIZE(subobj); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1574 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1575 else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len)) |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1576 /* XXX - the "expected a character buffer object" is pretty |
|
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
1577 confusing for a non-expert. remap to something else ? */ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1578 return -2; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1579 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1580 if (dir > 0) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1581 return stringlib_find_slice( |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1582 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1583 sub, sub_len, start, end); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1584 else |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1585 return stringlib_rfind_slice( |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1586 PyBytes_AS_STRING(self), PyBytes_GET_SIZE(self), |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1587 sub, sub_len, start, end); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1588 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1589 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1590 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1591 PyDoc_STRVAR(find__doc__, |
|
47389
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
1592 "B.find(sub[, start[, end]]) -> int\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1593 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1594 Return the lowest index in S where substring sub is found,\n\ |
|
42466
3fc3cedc49ca
Merged revisions 56753-56781 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42335
diff
changeset
|
1595 such that sub is contained within s[start:end]. Optional\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1596 arguments start and end are interpreted as in slice notation.\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1597 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1598 Return -1 on failure."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1599 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1600 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1601 bytes_find(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1602 { |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1603 Py_ssize_t result = bytes_find_internal(self, args, +1); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1604 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1605 return NULL; |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
44017
diff
changeset
|
1606 return PyLong_FromSsize_t(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1607 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1608 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1609 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1610 PyDoc_STRVAR(index__doc__, |
|
47389
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
1611 "B.index(sub[, start[, end]]) -> int\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1612 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1613 Like B.find() but raise ValueError when the substring is not found."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1614 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1615 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1616 bytes_index(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1617 { |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1618 Py_ssize_t result = bytes_find_internal(self, args, +1); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1619 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1620 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1621 if (result == -1) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1622 PyErr_SetString(PyExc_ValueError, |
|
27248
c4150f7f6a69
SF patch #664192 bug #661913: inconsistent error messages between string
Raymond Hettinger <python@rcn.com>
parents:
27123
diff
changeset
|
1623 "substring not found"); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1624 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1625 } |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
44017
diff
changeset
|
1626 return PyLong_FromSsize_t(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1627 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1628 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1629 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1630 PyDoc_STRVAR(rfind__doc__, |
|
47389
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
1631 "B.rfind(sub[, start[, end]]) -> int\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1632 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1633 Return the highest index in B where substring sub is found,\n\ |
|
42466
3fc3cedc49ca
Merged revisions 56753-56781 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42335
diff
changeset
|
1634 such that sub is contained within s[start:end]. Optional\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1635 arguments start and end are interpreted as in slice notation.\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1636 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1637 Return -1 on failure."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1638 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1639 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1640 bytes_rfind(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1641 { |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1642 Py_ssize_t result = bytes_find_internal(self, args, -1); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1643 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1644 return NULL; |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
44017
diff
changeset
|
1645 return PyLong_FromSsize_t(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1646 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1647 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1648 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1649 PyDoc_STRVAR(rindex__doc__, |
|
47389
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
1650 "B.rindex(sub[, start[, end]]) -> int\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1651 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1652 Like B.rfind() but raise ValueError when the substring is not found."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1653 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1654 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1655 bytes_rindex(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1656 { |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1657 Py_ssize_t result = bytes_find_internal(self, args, -1); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1658 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1659 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1660 if (result == -1) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1661 PyErr_SetString(PyExc_ValueError, |
|
27248
c4150f7f6a69
SF patch #664192 bug #661913: inconsistent error messages between string
Raymond Hettinger <python@rcn.com>
parents:
27123
diff
changeset
|
1662 "substring not found"); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1663 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1664 } |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
44017
diff
changeset
|
1665 return PyLong_FromSsize_t(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1666 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1667 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1668 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1669 Py_LOCAL_INLINE(PyObject *) |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1670 do_xstrip(PyBytesObject *self, int striptype, PyObject *sepobj) |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1671 { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1672 Py_buffer vsep; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1673 char *s = PyBytes_AS_STRING(self); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1674 Py_ssize_t len = PyBytes_GET_SIZE(self); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1675 char *sep; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1676 Py_ssize_t seplen; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1677 Py_ssize_t i, j; |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1678 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1679 if (_getbuffer(sepobj, &vsep) < 0) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1680 return NULL; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1681 sep = vsep.buf; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1682 seplen = vsep.len; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1683 |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1684 i = 0; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1685 if (striptype != RIGHTSTRIP) { |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1686 while (i < len && memchr(sep, Py_CHARMASK(s[i]), seplen)) { |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1687 i++; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1688 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1689 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1690 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1691 j = len; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1692 if (striptype != LEFTSTRIP) { |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1693 do { |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1694 j--; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1695 } while (j >= i && memchr(sep, Py_CHARMASK(s[j]), seplen)); |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1696 j++; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1697 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1698 |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
1699 PyBuffer_Release(&vsep); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1700 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1701 if (i == 0 && j == len && PyBytes_CheckExact(self)) { |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1702 Py_INCREF(self); |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1703 return (PyObject*)self; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1704 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1705 else |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1706 return PyBytes_FromStringAndSize(s+i, j-i); |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1707 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1708 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1709 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1710 Py_LOCAL_INLINE(PyObject *) |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1711 do_strip(PyBytesObject *self, int striptype) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1712 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1713 char *s = PyBytes_AS_STRING(self); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1714 Py_ssize_t len = PyBytes_GET_SIZE(self), i, j; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1715 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1716 i = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1717 if (striptype != RIGHTSTRIP) { |
|
43503
98f6180b9b33
Replace all (locale-dependent) uses of isupper(), tolower(), etc., by
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
1718 while (i < len && ISSPACE(s[i])) { |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1719 i++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1720 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1721 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1722 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1723 j = len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1724 if (striptype != LEFTSTRIP) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1725 do { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1726 j--; |
|
43503
98f6180b9b33
Replace all (locale-dependent) uses of isupper(), tolower(), etc., by
Guido van Rossum <guido@python.org>
parents:
43423
diff
changeset
|
1727 } while (j >= i && ISSPACE(s[j])); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1728 j++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1729 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1730 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1731 if (i == 0 && j == len && PyBytes_CheckExact(self)) { |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1732 Py_INCREF(self); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1733 return (PyObject*)self; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1734 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1735 else |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1736 return PyBytes_FromStringAndSize(s+i, j-i); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1737 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1738 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1739 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1740 Py_LOCAL_INLINE(PyObject *) |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1741 do_argstrip(PyBytesObject *self, int striptype, PyObject *args) |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1742 { |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1743 PyObject *sep = NULL; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1744 |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1745 if (!PyArg_ParseTuple(args, (char *)stripformat[striptype], &sep)) |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1746 return NULL; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1747 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1748 if (sep != NULL && sep != Py_None) { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1749 return do_xstrip(self, striptype, sep); |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1750 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1751 return do_strip(self, striptype); |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1752 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1753 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1754 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1755 PyDoc_STRVAR(strip__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1756 "B.strip([bytes]) -> bytes\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1757 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1758 Strip leading and trailing bytes contained in the argument.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1759 If the argument is omitted, strip trailing ASCII whitespace."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1760 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1761 bytes_strip(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1762 { |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1763 if (PyTuple_GET_SIZE(args) == 0) |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1764 return do_strip(self, BOTHSTRIP); /* Common case */ |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1765 else |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1766 return do_argstrip(self, BOTHSTRIP, args); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1767 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1768 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1769 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1770 PyDoc_STRVAR(lstrip__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1771 "B.lstrip([bytes]) -> bytes\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1772 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1773 Strip leading bytes contained in the argument.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1774 If the argument is omitted, strip leading ASCII whitespace."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1775 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1776 bytes_lstrip(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1777 { |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1778 if (PyTuple_GET_SIZE(args) == 0) |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1779 return do_strip(self, LEFTSTRIP); /* Common case */ |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1780 else |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1781 return do_argstrip(self, LEFTSTRIP, args); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1782 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1783 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1784 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1785 PyDoc_STRVAR(rstrip__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1786 "B.rstrip([bytes]) -> bytes\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1787 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1788 Strip trailing bytes contained in the argument.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1789 If the argument is omitted, strip trailing ASCII whitespace."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1790 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1791 bytes_rstrip(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1792 { |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1793 if (PyTuple_GET_SIZE(args) == 0) |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1794 return do_strip(self, RIGHTSTRIP); /* Common case */ |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1795 else |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1796 return do_argstrip(self, RIGHTSTRIP, args); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1797 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1798 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1799 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1800 PyDoc_STRVAR(count__doc__, |
|
47389
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
1801 "B.count(sub[, start[, end]]) -> int\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1802 \n\ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1803 Return the number of non-overlapping occurrences of substring sub in\n\ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1804 string S[start:end]. Optional arguments start and end are interpreted\n\ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1805 as in slice notation."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1806 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1807 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1808 bytes_count(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1809 { |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1810 PyObject *sub_obj; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1811 const char *str = PyBytes_AS_STRING(self), *sub; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1812 Py_ssize_t sub_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1813 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1814 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1815 if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1816 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1817 return NULL; |
| 12615 | 1818 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1819 if (PyBytes_Check(sub_obj)) { |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1820 sub = PyBytes_AS_STRING(sub_obj); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1821 sub_len = PyBytes_GET_SIZE(sub_obj); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1822 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1823 else if (PyObject_AsCharBuffer(sub_obj, &sub, &sub_len)) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1824 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1825 |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1826 bytes_adjust_indices(&start, &end, PyBytes_GET_SIZE(self)); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1827 |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
44017
diff
changeset
|
1828 return PyLong_FromSsize_t( |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1829 stringlib_count(str + start, end - start, sub, sub_len) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1830 ); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1831 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1832 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1833 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1834 PyDoc_STRVAR(translate__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1835 "B.translate(table[, deletechars]) -> bytes\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1836 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1837 Return a copy of B, where all characters occurring in the\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1838 optional argument deletechars are removed, and the remaining\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1839 characters have been mapped through the given translation\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
1840 table, which must be a bytes object of length 256."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1841 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1842 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1843 bytes_translate(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1844 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1845 register char *input, *output; |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
1846 const char *table; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1847 register Py_ssize_t i, c, changed = 0; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1848 PyObject *input_obj = (PyObject*)self; |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
1849 const char *output_start, *del_table=NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1850 Py_ssize_t inlen, tablen, dellen = 0; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1851 PyObject *result; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1852 int trans_table[256]; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1853 PyObject *tableobj, *delobj = NULL; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1854 |
|
26818
e094a03a80ff
SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents:
26625
diff
changeset
|
1855 if (!PyArg_UnpackTuple(args, "translate", 1, 2, |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1856 &tableobj, &delobj)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1857 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1858 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1859 if (PyBytes_Check(tableobj)) { |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1860 table = PyBytes_AS_STRING(tableobj); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1861 tablen = PyBytes_GET_SIZE(tableobj); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1862 } |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
1863 else if (tableobj == Py_None) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
1864 table = NULL; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
1865 tablen = 256; |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
1866 } |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
1867 else if (PyObject_AsCharBuffer(tableobj, &table, &tablen)) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1868 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1869 |
|
26625
ba1578be5706
Patch #650653: Raise always value error if the table is not 256 bytes long.
Martin v. Löwis <martin@v.loewis.de>
parents:
26550
diff
changeset
|
1870 if (tablen != 256) { |
|
ba1578be5706
Patch #650653: Raise always value error if the table is not 256 bytes long.
Martin v. Löwis <martin@v.loewis.de>
parents:
26550
diff
changeset
|
1871 PyErr_SetString(PyExc_ValueError, |
|
ba1578be5706
Patch #650653: Raise always value error if the table is not 256 bytes long.
Martin v. Löwis <martin@v.loewis.de>
parents:
26550
diff
changeset
|
1872 "translation table must be 256 characters long"); |
|
ba1578be5706
Patch #650653: Raise always value error if the table is not 256 bytes long.
Martin v. Löwis <martin@v.loewis.de>
parents:
26550
diff
changeset
|
1873 return NULL; |
|
ba1578be5706
Patch #650653: Raise always value error if the table is not 256 bytes long.
Martin v. Löwis <martin@v.loewis.de>
parents:
26550
diff
changeset
|
1874 } |
|
ba1578be5706
Patch #650653: Raise always value error if the table is not 256 bytes long.
Martin v. Löwis <martin@v.loewis.de>
parents:
26550
diff
changeset
|
1875 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1876 if (delobj != NULL) { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1877 if (PyBytes_Check(delobj)) { |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1878 del_table = PyBytes_AS_STRING(delobj); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1879 dellen = PyBytes_GET_SIZE(delobj); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1880 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1881 else if (PyObject_AsCharBuffer(delobj, &del_table, &dellen)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1882 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1883 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1884 else { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1885 del_table = NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1886 dellen = 0; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1887 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1888 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1889 inlen = PyBytes_GET_SIZE(input_obj); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1890 result = PyBytes_FromStringAndSize((char *)NULL, inlen); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1891 if (result == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1892 return NULL; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1893 output_start = output = PyBytes_AsString(result); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1894 input = PyBytes_AS_STRING(input_obj); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1895 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
1896 if (dellen == 0 && table != NULL) { |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1897 /* If no deletions are required, use faster code */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1898 for (i = inlen; --i >= 0; ) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1899 c = Py_CHARMASK(*input++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1900 if (Py_CHARMASK((*output++ = table[c])) != c) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1901 changed = 1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1902 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1903 if (changed || !PyBytes_CheckExact(input_obj)) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1904 return result; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1905 Py_DECREF(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1906 Py_INCREF(input_obj); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1907 return input_obj; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1908 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1909 |
|
41554
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
1910 if (table == NULL) { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
1911 for (i = 0; i < 256; i++) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
1912 trans_table[i] = Py_CHARMASK(i); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
1913 } else { |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
1914 for (i = 0; i < 256; i++) |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
1915 trans_table[i] = Py_CHARMASK(table[i]); |
|
06145fbc7ab9
Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
41450
diff
changeset
|
1916 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1917 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1918 for (i = 0; i < dellen; i++) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1919 trans_table[(int) Py_CHARMASK(del_table[i])] = -1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1920 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1921 for (i = inlen; --i >= 0; ) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1922 c = Py_CHARMASK(*input++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1923 if (trans_table[c] != -1) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1924 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1925 continue; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1926 changed = 1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1927 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1928 if (!changed && PyBytes_CheckExact(input_obj)) { |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1929 Py_DECREF(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1930 Py_INCREF(input_obj); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1931 return input_obj; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1932 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1933 /* Fix the size of the resulting string */ |
|
23295
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
1934 if (inlen > 0) |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1935 _PyBytes_Resize(&result, output - output_start); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1936 return result; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1937 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1938 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1939 |
|
53094
4407e4d07084
Add bytes/bytearray.maketrans() to mirror str.maketrans(), and deprecate
Georg Brandl <georg@python.org>
parents:
52773
diff
changeset
|
1940 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
1941 bytes_maketrans(PyObject *null, PyObject *args) |
|
53094
4407e4d07084
Add bytes/bytearray.maketrans() to mirror str.maketrans(), and deprecate
Georg Brandl <georg@python.org>
parents:
52773
diff
changeset
|
1942 { |
|
4407e4d07084
Add bytes/bytearray.maketrans() to mirror str.maketrans(), and deprecate
Georg Brandl <georg@python.org>
parents:
52773
diff
changeset
|
1943 return _Py_bytes_maketrans(args); |
|
4407e4d07084
Add bytes/bytearray.maketrans() to mirror str.maketrans(), and deprecate
Georg Brandl <georg@python.org>
parents:
52773
diff
changeset
|
1944 } |
|
4407e4d07084
Add bytes/bytearray.maketrans() to mirror str.maketrans(), and deprecate
Georg Brandl <georg@python.org>
parents:
52773
diff
changeset
|
1945 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1946 #define FORWARD 1 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1947 #define REVERSE -1 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1948 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1949 /* find and count characters and substrings */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1950 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1951 #define findchar(target, target_len, c) \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1952 ((char *)memchr((const void *)(target), c, target_len)) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1953 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1954 /* String ops must return a string. */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1955 /* If the object is subclass of string, create a copy */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1956 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1957 return_self(PyBytesObject *self) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1958 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1959 if (PyBytes_CheckExact(self)) { |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1960 Py_INCREF(self); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1961 return self; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1962 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1963 return (PyBytesObject *)PyBytes_FromStringAndSize( |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1964 PyBytes_AS_STRING(self), |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
1965 PyBytes_GET_SIZE(self)); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1966 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1967 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1968 Py_LOCAL_INLINE(Py_ssize_t) |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
1969 countchar(const char *target, int target_len, char c, Py_ssize_t maxcount) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1970 { |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1971 Py_ssize_t count=0; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
1972 const char *start=target; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
1973 const char *end=target+target_len; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1974 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1975 while ( (start=findchar(start, end-start, c)) != NULL ) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1976 count++; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1977 if (count >= maxcount) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1978 break; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1979 start += 1; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1980 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1981 return count; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1982 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1983 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1984 Py_LOCAL(Py_ssize_t) |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
1985 findstring(const char *target, Py_ssize_t target_len, |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
1986 const char *pattern, Py_ssize_t pattern_len, |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1987 Py_ssize_t start, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1988 Py_ssize_t end, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1989 int direction) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1990 { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1991 if (start < 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1992 start += target_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1993 if (start < 0) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1994 start = 0; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1995 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1996 if (end > target_len) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1997 end = target_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1998 } else if (end < 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
1999 end += target_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2000 if (end < 0) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2001 end = 0; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2002 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2003 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2004 /* zero-length substrings always match at the first attempt */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2005 if (pattern_len == 0) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2006 return (direction > 0) ? start : end; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2007 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2008 end -= pattern_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2009 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2010 if (direction < 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2011 for (; end >= start; end--) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2012 if (Py_STRING_MATCH(target, end, pattern, pattern_len)) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2013 return end; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2014 } else { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2015 for (; start <= end; start++) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2016 if (Py_STRING_MATCH(target, start,pattern,pattern_len)) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2017 return start; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2018 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2019 return -1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2020 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2021 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2022 Py_LOCAL_INLINE(Py_ssize_t) |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2023 countstring(const char *target, Py_ssize_t target_len, |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2024 const char *pattern, Py_ssize_t pattern_len, |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2025 Py_ssize_t start, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2026 Py_ssize_t end, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2027 int direction, Py_ssize_t maxcount) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2028 { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2029 Py_ssize_t count=0; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2030 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2031 if (start < 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2032 start += target_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2033 if (start < 0) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2034 start = 0; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2035 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2036 if (end > target_len) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2037 end = target_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2038 } else if (end < 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2039 end += target_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2040 if (end < 0) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2041 end = 0; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2042 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2043 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2044 /* zero-length substrings match everywhere */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2045 if (pattern_len == 0 || maxcount == 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2046 if (target_len+1 < maxcount) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2047 return target_len+1; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2048 return maxcount; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2049 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2050 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2051 end -= pattern_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2052 if (direction < 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2053 for (; (end >= start); end--) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2054 if (Py_STRING_MATCH(target, end,pattern,pattern_len)) { |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2055 count++; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2056 if (--maxcount <= 0) break; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2057 end -= pattern_len-1; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2058 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2059 } else { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2060 for (; (start <= end); start++) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2061 if (Py_STRING_MATCH(target, start, |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2062 pattern, pattern_len)) { |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2063 count++; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2064 if (--maxcount <= 0) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2065 break; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2066 start += pattern_len-1; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2067 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2068 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2069 return count; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2070 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2071 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2072 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2073 /* Algorithms for different cases of string replacement */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2074 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2075 /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2076 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2077 replace_interleave(PyBytesObject *self, |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2078 const char *to_s, Py_ssize_t to_len, |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2079 Py_ssize_t maxcount) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2080 { |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2081 char *self_s, *result_s; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2082 Py_ssize_t self_len, result_len; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2083 Py_ssize_t count, i, product; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2084 PyBytesObject *result; |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2085 |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2086 self_len = PyBytes_GET_SIZE(self); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2087 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2088 /* 1 at the end plus 1 after every character */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2089 count = self_len+1; |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2090 if (maxcount < count) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2091 count = maxcount; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2092 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2093 /* Check for overflow */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2094 /* result_len = count * to_len + self_len; */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2095 product = count * to_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2096 if (product / to_len != count) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2097 PyErr_SetString(PyExc_OverflowError, |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
2098 "replacement bytes are too long"); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2099 return NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2100 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2101 result_len = product + self_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2102 if (result_len < 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2103 PyErr_SetString(PyExc_OverflowError, |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
2104 "replacement bytes are too long"); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2105 return NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2106 } |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2107 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2108 if (! (result = (PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2109 PyBytes_FromStringAndSize(NULL, result_len)) ) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2110 return NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2111 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2112 self_s = PyBytes_AS_STRING(self); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2113 result_s = PyBytes_AS_STRING(result); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2114 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2115 /* TODO: special case single character, which doesn't need memcpy */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2116 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2117 /* Lay the first one down (guaranteed this will occur) */ |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2118 Py_MEMCPY(result_s, to_s, to_len); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2119 result_s += to_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2120 count -= 1; |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2121 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2122 for (i=0; i<count; i++) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2123 *result_s++ = *self_s++; |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2124 Py_MEMCPY(result_s, to_s, to_len); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2125 result_s += to_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2126 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2127 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2128 /* Copy the rest of the original string */ |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2129 Py_MEMCPY(result_s, self_s, self_len-i); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2130 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2131 return result; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2132 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2133 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2134 /* Special case for deleting a single character */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2135 /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2136 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2137 replace_delete_single_character(PyBytesObject *self, |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2138 char from_c, Py_ssize_t maxcount) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2139 { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2140 char *self_s, *result_s; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2141 char *start, *next, *end; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2142 Py_ssize_t self_len, result_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2143 Py_ssize_t count; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2144 PyBytesObject *result; |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2145 |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2146 self_len = PyBytes_GET_SIZE(self); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2147 self_s = PyBytes_AS_STRING(self); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2148 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2149 count = countchar(self_s, self_len, from_c, maxcount); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2150 if (count == 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2151 return return_self(self); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2152 } |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2153 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2154 result_len = self_len - count; /* from_len == 1 */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2155 assert(result_len>=0); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2156 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2157 if ( (result = (PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2158 PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2159 return NULL; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2160 result_s = PyBytes_AS_STRING(result); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2161 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2162 start = self_s; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2163 end = self_s + self_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2164 while (count-- > 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2165 next = findchar(start, end-start, from_c); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2166 if (next == NULL) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2167 break; |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2168 Py_MEMCPY(result_s, start, next-start); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2169 result_s += (next-start); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2170 start = next+1; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2171 } |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2172 Py_MEMCPY(result_s, start, end-start); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2173 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2174 return result; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2175 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2176 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2177 /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2178 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2179 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2180 replace_delete_substring(PyBytesObject *self, |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2181 const char *from_s, Py_ssize_t from_len, |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2182 Py_ssize_t maxcount) { |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2183 char *self_s, *result_s; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2184 char *start, *next, *end; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2185 Py_ssize_t self_len, result_len; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2186 Py_ssize_t count, offset; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2187 PyBytesObject *result; |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2188 |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2189 self_len = PyBytes_GET_SIZE(self); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2190 self_s = PyBytes_AS_STRING(self); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2191 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2192 count = countstring(self_s, self_len, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2193 from_s, from_len, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2194 0, self_len, 1, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2195 maxcount); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2196 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2197 if (count == 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2198 /* no matches */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2199 return return_self(self); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2200 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2201 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2202 result_len = self_len - (count * from_len); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2203 assert (result_len>=0); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2204 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2205 if ( (result = (PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2206 PyBytes_FromStringAndSize(NULL, result_len)) == NULL ) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2207 return NULL; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2208 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2209 result_s = PyBytes_AS_STRING(result); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2210 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2211 start = self_s; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2212 end = self_s + self_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2213 while (count-- > 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2214 offset = findstring(start, end-start, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2215 from_s, from_len, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2216 0, end-start, FORWARD); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2217 if (offset == -1) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2218 break; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2219 next = start + offset; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2220 |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2221 Py_MEMCPY(result_s, start, next-start); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2222 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2223 result_s += (next-start); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2224 start = next+from_len; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2225 } |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2226 Py_MEMCPY(result_s, start, end-start); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2227 return result; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2228 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2229 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2230 /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2231 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2232 replace_single_character_in_place(PyBytesObject *self, |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2233 char from_c, char to_c, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2234 Py_ssize_t maxcount) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2235 { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2236 char *self_s, *result_s, *start, *end, *next; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2237 Py_ssize_t self_len; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2238 PyBytesObject *result; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2239 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2240 /* The result string will be the same size */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2241 self_s = PyBytes_AS_STRING(self); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2242 self_len = PyBytes_GET_SIZE(self); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2243 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2244 next = findchar(self_s, self_len, from_c); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2245 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2246 if (next == NULL) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2247 /* No matches; return the original string */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2248 return return_self(self); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2249 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2250 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2251 /* Need to make a new string */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2252 result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2253 if (result == NULL) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2254 return NULL; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2255 result_s = PyBytes_AS_STRING(result); |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2256 Py_MEMCPY(result_s, self_s, self_len); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2257 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2258 /* change everything in-place, starting with this one */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2259 start = result_s + (next-self_s); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2260 *start = to_c; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2261 start++; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2262 end = result_s + self_len; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2263 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2264 while (--maxcount > 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2265 next = findchar(start, end-start, from_c); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2266 if (next == NULL) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2267 break; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2268 *next = to_c; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2269 start = next+1; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2270 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2271 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2272 return result; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2273 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2274 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2275 /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2276 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2277 replace_substring_in_place(PyBytesObject *self, |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2278 const char *from_s, Py_ssize_t from_len, |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2279 const char *to_s, Py_ssize_t to_len, |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2280 Py_ssize_t maxcount) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2281 { |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2282 char *result_s, *start, *end; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2283 char *self_s; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2284 Py_ssize_t self_len, offset; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2285 PyBytesObject *result; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2286 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2287 /* The result string will be the same size */ |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2288 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2289 self_s = PyBytes_AS_STRING(self); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2290 self_len = PyBytes_GET_SIZE(self); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2291 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2292 offset = findstring(self_s, self_len, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2293 from_s, from_len, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2294 0, self_len, FORWARD); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2295 if (offset == -1) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2296 /* No matches; return the original string */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2297 return return_self(self); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2298 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2299 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2300 /* Need to make a new string */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2301 result = (PyBytesObject *) PyBytes_FromStringAndSize(NULL, self_len); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2302 if (result == NULL) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2303 return NULL; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2304 result_s = PyBytes_AS_STRING(result); |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2305 Py_MEMCPY(result_s, self_s, self_len); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2306 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2307 /* change everything in-place, starting with this one */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2308 start = result_s + offset; |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2309 Py_MEMCPY(start, to_s, from_len); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2310 start += from_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2311 end = result_s + self_len; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2312 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2313 while ( --maxcount > 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2314 offset = findstring(start, end-start, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2315 from_s, from_len, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2316 0, end-start, FORWARD); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2317 if (offset==-1) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2318 break; |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2319 Py_MEMCPY(start+offset, to_s, from_len); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2320 start += offset+from_len; |
|
17750
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
2321 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2322 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2323 return result; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2324 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2325 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2326 /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2327 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2328 replace_single_character(PyBytesObject *self, |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2329 char from_c, |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2330 const char *to_s, Py_ssize_t to_len, |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2331 Py_ssize_t maxcount) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2332 { |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2333 char *self_s, *result_s; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2334 char *start, *next, *end; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2335 Py_ssize_t self_len, result_len; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2336 Py_ssize_t count, product; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2337 PyBytesObject *result; |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2338 |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2339 self_s = PyBytes_AS_STRING(self); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2340 self_len = PyBytes_GET_SIZE(self); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2341 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2342 count = countchar(self_s, self_len, from_c, maxcount); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2343 if (count == 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2344 /* no matches, return unchanged */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2345 return return_self(self); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2346 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2347 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2348 /* use the difference between current and new, hence the "-1" */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2349 /* result_len = self_len + count * (to_len-1) */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2350 product = count * (to_len-1); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2351 if (product / (to_len-1) != count) { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2352 PyErr_SetString(PyExc_OverflowError, |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
2353 "replacement bytes are too long"); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2354 return NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2355 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2356 result_len = self_len + product; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2357 if (result_len < 0) { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2358 PyErr_SetString(PyExc_OverflowError, |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
2359 "replacment bytes are too long"); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2360 return NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2361 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2362 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2363 if ( (result = (PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2364 PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2365 return NULL; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2366 result_s = PyBytes_AS_STRING(result); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2367 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2368 start = self_s; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2369 end = self_s + self_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2370 while (count-- > 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2371 next = findchar(start, end-start, from_c); |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2372 if (next == NULL) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2373 break; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2374 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2375 if (next == start) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2376 /* replace with the 'to' */ |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2377 Py_MEMCPY(result_s, to_s, to_len); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2378 result_s += to_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2379 start += 1; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2380 } else { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2381 /* copy the unchanged old then the 'to' */ |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2382 Py_MEMCPY(result_s, start, next-start); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2383 result_s += (next-start); |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2384 Py_MEMCPY(result_s, to_s, to_len); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2385 result_s += to_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2386 start = next+1; |
|
25004
762dd09edb83
Code by Inyeol Lee, submitted to SF bug 595350, to implement
Guido van Rossum <guido@python.org>
parents:
24918
diff
changeset
|
2387 } |
|
17750
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
2388 } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2389 /* Copy the remainder of the remaining string */ |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2390 Py_MEMCPY(result_s, start, end-start); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2391 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2392 return result; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2393 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2394 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2395 /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2396 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2397 replace_substring(PyBytesObject *self, |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2398 const char *from_s, Py_ssize_t from_len, |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2399 const char *to_s, Py_ssize_t to_len, |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2400 Py_ssize_t maxcount) { |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2401 char *self_s, *result_s; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2402 char *start, *next, *end; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2403 Py_ssize_t self_len, result_len; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2404 Py_ssize_t count, offset, product; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2405 PyBytesObject *result; |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2406 |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2407 self_s = PyBytes_AS_STRING(self); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2408 self_len = PyBytes_GET_SIZE(self); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2409 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2410 count = countstring(self_s, self_len, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2411 from_s, from_len, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2412 0, self_len, FORWARD, maxcount); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2413 if (count == 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2414 /* no matches, return unchanged */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2415 return return_self(self); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2416 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2417 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2418 /* Check for overflow */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2419 /* result_len = self_len + count * (to_len-from_len) */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2420 product = count * (to_len-from_len); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2421 if (product / (to_len-from_len) != count) { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2422 PyErr_SetString(PyExc_OverflowError, |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
2423 "replacement bytes are too long"); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2424 return NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2425 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2426 result_len = self_len + product; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2427 if (result_len < 0) { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2428 PyErr_SetString(PyExc_OverflowError, |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
2429 "replacement bytes are too long"); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2430 return NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2431 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2432 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2433 if ( (result = (PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2434 PyBytes_FromStringAndSize(NULL, result_len)) == NULL) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2435 return NULL; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2436 result_s = PyBytes_AS_STRING(result); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2437 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2438 start = self_s; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2439 end = self_s + self_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2440 while (count-- > 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2441 offset = findstring(start, end-start, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2442 from_s, from_len, |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2443 0, end-start, FORWARD); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2444 if (offset == -1) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2445 break; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2446 next = start+offset; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2447 if (next == start) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2448 /* replace with the 'to' */ |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2449 Py_MEMCPY(result_s, to_s, to_len); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2450 result_s += to_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2451 start += from_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2452 } else { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2453 /* copy the unchanged old then the 'to' */ |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2454 Py_MEMCPY(result_s, start, next-start); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2455 result_s += (next-start); |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2456 Py_MEMCPY(result_s, to_s, to_len); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2457 result_s += to_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2458 start = next+from_len; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2459 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2460 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2461 /* Copy the remainder of the remaining string */ |
|
38669
19fd6141940d
Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
2462 Py_MEMCPY(result_s, start, end-start); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2463 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2464 return result; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2465 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2466 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2467 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2468 Py_LOCAL(PyBytesObject *) |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2469 replace(PyBytesObject *self, |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2470 const char *from_s, Py_ssize_t from_len, |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2471 const char *to_s, Py_ssize_t to_len, |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2472 Py_ssize_t maxcount) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2473 { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2474 if (maxcount < 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2475 maxcount = PY_SSIZE_T_MAX; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2476 } else if (maxcount == 0 || PyBytes_GET_SIZE(self) == 0) { |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2477 /* nothing to do; return the original string */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2478 return return_self(self); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2479 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2480 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2481 if (maxcount == 0 || |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2482 (from_len == 0 && to_len == 0)) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2483 /* nothing to do; return the original string */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2484 return return_self(self); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2485 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2486 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2487 /* Handle zero-length special cases */ |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2488 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2489 if (from_len == 0) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2490 /* insert the 'to' string everywhere. */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2491 /* >>> "Python".replace("", ".") */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2492 /* '.P.y.t.h.o.n.' */ |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2493 return replace_interleave(self, to_s, to_len, maxcount); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2494 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2495 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2496 /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2497 /* point for an empty self string to generate a non-empty string */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2498 /* Special case so the remaining code always gets a non-empty string */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2499 if (PyBytes_GET_SIZE(self) == 0) { |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2500 return return_self(self); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2501 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2502 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2503 if (to_len == 0) { |
|
47389
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
2504 /* delete all occurrences of 'from' string */ |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2505 if (from_len == 1) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2506 return replace_delete_single_character( |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2507 self, from_s[0], maxcount); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2508 } else { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2509 return replace_delete_substring(self, from_s, |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2510 from_len, maxcount); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2511 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2512 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2513 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2514 /* Handle special case where both strings have the same length */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2515 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2516 if (from_len == to_len) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2517 if (from_len == 1) { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2518 return replace_single_character_in_place( |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2519 self, |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2520 from_s[0], |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2521 to_s[0], |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2522 maxcount); |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2523 } else { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2524 return replace_substring_in_place( |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2525 self, from_s, from_len, to_s, to_len, |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2526 maxcount); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2527 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2528 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2529 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2530 /* Otherwise use the more generic algorithms */ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2531 if (from_len == 1) { |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2532 return replace_single_character(self, from_s[0], |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2533 to_s, to_len, maxcount); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2534 } else { |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2535 /* len('from')>=2, len('to')>=1 */ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2536 return replace_substring(self, from_s, from_len, to_s, to_len, |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2537 maxcount); |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2538 } |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2539 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2540 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2541 PyDoc_STRVAR(replace__doc__, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2542 "B.replace(old, new[, count]) -> bytes\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2543 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2544 Return a copy of B with all occurrences of subsection\n\ |
|
30751
6fdde96a7222
Avoid confusing name for the 3rd argument to str.replace().
Fred Drake <fdrake@acm.org>
parents:
30706
diff
changeset
|
2545 old replaced by new. If the optional argument count is\n\ |
|
6fdde96a7222
Avoid confusing name for the 3rd argument to str.replace().
Fred Drake <fdrake@acm.org>
parents:
30706
diff
changeset
|
2546 given, only the first count occurrences are replaced."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2547 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2548 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2549 bytes_replace(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2550 { |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
2551 Py_ssize_t count = -1; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2552 PyObject *from, *to; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2553 const char *from_s, *to_s; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2554 Py_ssize_t from_len, to_len; |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2555 |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2556 if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count)) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2557 return NULL; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2558 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2559 if (PyBytes_Check(from)) { |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2560 from_s = PyBytes_AS_STRING(from); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2561 from_len = PyBytes_GET_SIZE(from); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2562 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2563 else if (PyObject_AsCharBuffer(from, &from_s, &from_len)) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2564 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2565 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2566 if (PyBytes_Check(to)) { |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2567 to_s = PyBytes_AS_STRING(to); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2568 to_len = PyBytes_GET_SIZE(to); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2569 } |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2570 else if (PyObject_AsCharBuffer(to, &to_s, &to_len)) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2571 return NULL; |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2572 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2573 return (PyObject *)replace((PyBytesObject *) self, |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2574 from_s, from_len, |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2575 to_s, to_len, count); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2576 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2577 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2578 /** End DALKE **/ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2579 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2580 /* Matches the end (direction >= 0) or start (direction < 0) of self |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2581 * against substr, using the start and end arguments. Returns |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2582 * -1 on error, 0 if not found and 1 if found. |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2583 */ |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2584 Py_LOCAL(int) |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2585 _bytes_tailmatch(PyBytesObject *self, PyObject *substr, Py_ssize_t start, |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2586 Py_ssize_t end, int direction) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2587 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2588 Py_ssize_t len = PyBytes_GET_SIZE(self); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2589 Py_ssize_t slen; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2590 const char* sub; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2591 const char* str; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2592 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2593 if (PyBytes_Check(substr)) { |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2594 sub = PyBytes_AS_STRING(substr); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2595 slen = PyBytes_GET_SIZE(substr); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2596 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2597 else if (PyObject_AsCharBuffer(substr, &sub, &slen)) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2598 return -1; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2599 str = PyBytes_AS_STRING(self); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2600 |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2601 bytes_adjust_indices(&start, &end, len); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2602 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2603 if (direction < 0) { |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2604 /* startswith */ |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2605 if (start+slen > len) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2606 return 0; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2607 } else { |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2608 /* endswith */ |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2609 if (end-start < slen || start > len) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2610 return 0; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2611 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2612 if (end-slen > start) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2613 start = end - slen; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2614 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2615 if (end-start >= slen) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2616 return ! memcmp(str+start, sub, slen); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2617 return 0; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2618 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2619 |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2620 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2621 PyDoc_STRVAR(startswith__doc__, |
|
47389
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
2622 "B.startswith(prefix[, start[, end]]) -> bool\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2623 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2624 Return True if B starts with the specified prefix, False otherwise.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2625 With optional start, test B beginning at that position.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2626 With optional end, stop comparing B at that position.\n\ |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
2627 prefix can also be a tuple of bytes to try."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2628 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2629 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2630 bytes_startswith(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2631 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2632 Py_ssize_t start = 0; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
2633 Py_ssize_t end = PY_SSIZE_T_MAX; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2634 PyObject *subobj; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2635 int result; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2636 |
| 12615 | 2637 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 2638 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) | |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2639 return NULL; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2640 if (PyTuple_Check(subobj)) { |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2641 Py_ssize_t i; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2642 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2643 result = _bytes_tailmatch(self, |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2644 PyTuple_GET_ITEM(subobj, i), |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2645 start, end, -1); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2646 if (result == -1) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2647 return NULL; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2648 else if (result) { |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2649 Py_RETURN_TRUE; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2650 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2651 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2652 Py_RETURN_FALSE; |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2653 } |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2654 result = _bytes_tailmatch(self, subobj, start, end, -1); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2655 if (result == -1) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2656 return NULL; |
|
23850
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
2657 else |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2658 return PyBool_FromLong(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2659 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2660 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2661 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2662 PyDoc_STRVAR(endswith__doc__, |
|
47389
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
2663 "B.endswith(suffix[, start[, end]]) -> bool\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2664 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2665 Return True if B ends with the specified suffix, False otherwise.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2666 With optional start, test B beginning at that position.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2667 With optional end, stop comparing B at that position.\n\ |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
2668 suffix can also be a tuple of bytes to try."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2669 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2670 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2671 bytes_endswith(PyBytesObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2672 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2673 Py_ssize_t start = 0; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
2674 Py_ssize_t end = PY_SSIZE_T_MAX; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2675 PyObject *subobj; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2676 int result; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2677 |
| 12615 | 2678 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 2679 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) | |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2680 return NULL; |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2681 if (PyTuple_Check(subobj)) { |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2682 Py_ssize_t i; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2683 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2684 result = _bytes_tailmatch(self, |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2685 PyTuple_GET_ITEM(subobj, i), |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2686 start, end, +1); |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2687 if (result == -1) |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2688 return NULL; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2689 else if (result) { |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2690 Py_RETURN_TRUE; |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2691 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2692 } |
|
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2693 Py_RETURN_FALSE; |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2694 } |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2695 result = _bytes_tailmatch(self, subobj, start, end, +1); |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2696 if (result == -1) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2697 return NULL; |
|
23850
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
2698 else |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
39222
diff
changeset
|
2699 return PyBool_FromLong(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2700 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2701 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2702 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2703 PyDoc_STRVAR(decode__doc__, |
|
47389
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
2704 "B.decode([encoding[, errors]]) -> str\n\ |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2705 \n\ |
|
47389
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
2706 Decode S using the codec registered for encoding. encoding defaults\n\ |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2707 to the default encoding. errors may be given to set a different error\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2708 handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2709 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
|
25091
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25017
diff
changeset
|
2710 as well as any other name registerd with codecs.register_error that is\n\ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25017
diff
changeset
|
2711 able to handle UnicodeDecodeErrors."); |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2712 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2713 static PyObject * |
|
56099
1b7bdae02179
Merged revisions 74929 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53235
diff
changeset
|
2714 bytes_decode(PyObject *self, PyObject *args, PyObject *kwargs) |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2715 { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2716 const char *encoding = NULL; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2717 const char *errors = NULL; |
|
56099
1b7bdae02179
Merged revisions 74929 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53235
diff
changeset
|
2718 static char *kwlist[] = {"encoding", "errors", 0}; |
|
1b7bdae02179
Merged revisions 74929 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53235
diff
changeset
|
2719 |
|
1b7bdae02179
Merged revisions 74929 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53235
diff
changeset
|
2720 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", kwlist, &encoding, &errors)) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2721 return NULL; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2722 if (encoding == NULL) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2723 encoding = PyUnicode_GetDefaultEncoding(); |
|
47496
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47466
diff
changeset
|
2724 return PyUnicode_FromEncodedObject(self, encoding, errors); |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2725 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2726 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2727 |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2728 PyDoc_STRVAR(fromhex_doc, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2729 "bytes.fromhex(string) -> bytes\n\ |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2730 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2731 Create a bytes object from a string of hexadecimal numbers.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2732 Spaces between two numbers are accepted.\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2733 Example: bytes.fromhex('B9 01EF') -> b'\\xb9\\x01\\xef'."); |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2734 |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2735 static int |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2736 hex_digit_to_int(Py_UNICODE c) |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2737 { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2738 if (c >= 128) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2739 return -1; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2740 if (ISDIGIT(c)) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2741 return c - '0'; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2742 else { |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2743 if (ISUPPER(c)) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2744 c = TOLOWER(c); |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2745 if (c >= 'a' && c <= 'f') |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2746 return c - 'a' + 10; |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2747 } |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2748 return -1; |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2749 } |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2750 |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2751 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2752 bytes_fromhex(PyObject *cls, PyObject *args) |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2753 { |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2754 PyObject *newstring, *hexobj; |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2755 char *buf; |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2756 Py_UNICODE *hex; |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2757 Py_ssize_t hexlen, byteslen, i, j; |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2758 int top, bot; |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2759 |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2760 if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2761 return NULL; |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2762 assert(PyUnicode_Check(hexobj)); |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2763 hexlen = PyUnicode_GET_SIZE(hexobj); |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2764 hex = PyUnicode_AS_UNICODE(hexobj); |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2765 byteslen = hexlen/2; /* This overestimates if there are spaces */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2766 newstring = PyBytes_FromStringAndSize(NULL, byteslen); |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2767 if (!newstring) |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2768 return NULL; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2769 buf = PyBytes_AS_STRING(newstring); |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2770 for (i = j = 0; i < hexlen; i += 2) { |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2771 /* skip over spaces in the input */ |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2772 while (hex[i] == ' ') |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2773 i++; |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2774 if (i >= hexlen) |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2775 break; |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2776 top = hex_digit_to_int(hex[i]); |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2777 bot = hex_digit_to_int(hex[i+1]); |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2778 if (top == -1 || bot == -1) { |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2779 PyErr_Format(PyExc_ValueError, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2780 "non-hexadecimal number found in " |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2781 "fromhex() arg at position %zd", i); |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2782 goto error; |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2783 } |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2784 buf[j++] = (top << 4) + bot; |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2785 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2786 if (j != byteslen && _PyBytes_Resize(&newstring, j) < 0) |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2787 goto error; |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2788 return newstring; |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2789 |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2790 error: |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2791 Py_XDECREF(newstring); |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2792 return NULL; |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2793 } |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2794 |
|
47466
58f4b60773fe
Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
47461
diff
changeset
|
2795 PyDoc_STRVAR(sizeof__doc__, |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2796 "B.__sizeof__() -> size of B in memory, in bytes"); |
|
47466
58f4b60773fe
Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
47461
diff
changeset
|
2797 |
|
58f4b60773fe
Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
47461
diff
changeset
|
2798 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2799 bytes_sizeof(PyBytesObject *v) |
|
47466
58f4b60773fe
Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
47461
diff
changeset
|
2800 { |
|
58f4b60773fe
Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
47461
diff
changeset
|
2801 Py_ssize_t res; |
|
50044
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
2802 res = PyBytesObject_SIZE + Py_SIZE(v) * Py_TYPE(v)->tp_itemsize; |
|
47466
58f4b60773fe
Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
47461
diff
changeset
|
2803 return PyLong_FromSsize_t(res); |
|
58f4b60773fe
Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
47461
diff
changeset
|
2804 } |
|
58f4b60773fe
Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
47461
diff
changeset
|
2805 |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2806 |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27248
diff
changeset
|
2807 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2808 bytes_getnewargs(PyBytesObject *v) |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27248
diff
changeset
|
2809 { |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
2810 return Py_BuildValue("(s#)", v->ob_sval, Py_SIZE(v)); |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27248
diff
changeset
|
2811 } |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27248
diff
changeset
|
2812 |
|
45052
e43238760c38
Fixed issue #1973: bytes.fromhex('') raises SystemError
Christian Heimes <christian@cheimes.de>
parents:
44548
diff
changeset
|
2813 |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2814 static PyMethodDef |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2815 bytes_methods[] = { |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2816 {"__getnewargs__", (PyCFunction)bytes_getnewargs, METH_NOARGS}, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2817 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2818 _Py_capitalize__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2819 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2820 {"count", (PyCFunction)bytes_count, METH_VARARGS, count__doc__}, |
|
56099
1b7bdae02179
Merged revisions 74929 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53235
diff
changeset
|
2821 {"decode", (PyCFunction)bytes_decode, METH_VARARGS | METH_KEYWORDS, decode__doc__}, |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2822 {"endswith", (PyCFunction)bytes_endswith, METH_VARARGS, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2823 endswith__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2824 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2825 expandtabs__doc__}, |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2826 {"find", (PyCFunction)bytes_find, METH_VARARGS, find__doc__}, |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2827 {"fromhex", (PyCFunction)bytes_fromhex, METH_VARARGS|METH_CLASS, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2828 fromhex_doc}, |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2829 {"index", (PyCFunction)bytes_index, METH_VARARGS, index__doc__}, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2830 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2831 _Py_isalnum__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2832 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2833 _Py_isalpha__doc__}, |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
2834 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
2835 _Py_isdigit__doc__}, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2836 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2837 _Py_islower__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2838 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2839 _Py_isspace__doc__}, |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
2840 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
2841 _Py_istitle__doc__}, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2842 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2843 _Py_isupper__doc__}, |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2844 {"join", (PyCFunction)bytes_join, METH_O, join__doc__}, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2845 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2846 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2847 {"lstrip", (PyCFunction)bytes_lstrip, METH_VARARGS, lstrip__doc__}, |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2848 {"maketrans", (PyCFunction)bytes_maketrans, METH_VARARGS|METH_STATIC, |
|
53094
4407e4d07084
Add bytes/bytearray.maketrans() to mirror str.maketrans(), and deprecate
Georg Brandl <georg@python.org>
parents:
52773
diff
changeset
|
2849 _Py_maketrans__doc__}, |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2850 {"partition", (PyCFunction)bytes_partition, METH_O, partition__doc__}, |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2851 {"replace", (PyCFunction)bytes_replace, METH_VARARGS, replace__doc__}, |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2852 {"rfind", (PyCFunction)bytes_rfind, METH_VARARGS, rfind__doc__}, |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2853 {"rindex", (PyCFunction)bytes_rindex, METH_VARARGS, rindex__doc__}, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2854 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2855 {"rpartition", (PyCFunction)bytes_rpartition, METH_O, |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37904
diff
changeset
|
2856 rpartition__doc__}, |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2857 {"rsplit", (PyCFunction)bytes_rsplit, METH_VARARGS, rsplit__doc__}, |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2858 {"rstrip", (PyCFunction)bytes_rstrip, METH_VARARGS, rstrip__doc__}, |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2859 {"split", (PyCFunction)bytes_split, METH_VARARGS, split__doc__}, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2860 {"splitlines", (PyCFunction)stringlib_splitlines, METH_VARARGS, |
|
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2861 splitlines__doc__}, |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2862 {"startswith", (PyCFunction)bytes_startswith, METH_VARARGS, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2863 startswith__doc__}, |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2864 {"strip", (PyCFunction)bytes_strip, METH_VARARGS, strip__doc__}, |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
2865 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
|
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
2866 _Py_swapcase__doc__}, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2867 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2868 {"translate", (PyCFunction)bytes_translate, METH_VARARGS, |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
2869 translate__doc__}, |
|
43693
0502a8c64ef2
Sort the method lists for str8 and bytes so differences are more apparent.
Guido van Rossum <guido@python.org>
parents:
43664
diff
changeset
|
2870 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
|
43576
697cc31a83f6
For PEP3137: Adds missing methods to the mutable PyBytes object (soon
Gregory P. Smith <greg@mad-scientist.com>
parents:
43503
diff
changeset
|
2871 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2872 {"__sizeof__", (PyCFunction)bytes_sizeof, METH_NOARGS, |
|
47466
58f4b60773fe
Merged revisions 63856-63857,63859-63860 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
47461
diff
changeset
|
2873 sizeof__doc__}, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2874 {NULL, NULL} /* sentinel */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2875 }; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2876 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24184
diff
changeset
|
2877 static PyObject * |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2878 str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2879 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2880 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
2881 bytes_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2882 { |
|
48871
548dccc4008e
make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents:
48851
diff
changeset
|
2883 PyObject *x = NULL; |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2884 const char *encoding = NULL; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2885 const char *errors = NULL; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2886 PyObject *new = NULL; |
|
58029
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2887 Py_ssize_t size; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2888 static char *kwlist[] = {"source", "encoding", "errors", 0}; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2889 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2890 if (type != &PyBytes_Type) |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2891 return str_subtype_new(type, args, kwds); |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2892 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytes", kwlist, &x, |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2893 &encoding, &errors)) |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2894 return NULL; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2895 if (x == NULL) { |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2896 if (encoding != NULL || errors != NULL) { |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2897 PyErr_SetString(PyExc_TypeError, |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2898 "encoding or errors without sequence " |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2899 "argument"); |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2900 return NULL; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2901 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2902 return PyBytes_FromString(""); |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2903 } |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2904 |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2905 if (PyUnicode_Check(x)) { |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2906 /* Encode via the codec registry */ |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2907 if (encoding == NULL) { |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2908 PyErr_SetString(PyExc_TypeError, |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2909 "string argument without an encoding"); |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2910 return NULL; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2911 } |
|
47496
2284fa89ab08
Move the codec decode type checks to bytes/bytearray.decode().
Marc-André Lemburg <mal@egenix.com>
parents:
47466
diff
changeset
|
2912 new = PyUnicode_AsEncodedString(x, encoding, errors); |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2913 if (new == NULL) |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2914 return NULL; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2915 assert(PyBytes_Check(new)); |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2916 return new; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2917 } |
|
58029
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2918 /* Is it an integer? */ |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2919 size = PyNumber_AsSsize_t(x, PyExc_ValueError); |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2920 if (size == -1 && PyErr_Occurred()) { |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2921 PyErr_Clear(); |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2922 } |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2923 else { |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2924 if (size < 0) { |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2925 PyErr_SetString(PyExc_ValueError, "negative count"); |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2926 return NULL; |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2927 } |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2928 new = PyBytes_FromStringAndSize(NULL, size); |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2929 if (new == NULL) { |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2930 return NULL; |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2931 } |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2932 if (size > 0) { |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2933 memset(((PyBytesObject*)new)->ob_sval, 0, size); |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2934 } |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2935 return new; |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2936 } |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2937 |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2938 /* If it's not unicode, there can't be encoding or errors */ |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2939 if (encoding != NULL || errors != NULL) { |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2940 PyErr_SetString(PyExc_TypeError, |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2941 "encoding or errors without a string argument"); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2942 return NULL; |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2943 } |
|
48871
548dccc4008e
make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents:
48851
diff
changeset
|
2944 return PyObject_Bytes(x); |
|
548dccc4008e
make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents:
48851
diff
changeset
|
2945 } |
|
548dccc4008e
make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents:
48851
diff
changeset
|
2946 |
|
548dccc4008e
make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents:
48851
diff
changeset
|
2947 PyObject * |
|
548dccc4008e
make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents:
48851
diff
changeset
|
2948 PyBytes_FromObject(PyObject *x) |
|
548dccc4008e
make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents:
48851
diff
changeset
|
2949 { |
|
548dccc4008e
make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents:
48851
diff
changeset
|
2950 PyObject *new, *it; |
|
548dccc4008e
make bytes(o) respect __bytes__ #2415
Benjamin Peterson <benjamin@python.org>
parents:
48851
diff
changeset
|
2951 Py_ssize_t i, size; |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2952 |
|
48879
d9fe29e95d60
add NULL checking for PyBytes_FromObject; R=Neal
Benjamin Peterson <benjamin@python.org>
parents:
48871
diff
changeset
|
2953 if (x == NULL) { |
|
d9fe29e95d60
add NULL checking for PyBytes_FromObject; R=Neal
Benjamin Peterson <benjamin@python.org>
parents:
48871
diff
changeset
|
2954 PyErr_BadInternalCall(); |
|
d9fe29e95d60
add NULL checking for PyBytes_FromObject; R=Neal
Benjamin Peterson <benjamin@python.org>
parents:
48871
diff
changeset
|
2955 return NULL; |
|
d9fe29e95d60
add NULL checking for PyBytes_FromObject; R=Neal
Benjamin Peterson <benjamin@python.org>
parents:
48871
diff
changeset
|
2956 } |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2957 /* Use the modern buffer interface */ |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2958 if (PyObject_CheckBuffer(x)) { |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2959 Py_buffer view; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2960 if (PyObject_GetBuffer(x, &view, PyBUF_FULL_RO) < 0) |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2961 return NULL; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2962 new = PyBytes_FromStringAndSize(NULL, view.len); |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2963 if (!new) |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2964 goto fail; |
|
49387
86893a397dd0
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
48879
diff
changeset
|
2965 /* XXX(brett.cannon): Better way to get to internal buffer? */ |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2966 if (PyBuffer_ToContiguous(((PyBytesObject *)new)->ob_sval, |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2967 &view, view.len, 'C') < 0) |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2968 goto fail; |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
2969 PyBuffer_Release(&view); |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2970 return new; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2971 fail: |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2972 Py_XDECREF(new); |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
2973 PyBuffer_Release(&view); |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2974 return NULL; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2975 } |
|
58029
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2976 if (PyUnicode_Check(x)) { |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2977 PyErr_SetString(PyExc_TypeError, |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2978 "cannot convert unicode object to bytes"); |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2979 return NULL; |
|
d52a7179eece
Issue #6687: Moved the special-case for integers out of PyBytes_FromObject.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
57515
diff
changeset
|
2980 } |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2981 |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2982 /* For iterator version, create a string object and resize as needed */ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2983 /* XXX(gb): is 64 a good value? also, optimize if length is known */ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2984 /* XXX(guido): perhaps use Pysequence_Fast() -- I can't imagine the |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
2985 input being a truly long iterator. */ |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2986 size = 64; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
2987 new = PyBytes_FromStringAndSize(NULL, size); |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2988 if (new == NULL) |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2989 return NULL; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2990 |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2991 /* XXX Optimize this if the arguments is a list, tuple */ |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2992 |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2993 /* Get the iterator */ |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2994 it = PyObject_GetIter(x); |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2995 if (it == NULL) |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2996 goto error; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2997 |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2998 /* Run the iterator to exhaustion */ |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
2999 for (i = 0; ; i++) { |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3000 PyObject *item; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3001 Py_ssize_t value; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3002 |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3003 /* Get the next item */ |
|
44016
22e09905e400
Use proper API for iter.__next__().
Georg Brandl <georg@python.org>
parents:
43976
diff
changeset
|
3004 item = PyIter_Next(it); |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3005 if (item == NULL) { |
|
44016
22e09905e400
Use proper API for iter.__next__().
Georg Brandl <georg@python.org>
parents:
43976
diff
changeset
|
3006 if (PyErr_Occurred()) |
|
22e09905e400
Use proper API for iter.__next__().
Georg Brandl <georg@python.org>
parents:
43976
diff
changeset
|
3007 goto error; |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3008 break; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3009 } |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3010 |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3011 /* Interpret it as an int (__index__) */ |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3012 value = PyNumber_AsSsize_t(item, PyExc_ValueError); |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3013 Py_DECREF(item); |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3014 if (value == -1 && PyErr_Occurred()) |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3015 goto error; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3016 |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3017 /* Range check */ |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3018 if (value < 0 || value >= 256) { |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3019 PyErr_SetString(PyExc_ValueError, |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3020 "bytes must be in range(0, 256)"); |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3021 goto error; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3022 } |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3023 |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3024 /* Append the byte */ |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3025 if (i >= size) { |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3026 size *= 2; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3027 if (_PyBytes_Resize(&new, size) < 0) |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3028 goto error; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3029 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3030 ((PyBytesObject *)new)->ob_sval[i] = value; |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3031 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3032 _PyBytes_Resize(&new, i); |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3033 |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3034 /* Clean up and return success */ |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3035 Py_DECREF(it); |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3036 return new; |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3037 |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3038 error: |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
3039 /* Error handling when new != NULL */ |
|
43664
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3040 Py_XDECREF(it); |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3041 Py_DECREF(new); |
|
33efbfc28d2b
Patch #1303: Adapt str8 constructor to bytes (now buffer) one.
Georg Brandl <georg@python.org>
parents:
43604
diff
changeset
|
3042 return NULL; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3043 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3044 |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
3045 static PyObject * |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
3046 str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
3047 { |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
3048 PyObject *tmp, *pnew; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
3049 Py_ssize_t n; |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
3050 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3051 assert(PyType_IsSubtype(type, &PyBytes_Type)); |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3052 tmp = bytes_new(&PyBytes_Type, args, kwds); |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
3053 if (tmp == NULL) |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
3054 return NULL; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3055 assert(PyBytes_CheckExact(tmp)); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3056 n = PyBytes_GET_SIZE(tmp); |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
3057 pnew = type->tp_alloc(type, n); |
|
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
3058 if (pnew != NULL) { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3059 Py_MEMCPY(PyBytes_AS_STRING(pnew), |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3060 PyBytes_AS_STRING(tmp), n+1); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3061 ((PyBytesObject *)pnew)->ob_shash = |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3062 ((PyBytesObject *)tmp)->ob_shash; |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
3063 } |
|
19632
20fb7591f885
Fix a memory leak in str_subtype_new(). (All the other
Guido van Rossum <guido@python.org>
parents:
19592
diff
changeset
|
3064 Py_DECREF(tmp); |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
3065 return pnew; |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
3066 } |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
3067 |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3068 PyDoc_STRVAR(bytes_doc, |
|
47389
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
3069 "bytes(iterable_of_ints) -> bytes\n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
3070 bytes(string, encoding[, errors]) -> bytes\n\ |
|
47389
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
3071 bytes(bytes_or_buffer) -> immutable copy of bytes_or_buffer\n\ |
|
6ce4435f15ad
Fix all return types for str/bytes/bytearray docstrings and make the wording more consistent.
Georg Brandl <georg@python.org>
parents:
47346
diff
changeset
|
3072 bytes(memory_view) -> bytes\n\ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3073 \n\ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
3074 Construct an immutable array of bytes from:\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
3075 - an iterable yielding integers in range(256)\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
3076 - a text string encoded using the specified encoding\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
3077 - a bytes or a buffer object\n\ |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
3078 - any object implementing the buffer API."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3079 |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3080 static PyObject *bytes_iter(PyObject *seq); |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3081 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3082 PyTypeObject PyBytes_Type = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42309
diff
changeset
|
3083 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
3084 "bytes", |
|
50044
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
3085 PyBytesObject_SIZE, |
| 6 | 3086 sizeof(char), |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3087 bytes_dealloc, /* tp_dealloc */ |
|
42477
e7fd327e0c19
Kill all uses and definitions of tp_print under Objects/. (Others will follow.)
Guido van Rossum <guido@python.org>
parents:
42466
diff
changeset
|
3088 0, /* tp_print */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3089 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3090 0, /* tp_setattr */ |
|
51270
debd14c489b9
Issue #1717: rename tp_compare to tp_reserved. I'll change the
Mark Dickinson <dickinsm@gmail.com>
parents:
50735
diff
changeset
|
3091 0, /* tp_reserved */ |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3092 (reprfunc)bytes_repr, /* tp_repr */ |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
3093 0, /* tp_as_number */ |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3094 &bytes_as_sequence, /* tp_as_sequence */ |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3095 &bytes_as_mapping, /* tp_as_mapping */ |
|
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3096 (hashfunc)bytes_hash, /* tp_hash */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3097 0, /* tp_call */ |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3098 bytes_str, /* tp_str */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3099 PyObject_GenericGetAttr, /* tp_getattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3100 0, /* tp_setattro */ |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3101 &bytes_as_buffer, /* tp_as_buffer */ |
|
40909
f2f0b41f270d
Merged revisions 53875-53911 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
40626
diff
changeset
|
3102 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3103 Py_TPFLAGS_BYTES_SUBCLASS, /* tp_flags */ |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3104 bytes_doc, /* tp_doc */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3105 0, /* tp_traverse */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3106 0, /* tp_clear */ |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3107 (richcmpfunc)bytes_richcompare, /* tp_richcompare */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3108 0, /* tp_weaklistoffset */ |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3109 bytes_iter, /* tp_iter */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3110 0, /* tp_iternext */ |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3111 bytes_methods, /* tp_methods */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3112 0, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3113 0, /* tp_getset */ |
|
43577
ace45b361e87
Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents:
43576
diff
changeset
|
3114 &PyBaseObject_Type, /* tp_base */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3115 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3116 0, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3117 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3118 0, /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3119 0, /* tp_init */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
3120 0, /* tp_alloc */ |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3121 bytes_new, /* tp_new */ |
|
22977
4ba091cc70b8
Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22785
diff
changeset
|
3122 PyObject_Del, /* tp_free */ |
| 6 | 3123 }; |
| 3124 | |
| 3125 void | |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3126 PyBytes_Concat(register PyObject **pv, register PyObject *w) |
| 6 | 3127 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3128 register PyObject *v; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
3129 assert(pv != NULL); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3130 if (*pv == NULL) |
| 6 | 3131 return; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
3132 if (w == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3133 Py_DECREF(*pv); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3134 *pv = NULL; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3135 return; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3136 } |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3137 v = bytes_concat(*pv, w); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3138 Py_DECREF(*pv); |
| 6 | 3139 *pv = v; |
| 3140 } | |
| 3141 | |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3142 void |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3143 PyBytes_ConcatAndDel(register PyObject **pv, register PyObject *w) |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3144 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3145 PyBytes_Concat(pv, w); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3146 Py_XDECREF(w); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3147 } |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3148 |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3149 |
| 6 | 3150 /* The following function breaks the notion that strings are immutable: |
| 3151 it changes the size of a string. We get away with this only if there | |
| 3152 is only one module referencing the object. You can also think of it | |
| 3153 as creating a new string object and destroying the old one, only | |
| 3154 more efficiently. In any case, don't use this if the string may | |
|
23295
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
3155 already be known to some other part of the code... |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
3156 Note that if there's not enough memory to resize the string, the original |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
3157 string object at *pv is deallocated, *pv is set to NULL, an "out of |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
3158 memory" exception is set, and -1 is returned. Else (on success) 0 is |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
3159 returned, and the value in *pv may or may not be the same as on input. |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
3160 As always, an extra byte is allocated for a trailing \0 byte (newsize |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
3161 does *not* include that), and a trailing \0 byte is stored. |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
3162 */ |
| 6 | 3163 |
| 3164 int | |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3165 _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize) |
| 6 | 3166 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3167 register PyObject *v; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3168 register PyBytesObject *sv; |
|
71
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
3169 v = *pv; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3170 if (!PyBytes_Check(v) || Py_REFCNT(v) != 1 || newsize < 0) { |
| 6 | 3171 *pv = 0; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3172 Py_DECREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3173 PyErr_BadInternalCall(); |
| 27 | 3174 return -1; |
| 6 | 3175 } |
|
71
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
3176 /* XXX UNREF/NEWREF interface should be more symmetrical */ |
|
24184
c4dcb80f48ea
object.h special-build macro minefield: renamed all the new lexical
Tim Peters <tim.peters@gmail.com>
parents:
23850
diff
changeset
|
3177 _Py_DEC_REFTOTAL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3178 _Py_ForgetReference(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3179 *pv = (PyObject *) |
|
50044
67ce745a747a
Issue #4445: save 3 bytes of memory (on average) per bytes allocation.
Mark Dickinson <dickinsm@gmail.com>
parents:
49538
diff
changeset
|
3180 PyObject_REALLOC((char *)v, PyBytesObject_SIZE + newsize); |
| 6 | 3181 if (*pv == NULL) { |
|
22977
4ba091cc70b8
Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22785
diff
changeset
|
3182 PyObject_Del(v); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3183 PyErr_NoMemory(); |
| 27 | 3184 return -1; |
| 6 | 3185 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3186 _Py_NewReference(*pv); |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3187 sv = (PyBytesObject *) *pv; |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
3188 Py_SIZE(sv) = newsize; |
|
71
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
3189 sv->ob_sval[newsize] = '\0'; |
|
33833
10512ceba30c
SF bug #1054139: serious string hashing error in 2.4b1
Raymond Hettinger <python@rcn.com>
parents:
33246
diff
changeset
|
3190 sv->ob_shash = -1; /* invalidate cached hash value */ |
| 6 | 3191 return 0; |
| 3192 } | |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3193 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3194 /* _PyBytes_FormatLong emulates the format codes d, u, o, x and X, and |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3195 * the F_ALT flag, for Python's long (unbounded) ints. It's not used for |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3196 * Python's regular ints. |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3197 * Return value: a new PyString*, or NULL if error. |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3198 * . *pbuf is set to point into it, |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3199 * *plen set to the # of chars following that. |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3200 * Caller must decref it when done using pbuf. |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3201 * The string starting at *pbuf is of the form |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3202 * "-"? ("0x" | "0X")? digit+ |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3203 * "0x"/"0X" are present only for x and X conversions, with F_ALT |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
3204 * set in flags. The case of hex digits will be correct, |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3205 * There will be at least prec digits, zero-filled on the left if |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3206 * necessary to get that many. |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3207 * val object to be converted |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3208 * flags bitmask of format flags; only F_ALT is looked at |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3209 * prec minimum number of digits; 0-fill on left if needed |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3210 * type a character in [duoxX]; u acts the same as d |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3211 * |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3212 * CAUTION: o, x and X conversions on regular ints can never |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3213 * produce a '-' sign, but can for Python's unbounded ints. |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3214 */ |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3215 PyObject* |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3216 _PyBytes_FormatLong(PyObject *val, int flags, int prec, int type, |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3217 char **pbuf, int *plen) |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3218 { |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3219 PyObject *result = NULL; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3220 char *buf; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
3221 Py_ssize_t i; |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3222 int sign; /* 1 if '-', else 0 */ |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3223 int len; /* number of characters */ |
|
36866
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36675
diff
changeset
|
3224 Py_ssize_t llen; |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3225 int numdigits; /* len == numnondigits + numdigits */ |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3226 int numnondigits = 0; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3227 |
|
40626
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
3228 /* Avoid exceeding SSIZE_T_MAX */ |
|
48851
a2720353f0d3
Fixed yet another compiler warning of 64bit builds.
Christian Heimes <christian@cheimes.de>
parents:
48850
diff
changeset
|
3229 if (prec > INT_MAX-3) { |
|
40626
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
3230 PyErr_SetString(PyExc_OverflowError, |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
3231 "precision too large"); |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
3232 return NULL; |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
3233 } |
|
4aee3f672f77
Merged the int/long unification branch, by very crude means (sorry Thomas!).
Guido van Rossum <guido@python.org>
parents:
40450
diff
changeset
|
3234 |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3235 switch (type) { |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3236 case 'd': |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3237 case 'u': |
|
42594
ac5ce7c43ee7
Format bools properly in %d.
Martin v. Löwis <martin@v.loewis.de>
parents:
42477
diff
changeset
|
3238 /* Special-case boolean: we want 0/1 */ |
|
ac5ce7c43ee7
Format bools properly in %d.
Martin v. Löwis <martin@v.loewis.de>
parents:
42477
diff
changeset
|
3239 if (PyBool_Check(val)) |
|
ac5ce7c43ee7
Format bools properly in %d.
Martin v. Löwis <martin@v.loewis.de>
parents:
42477
diff
changeset
|
3240 result = PyNumber_ToBase(val, 10); |
|
ac5ce7c43ee7
Format bools properly in %d.
Martin v. Löwis <martin@v.loewis.de>
parents:
42477
diff
changeset
|
3241 else |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
3242 result = Py_TYPE(val)->tp_str(val); |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3243 break; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3244 case 'o': |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
3245 numnondigits = 2; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
3246 result = PyNumber_ToBase(val, 8); |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3247 break; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3248 case 'x': |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3249 case 'X': |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3250 numnondigits = 2; |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
3251 result = PyNumber_ToBase(val, 16); |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3252 break; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3253 default: |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3254 assert(!"'type' not in [duoxX]"); |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3255 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3256 if (!result) |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3257 return NULL; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3258 |
|
48660
85b67fb5672b
Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents:
48623
diff
changeset
|
3259 buf = _PyUnicode_AsString(result); |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39544
diff
changeset
|
3260 if (!buf) { |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39544
diff
changeset
|
3261 Py_DECREF(result); |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39544
diff
changeset
|
3262 return NULL; |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39544
diff
changeset
|
3263 } |
|
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
39544
diff
changeset
|
3264 |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3265 /* To modify the string in-place, there can only be one reference. */ |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44252
diff
changeset
|
3266 if (Py_REFCNT(result) != 1) { |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3267 PyErr_BadInternalCall(); |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3268 return NULL; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3269 } |
|
43961
b231bda653a4
PyString_AsString is permissive and accepts unicode strings.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
43947
diff
changeset
|
3270 llen = PyUnicode_GetSize(result); |
|
40450
cc992d75d5b3
Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents:
39610
diff
changeset
|
3271 if (llen > INT_MAX) { |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
3272 PyErr_SetString(PyExc_ValueError, |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3273 "string too large in _PyBytes_FormatLong"); |
|
36866
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36675
diff
changeset
|
3274 return NULL; |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36675
diff
changeset
|
3275 } |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36675
diff
changeset
|
3276 len = (int)llen; |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3277 if (buf[len-1] == 'L') { |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3278 --len; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3279 buf[len] = '\0'; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3280 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3281 sign = buf[0] == '-'; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3282 numnondigits += sign; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3283 numdigits = len - numnondigits; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3284 assert(numdigits > 0); |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3285 |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3286 /* Get rid of base marker unless F_ALT */ |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
3287 if (((flags & F_ALT) == 0 && |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
3288 (type == 'o' || type == 'x' || type == 'X'))) { |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
3289 assert(buf[sign] == '0'); |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
3290 assert(buf[sign+1] == 'x' || buf[sign+1] == 'X' || |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43734
diff
changeset
|
3291 buf[sign+1] == 'o'); |
|
42084
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
3292 numnondigits -= 2; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
3293 buf += 2; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
3294 len -= 2; |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
3295 if (sign) |
|
229c28cb9afb
Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
42013
diff
changeset
|
3296 buf[0] = '-'; |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3297 assert(len == numnondigits + numdigits); |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3298 assert(numdigits > 0); |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3299 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3300 |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3301 /* Fill with leading zeroes to meet minimum width. */ |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3302 if (prec > numdigits) { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3303 PyObject *r1 = PyBytes_FromStringAndSize(NULL, |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3304 numnondigits + prec); |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3305 char *b1; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3306 if (!r1) { |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3307 Py_DECREF(result); |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3308 return NULL; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3309 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3310 b1 = PyBytes_AS_STRING(r1); |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3311 for (i = 0; i < numnondigits; ++i) |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3312 *b1++ = *buf++; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3313 for (i = 0; i < prec - numdigits; i++) |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3314 *b1++ = '0'; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3315 for (i = 0; i < numdigits; i++) |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3316 *b1++ = *buf++; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3317 *b1 = '\0'; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3318 Py_DECREF(result); |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3319 result = r1; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3320 buf = PyBytes_AS_STRING(result); |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3321 len = numnondigits + prec; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3322 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3323 |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3324 /* Fix up case for hex conversions. */ |
|
35177
27a718221e72
SF bug #1224347: int/long unification and hex()
Raymond Hettinger <python@rcn.com>
parents:
34621
diff
changeset
|
3325 if (type == 'X') { |
|
27a718221e72
SF bug #1224347: int/long unification and hex()
Raymond Hettinger <python@rcn.com>
parents:
34621
diff
changeset
|
3326 /* Need to convert all lower case letters to upper case. |
|
27a718221e72
SF bug #1224347: int/long unification and hex()
Raymond Hettinger <python@rcn.com>
parents:
34621
diff
changeset
|
3327 and need to convert 0x to 0X (and -0x to -0X). */ |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3328 for (i = 0; i < len; i++) |
|
35177
27a718221e72
SF bug #1224347: int/long unification and hex()
Raymond Hettinger <python@rcn.com>
parents:
34621
diff
changeset
|
3329 if (buf[i] >= 'a' && buf[i] <= 'x') |
|
27a718221e72
SF bug #1224347: int/long unification and hex()
Raymond Hettinger <python@rcn.com>
parents:
34621
diff
changeset
|
3330 buf[i] -= 'a'-'A'; |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3331 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3332 *pbuf = buf; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3333 *plen = len; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3334 return result; |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3335 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3336 |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3337 void |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3338 PyBytes_Fini(void) |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3339 { |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3340 int i; |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3341 for (i = 0; i < UCHAR_MAX + 1; i++) { |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3342 Py_XDECREF(characters[i]); |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3343 characters[i] = NULL; |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3344 } |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3345 Py_XDECREF(nullstring); |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3346 nullstring = NULL; |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3347 } |
|
16751
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3348 |
|
47346
23379695b4db
clean up some docstrings and errors in bytesobject.c
Benjamin Peterson <benjamin@python.org>
parents:
47302
diff
changeset
|
3349 /*********************** Bytes Iterator ****************************/ |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3350 |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3351 typedef struct { |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3352 PyObject_HEAD |
|
39544
01034018e8b2
Make the it_index field in the str/unicode iterators Py_ssize_t's.
Guido van Rossum <guido@python.org>
parents:
39536
diff
changeset
|
3353 Py_ssize_t it_index; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3354 PyBytesObject *it_seq; /* Set to NULL when iterator is exhausted */ |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3355 } striterobject; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3356 |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3357 static void |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3358 striter_dealloc(striterobject *it) |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3359 { |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3360 _PyObject_GC_UNTRACK(it); |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3361 Py_XDECREF(it->it_seq); |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3362 PyObject_GC_Del(it); |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3363 } |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3364 |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3365 static int |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3366 striter_traverse(striterobject *it, visitproc visit, void *arg) |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3367 { |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3368 Py_VISIT(it->it_seq); |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3369 return 0; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3370 } |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3371 |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3372 static PyObject * |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3373 striter_next(striterobject *it) |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3374 { |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3375 PyBytesObject *seq; |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3376 PyObject *item; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3377 |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3378 assert(it != NULL); |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3379 seq = it->it_seq; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3380 if (seq == NULL) |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3381 return NULL; |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3382 assert(PyBytes_Check(seq)); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3383 |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3384 if (it->it_index < PyBytes_GET_SIZE(seq)) { |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
44017
diff
changeset
|
3385 item = PyLong_FromLong( |
|
43604
b18f6092f6e0
Patch 1280, by Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents:
43577
diff
changeset
|
3386 (unsigned char)seq->ob_sval[it->it_index]); |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3387 if (item != NULL) |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3388 ++it->it_index; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3389 return item; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3390 } |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3391 |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3392 Py_DECREF(seq); |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3393 it->it_seq = NULL; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3394 return NULL; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3395 } |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3396 |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3397 static PyObject * |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3398 striter_len(striterobject *it) |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3399 { |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3400 Py_ssize_t len = 0; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3401 if (it->it_seq) |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3402 len = PyBytes_GET_SIZE(it->it_seq) - it->it_index; |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
44017
diff
changeset
|
3403 return PyLong_FromSsize_t(len); |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3404 } |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3405 |
|
39544
01034018e8b2
Make the it_index field in the str/unicode iterators Py_ssize_t's.
Guido van Rossum <guido@python.org>
parents:
39536
diff
changeset
|
3406 PyDoc_STRVAR(length_hint_doc, |
|
01034018e8b2
Make the it_index field in the str/unicode iterators Py_ssize_t's.
Guido van Rossum <guido@python.org>
parents:
39536
diff
changeset
|
3407 "Private method returning an estimate of len(list(it))."); |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3408 |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3409 static PyMethodDef striter_methods[] = { |
|
39544
01034018e8b2
Make the it_index field in the str/unicode iterators Py_ssize_t's.
Guido van Rossum <guido@python.org>
parents:
39536
diff
changeset
|
3410 {"__length_hint__", (PyCFunction)striter_len, METH_NOARGS, |
|
01034018e8b2
Make the it_index field in the str/unicode iterators Py_ssize_t's.
Guido van Rossum <guido@python.org>
parents:
39536
diff
changeset
|
3411 length_hint_doc}, |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3412 {NULL, NULL} /* sentinel */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3413 }; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3414 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3415 PyTypeObject PyBytesIter_Type = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42309
diff
changeset
|
3416 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
43947
cbc8b3bd3157
Rename buffer -> bytearray.
Guido van Rossum <guido@python.org>
parents:
43897
diff
changeset
|
3417 "bytes_iterator", /* tp_name */ |
|
39544
01034018e8b2
Make the it_index field in the str/unicode iterators Py_ssize_t's.
Guido van Rossum <guido@python.org>
parents:
39536
diff
changeset
|
3418 sizeof(striterobject), /* tp_basicsize */ |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3419 0, /* tp_itemsize */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3420 /* methods */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3421 (destructor)striter_dealloc, /* tp_dealloc */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3422 0, /* tp_print */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3423 0, /* tp_getattr */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3424 0, /* tp_setattr */ |
|
51270
debd14c489b9
Issue #1717: rename tp_compare to tp_reserved. I'll change the
Mark Dickinson <dickinsm@gmail.com>
parents:
50735
diff
changeset
|
3425 0, /* tp_reserved */ |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3426 0, /* tp_repr */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3427 0, /* tp_as_number */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3428 0, /* tp_as_sequence */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3429 0, /* tp_as_mapping */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3430 0, /* tp_hash */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3431 0, /* tp_call */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3432 0, /* tp_str */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3433 PyObject_GenericGetAttr, /* tp_getattro */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3434 0, /* tp_setattro */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3435 0, /* tp_as_buffer */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3436 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3437 0, /* tp_doc */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3438 (traverseproc)striter_traverse, /* tp_traverse */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3439 0, /* tp_clear */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3440 0, /* tp_richcompare */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3441 0, /* tp_weaklistoffset */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3442 PyObject_SelfIter, /* tp_iter */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3443 (iternextfunc)striter_next, /* tp_iternext */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3444 striter_methods, /* tp_methods */ |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3445 0, |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3446 }; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3447 |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3448 static PyObject * |
|
53235
a4f117e920fd
rename internal methods of the bytes object to bytes_
Benjamin Peterson <benjamin@python.org>
parents:
53187
diff
changeset
|
3449 bytes_iter(PyObject *seq) |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3450 { |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3451 striterobject *it; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3452 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3453 if (!PyBytes_Check(seq)) { |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3454 PyErr_BadInternalCall(); |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3455 return NULL; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3456 } |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3457 it = PyObject_GC_New(striterobject, &PyBytesIter_Type); |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3458 if (it == NULL) |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3459 return NULL; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3460 it->it_index = 0; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3461 Py_INCREF(seq); |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
47300
diff
changeset
|
3462 it->it_seq = (PyBytesObject *)seq; |
|
39536
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3463 _PyObject_GC_TRACK(it); |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3464 return (PyObject *)it; |
|
8f5b9982beba
Completely get rid of PyClass and PyInstance.
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
3465 } |
