Mercurial > cpython-lru_cache-weakref
annotate Objects/stringobject.c @ 57303:4aa4c19dfaed legacy-trunk
#1757126: fix typo with the cyrillic_asian alias
| author | Philip Jenvey <pjenvey@underboss.org> |
|---|---|
| date | Tue, 17 Nov 2009 02:42:26 +0000 |
| parents | 51028709abc3 |
| children | 808f9c0d4ac4 |
| rev | line source |
|---|---|
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1 /* String (str/bytes) object implementation */ |
| 6 | 2 |
|
37702
9b22d8b7bcaa
Make Py_BuildValue, PyObject_CallFunction and
Martin v. Löwis <martin@v.loewis.de>
parents:
37678
diff
changeset
|
3 #define PY_SSIZE_T_CLEAN |
|
38376
a16948e9915e
needforspeed: use Py_LOCAL on a few more locals in stringobject.c
Fredrik Lundh <fredrik@pythonware.com>
parents:
38372
diff
changeset
|
4 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
5 #include "Python.h" |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
6 #include <ctype.h> |
|
50036
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
7 #include <stddef.h> |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
8 |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
9 #ifdef COUNT_ALLOCS |
|
50627
f0e0c3536e22
Issue #4850: Change COUNT_ALLOCS variables to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
50036
diff
changeset
|
10 Py_ssize_t null_strings, one_strings; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
11 #endif |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
12 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
13 static PyStringObject *characters[UCHAR_MAX + 1]; |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
14 static PyStringObject *nullstring; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
15 |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
16 /* This dictionary holds all interned strings. Note that references to |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
17 strings in this dictionary are *not* counted in the string's ob_refcnt. |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
18 When the interned string reaches a refcnt of 0 the string deallocation |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
19 function will delete the reference from this dictionary. |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
20 |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
21 Another way to look at this is that to say that the actual reference |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
22 count of a string is: s->ob_refcnt + (s->ob_sstate?2:0) |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
23 */ |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
24 static PyObject *interned; |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
25 |
|
50036
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
26 /* PyStringObject_SIZE gives the basic size of a string; any memory allocation |
|
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
27 for a string of length n should request PyStringObject_SIZE + n bytes. |
|
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
28 |
|
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
29 Using PyStringObject_SIZE instead of sizeof(PyStringObject) saves |
|
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
30 3 bytes per string allocation on a typical system. |
|
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
31 */ |
|
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
32 #define PyStringObject_SIZE (offsetof(PyStringObject, ob_sval) + 1) |
|
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
33 |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
34 /* |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
35 For both PyString_FromString() and PyString_FromStringAndSize(), the |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
36 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
|
37 null terminating character. |
|
30a817038af0
Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents:
21327
diff
changeset
|
38 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
39 For PyString_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
|
40 string containing exactly `size' bytes. |
|
30a817038af0
Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents:
21327
diff
changeset
|
41 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
42 For PyString_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
|
43 either NULL or else points to a string containing at least `size' bytes. |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
44 For PyString_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
|
45 not have to be null-terminated. (Therefore it is safe to construct a |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
46 substring by calling `PyString_FromStringAndSize(origstring, substrlen)'.) |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
47 If `str' is NULL then PyString_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
|
48 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
|
49 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
|
50 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
|
51 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
|
52 |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
53 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
|
54 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
|
55 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
|
56 is therefore equal to the equal to the `size' parameter (for |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
57 PyString_FromStringAndSize()) or the length of the string in the `str' |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
58 parameter (for PyString_FromString()). |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
59 */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
60 PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
61 PyString_FromStringAndSize(const char *str, Py_ssize_t size) |
| 6 | 62 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
63 register PyStringObject *op; |
|
46383
0b55875c1c9c
Raise SystemError when size < 0 is passed into PyString_FromStringAndSize,
Gregory P. Smith <greg@mad-scientist.com>
parents:
46107
diff
changeset
|
64 if (size < 0) { |
|
0b55875c1c9c
Raise SystemError when size < 0 is passed into PyString_FromStringAndSize,
Gregory P. Smith <greg@mad-scientist.com>
parents:
46107
diff
changeset
|
65 PyErr_SetString(PyExc_SystemError, |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
66 "Negative size passed to PyString_FromStringAndSize"); |
|
46383
0b55875c1c9c
Raise SystemError when size < 0 is passed into PyString_FromStringAndSize,
Gregory P. Smith <greg@mad-scientist.com>
parents:
46107
diff
changeset
|
67 return NULL; |
|
0b55875c1c9c
Raise SystemError when size < 0 is passed into PyString_FromStringAndSize,
Gregory P. Smith <greg@mad-scientist.com>
parents:
46107
diff
changeset
|
68 } |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
69 if (size == 0 && (op = nullstring) != NULL) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
70 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
71 null_strings++; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
72 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
73 Py_INCREF(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
74 return (PyObject *)op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
75 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
76 if (size == 1 && str != NULL && |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
77 (op = characters[*str & UCHAR_MAX]) != NULL) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
78 { |
|
1356
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 one_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 } |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
85 |
|
50036
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
86 if (size > PY_SSIZE_T_MAX - PyStringObject_SIZE) { |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48003
diff
changeset
|
87 PyErr_SetString(PyExc_OverflowError, "string is too large"); |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48003
diff
changeset
|
88 return NULL; |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48003
diff
changeset
|
89 } |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48003
diff
changeset
|
90 |
|
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
|
91 /* Inline PyObject_NewVar */ |
|
50036
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
92 op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + size); |
| 27 | 93 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
94 return PyErr_NoMemory(); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
95 PyObject_INIT_VAR(op, &PyString_Type, size); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
96 op->ob_shash = -1; |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
97 op->ob_sstate = SSTATE_NOT_INTERNED; |
| 27 | 98 if (str != NULL) |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
99 Py_MEMCPY(op->ob_sval, str, size); |
| 27 | 100 op->ob_sval[size] = '\0'; |
|
22689
614167df2096
Eliminate DONT_SHARE_SHORT_STRINGS.
Tim Peters <tim.peters@gmail.com>
parents:
22651
diff
changeset
|
101 /* share short strings */ |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
102 if (size == 0) { |
|
17720
ede29ad6440a
Mark Favas reported that gcc caught me using casts as lvalues. Dodge it.
Tim Peters <tim.peters@gmail.com>
parents:
17712
diff
changeset
|
103 PyObject *t = (PyObject *)op; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
104 PyString_InternInPlace(&t); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
105 op = (PyStringObject *)t; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
106 nullstring = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
107 Py_INCREF(op); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
108 } else if (size == 1 && str != NULL) { |
|
17720
ede29ad6440a
Mark Favas reported that gcc caught me using casts as lvalues. Dodge it.
Tim Peters <tim.peters@gmail.com>
parents:
17712
diff
changeset
|
109 PyObject *t = (PyObject *)op; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
110 PyString_InternInPlace(&t); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
111 op = (PyStringObject *)t; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
112 characters[*str & UCHAR_MAX] = 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 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
115 return (PyObject *) op; |
| 6 | 116 } |
| 117 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
118 PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
119 PyString_FromString(const char *str) |
| 6 | 120 { |
|
21263
2f2abeeefd87
PyString_FromString: this requires its argument be non-NULL, but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21254
diff
changeset
|
121 register size_t size; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
122 register PyStringObject *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
|
123 |
|
2f2abeeefd87
PyString_FromString: this requires its argument be non-NULL, but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21254
diff
changeset
|
124 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
|
125 size = strlen(str); |
|
50036
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
126 if (size > PY_SSIZE_T_MAX - PyStringObject_SIZE) { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
127 PyErr_SetString(PyExc_OverflowError, |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
128 "string is too long for a Python string"); |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
129 return NULL; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
130 } |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
131 if (size == 0 && (op = nullstring) != NULL) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
132 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
133 null_strings++; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
134 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
135 Py_INCREF(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
136 return (PyObject *)op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
137 } |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
138 if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
139 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
140 one_strings++; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
141 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
142 Py_INCREF(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
143 return (PyObject *)op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
144 } |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
145 |
|
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
|
146 /* Inline PyObject_NewVar */ |
|
50036
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
147 op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + size); |
| 27 | 148 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
149 return PyErr_NoMemory(); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
150 PyObject_INIT_VAR(op, &PyString_Type, size); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
151 op->ob_shash = -1; |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
152 op->ob_sstate = SSTATE_NOT_INTERNED; |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
153 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
|
154 /* share short strings */ |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
155 if (size == 0) { |
|
17720
ede29ad6440a
Mark Favas reported that gcc caught me using casts as lvalues. Dodge it.
Tim Peters <tim.peters@gmail.com>
parents:
17712
diff
changeset
|
156 PyObject *t = (PyObject *)op; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
157 PyString_InternInPlace(&t); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
158 op = (PyStringObject *)t; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
159 nullstring = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
160 Py_INCREF(op); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
161 } else if (size == 1) { |
|
17720
ede29ad6440a
Mark Favas reported that gcc caught me using casts as lvalues. Dodge it.
Tim Peters <tim.peters@gmail.com>
parents:
17712
diff
changeset
|
162 PyObject *t = (PyObject *)op; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
163 PyString_InternInPlace(&t); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
164 op = (PyStringObject *)t; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
165 characters[*str & UCHAR_MAX] = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
166 Py_INCREF(op); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
167 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
168 return (PyObject *) op; |
| 6 | 169 } |
| 170 | |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
171 PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
172 PyString_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
|
173 { |
|
20221
abaf9ea2e560
SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents:
20140
diff
changeset
|
174 va_list count; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
175 Py_ssize_t n = 0; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
176 const char* f; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
177 char *s; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
178 PyObject* string; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
179 |
|
20221
abaf9ea2e560
SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents:
20140
diff
changeset
|
180 #ifdef VA_LIST_IS_ARRAY |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
181 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
|
182 #else |
|
24400
81ca6ca5c307
Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
183 #ifdef __va_copy |
|
81ca6ca5c307
Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
184 __va_copy(count, vargs); |
|
81ca6ca5c307
Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
185 #else |
|
20221
abaf9ea2e560
SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents:
20140
diff
changeset
|
186 count = vargs; |
|
abaf9ea2e560
SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents:
20140
diff
changeset
|
187 #endif |
|
24400
81ca6ca5c307
Patch #554716: Use __va_copy where available.
Martin v. Löwis <martin@v.loewis.de>
parents:
24280
diff
changeset
|
188 #endif |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
189 /* 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
|
190 for (f = format; *f; f++) { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
191 if (*f == '%') { |
|
57277
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
192 #ifdef HAVE_LONG_LONG |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
193 int longlongflag = 0; |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
194 #endif |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
195 const char* p = f; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
196 while (*++f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
197 ; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
198 |
|
38173
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
199 /* skip the 'l' or 'z' in {%ld, %zd, %lu, %zu} since |
|
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
200 * they don't affect the amount of space we reserve. |
|
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
201 */ |
|
57277
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
202 if (*f == 'l') { |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
203 if (f[1] == 'd' || f[1] == 'u') { |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
204 ++f; |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
205 } |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
206 #ifdef HAVE_LONG_LONG |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
207 else if (f[1] == 'l' && |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
208 (f[2] == 'd' || f[2] == 'u')) { |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
209 longlongflag = 1; |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
210 f += 2; |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
211 } |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
212 #endif |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
213 } |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
214 else if (*f == 'z' && (f[1] == 'd' || f[1] == 'u')) { |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
215 ++f; |
|
57277
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
216 } |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
217 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
218 switch (*f) { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
219 case 'c': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
220 (void)va_arg(count, int); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
221 /* fall through... */ |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
222 case '%': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
223 n++; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
224 break; |
|
38173
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
225 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
|
226 (void) va_arg(count, int); |
|
57277
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
227 #ifdef HAVE_LONG_LONG |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
228 /* Need at most |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
229 ceil(log10(256)*SIZEOF_LONG_LONG) digits, |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
230 plus 1 for the sign. 53/22 is an upper |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
231 bound for log10(256). */ |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
232 if (longlongflag) |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
233 n += 2 + (SIZEOF_LONG_LONG*53-1) / 22; |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
234 else |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
235 #endif |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
236 /* 20 bytes is enough to hold a 64-bit |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
237 integer. Decimal takes the most |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
238 space. This isn't enough for |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
239 octal. */ |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
240 n += 20; |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
241 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
242 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
243 case 's': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
244 s = va_arg(count, char*); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
245 n += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
246 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
247 case 'p': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
248 (void) va_arg(count, int); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
249 /* maximum 64-bit pointer representation: |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
250 * 0xffffffffffffffff |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
251 * 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
|
252 * 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
|
253 */ |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
254 n += 19; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
255 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
256 default: |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
257 /* if we stumble upon an unknown |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
258 formatting code, copy the rest of |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
259 the format string to the output |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
260 string. (we cannot just skip the |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
261 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
|
262 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
|
263 n += strlen(p); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
264 goto expand; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
265 } |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
266 } else |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
267 n++; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
268 } |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
269 expand: |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
270 /* 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
|
271 /* 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
|
272 use sprintf directly instead of the slower PyOS_snprintf. */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
273 string = PyString_FromStringAndSize(NULL, n); |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
274 if (!string) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
275 return NULL; |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
276 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
277 s = PyString_AsString(string); |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
278 |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
279 for (f = format; *f; f++) { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
280 if (*f == '%') { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
281 const char* p = f++; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
282 Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
283 int longflag = 0; |
|
57277
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
284 #ifdef HAVE_LONG_LONG |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
285 int longlongflag = 0; |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
286 #endif |
|
36510
0f3894bb2ada
Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
287 int size_tflag = 0; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
288 /* 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
|
289 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
|
290 n = 0; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
291 while (isdigit(Py_CHARMASK(*f))) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
292 n = (n*10) + *f++ - '0'; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
293 if (*f == '.') { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
294 f++; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
295 n = 0; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
296 while (isdigit(Py_CHARMASK(*f))) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
297 n = (n*10) + *f++ - '0'; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
298 } |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
299 while (*f && *f != '%' && !isalpha(Py_CHARMASK(*f))) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
300 f++; |
|
57277
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
301 /* Handle %ld, %lu, %lld and %llu. */ |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
302 if (*f == 'l') { |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
303 if (f[1] == 'd' || f[1] == 'u') { |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
304 longflag = 1; |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
305 ++f; |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
306 } |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
307 #ifdef HAVE_LONG_LONG |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
308 else if (f[1] == 'l' && |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
309 (f[2] == 'd' || f[2] == 'u')) { |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
310 longlongflag = 1; |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
311 f += 2; |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
312 } |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
313 #endif |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
314 } |
|
36510
0f3894bb2ada
Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
315 /* handle the size_t flag. */ |
|
57277
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
316 else 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
|
317 size_tflag = 1; |
|
0f3894bb2ada
Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
318 ++f; |
|
0f3894bb2ada
Support %zd in PyErr_Format and PyString_FromFormat.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
319 } |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
320 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
321 switch (*f) { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
322 case 'c': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
323 *s++ = va_arg(vargs, int); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
324 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
325 case 'd': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
326 if (longflag) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
327 sprintf(s, "%ld", va_arg(vargs, long)); |
|
57277
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
328 #ifdef HAVE_LONG_LONG |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
329 else if (longlongflag) |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
330 sprintf(s, "%" PY_FORMAT_LONG_LONG "d", |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
331 va_arg(vargs, PY_LONG_LONG)); |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
332 #endif |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
333 else if (size_tflag) |
|
38172
32472ea5f3cd
Revert 43315: Printing of %zd must be signed.
Martin v. Löwis <martin@v.loewis.de>
parents:
37896
diff
changeset
|
334 sprintf(s, "%" PY_FORMAT_SIZE_T "d", |
|
32472ea5f3cd
Revert 43315: Printing of %zd must be signed.
Martin v. Löwis <martin@v.loewis.de>
parents:
37896
diff
changeset
|
335 va_arg(vargs, Py_ssize_t)); |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
336 else |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
337 sprintf(s, "%d", va_arg(vargs, int)); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
338 s += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
339 break; |
|
38173
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
340 case 'u': |
|
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
341 if (longflag) |
|
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
342 sprintf(s, "%lu", |
|
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
343 va_arg(vargs, unsigned long)); |
|
57277
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
344 #ifdef HAVE_LONG_LONG |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
345 else if (longlongflag) |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
346 sprintf(s, "%" PY_FORMAT_LONG_LONG "u", |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
347 va_arg(vargs, PY_LONG_LONG)); |
|
51028709abc3
Issue #7228: Add '%lld' and '%llu' support to PyFormat_FromString,
Mark Dickinson <dickinsm@gmail.com>
parents:
56758
diff
changeset
|
348 #endif |
|
38173
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
349 else if (size_tflag) |
|
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
350 sprintf(s, "%" PY_FORMAT_SIZE_T "u", |
|
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
351 va_arg(vargs, size_t)); |
|
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
352 else |
|
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
353 sprintf(s, "%u", |
|
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
354 va_arg(vargs, unsigned int)); |
|
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
355 s += strlen(s); |
|
380dad395599
Teach PyString_FromFormat, PyErr_Format, and PyString_FromFormatV
Tim Peters <tim.peters@gmail.com>
parents:
38172
diff
changeset
|
356 break; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
357 case 'i': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
358 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
|
359 s += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
360 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
361 case 'x': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
362 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
|
363 s += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
364 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
365 case 's': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
366 p = va_arg(vargs, char*); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
367 i = strlen(p); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
368 if (n > 0 && i > n) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
369 i = n; |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
370 Py_MEMCPY(s, p, i); |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
371 s += i; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
372 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
373 case 'p': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
374 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
|
375 /* %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
|
376 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
|
377 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
|
378 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
|
379 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
|
380 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
|
381 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
|
382 } |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
383 s += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
384 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
385 case '%': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
386 *s++ = '%'; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
387 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
388 default: |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
389 strcpy(s, p); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
390 s += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
391 goto end; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
392 } |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
393 } else |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
394 *s++ = *f; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
395 } |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
396 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
397 end: |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
398 _PyString_Resize(&string, s - PyString_AS_STRING(string)); |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
399 return string; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
400 } |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
401 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
402 PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
403 PyString_FromFormat(const char *format, ...) |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
404 { |
|
19489
3e74318a1b76
Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents:
19486
diff
changeset
|
405 PyObject* ret; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
406 va_list vargs; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
407 |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
408 #ifdef HAVE_STDARG_PROTOTYPES |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
409 va_start(vargs, format); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
410 #else |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
411 va_start(vargs); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
412 #endif |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
413 ret = PyString_FromFormatV(format, vargs); |
|
19489
3e74318a1b76
Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents:
19486
diff
changeset
|
414 va_end(vargs); |
|
3e74318a1b76
Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents:
19486
diff
changeset
|
415 return ret; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
416 } |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
417 |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
418 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
419 PyObject *PyString_Decode(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
420 Py_ssize_t size, |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
421 const char *encoding, |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
422 const char *errors) |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
423 { |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
424 PyObject *v, *str; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
425 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
426 str = PyString_FromStringAndSize(s, size); |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
427 if (str == NULL) |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
428 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
429 v = PyString_AsDecodedString(str, encoding, errors); |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
430 Py_DECREF(str); |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
431 return v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
432 } |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
433 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
434 PyObject *PyString_AsDecodedObject(PyObject *str, |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
435 const char *encoding, |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
436 const char *errors) |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
437 { |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
438 PyObject *v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
439 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
440 if (!PyString_Check(str)) { |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
441 PyErr_BadArgument(); |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
442 goto onError; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
443 } |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
444 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
445 if (encoding == NULL) { |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
446 #ifdef Py_USING_UNICODE |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
447 encoding = PyUnicode_GetDefaultEncoding(); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
448 #else |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
449 PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
450 goto onError; |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
451 #endif |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
452 } |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
453 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
454 /* Decode via the codec registry */ |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
455 v = PyCodec_Decode(str, encoding, errors); |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
456 if (v == NULL) |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
457 goto onError; |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
458 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
459 return v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
460 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
461 onError: |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
462 return NULL; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
463 } |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
464 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
465 PyObject *PyString_AsDecodedString(PyObject *str, |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
466 const char *encoding, |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
467 const char *errors) |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
468 { |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
469 PyObject *v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
470 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
471 v = PyString_AsDecodedObject(str, encoding, errors); |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
472 if (v == NULL) |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
473 goto onError; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
474 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
475 #ifdef Py_USING_UNICODE |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
476 /* Convert Unicode to a string using the default encoding */ |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
477 if (PyUnicode_Check(v)) { |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
478 PyObject *temp = v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
479 v = PyUnicode_AsEncodedString(v, NULL, NULL); |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
480 Py_DECREF(temp); |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
481 if (v == NULL) |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
482 goto onError; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
483 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
484 #endif |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
485 if (!PyString_Check(v)) { |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
486 PyErr_Format(PyExc_TypeError, |
|
13561
ea732719b3fd
Fix typo in error message
Andrew M. Kuchling <amk@amk.ca>
parents:
13544
diff
changeset
|
487 "decoder did not return a string object (type=%.400s)", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
488 Py_TYPE(v)->tp_name); |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
489 Py_DECREF(v); |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
490 goto onError; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
491 } |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
492 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
493 return v; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
494 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
495 onError: |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
496 return NULL; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
497 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
498 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
499 PyObject *PyString_Encode(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
500 Py_ssize_t size, |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
501 const char *encoding, |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
502 const char *errors) |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
503 { |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
504 PyObject *v, *str; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
505 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
506 str = PyString_FromStringAndSize(s, size); |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
507 if (str == NULL) |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
508 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
509 v = PyString_AsEncodedString(str, 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
|
510 Py_DECREF(str); |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
511 return v; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
512 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
513 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
514 PyObject *PyString_AsEncodedObject(PyObject *str, |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
515 const char *encoding, |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
516 const char *errors) |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
517 { |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
518 PyObject *v; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
519 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
520 if (!PyString_Check(str)) { |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
521 PyErr_BadArgument(); |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
522 goto onError; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
523 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
524 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
525 if (encoding == NULL) { |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
526 #ifdef Py_USING_UNICODE |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
527 encoding = PyUnicode_GetDefaultEncoding(); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
528 #else |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
529 PyErr_SetString(PyExc_ValueError, "no encoding specified"); |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
530 goto onError; |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
531 #endif |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
532 } |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
533 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
534 /* Encode via the codec registry */ |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
535 v = PyCodec_Encode(str, encoding, errors); |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
536 if (v == NULL) |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
537 goto onError; |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
538 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
539 return v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
540 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
541 onError: |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
542 return NULL; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
543 } |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
544 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
545 PyObject *PyString_AsEncodedString(PyObject *str, |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
546 const char *encoding, |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
547 const char *errors) |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
548 { |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
549 PyObject *v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
550 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
551 v = PyString_AsEncodedObject(str, encoding, errors); |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
552 if (v == NULL) |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
553 goto onError; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
554 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
555 #ifdef Py_USING_UNICODE |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
556 /* Convert Unicode to a string using the default encoding */ |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
557 if (PyUnicode_Check(v)) { |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
558 PyObject *temp = v; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
559 v = PyUnicode_AsEncodedString(v, NULL, NULL); |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
560 Py_DECREF(temp); |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
561 if (v == NULL) |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
562 goto onError; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
563 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
564 #endif |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
565 if (!PyString_Check(v)) { |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
566 PyErr_Format(PyExc_TypeError, |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
567 "encoder did not return a string object (type=%.400s)", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
568 Py_TYPE(v)->tp_name); |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
569 Py_DECREF(v); |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
570 goto onError; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
571 } |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
572 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
573 return v; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
574 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
575 onError: |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
576 return NULL; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
577 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
578 |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
579 static void |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
580 string_dealloc(PyObject *op) |
| 750 | 581 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
582 switch (PyString_CHECK_INTERNED(op)) { |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
583 case SSTATE_NOT_INTERNED: |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
584 break; |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
585 |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
586 case SSTATE_INTERNED_MORTAL: |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
587 /* revive dead object temporarily for DelItem */ |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
588 Py_REFCNT(op) = 3; |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
589 if (PyDict_DelItem(interned, op) != 0) |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
590 Py_FatalError( |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
591 "deletion of interned string failed"); |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
592 break; |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
593 |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
594 case SSTATE_INTERNED_IMMORTAL: |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
595 Py_FatalError("Immortal interned string died."); |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
596 |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
597 default: |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
598 Py_FatalError("Inconsistent interned string state."); |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
599 } |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
600 Py_TYPE(op)->tp_free(op); |
| 750 | 601 } |
| 602 | |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
603 /* 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
|
604 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
|
605 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
|
606 specified encoding. */ |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
607 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
608 PyObject *PyString_DecodeEscape(const char *s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
609 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
|
610 const char *errors, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
611 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
|
612 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
|
613 { |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
614 int c; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
615 char *p, *buf; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
616 const char *end; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
617 PyObject *v; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
618 Py_ssize_t newlen = recode_encoding ? 4*len:len; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
619 v = PyString_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
|
620 if (v == NULL) |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
621 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
622 p = buf = PyString_AsString(v); |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
623 end = s + len; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
624 while (s < end) { |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
625 if (*s != '\\') { |
|
25173
f4e9f09df4f2
Fix escaping of non-ASCII characters.
Martin v. Löwis <martin@v.loewis.de>
parents:
25100
diff
changeset
|
626 non_esc: |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
627 #ifdef Py_USING_UNICODE |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
628 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
|
629 PyObject *u, *w; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
630 char *r; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
631 const char* t; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
632 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
|
633 t = s; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
634 /* 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
|
635 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
|
636 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
|
637 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
|
638 |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
639 /* 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
|
640 w = PyUnicode_AsEncodedString( |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
641 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
|
642 Py_DECREF(u); |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
643 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
|
644 |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
645 /* Append bytes to output buffer. */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
646 assert(PyString_Check(w)); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
647 r = PyString_AS_STRING(w); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
648 rn = PyString_GET_SIZE(w); |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
649 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
|
650 p += rn; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
651 Py_DECREF(w); |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
652 s = t; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
653 } else { |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
654 *p++ = *s++; |
|
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 #else |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
657 *p++ = *s++; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
658 #endif |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
659 continue; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
660 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
661 s++; |
|
24809
c75cdc471b22
Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents:
24808
diff
changeset
|
662 if (s==end) { |
|
c75cdc471b22
Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents:
24808
diff
changeset
|
663 PyErr_SetString(PyExc_ValueError, |
|
c75cdc471b22
Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents:
24808
diff
changeset
|
664 "Trailing \\ in string"); |
|
c75cdc471b22
Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents:
24808
diff
changeset
|
665 goto failed; |
|
c75cdc471b22
Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents:
24808
diff
changeset
|
666 } |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
667 switch (*s++) { |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
668 /* 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
|
669 case '\n': break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
670 case '\\': *p++ = '\\'; break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
671 case '\'': *p++ = '\''; break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
672 case '\"': *p++ = '\"'; break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
673 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
|
674 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
|
675 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
|
676 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
|
677 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
|
678 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
|
679 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
|
680 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
|
681 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
|
682 c = s[-1] - '0'; |
|
43710
124cbf1a0e3e
Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
683 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
|
684 c = (c<<3) + *s++ - '0'; |
|
43710
124cbf1a0e3e
Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
685 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
|
686 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
|
687 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
688 *p++ = c; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
689 break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
690 case 'x': |
|
43710
124cbf1a0e3e
Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
691 if (s+1 < end && |
|
124cbf1a0e3e
Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
692 isxdigit(Py_CHARMASK(s[0])) && |
|
124cbf1a0e3e
Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
693 isxdigit(Py_CHARMASK(s[1]))) |
|
124cbf1a0e3e
Backport fixes for the code that decodes octal escapes (and for PyString
Guido van Rossum <guido@python.org>
parents:
43378
diff
changeset
|
694 { |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
695 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
|
696 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
|
697 s++; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
698 if (isdigit(c)) |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
699 x = c - '0'; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
700 else if (islower(c)) |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
701 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
|
702 else |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
703 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
|
704 x = x << 4; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
705 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
|
706 s++; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
707 if (isdigit(c)) |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
708 x += c - '0'; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
709 else if (islower(c)) |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
710 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
|
711 else |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
712 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
|
713 *p++ = x; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
714 break; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
715 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
716 if (!errors || strcmp(errors, "strict") == 0) { |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
717 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
|
718 "invalid \\x escape"); |
|
24809
c75cdc471b22
Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents:
24808
diff
changeset
|
719 goto failed; |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
720 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
721 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
|
722 *p++ = '?'; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
723 } 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
|
724 /* do nothing */; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
725 else { |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
726 PyErr_Format(PyExc_ValueError, |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
727 "decoding error; " |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
728 "unknown error handling code: %.400s", |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
729 errors); |
|
24809
c75cdc471b22
Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents:
24808
diff
changeset
|
730 goto failed; |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
731 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
732 #ifndef Py_USING_UNICODE |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
733 case 'u': |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
734 case 'U': |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
735 case 'N': |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
736 if (unicode) { |
|
24872
cf561e612378
Get this to compile again if Py_USING_UNICODE is not defined.
Neal Norwitz <nnorwitz@gmail.com>
parents:
24831
diff
changeset
|
737 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
|
738 "Unicode escapes not legal " |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
739 "when Unicode disabled"); |
|
24809
c75cdc471b22
Check for trailing backslash. Fixes #593656.
Martin v. Löwis <martin@v.loewis.de>
parents:
24808
diff
changeset
|
740 goto failed; |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
741 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
742 #endif |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
743 default: |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
744 *p++ = '\\'; |
|
25173
f4e9f09df4f2
Fix escaping of non-ASCII characters.
Martin v. Löwis <martin@v.loewis.de>
parents:
25100
diff
changeset
|
745 s--; |
|
f4e9f09df4f2
Fix escaping of non-ASCII characters.
Martin v. Löwis <martin@v.loewis.de>
parents:
25100
diff
changeset
|
746 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
|
747 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
|
748 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
749 } |
|
25100
bc590beadf0b
Check whether a string resize is necessary at the end
Walter Dörwald <walter@livinglogic.de>
parents:
25091
diff
changeset
|
750 if (p-buf < newlen) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
751 _PyString_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
|
752 return v; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
753 failed: |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
754 Py_DECREF(v); |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
755 return NULL; |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
756 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
757 |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
758 /* -------------------------------------------------------------------- */ |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
759 /* object api */ |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
760 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
761 static Py_ssize_t |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
762 string_getsize(register PyObject *op) |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
763 { |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
764 char *s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
765 Py_ssize_t len; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
766 if (PyString_AsStringAndSize(op, &s, &len)) |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
767 return -1; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
768 return len; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
769 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
770 |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
771 static /*const*/ char * |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
772 string_getbuffer(register PyObject *op) |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
773 { |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
774 char *s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
775 Py_ssize_t len; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
776 if (PyString_AsStringAndSize(op, &s, &len)) |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
777 return NULL; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
778 return s; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
779 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
780 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
781 Py_ssize_t |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
782 PyString_Size(register PyObject *op) |
| 6 | 783 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
784 if (!PyString_Check(op)) |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
785 return string_getsize(op); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
786 return Py_SIZE(op); |
| 6 | 787 } |
| 788 | |
| 789 /*const*/ char * | |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
790 PyString_AsString(register PyObject *op) |
| 6 | 791 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
792 if (!PyString_Check(op)) |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
793 return string_getbuffer(op); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
794 return ((PyStringObject *)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
|
795 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
796 |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
797 int |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
798 PyString_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
|
799 register char **s, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
800 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
|
801 { |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
802 if (s == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
803 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
|
804 return -1; |
| 6 | 805 } |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
806 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
807 if (!PyString_Check(obj)) { |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
808 #ifdef Py_USING_UNICODE |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
809 if (PyUnicode_Check(obj)) { |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
810 obj = _PyUnicode_AsDefaultEncodedString(obj, NULL); |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
811 if (obj == NULL) |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
812 return -1; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
813 } |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
814 else |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
815 #endif |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
816 { |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
817 PyErr_Format(PyExc_TypeError, |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
818 "expected string or Unicode object, " |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
819 "%.200s found", Py_TYPE(obj)->tp_name); |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
820 return -1; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
821 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
822 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
823 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
824 *s = PyString_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
|
825 if (len != NULL) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
826 *len = PyString_GET_SIZE(obj); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
827 else if (strlen(*s) != (size_t)PyString_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
|
828 PyErr_SetString(PyExc_TypeError, |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
829 "expected string without null bytes"); |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
830 return -1; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
831 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
832 return 0; |
| 6 | 833 } |
| 834 | |
|
38303
17eaa8b57e05
needforspeed: use "fastsearch" for count. this results in a 3x speedup
Fredrik Lundh <fredrik@pythonware.com>
parents:
38302
diff
changeset
|
835 /* -------------------------------------------------------------------- */ |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
836 /* Methods */ |
|
38381
b32f9bb2e077
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38376
diff
changeset
|
837 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
838 #include "stringlib/stringdefs.h" |
|
38381
b32f9bb2e077
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38376
diff
changeset
|
839 #include "stringlib/fastsearch.h" |
|
38404
52f2c9f74f5a
needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
840 |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
841 #include "stringlib/count.h" |
|
38404
52f2c9f74f5a
needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
842 #include "stringlib/find.h" |
|
38383
05744917b7c3
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38381
diff
changeset
|
843 #include "stringlib/partition.h" |
|
38381
b32f9bb2e077
needforspeed: stringlib refactoring (in progress)
Fredrik Lundh <fredrik@pythonware.com>
parents:
38376
diff
changeset
|
844 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
845 #define _Py_InsertThousandsGrouping _PyString_InsertThousandsGrouping |
|
46898
206700d749f0
Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents:
46871
diff
changeset
|
846 #include "stringlib/localeutil.h" |
|
206700d749f0
Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents:
46871
diff
changeset
|
847 |
|
206700d749f0
Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents:
46871
diff
changeset
|
848 |
| 6 | 849 |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
850 static int |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
851 string_print(PyStringObject *op, FILE *fp, int flags) |
| 6 | 852 { |
|
43378
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42951
diff
changeset
|
853 Py_ssize_t i, str_len; |
| 6 | 854 char c; |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
855 int quote; |
|
20464
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
856 |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
857 /* XXX Ought to check for interrupts when writing long strings */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
858 if (! PyString_CheckExact(op)) { |
|
20464
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
859 int ret; |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
860 /* A str subclass may have its own __str__ method. */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
861 op = (PyStringObject *) PyObject_Str((PyObject *)op); |
|
20464
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
862 if (op == NULL) |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
863 return -1; |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
864 ret = string_print(op, fp, flags); |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
865 Py_DECREF(op); |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
866 return ret; |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
867 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
868 if (flags & Py_PRINT_RAW) { |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
869 char *data = op->ob_sval; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
870 Py_ssize_t size = Py_SIZE(op); |
|
43378
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42951
diff
changeset
|
871 Py_BEGIN_ALLOW_THREADS |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
872 while (size > INT_MAX) { |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
873 /* Very long strings cannot be written atomically. |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
874 * But don't write exactly INT_MAX bytes at a time |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
875 * to avoid memory aligment issues. |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
876 */ |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
877 const int chunk_size = INT_MAX & ~0x3FFF; |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
878 fwrite(data, 1, chunk_size, fp); |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
879 data += chunk_size; |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
880 size -= chunk_size; |
|
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
881 } |
|
26550
b9c912e2c3c0
Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents:
26273
diff
changeset
|
882 #ifdef __VMS |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
883 if (size) fwrite(data, (int)size, 1, fp); |
|
26550
b9c912e2c3c0
Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents:
26273
diff
changeset
|
884 #else |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
885 fwrite(data, 1, (int)size, fp); |
|
26550
b9c912e2c3c0
Patch #614055: Support OpenVMS.
Martin v. Löwis <martin@v.loewis.de>
parents:
26273
diff
changeset
|
886 #endif |
|
43378
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42951
diff
changeset
|
887 Py_END_ALLOW_THREADS |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
888 return 0; |
| 6 | 889 } |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
890 |
|
13620
5b727ab37092
Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents:
13576
diff
changeset
|
891 /* 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
|
892 quote = '\''; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
893 if (memchr(op->ob_sval, '\'', Py_SIZE(op)) && |
|
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
894 !memchr(op->ob_sval, '"', Py_SIZE(op))) |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
895 quote = '"'; |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
896 |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
897 str_len = Py_SIZE(op); |
|
43378
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42951
diff
changeset
|
898 Py_BEGIN_ALLOW_THREADS |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
899 fputc(quote, fp); |
|
43378
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42951
diff
changeset
|
900 for (i = 0; i < str_len; i++) { |
|
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42951
diff
changeset
|
901 /* Since strings are immutable and the caller should have a |
|
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42951
diff
changeset
|
902 reference, accessing the interal buffer should not be an issue |
|
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42951
diff
changeset
|
903 with the GIL released. */ |
| 6 | 904 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
|
905 if (c == quote || c == '\\') |
| 25721 | 906 fprintf(fp, "\\%c", c); |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
907 else if (c == '\t') |
| 25721 | 908 fprintf(fp, "\\t"); |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
909 else if (c == '\n') |
| 25721 | 910 fprintf(fp, "\\n"); |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
911 else if (c == '\r') |
| 25721 | 912 fprintf(fp, "\\r"); |
| 913 else if (c < ' ' || c >= 0x7f) | |
| 914 fprintf(fp, "\\x%02x", c & 0xff); | |
| 6 | 915 else |
| 25721 | 916 fputc(c, fp); |
| 6 | 917 } |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
918 fputc(quote, fp); |
|
43378
7630248b593c
Add a bunch of GIL release/acquire points in tp_print implementations and for
Brett Cannon <bcannon@gmail.com>
parents:
42951
diff
changeset
|
919 Py_END_ALLOW_THREADS |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
920 return 0; |
| 6 | 921 } |
| 922 | |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
923 PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
924 PyString_Repr(PyObject *obj, int smartquotes) |
| 6 | 925 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
926 register PyStringObject* op = (PyStringObject*) obj; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
927 size_t newsize = 2 + 4 * Py_SIZE(op); |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
928 PyObject *v; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
929 if (newsize > PY_SSIZE_T_MAX || newsize / 4 != Py_SIZE(op)) { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
930 PyErr_SetString(PyExc_OverflowError, |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
931 "string is too large to make repr"); |
|
43780
7347f75b9c34
Add missing "return NULL" in overflow check in PyObject_Repr().
Guido van Rossum <guido@python.org>
parents:
43710
diff
changeset
|
932 return NULL; |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
933 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
934 v = PyString_FromStringAndSize((char *)NULL, newsize); |
| 6 | 935 if (v == NULL) { |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
936 return NULL; |
| 6 | 937 } |
| 938 else { | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
939 register Py_ssize_t i; |
| 6 | 940 register char c; |
| 941 register char *p; | |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
942 int quote; |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
943 |
|
13620
5b727ab37092
Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents:
13576
diff
changeset
|
944 /* 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
|
945 quote = '\''; |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
946 if (smartquotes && |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
947 memchr(op->ob_sval, '\'', Py_SIZE(op)) && |
|
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
948 !memchr(op->ob_sval, '"', Py_SIZE(op))) |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
949 quote = '"'; |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
950 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
951 p = PyString_AS_STRING(v); |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
952 *p++ = quote; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
953 for (i = 0; i < Py_SIZE(op); i++) { |
|
21147
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
954 /* 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
|
955 and a closing quote. */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
956 assert(newsize - (p - PyString_AS_STRING(v)) >= 5); |
| 6 | 957 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
|
958 if (c == quote || c == '\\') |
| 6 | 959 *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
|
960 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
|
961 *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
|
962 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
|
963 *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
|
964 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
|
965 *p++ = '\\', *p++ = 'r'; |
| 25721 | 966 else if (c < ' ' || c >= 0x7f) { |
| 967 /* For performance, we don't want to call | |
| 968 PyOS_snprintf here (extra layers of | |
| 969 function call). */ | |
| 970 sprintf(p, "\\x%02x", c & 0xff); | |
| 971 p += 4; | |
| 972 } | |
| 973 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
|
974 *p++ = c; |
| 6 | 975 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
976 assert(newsize - (p - PyString_AS_STRING(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
|
977 *p++ = quote; |
| 6 | 978 *p = '\0'; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
979 _PyString_Resize( |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
980 &v, (p - PyString_AS_STRING(v))); |
| 27 | 981 return v; |
| 6 | 982 } |
| 983 } | |
| 984 | |
|
17645
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
985 static PyObject * |
|
24808
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
986 string_repr(PyObject *op) |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
987 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
988 return PyString_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
|
989 } |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
990 |
|
ab63af3ec349
Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents:
24736
diff
changeset
|
991 static PyObject * |
|
17645
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
992 string_str(PyObject *s) |
|
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
993 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
994 assert(PyString_Check(s)); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
995 if (PyString_CheckExact(s)) { |
|
20464
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
996 Py_INCREF(s); |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
997 return s; |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
998 } |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
999 else { |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
1000 /* Subtype -- return genuine string with the same value. */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1001 PyStringObject *t = (PyStringObject *) s; |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1002 return PyString_FromStringAndSize(t->ob_sval, Py_SIZE(t)); |
|
20464
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
1003 } |
|
17645
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
1004 } |
|
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
1005 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1006 static Py_ssize_t |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1007 string_length(PyStringObject *a) |
| 6 | 1008 { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1009 return Py_SIZE(a); |
| 6 | 1010 } |
| 1011 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1012 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1013 string_concat(register PyStringObject *a, register PyObject *bb) |
| 6 | 1014 { |
|
38306
c5d61965cad4
Added overflow test for adding two (very) large strings where the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38305
diff
changeset
|
1015 register Py_ssize_t size; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1016 register PyStringObject *op; |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1017 if (!PyString_Check(bb)) { |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1018 #ifdef Py_USING_UNICODE |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1019 if (PyUnicode_Check(bb)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1020 return PyUnicode_Concat((PyObject *)a, bb); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1021 #endif |
|
47291
04bd3690fb1f
First step of the C API rename:
Christian Heimes <christian@cheimes.de>
parents:
46908
diff
changeset
|
1022 if (PyByteArray_Check(bb)) |
|
04bd3690fb1f
First step of the C API rename:
Christian Heimes <christian@cheimes.de>
parents:
46908
diff
changeset
|
1023 return PyByteArray_Concat((PyObject *)a, bb); |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
1024 PyErr_Format(PyExc_TypeError, |
|
20604
7945639df265
Make the error message for unsupported operand types cleaner, in
Guido van Rossum <guido@python.org>
parents:
20464
diff
changeset
|
1025 "cannot concatenate 'str' and '%.200s' objects", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1026 Py_TYPE(bb)->tp_name); |
| 6 | 1027 return NULL; |
| 1028 } | |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1029 #define b ((PyStringObject *)bb) |
| 6 | 1030 /* Optimize cases with empty left or right operand */ |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1031 if ((Py_SIZE(a) == 0 || Py_SIZE(b) == 0) && |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1032 PyString_CheckExact(a) && PyString_CheckExact(b)) { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1033 if (Py_SIZE(a) == 0) { |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
1034 Py_INCREF(bb); |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
1035 return bb; |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
1036 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1037 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1038 return (PyObject *)a; |
| 6 | 1039 } |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1040 size = Py_SIZE(a) + Py_SIZE(b); |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48003
diff
changeset
|
1041 /* Check that string sizes are not negative, to prevent an |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48003
diff
changeset
|
1042 overflow in cases where we are passed incorrectly-created |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48003
diff
changeset
|
1043 strings with negative lengths (due to a bug in other code). |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48003
diff
changeset
|
1044 */ |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48003
diff
changeset
|
1045 if (Py_SIZE(a) < 0 || Py_SIZE(b) < 0 || |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48003
diff
changeset
|
1046 Py_SIZE(a) > PY_SSIZE_T_MAX - Py_SIZE(b)) { |
|
38306
c5d61965cad4
Added overflow test for adding two (very) large strings where the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38305
diff
changeset
|
1047 PyErr_SetString(PyExc_OverflowError, |
|
c5d61965cad4
Added overflow test for adding two (very) large strings where the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38305
diff
changeset
|
1048 "strings are too large to concat"); |
|
c5d61965cad4
Added overflow test for adding two (very) large strings where the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38305
diff
changeset
|
1049 return NULL; |
|
c5d61965cad4
Added overflow test for adding two (very) large strings where the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38305
diff
changeset
|
1050 } |
|
50036
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
1051 |
|
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
|
1052 /* Inline PyObject_NewVar */ |
|
50036
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
1053 if (size > PY_SSIZE_T_MAX - PyStringObject_SIZE) { |
|
48114
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48003
diff
changeset
|
1054 PyErr_SetString(PyExc_OverflowError, |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48003
diff
changeset
|
1055 "strings are too large to concat"); |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48003
diff
changeset
|
1056 return NULL; |
|
c4d24fc8ba8a
Security patches from Apple: prevent int overflow when allocating memory
Neal Norwitz <nnorwitz@gmail.com>
parents:
48003
diff
changeset
|
1057 } |
|
50036
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
1058 op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + size); |
| 27 | 1059 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1060 return PyErr_NoMemory(); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1061 PyObject_INIT_VAR(op, &PyString_Type, size); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
1062 op->ob_shash = -1; |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
1063 op->ob_sstate = SSTATE_NOT_INTERNED; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1064 Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a)); |
|
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1065 Py_MEMCPY(op->ob_sval + Py_SIZE(a), b->ob_sval, Py_SIZE(b)); |
| 27 | 1066 op->ob_sval[size] = '\0'; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1067 return (PyObject *) op; |
| 6 | 1068 #undef b |
| 1069 } | |
| 1070 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1071 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1072 string_repeat(register PyStringObject *a, register Py_ssize_t n) |
| 6 | 1073 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1074 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1075 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1076 register Py_ssize_t size; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1077 register PyStringObject *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
|
1078 size_t nbytes; |
| 6 | 1079 if (n < 0) |
| 1080 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
|
1081 /* 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
|
1082 * 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
|
1083 */ |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1084 size = Py_SIZE(a) * n; |
|
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1085 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
|
1086 PyErr_SetString(PyExc_OverflowError, |
|
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
|
1087 "repeated string is too long"); |
|
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
|
1088 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
|
1089 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1090 if (size == Py_SIZE(a) && PyString_CheckExact(a)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1091 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1092 return (PyObject *)a; |
| 6 | 1093 } |
|
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
|
1094 nbytes = (size_t)size; |
|
50036
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
1095 if (nbytes + PyStringObject_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
|
1096 PyErr_SetString(PyExc_OverflowError, |
|
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
|
1097 "repeated string is too long"); |
|
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
|
1098 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
|
1099 } |
|
50036
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
1100 op = (PyStringObject *)PyObject_MALLOC(PyStringObject_SIZE + nbytes); |
| 27 | 1101 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1102 return PyErr_NoMemory(); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1103 PyObject_INIT_VAR(op, &PyString_Type, size); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
1104 op->ob_shash = -1; |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
1105 op->ob_sstate = SSTATE_NOT_INTERNED; |
|
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
|
1106 op->ob_sval[size] = '\0'; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1107 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
|
1108 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
|
1109 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
|
1110 } |
|
27101
6d7daa5329c6
Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents:
26825
diff
changeset
|
1111 i = 0; |
|
6d7daa5329c6
Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents:
26825
diff
changeset
|
1112 if (i < size) { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1113 Py_MEMCPY(op->ob_sval, a->ob_sval, Py_SIZE(a)); |
|
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1114 i = Py_SIZE(a); |
|
27101
6d7daa5329c6
Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents:
26825
diff
changeset
|
1115 } |
|
6d7daa5329c6
Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents:
26825
diff
changeset
|
1116 while (i < size) { |
|
6d7daa5329c6
Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents:
26825
diff
changeset
|
1117 j = (i <= size-i) ? i : size-i; |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
1118 Py_MEMCPY(op->ob_sval+i, op->ob_sval, j); |
|
27101
6d7daa5329c6
Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents:
26825
diff
changeset
|
1119 i += j; |
|
6d7daa5329c6
Optimize string_repeat.
Raymond Hettinger <python@rcn.com>
parents:
26825
diff
changeset
|
1120 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1121 return (PyObject *) op; |
| 6 | 1122 } |
| 1123 | |
| 1124 /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ | |
| 1125 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1126 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1127 string_slice(register PyStringObject *a, register Py_ssize_t i, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1128 register Py_ssize_t j) |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1129 /* j -- may be negative! */ |
| 6 | 1130 { |
| 1131 if (i < 0) | |
| 1132 i = 0; | |
| 1133 if (j < 0) | |
| 1134 j = 0; /* Avoid signed/unsigned bug in next line */ | |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1135 if (j > Py_SIZE(a)) |
|
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1136 j = Py_SIZE(a); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1137 if (i == 0 && j == Py_SIZE(a) && PyString_CheckExact(a)) { |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
1138 /* It's the same as a */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1139 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1140 return (PyObject *)a; |
| 6 | 1141 } |
| 1142 if (j < i) | |
| 1143 j = i; | |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1144 return PyString_FromStringAndSize(a->ob_sval + i, j-i); |
| 6 | 1145 } |
| 1146 | |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
1147 static int |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
1148 string_contains(PyObject *str_obj, PyObject *sub_obj) |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
1149 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1150 if (!PyString_CheckExact(sub_obj)) { |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1151 #ifdef Py_USING_UNICODE |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
1152 if (PyUnicode_Check(sub_obj)) |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
1153 return PyUnicode_Contains(str_obj, sub_obj); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1154 #endif |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1155 if (!PyString_Check(sub_obj)) { |
|
40357
b871e7bd8355
Patch [ 1586791 ] better error msgs for some TypeErrors
Georg Brandl <georg@python.org>
parents:
39995
diff
changeset
|
1156 PyErr_Format(PyExc_TypeError, |
|
b871e7bd8355
Patch [ 1586791 ] better error msgs for some TypeErrors
Georg Brandl <georg@python.org>
parents:
39995
diff
changeset
|
1157 "'in <string>' requires string as left operand, " |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1158 "not %.200s", Py_TYPE(sub_obj)->tp_name); |
|
25017
aa4503556339
string_contains(): speed up by avoiding function calls where
Guido van Rossum <guido@python.org>
parents:
25004
diff
changeset
|
1159 return -1; |
|
aa4503556339
string_contains(): speed up by avoiding function calls where
Guido van Rossum <guido@python.org>
parents:
25004
diff
changeset
|
1160 } |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
1161 } |
|
34620
556f6f1056a8
* Beef-up testing of str.__contains__() and str.find().
Raymond Hettinger <python@rcn.com>
parents:
34522
diff
changeset
|
1162 |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
1163 return stringlib_contains_obj(str_obj, sub_obj); |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
1164 } |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
1165 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1166 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1167 string_item(PyStringObject *a, register Py_ssize_t i) |
| 6 | 1168 { |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
1169 char pchar; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1170 PyObject *v; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1171 if (i < 0 || i >= Py_SIZE(a)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1172 PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6 | 1173 return NULL; |
| 1174 } | |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
1175 pchar = a->ob_sval[i]; |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
1176 v = (PyObject *)characters[pchar & UCHAR_MAX]; |
|
17710
6199e5bd370b
Intern 1-character strings as soon as they're created. As-is, they aren't
Tim Peters <tim.peters@gmail.com>
parents:
17687
diff
changeset
|
1177 if (v == NULL) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1178 v = PyString_FromStringAndSize(&pchar, 1); |
|
17712
2a7cfc172a68
Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents:
17711
diff
changeset
|
1179 else { |
|
2a7cfc172a68
Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents:
17711
diff
changeset
|
1180 #ifdef COUNT_ALLOCS |
|
2a7cfc172a68
Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents:
17711
diff
changeset
|
1181 one_strings++; |
|
2a7cfc172a68
Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents:
17711
diff
changeset
|
1182 #endif |
|
17711
350be19ca6a5
My change to string_item() left an extra reference to each 1-character
Tim Peters <tim.peters@gmail.com>
parents:
17710
diff
changeset
|
1183 Py_INCREF(v); |
|
17712
2a7cfc172a68
Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents:
17711
diff
changeset
|
1184 } |
|
180
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
1185 return v; |
| 6 | 1186 } |
| 1187 | |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1188 static PyObject* |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1189 string_richcompare(PyStringObject *a, PyStringObject *b, int op) |
| 6 | 1190 { |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1191 int c; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1192 Py_ssize_t len_a, len_b; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1193 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
|
1194 PyObject *result; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1195 |
|
20140
225f17ec8932
Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents:
20057
diff
changeset
|
1196 /* Make sure both arguments are strings. */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1197 if (!(PyString_Check(a) && PyString_Check(b))) { |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1198 result = Py_NotImplemented; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1199 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1200 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1201 if (a == b) { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1202 switch (op) { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1203 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
|
1204 result = Py_True; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1205 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1206 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
|
1207 result = Py_False; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1208 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1209 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1210 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1211 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
|
1212 /* 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
|
1213 much time, since Py_NE is rarely used. */ |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1214 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
|
1215 && (a->ob_sval[0] == b->ob_sval[0] |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1216 && 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
|
1217 result = Py_True; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1218 } else { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1219 result = Py_False; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1220 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1221 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1222 } |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1223 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
|
1224 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
|
1225 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
|
1226 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
|
1227 if (c==0) |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1228 c = memcmp(a->ob_sval, b->ob_sval, min_len); |
|
40902
65cd1f280e63
Whitespace only changes
Neal Norwitz <nnorwitz@gmail.com>
parents:
40901
diff
changeset
|
1229 } else |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1230 c = 0; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1231 if (c == 0) |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1232 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
|
1233 switch (op) { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1234 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
|
1235 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
|
1236 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
|
1237 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
|
1238 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
|
1239 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
|
1240 default: |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1241 result = Py_NotImplemented; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1242 goto out; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
1243 } |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1244 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
|
1245 out: |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1246 Py_INCREF(result); |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1247 return result; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1248 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1249 |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1250 int |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1251 _PyString_Eq(PyObject *o1, PyObject *o2) |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
1252 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1253 PyStringObject *a = (PyStringObject*) o1; |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1254 PyStringObject *b = (PyStringObject*) o2; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1255 return 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
|
1256 && *a->ob_sval == *b->ob_sval |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1257 && memcmp(a->ob_sval, b->ob_sval, Py_SIZE(a)) == 0; |
| 6 | 1258 } |
| 1259 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1260 static long |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1261 string_hash(PyStringObject *a) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1262 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1263 register Py_ssize_t len; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
1264 register unsigned char *p; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
1265 register long x; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
1266 |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
1267 if (a->ob_shash != -1) |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
1268 return a->ob_shash; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1269 len = Py_SIZE(a); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
1270 p = (unsigned char *) a->ob_sval; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
1271 x = *p << 7; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1272 while (--len >= 0) |
|
4283
839f72610ae1
Multiply by 1000003 instead of 3 in string hach
Guido van Rossum <guido@python.org>
parents:
3586
diff
changeset
|
1273 x = (1000003*x) ^ *p++; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1274 x ^= Py_SIZE(a); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1275 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1276 x = -2; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
1277 a->ob_shash = x; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1278 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1279 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
1280 |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1281 static PyObject* |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1282 string_subscript(PyStringObject* self, PyObject* item) |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1283 { |
|
39463
6cafdc81cfde
Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents:
39303
diff
changeset
|
1284 if (PyIndex_Check(item)) { |
|
6cafdc81cfde
Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents:
39303
diff
changeset
|
1285 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
|
1286 if (i == -1 && PyErr_Occurred()) |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1287 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1288 if (i < 0) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1289 i += PyString_GET_SIZE(self); |
|
36882
6a9c7165b17b
Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents:
36877
diff
changeset
|
1290 return string_item(self, i); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1291 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1292 else if (PySlice_Check(item)) { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1293 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
|
1294 char* source_buf; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1295 char* result_buf; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1296 PyObject* result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1297 |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
1298 if (PySlice_GetIndicesEx((PySliceObject*)item, |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1299 PyString_GET_SIZE(self), |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1300 &start, &stop, &step, &slicelength) < 0) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1301 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1302 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1303 |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1304 if (slicelength <= 0) { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1305 return PyString_FromStringAndSize("", 0); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1306 } |
|
42951
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42402
diff
changeset
|
1307 else if (start == 0 && step == 1 && |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1308 slicelength == PyString_GET_SIZE(self) && |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1309 PyString_CheckExact(self)) { |
|
42951
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42402
diff
changeset
|
1310 Py_INCREF(self); |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42402
diff
changeset
|
1311 return (PyObject *)self; |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42402
diff
changeset
|
1312 } |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42402
diff
changeset
|
1313 else if (step == 1) { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1314 return PyString_FromStringAndSize( |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1315 PyString_AS_STRING(self) + start, |
|
42951
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42402
diff
changeset
|
1316 slicelength); |
|
9620134010a5
Improve extended slicing support in builtin types and classes. Specifically:
Thomas Wouters <thomas@python.org>
parents:
42402
diff
changeset
|
1317 } |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1318 else { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1319 source_buf = PyString_AsString((PyObject*)self); |
|
37596
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
1320 result_buf = (char *)PyMem_Malloc(slicelength); |
|
35730
8bec120dbb16
SF bug #1331563 ] string_subscript doesn't check for failed PyMem_Malloc. Will backport
Neal Norwitz <nnorwitz@gmail.com>
parents:
35665
diff
changeset
|
1321 if (result_buf == NULL) |
|
8bec120dbb16
SF bug #1331563 ] string_subscript doesn't check for failed PyMem_Malloc. Will backport
Neal Norwitz <nnorwitz@gmail.com>
parents:
35665
diff
changeset
|
1322 return PyErr_NoMemory(); |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1323 |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
1324 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
|
1325 cur += step, i++) { |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1326 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
|
1327 } |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
1328 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1329 result = PyString_FromStringAndSize(result_buf, |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1330 slicelength); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1331 PyMem_Free(result_buf); |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1332 return result; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1333 } |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
1334 } |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1335 else { |
|
40357
b871e7bd8355
Patch [ 1586791 ] better error msgs for some TypeErrors
Georg Brandl <georg@python.org>
parents:
39995
diff
changeset
|
1336 PyErr_Format(PyExc_TypeError, |
|
b871e7bd8355
Patch [ 1586791 ] better error msgs for some TypeErrors
Georg Brandl <georg@python.org>
parents:
39995
diff
changeset
|
1337 "string indices must be integers, not %.200s", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1338 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
|
1339 return NULL; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1340 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1341 } |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1342 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1343 static Py_ssize_t |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1344 string_buffer_getreadbuf(PyStringObject *self, Py_ssize_t index, const void **ptr) |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1345 { |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1346 if ( index != 0 ) { |
|
6286
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
1347 PyErr_SetString(PyExc_SystemError, |
|
9305
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
1348 "accessing non-existent string segment"); |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1349 return -1; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1350 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1351 *ptr = (void *)self->ob_sval; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1352 return Py_SIZE(self); |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1353 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1354 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1355 static Py_ssize_t |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1356 string_buffer_getwritebuf(PyStringObject *self, Py_ssize_t index, const void **ptr) |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1357 { |
|
6286
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
1358 PyErr_SetString(PyExc_TypeError, |
|
9191
4fe8a7670c0d
Typo reported by Greg Stein: "modifiable" is the correct spelling.
Guido van Rossum <guido@python.org>
parents:
8468
diff
changeset
|
1359 "Cannot use string as modifiable buffer"); |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1360 return -1; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1361 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1362 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1363 static Py_ssize_t |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1364 string_buffer_getsegcount(PyStringObject *self, Py_ssize_t *lenp) |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1365 { |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1366 if ( lenp ) |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1367 *lenp = Py_SIZE(self); |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1368 return 1; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1369 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1370 |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1371 static Py_ssize_t |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1372 string_buffer_getcharbuf(PyStringObject *self, Py_ssize_t index, const char **ptr) |
|
9305
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
1373 { |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
1374 if ( index != 0 ) { |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
1375 PyErr_SetString(PyExc_SystemError, |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
1376 "accessing non-existent string segment"); |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
1377 return -1; |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
1378 } |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
1379 *ptr = self->ob_sval; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1380 return Py_SIZE(self); |
|
9305
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
1381 } |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
1382 |
|
46107
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45731
diff
changeset
|
1383 static int |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1384 string_buffer_getbuffer(PyStringObject *self, Py_buffer *view, int flags) |
|
46107
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45731
diff
changeset
|
1385 { |
|
48221
b8c4bf393794
Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents:
48139
diff
changeset
|
1386 return PyBuffer_FillInfo(view, (PyObject*)self, |
|
b8c4bf393794
Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents:
48139
diff
changeset
|
1387 (void *)self->ob_sval, Py_SIZE(self), |
|
48139
97607fcb7b26
Preemptively backport the relevant parts of r65420
Antoine Pitrou <solipsis@pitrou.net>
parents:
48114
diff
changeset
|
1388 1, flags); |
|
46107
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45731
diff
changeset
|
1389 } |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45731
diff
changeset
|
1390 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
1391 static PySequenceMethods string_as_sequence = { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1392 (lenfunc)string_length, /*sq_length*/ |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
1393 (binaryfunc)string_concat, /*sq_concat*/ |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1394 (ssizeargfunc)string_repeat, /*sq_repeat*/ |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1395 (ssizeargfunc)string_item, /*sq_item*/ |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1396 (ssizessizeargfunc)string_slice, /*sq_slice*/ |
|
259
6321d3dc57e0
Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents:
180
diff
changeset
|
1397 0, /*sq_ass_item*/ |
|
6321d3dc57e0
Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents:
180
diff
changeset
|
1398 0, /*sq_ass_slice*/ |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
1399 (objobjproc)string_contains /*sq_contains*/ |
| 6 | 1400 }; |
| 1401 | |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1402 static PyMappingMethods string_as_mapping = { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1403 (lenfunc)string_length, |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1404 (binaryfunc)string_subscript, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1405 0, |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1406 }; |
|
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
1407 |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1408 static PyBufferProcs string_as_buffer = { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1409 (readbufferproc)string_buffer_getreadbuf, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1410 (writebufferproc)string_buffer_getwritebuf, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1411 (segcountproc)string_buffer_getsegcount, |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1412 (charbufferproc)string_buffer_getcharbuf, |
|
46107
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45731
diff
changeset
|
1413 (getbufferproc)string_buffer_getbuffer, |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45731
diff
changeset
|
1414 0, /* XXX */ |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1415 }; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1416 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1417 |
|
48221
b8c4bf393794
Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents:
48139
diff
changeset
|
1418 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1419 #define LEFTSTRIP 0 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1420 #define RIGHTSTRIP 1 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1421 #define BOTHSTRIP 2 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1422 |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1423 /* Arrays indexed by above */ |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1424 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
|
1425 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1426 #define STRIPNAME(i) (stripformat[i]+3) |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1427 |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1428 |
|
38394
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1429 /* Don't call if length < 2 */ |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1430 #define Py_STRING_MATCH(target, offset, pattern, length) \ |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1431 (target[offset] == pattern[0] && \ |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1432 target[offset+length-1] == pattern[length-1] && \ |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1433 !memcmp(target+offset+1, pattern+1, length-2) ) |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1434 |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1435 |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1436 /* Overallocate the initial list to reduce the number of reallocs for small |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1437 split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1438 resizes, to sizes 4, 8, then 16. Most observed string splits are for human |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1439 text (roughly 11 words per line) and field delimited data (usually 1-10 |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1440 fields). For large strings the split algorithms are bandwidth limited |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1441 so increasing the preallocation likely will not improve things.*/ |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1442 |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1443 #define MAX_PREALLOC 12 |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1444 |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1445 /* 5 splits gives 6 elements */ |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1446 #define PREALLOC_SIZE(maxsplit) \ |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1447 (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1) |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1448 |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1449 #define SPLIT_APPEND(data, left, right) \ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1450 str = PyString_FromStringAndSize((data) + (left), \ |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1451 (right) - (left)); \ |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1452 if (str == NULL) \ |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1453 goto onError; \ |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1454 if (PyList_Append(list, str)) { \ |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1455 Py_DECREF(str); \ |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1456 goto onError; \ |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1457 } \ |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1458 else \ |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1459 Py_DECREF(str); |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1460 |
|
38372
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1461 #define SPLIT_ADD(data, left, right) { \ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1462 str = PyString_FromStringAndSize((data) + (left), \ |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1463 (right) - (left)); \ |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1464 if (str == NULL) \ |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1465 goto onError; \ |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1466 if (count < MAX_PREALLOC) { \ |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1467 PyList_SET_ITEM(list, count, str); \ |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1468 } else { \ |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1469 if (PyList_Append(list, str)) { \ |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1470 Py_DECREF(str); \ |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1471 goto onError; \ |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1472 } \ |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1473 else \ |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1474 Py_DECREF(str); \ |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1475 } \ |
|
38372
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1476 count++; } |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1477 |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1478 /* Always force the list to the expected size. */ |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1479 #define FIX_PREALLOC_SIZE(list) Py_SIZE(list) = count |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1480 |
|
38372
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1481 #define SKIP_SPACE(s, i, len) { while (i<len && isspace(Py_CHARMASK(s[i]))) i++; } |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1482 #define SKIP_NONSPACE(s, i, len) { while (i<len && !isspace(Py_CHARMASK(s[i]))) i++; } |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1483 #define RSKIP_SPACE(s, i) { while (i>=0 && isspace(Py_CHARMASK(s[i]))) i--; } |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1484 #define RSKIP_NONSPACE(s, i) { while (i>=0 && !isspace(Py_CHARMASK(s[i]))) i--; } |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1485 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
1486 Py_LOCAL_INLINE(PyObject *) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1487 split_whitespace(PyStringObject *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
|
1488 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1489 const char *s = PyString_AS_STRING(self); |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1490 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
|
1491 PyObject *str; |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1492 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
|
1493 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1494 if (list == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1495 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1496 |
|
38372
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1497 i = j = 0; |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1498 |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1499 while (maxsplit-- > 0) { |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1500 SKIP_SPACE(s, i, len); |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1501 if (i==len) break; |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1502 j = i; i++; |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1503 SKIP_NONSPACE(s, i, len); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1504 if (j == 0 && i == len && PyString_CheckExact(self)) { |
|
44225
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1505 /* No whitespace in self, so just use it as list[0] */ |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1506 Py_INCREF(self); |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1507 PyList_SET_ITEM(list, 0, (PyObject *)self); |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1508 count++; |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1509 break; |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1510 } |
|
38372
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1511 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
|
1512 } |
|
38372
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1513 |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1514 if (i < len) { |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1515 /* Only occurs when maxsplit was reached */ |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1516 /* Skip any remaining whitespace and copy to end of string */ |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1517 SKIP_SPACE(s, i, len); |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1518 if (i != len) |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1519 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
|
1520 } |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1521 FIX_PREALLOC_SIZE(list); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1522 return list; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1523 onError: |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1524 Py_DECREF(list); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1525 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1526 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1527 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
1528 Py_LOCAL_INLINE(PyObject *) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1529 split_char(PyStringObject *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
|
1530 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1531 const char *s = PyString_AS_STRING(self); |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1532 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
|
1533 PyObject *str; |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1534 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
|
1535 |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1536 if (list == NULL) |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1537 return NULL; |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1538 |
|
38394
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1539 i = j = 0; |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1540 while ((j < len) && (maxcount-- > 0)) { |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1541 for(; j<len; j++) { |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1542 /* I found that using memchr makes no difference */ |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1543 if (s[j] == ch) { |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1544 SPLIT_ADD(s, i, j); |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1545 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
|
1546 break; |
|
38394
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1547 } |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1548 } |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1549 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1550 if (i == 0 && count == 0 && PyString_CheckExact(self)) { |
|
44225
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1551 /* ch not in self, so just use self as list[0] */ |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1552 Py_INCREF(self); |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1553 PyList_SET_ITEM(list, 0, (PyObject *)self); |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1554 count++; |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1555 } |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1556 else if (i <= len) { |
|
38394
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1557 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
|
1558 } |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1559 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
|
1560 return list; |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1561 |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1562 onError: |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1563 Py_DECREF(list); |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1564 return NULL; |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1565 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1566 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1567 PyDoc_STRVAR(split__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1568 "S.split([sep [,maxsplit]]) -> list of strings\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1569 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1570 Return a list of the words in the string S, using sep as the\n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1571 delimiter string. If maxsplit is given, at most maxsplit\n\ |
|
24561
b25c31e008c8
SF 582071 clarified the .split() method's docstring to note that sep=None
Raymond Hettinger <python@rcn.com>
parents:
24415
diff
changeset
|
1572 splits are done. If sep is not specified or is None, any\n\ |
|
46908
ed2c1888e143
#2809 followup: even better split docstring.
Georg Brandl <georg@python.org>
parents:
46898
diff
changeset
|
1573 whitespace string is a separator and empty strings are removed\n\ |
|
ed2c1888e143
#2809 followup: even better split docstring.
Georg Brandl <georg@python.org>
parents:
46898
diff
changeset
|
1574 from the result."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1575 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1576 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1577 string_split(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1578 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1579 Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1580 Py_ssize_t maxsplit = -1, count=0; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1581 const char *s = PyString_AS_STRING(self), *sub; |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1582 PyObject *list, *str, *subobj = Py_None; |
|
38394
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1583 #ifdef USE_FAST |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1584 Py_ssize_t pos; |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1585 #endif |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1586 |
|
37677
e795d6c86a5e
Change maxsplit types to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37667
diff
changeset
|
1587 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
|
1588 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1589 if (maxsplit < 0) |
|
37667
b1724e926e07
Replace most INT_MAX with PY_SSIZE_T_MAX.
Martin v. Löwis <martin@v.loewis.de>
parents:
37596
diff
changeset
|
1590 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
|
1591 if (subobj == Py_None) |
|
44225
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1592 return split_whitespace(self, len, maxsplit); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1593 if (PyString_Check(subobj)) { |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1594 sub = PyString_AS_STRING(subobj); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1595 n = PyString_GET_SIZE(subobj); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1596 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1597 #ifdef Py_USING_UNICODE |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1598 else if (PyUnicode_Check(subobj)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1599 return PyUnicode_Split((PyObject *)self, subobj, maxsplit); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1600 #endif |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1601 else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1602 return NULL; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1603 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1604 if (n == 0) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1605 PyErr_SetString(PyExc_ValueError, "empty separator"); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1606 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1607 } |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1608 else if (n == 1) |
|
44225
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1609 return split_char(self, len, sub[0], maxsplit); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1610 |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1611 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
|
1612 if (list == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1613 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1614 |
|
38394
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1615 #ifdef USE_FAST |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1616 i = j = 0; |
|
38394
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1617 while (maxsplit-- > 0) { |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1618 pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH); |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1619 if (pos < 0) |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1620 break; |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1621 j = i+pos; |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1622 SPLIT_ADD(s, i, j); |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1623 i = j + n; |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1624 } |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1625 #else |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1626 i = j = 0; |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1627 while ((j+n <= len) && (maxsplit-- > 0)) { |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1628 for (; j+n <= len; j++) { |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1629 if (Py_STRING_MATCH(s, j, sub, n)) { |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1630 SPLIT_ADD(s, i, j); |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1631 i = j = j + n; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1632 break; |
|
38394
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1633 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1634 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1635 } |
|
38394
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1636 #endif |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1637 SPLIT_ADD(s, i, len); |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1638 FIX_PREALLOC_SIZE(list); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1639 return list; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1640 |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1641 onError: |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1642 Py_DECREF(list); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1643 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1644 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1645 |
|
38332
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1646 PyDoc_STRVAR(partition__doc__, |
|
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1647 "S.partition(sep) -> (head, sep, tail)\n\ |
|
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1648 \n\ |
|
49431
2e794757bdc8
Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48935
diff
changeset
|
1649 Search for the separator sep in S, and return the part before it,\n\ |
|
38332
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1650 the separator itself, and the part after it. If the separator is not\n\ |
|
49431
2e794757bdc8
Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48935
diff
changeset
|
1651 found, return S and two empty strings."); |
|
38332
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1652 |
|
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1653 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1654 string_partition(PyStringObject *self, PyObject *sep_obj) |
|
38332
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1655 { |
|
38384
96415105c29e
needforspeed: cleanup
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
1656 const char *sep; |
|
96415105c29e
needforspeed: cleanup
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
1657 Py_ssize_t sep_len; |
|
38332
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1658 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1659 if (PyString_Check(sep_obj)) { |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1660 sep = PyString_AS_STRING(sep_obj); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1661 sep_len = PyString_GET_SIZE(sep_obj); |
|
38332
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1662 } |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38332
diff
changeset
|
1663 #ifdef Py_USING_UNICODE |
|
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38332
diff
changeset
|
1664 else if (PyUnicode_Check(sep_obj)) |
|
38384
96415105c29e
needforspeed: cleanup
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
1665 return PyUnicode_Partition((PyObject *) self, sep_obj); |
|
38332
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1666 #endif |
|
38335
f9332ad4287a
needforspeed: partition implementation, part two.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38332
diff
changeset
|
1667 else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
|
38332
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1668 return NULL; |
|
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1669 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38394
diff
changeset
|
1670 return stringlib_partition( |
|
38384
96415105c29e
needforspeed: cleanup
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
1671 (PyObject*) self, |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1672 PyString_AS_STRING(self), PyString_GET_SIZE(self), |
|
38384
96415105c29e
needforspeed: cleanup
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
1673 sep_obj, sep, sep_len |
|
96415105c29e
needforspeed: cleanup
Fredrik Lundh <fredrik@pythonware.com>
parents:
38383
diff
changeset
|
1674 ); |
|
38332
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1675 } |
|
eb237a37ea02
needforspeed: partition for 8-bit strings. for some simple tests,
Fredrik Lundh <fredrik@pythonware.com>
parents:
38321
diff
changeset
|
1676 |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1677 PyDoc_STRVAR(rpartition__doc__, |
|
39734
cfb77fff0240
Fix endcase for str.rpartition()
Raymond Hettinger <python@rcn.com>
parents:
39494
diff
changeset
|
1678 "S.rpartition(sep) -> (tail, sep, head)\n\ |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1679 \n\ |
|
49431
2e794757bdc8
Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48935
diff
changeset
|
1680 Search for the separator sep in S, starting at the end of S, and return\n\ |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1681 the part before it, the separator itself, and the part after it. If the\n\ |
|
49431
2e794757bdc8
Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48935
diff
changeset
|
1682 separator is not found, return two empty strings and S."); |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1683 |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1684 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1685 string_rpartition(PyStringObject *self, PyObject *sep_obj) |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1686 { |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1687 const char *sep; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1688 Py_ssize_t sep_len; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1689 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1690 if (PyString_Check(sep_obj)) { |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1691 sep = PyString_AS_STRING(sep_obj); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1692 sep_len = PyString_GET_SIZE(sep_obj); |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1693 } |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1694 #ifdef Py_USING_UNICODE |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1695 else if (PyUnicode_Check(sep_obj)) |
|
48935
b5365f34640f
Issue #3751: str.rpartition would perform a left-partition when called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
48221
diff
changeset
|
1696 return PyUnicode_RPartition((PyObject *) self, sep_obj); |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1697 #endif |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1698 else if (PyObject_AsCharBuffer(sep_obj, &sep, &sep_len)) |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1699 return NULL; |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1700 |
|
38400
33c328896f52
needforspeed: stringlib refactoring, continued. added count and
Fredrik Lundh <fredrik@pythonware.com>
parents:
38394
diff
changeset
|
1701 return stringlib_rpartition( |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1702 (PyObject*) self, |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1703 PyString_AS_STRING(self), PyString_GET_SIZE(self), |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1704 sep_obj, sep, sep_len |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1705 ); |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1706 } |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
1707 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
1708 Py_LOCAL_INLINE(PyObject *) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1709 rsplit_whitespace(PyStringObject *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
|
1710 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1711 const char *s = PyString_AS_STRING(self); |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1712 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
|
1713 PyObject *str; |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1714 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
|
1715 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1716 if (list == NULL) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1717 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1718 |
|
38372
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1719 i = j = len-1; |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
1720 |
|
38372
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1721 while (maxsplit-- > 0) { |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1722 RSKIP_SPACE(s, i); |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1723 if (i<0) break; |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1724 j = i; i--; |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1725 RSKIP_NONSPACE(s, i); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1726 if (j == len-1 && i < 0 && PyString_CheckExact(self)) { |
|
44225
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1727 /* No whitespace in self, so just use it as list[0] */ |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1728 Py_INCREF(self); |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1729 PyList_SET_ITEM(list, 0, (PyObject *)self); |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1730 count++; |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1731 break; |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1732 } |
|
38372
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1733 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
|
1734 } |
|
38372
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1735 if (i >= 0) { |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1736 /* Only occurs when maxsplit was reached */ |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1737 /* Skip any remaining whitespace and copy to beginning of string */ |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1738 RSKIP_SPACE(s, i); |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1739 if (i >= 0) |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1740 SPLIT_ADD(s, 0, i + 1); |
|
c14e0c89a8b0
Eeked out another 3% or so performance in split whitespace by cleaning up the algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38365
diff
changeset
|
1741 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1742 } |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1743 FIX_PREALLOC_SIZE(list); |
|
38315
91c6c94490b1
needforspeed: use insert+reverse instead of append
Fredrik Lundh <fredrik@pythonware.com>
parents:
38311
diff
changeset
|
1744 if (PyList_Reverse(list) < 0) |
|
91c6c94490b1
needforspeed: use insert+reverse instead of append
Fredrik Lundh <fredrik@pythonware.com>
parents:
38311
diff
changeset
|
1745 goto onError; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1746 return list; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1747 onError: |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1748 Py_DECREF(list); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1749 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1750 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1751 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
1752 Py_LOCAL_INLINE(PyObject *) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1753 rsplit_char(PyStringObject *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
|
1754 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1755 const char *s = PyString_AS_STRING(self); |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1756 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
|
1757 PyObject *str; |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1758 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
|
1759 |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1760 if (list == NULL) |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1761 return NULL; |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1762 |
|
38394
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1763 i = j = len - 1; |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1764 while ((i >= 0) && (maxcount-- > 0)) { |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1765 for (; i >= 0; i--) { |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1766 if (s[i] == ch) { |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1767 SPLIT_ADD(s, i + 1, j + 1); |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1768 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
|
1769 break; |
|
38394
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1770 } |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1771 } |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1772 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1773 if (i < 0 && count == 0 && PyString_CheckExact(self)) { |
|
44225
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1774 /* ch not in self, so just use self as list[0] */ |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1775 Py_INCREF(self); |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1776 PyList_SET_ITEM(list, 0, (PyObject *)self); |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1777 count++; |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1778 } |
|
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1779 else if (j >= -1) { |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1780 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
|
1781 } |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1782 FIX_PREALLOC_SIZE(list); |
|
38315
91c6c94490b1
needforspeed: use insert+reverse instead of append
Fredrik Lundh <fredrik@pythonware.com>
parents:
38311
diff
changeset
|
1783 if (PyList_Reverse(list) < 0) |
|
91c6c94490b1
needforspeed: use insert+reverse instead of append
Fredrik Lundh <fredrik@pythonware.com>
parents:
38311
diff
changeset
|
1784 goto onError; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1785 return list; |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1786 |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1787 onError: |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1788 Py_DECREF(list); |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1789 return NULL; |
|
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1790 } |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1791 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1792 PyDoc_STRVAR(rsplit__doc__, |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1793 "S.rsplit([sep [,maxsplit]]) -> list of strings\n\ |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1794 \n\ |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1795 Return a list of the words in the string S, using sep as the\n\ |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1796 delimiter string, starting at the end of the string and working\n\ |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1797 to the front. If maxsplit is given, at most maxsplit splits are\n\ |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1798 done. If sep is not specified or is None, any whitespace string\n\ |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1799 is a separator."); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1800 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1801 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1802 string_rsplit(PyStringObject *self, PyObject *args) |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1803 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1804 Py_ssize_t len = PyString_GET_SIZE(self), n, i, j; |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1805 Py_ssize_t maxsplit = -1, count=0; |
|
44225
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1806 const char *s, *sub; |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1807 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
|
1808 |
|
37677
e795d6c86a5e
Change maxsplit types to Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
37667
diff
changeset
|
1809 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
|
1810 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1811 if (maxsplit < 0) |
|
37667
b1724e926e07
Replace most INT_MAX with PY_SSIZE_T_MAX.
Martin v. Löwis <martin@v.loewis.de>
parents:
37596
diff
changeset
|
1812 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
|
1813 if (subobj == Py_None) |
|
44225
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1814 return rsplit_whitespace(self, len, maxsplit); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1815 if (PyString_Check(subobj)) { |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1816 sub = PyString_AS_STRING(subobj); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1817 n = PyString_GET_SIZE(subobj); |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1818 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1819 #ifdef Py_USING_UNICODE |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1820 else if (PyUnicode_Check(subobj)) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1821 return PyUnicode_RSplit((PyObject *)self, subobj, maxsplit); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1822 #endif |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1823 else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1824 return NULL; |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1825 |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1826 if (n == 0) { |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1827 PyErr_SetString(PyExc_ValueError, "empty separator"); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1828 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1829 } |
|
31350
eaa1a4b74328
[SF #866875] Add a specialized routine for one character
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31250
diff
changeset
|
1830 else if (n == 1) |
|
44225
ad8b80460644
When splitting, avoid making a copy of the string if the split doesn't find
Skip Montanaro <skip@pobox.com>
parents:
43886
diff
changeset
|
1831 return rsplit_char(self, len, sub[0], maxsplit); |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1832 |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1833 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
|
1834 if (list == NULL) |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1835 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1836 |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1837 j = len; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1838 i = j - n; |
|
38394
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1839 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1840 s = PyString_AS_STRING(self); |
|
38394
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1841 while ( (i >= 0) && (maxsplit-- > 0) ) { |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1842 for (; i>=0; i--) { |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1843 if (Py_STRING_MATCH(s, i, sub, n)) { |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1844 SPLIT_ADD(s, i + n, j); |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1845 j = i; |
|
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1846 i -= n; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1847 break; |
|
38394
d153d2f66884
substring split now uses /F's fast string matching algorithm.
Andrew Dalke <dalke@dalkescientific.com>
parents:
38390
diff
changeset
|
1848 } |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1849 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1850 } |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1851 SPLIT_ADD(s, 0, j); |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1852 FIX_PREALLOC_SIZE(list); |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1853 if (PyList_Reverse(list) < 0) |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1854 goto onError; |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1855 return list; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1856 |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
1857 onError: |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1858 Py_DECREF(list); |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1859 return NULL; |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1860 } |
|
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
1861 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1862 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
1863 PyDoc_STRVAR(join__doc__, |
|
56526
d24298df2a66
#7116: str.join() takes an iterable.
Georg Brandl <georg@python.org>
parents:
56093
diff
changeset
|
1864 "S.join(iterable) -> string\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1865 \n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1866 Return a string which is the concatenation of the strings in the\n\ |
|
56526
d24298df2a66
#7116: str.join() takes an iterable.
Georg Brandl <georg@python.org>
parents:
56093
diff
changeset
|
1867 iterable. The separator between elements is S."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1868 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1869 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1870 string_join(PyStringObject *self, PyObject *orig) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1871 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1872 char *sep = PyString_AS_STRING(self); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1873 const Py_ssize_t seplen = PyString_GET_SIZE(self); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1874 PyObject *res = NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1875 char *p; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1876 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
|
1877 size_t sz = 0; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1878 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
|
1879 PyObject *seq, *item; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1880 |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1881 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
|
1882 if (seq == NULL) { |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1883 return NULL; |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1884 } |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1885 |
|
13576
61e4c29d53d8
replace PyXXX_Length calls with PyXXX_Size calls
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13561
diff
changeset
|
1886 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
|
1887 if (seqlen == 0) { |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1888 Py_DECREF(seq); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1889 return PyString_FromString(""); |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1890 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1891 if (seqlen == 1) { |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1892 item = PySequence_Fast_GET_ITEM(seq, 0); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1893 if (PyString_CheckExact(item) || PyUnicode_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
|
1894 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
|
1895 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
|
1896 return item; |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1897 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1898 } |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1899 |
|
33246
03440272028c
SF Patch #1007087: Return new string for single subclass joins (Bug #1001011)
Raymond Hettinger <python@rcn.com>
parents:
33030
diff
changeset
|
1900 /* There are at least two things to join, or else we have a subclass |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
1901 * 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
|
1902 * Do a pre-pass to figure out the total amount of space we'll |
|
03440272028c
SF Patch #1007087: Return new string for single subclass joins (Bug #1001011)
Raymond Hettinger <python@rcn.com>
parents:
33030
diff
changeset
|
1903 * need (sz), see whether any argument is absurd, and defer to |
|
03440272028c
SF Patch #1007087: Return new string for single subclass joins (Bug #1001011)
Raymond Hettinger <python@rcn.com>
parents:
33030
diff
changeset
|
1904 * the Unicode join if appropriate. |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1905 */ |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1906 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
|
1907 const size_t old_sz = sz; |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1908 item = PySequence_Fast_GET_ITEM(seq, i); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1909 if (!PyString_Check(item)){ |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1910 #ifdef Py_USING_UNICODE |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1911 if (PyUnicode_Check(item)) { |
|
17687
9e093bf8f6a9
Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents:
17655
diff
changeset
|
1912 /* Defer to Unicode join. |
|
9e093bf8f6a9
Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents:
17655
diff
changeset
|
1913 * CAUTION: There's no gurantee that the |
|
9e093bf8f6a9
Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents:
17655
diff
changeset
|
1914 * original sequence can be iterated over |
|
9e093bf8f6a9
Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents:
17655
diff
changeset
|
1915 * again, so we must pass seq here. |
|
9e093bf8f6a9
Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents:
17655
diff
changeset
|
1916 */ |
|
9e093bf8f6a9
Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents:
17655
diff
changeset
|
1917 PyObject *result; |
|
9e093bf8f6a9
Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents:
17655
diff
changeset
|
1918 result = PyUnicode_Join((PyObject *)self, seq); |
|
13528
e848d6ed3eb9
string_join(): Some cleaning up of reference counting. In the
Barry Warsaw <barry@python.org>
parents:
13526
diff
changeset
|
1919 Py_DECREF(seq); |
|
17687
9e093bf8f6a9
Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents:
17655
diff
changeset
|
1920 return result; |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1921 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1922 #endif |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1923 PyErr_Format(PyExc_TypeError, |
|
37787
ec00b798ab56
No need to cast a Py_ssize_t, use %z in PyErr_Format
Neal Norwitz <nnorwitz@gmail.com>
parents:
37702
diff
changeset
|
1924 "sequence item %zd: expected string," |
|
13544
3cf4f5aee00e
small updates to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13528
diff
changeset
|
1925 " %.80s found", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
1926 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
|
1927 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1928 return NULL; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1929 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1930 sz += PyString_GET_SIZE(item); |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1931 if (i != 0) |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1932 sz += seplen; |
|
37667
b1724e926e07
Replace most INT_MAX with PY_SSIZE_T_MAX.
Martin v. Löwis <martin@v.loewis.de>
parents:
37596
diff
changeset
|
1933 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
|
1934 PyErr_SetString(PyExc_OverflowError, |
|
38709
67450db26f81
Apply perky's fix for #1503157: "/".join([u"", u""]) raising OverflowError.
Georg Brandl <georg@python.org>
parents:
38697
diff
changeset
|
1935 "join() result is too long for a Python string"); |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1936 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1937 return NULL; |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1938 } |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1939 } |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1940 |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1941 /* Allocate result space. */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1942 res = PyString_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
|
1943 if (res == NULL) { |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1944 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1945 return NULL; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1946 } |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1947 |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1948 /* Catenate everything. */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1949 p = PyString_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
|
1950 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
|
1951 size_t n; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1952 item = PySequence_Fast_GET_ITEM(seq, i); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1953 n = PyString_GET_SIZE(item); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1954 Py_MEMCPY(p, PyString_AS_STRING(item), n); |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1955 p += n; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1956 if (i < seqlen - 1) { |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
1957 Py_MEMCPY(p, sep, seplen); |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1958 p += seplen; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1959 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1960 } |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1961 |
|
13526
3686d12362b8
fix two refcount bugs in new string_join implementation:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13524
diff
changeset
|
1962 Py_DECREF(seq); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1963 return res; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1964 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1965 |
|
18130
8eea2efb34cd
Reformat decl of new _PyString_Join. Add NEWS blurb about repr() speedup.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
1966 PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1967 _PyString_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
|
1968 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1969 assert(sep != NULL && PyString_Check(sep)); |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1970 assert(x != NULL); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1971 return string_join((PyStringObject *)sep, x); |
|
18129
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1972 } |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1973 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
1974 Py_LOCAL_INLINE(void) |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
1975 string_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
|
1976 { |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1977 if (*end > len) |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1978 *end = len; |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1979 else if (*end < 0) |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1980 *end += len; |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1981 if (*end < 0) |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1982 *end = 0; |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1983 if (*start < 0) |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1984 *start += len; |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1985 if (*start < 0) |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1986 *start = 0; |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1987 } |
|
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
1988 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
1989 Py_LOCAL_INLINE(Py_ssize_t) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1990 string_find_internal(PyStringObject *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
|
1991 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1992 PyObject *subobj; |
|
38442
8038e41f46cc
needforspeed: stringlib refactoring: use find_slice for stringobject
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
1993 const char *sub; |
|
8038e41f46cc
needforspeed: stringlib refactoring: use find_slice for stringobject
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
1994 Py_ssize_t sub_len; |
|
8038e41f46cc
needforspeed: stringlib refactoring: use find_slice for stringobject
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
1995 Py_ssize_t start=0, end=PY_SSIZE_T_MAX; |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43780
diff
changeset
|
1996 PyObject *obj_start=Py_None, *obj_end=Py_None; |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43780
diff
changeset
|
1997 |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43780
diff
changeset
|
1998 if (!PyArg_ParseTuple(args, "O|OO:find/rfind/index/rindex", &subobj, |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43780
diff
changeset
|
1999 &obj_start, &obj_end)) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2000 return -2; |
|
43886
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43780
diff
changeset
|
2001 /* To support None in "start" and "end" arguments, meaning |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43780
diff
changeset
|
2002 the same as if they were not passed. |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43780
diff
changeset
|
2003 */ |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43780
diff
changeset
|
2004 if (obj_start != Py_None) |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43780
diff
changeset
|
2005 if (!_PyEval_SliceIndex(obj_start, &start)) |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43780
diff
changeset
|
2006 return -2; |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43780
diff
changeset
|
2007 if (obj_end != Py_None) |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43780
diff
changeset
|
2008 if (!_PyEval_SliceIndex(obj_end, &end)) |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43780
diff
changeset
|
2009 return -2; |
|
25a5f6a30292
Now in find, rfind, index, and rindex, you can use None as defaults,
Facundo Batista <facundobatista@gmail.com>
parents:
43780
diff
changeset
|
2010 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2011 if (PyString_Check(subobj)) { |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2012 sub = PyString_AS_STRING(subobj); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2013 sub_len = PyString_GET_SIZE(subobj); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2014 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2015 #ifdef Py_USING_UNICODE |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2016 else if (PyUnicode_Check(subobj)) |
|
38442
8038e41f46cc
needforspeed: stringlib refactoring: use find_slice for stringobject
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
2017 return PyUnicode_Find( |
|
8038e41f46cc
needforspeed: stringlib refactoring: use find_slice for stringobject
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
2018 (PyObject *)self, subobj, start, end, dir); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2019 #endif |
|
38442
8038e41f46cc
needforspeed: stringlib refactoring: use find_slice for stringobject
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
2020 else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len)) |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
2021 /* XXX - the "expected a character buffer object" is pretty |
|
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
2022 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
|
2023 return -2; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2024 |
|
38404
52f2c9f74f5a
needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
2025 if (dir > 0) |
|
38442
8038e41f46cc
needforspeed: stringlib refactoring: use find_slice for stringobject
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
2026 return stringlib_find_slice( |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2027 PyString_AS_STRING(self), PyString_GET_SIZE(self), |
|
38442
8038e41f46cc
needforspeed: stringlib refactoring: use find_slice for stringobject
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
2028 sub, sub_len, start, end); |
|
38404
52f2c9f74f5a
needforspeed: stringlib refactoring: use stringlib/find for string find
Fredrik Lundh <fredrik@pythonware.com>
parents:
38400
diff
changeset
|
2029 else |
|
38442
8038e41f46cc
needforspeed: stringlib refactoring: use find_slice for stringobject
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
2030 return stringlib_rfind_slice( |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2031 PyString_AS_STRING(self), PyString_GET_SIZE(self), |
|
38442
8038e41f46cc
needforspeed: stringlib refactoring: use find_slice for stringobject
Fredrik Lundh <fredrik@pythonware.com>
parents:
38440
diff
changeset
|
2032 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
|
2033 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2034 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2035 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2036 PyDoc_STRVAR(find__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2037 "S.find(sub [,start [,end]]) -> int\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2038 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2039 Return the lowest index in S where substring sub is found,\n\ |
|
42402
251033a1c2e3
Bug #1763149: use proper slice syntax in docstring.
Georg Brandl <georg@python.org>
parents:
42333
diff
changeset
|
2040 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
|
2041 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
|
2042 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2043 Return -1 on failure."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2044 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2045 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2046 string_find(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2047 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2048 Py_ssize_t result = string_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
|
2049 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2050 return NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2051 return PyInt_FromSsize_t(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2052 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2053 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2054 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2055 PyDoc_STRVAR(index__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2056 "S.index(sub [,start [,end]]) -> int\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2057 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2058 Like S.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
|
2059 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2060 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2061 string_index(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2062 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2063 Py_ssize_t result = string_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
|
2064 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2065 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2066 if (result == -1) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2067 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
|
2068 "substring not found"); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2069 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2070 } |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2071 return PyInt_FromSsize_t(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2072 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2073 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2074 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2075 PyDoc_STRVAR(rfind__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2076 "S.rfind(sub [,start [,end]]) -> int\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2077 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2078 Return the highest index in S where substring sub is found,\n\ |
|
42402
251033a1c2e3
Bug #1763149: use proper slice syntax in docstring.
Georg Brandl <georg@python.org>
parents:
42333
diff
changeset
|
2079 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
|
2080 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
|
2081 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2082 Return -1 on failure."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2083 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2084 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2085 string_rfind(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2086 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2087 Py_ssize_t result = string_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
|
2088 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2089 return NULL; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2090 return PyInt_FromSsize_t(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2091 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2092 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2093 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2094 PyDoc_STRVAR(rindex__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2095 "S.rindex(sub [,start [,end]]) -> int\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2096 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2097 Like S.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
|
2098 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2099 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2100 string_rindex(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2101 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2102 Py_ssize_t result = string_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
|
2103 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2104 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2105 if (result == -1) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2106 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
|
2107 "substring not found"); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2108 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2109 } |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2110 return PyInt_FromSsize_t(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2111 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2112 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2113 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
2114 Py_LOCAL_INLINE(PyObject *) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2115 do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj) |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2116 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2117 char *s = PyString_AS_STRING(self); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2118 Py_ssize_t len = PyString_GET_SIZE(self); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2119 char *sep = PyString_AS_STRING(sepobj); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2120 Py_ssize_t seplen = PyString_GET_SIZE(sepobj); |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2121 Py_ssize_t i, j; |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2122 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2123 i = 0; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2124 if (striptype != RIGHTSTRIP) { |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2125 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
|
2126 i++; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2127 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2128 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2129 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2130 j = len; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2131 if (striptype != LEFTSTRIP) { |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2132 do { |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2133 j--; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2134 } 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
|
2135 j++; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2136 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2137 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2138 if (i == 0 && j == len && PyString_CheckExact(self)) { |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2139 Py_INCREF(self); |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2140 return (PyObject*)self; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2141 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2142 else |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2143 return PyString_FromStringAndSize(s+i, j-i); |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2144 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2145 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2146 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
2147 Py_LOCAL_INLINE(PyObject *) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2148 do_strip(PyStringObject *self, int striptype) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2149 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2150 char *s = PyString_AS_STRING(self); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2151 Py_ssize_t len = PyString_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
|
2152 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2153 i = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2154 if (striptype != RIGHTSTRIP) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2155 while (i < len && isspace(Py_CHARMASK(s[i]))) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2156 i++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2157 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2158 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2159 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2160 j = len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2161 if (striptype != LEFTSTRIP) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2162 do { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2163 j--; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2164 } while (j >= i && isspace(Py_CHARMASK(s[j]))); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2165 j++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2166 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2167 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2168 if (i == 0 && j == len && PyString_CheckExact(self)) { |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2169 Py_INCREF(self); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2170 return (PyObject*)self; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2171 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2172 else |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2173 return PyString_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
|
2174 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2175 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2176 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
2177 Py_LOCAL_INLINE(PyObject *) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2178 do_argstrip(PyStringObject *self, int striptype, PyObject *args) |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2179 { |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2180 PyObject *sep = NULL; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2181 |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
2182 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
|
2183 return NULL; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2184 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2185 if (sep != NULL && sep != Py_None) { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2186 if (PyString_Check(sep)) |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
2187 return do_xstrip(self, striptype, sep); |
|
23430
1be6e79b1d46
Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents:
23295
diff
changeset
|
2188 #ifdef Py_USING_UNICODE |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
2189 else if (PyUnicode_Check(sep)) { |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
2190 PyObject *uniself = PyUnicode_FromObject((PyObject *)self); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
2191 PyObject *res; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
2192 if (uniself==NULL) |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
2193 return NULL; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
2194 res = _PyUnicode_XStrip((PyUnicodeObject *)uniself, |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
2195 striptype, sep); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
2196 Py_DECREF(uniself); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
2197 return res; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
2198 } |
|
23430
1be6e79b1d46
Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents:
23295
diff
changeset
|
2199 #endif |
|
37516
7cd3bf2399e6
Remove dead code (reported by HP compiler).
Neal Norwitz <nnorwitz@gmail.com>
parents:
37304
diff
changeset
|
2200 PyErr_Format(PyExc_TypeError, |
|
23430
1be6e79b1d46
Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents:
23295
diff
changeset
|
2201 #ifdef Py_USING_UNICODE |
|
37516
7cd3bf2399e6
Remove dead code (reported by HP compiler).
Neal Norwitz <nnorwitz@gmail.com>
parents:
37304
diff
changeset
|
2202 "%s arg must be None, str or unicode", |
|
23430
1be6e79b1d46
Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents:
23295
diff
changeset
|
2203 #else |
|
37516
7cd3bf2399e6
Remove dead code (reported by HP compiler).
Neal Norwitz <nnorwitz@gmail.com>
parents:
37304
diff
changeset
|
2204 "%s arg must be None or str", |
|
23430
1be6e79b1d46
Add #ifdef PY_USING_UNICODE sections, so that
Walter Dörwald <walter@livinglogic.de>
parents:
23295
diff
changeset
|
2205 #endif |
|
37516
7cd3bf2399e6
Remove dead code (reported by HP compiler).
Neal Norwitz <nnorwitz@gmail.com>
parents:
37304
diff
changeset
|
2206 STRIPNAME(striptype)); |
|
7cd3bf2399e6
Remove dead code (reported by HP compiler).
Neal Norwitz <nnorwitz@gmail.com>
parents:
37304
diff
changeset
|
2207 return NULL; |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2208 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2209 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2210 return do_strip(self, striptype); |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2211 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2212 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2213 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2214 PyDoc_STRVAR(strip__doc__, |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
28559
diff
changeset
|
2215 "S.strip([chars]) -> string or unicode\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2216 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2217 Return a copy of the string S with leading and trailing\n\ |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2218 whitespace removed.\n\ |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
28559
diff
changeset
|
2219 If chars is given and not None, remove characters in chars instead.\n\ |
|
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
28559
diff
changeset
|
2220 If chars is unicode, S will be converted to unicode before stripping"); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2221 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2222 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2223 string_strip(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2224 { |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2225 if (PyTuple_GET_SIZE(args) == 0) |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2226 return do_strip(self, BOTHSTRIP); /* Common case */ |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2227 else |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2228 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
|
2229 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2230 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2231 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2232 PyDoc_STRVAR(lstrip__doc__, |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
28559
diff
changeset
|
2233 "S.lstrip([chars]) -> string or unicode\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2234 \n\ |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2235 Return a copy of the string S with leading whitespace removed.\n\ |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
28559
diff
changeset
|
2236 If chars is given and not None, remove characters in chars instead.\n\ |
|
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
28559
diff
changeset
|
2237 If chars is unicode, S will be converted to unicode before stripping"); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2238 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2239 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2240 string_lstrip(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2241 { |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2242 if (PyTuple_GET_SIZE(args) == 0) |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2243 return do_strip(self, LEFTSTRIP); /* Common case */ |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2244 else |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2245 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
|
2246 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2247 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2248 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2249 PyDoc_STRVAR(rstrip__doc__, |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
28559
diff
changeset
|
2250 "S.rstrip([chars]) -> string or unicode\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2251 \n\ |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2252 Return a copy of the string S with trailing whitespace removed.\n\ |
|
28575
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
28559
diff
changeset
|
2253 If chars is given and not None, remove characters in chars instead.\n\ |
|
215f1f4851a4
Attempt to make all the various string *strip methods the same.
Neal Norwitz <nnorwitz@gmail.com>
parents:
28559
diff
changeset
|
2254 If chars is unicode, S will be converted to unicode before stripping"); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2255 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2256 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2257 string_rstrip(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2258 { |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2259 if (PyTuple_GET_SIZE(args) == 0) |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2260 return do_strip(self, RIGHTSTRIP); /* Common case */ |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2261 else |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
2262 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
|
2263 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2264 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2265 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2266 PyDoc_STRVAR(lower__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2267 "S.lower() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2268 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2269 Return a copy of the string S converted to lowercase."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2270 |
|
38294
41768de17ead
needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents:
38292
diff
changeset
|
2271 /* _tolower and _toupper are defined by SUSv2, but they're not ISO C */ |
|
41768de17ead
needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents:
38292
diff
changeset
|
2272 #ifndef _tolower |
|
41768de17ead
needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents:
38292
diff
changeset
|
2273 #define _tolower tolower |
|
41768de17ead
needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents:
38292
diff
changeset
|
2274 #endif |
|
41768de17ead
needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents:
38292
diff
changeset
|
2275 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2276 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2277 string_lower(PyStringObject *self) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2278 { |
|
38291
ad1e2438cfaa
needforspeed: speed up upper and lower for 8-bit string objects.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
2279 char *s; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2280 Py_ssize_t i, n = PyString_GET_SIZE(self); |
|
37596
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2281 PyObject *newobj; |
|
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2282 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2283 newobj = PyString_FromStringAndSize(NULL, n); |
|
38291
ad1e2438cfaa
needforspeed: speed up upper and lower for 8-bit string objects.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
2284 if (!newobj) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2285 return NULL; |
|
38291
ad1e2438cfaa
needforspeed: speed up upper and lower for 8-bit string objects.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
2286 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2287 s = PyString_AS_STRING(newobj); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2288 |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2289 Py_MEMCPY(s, PyString_AS_STRING(self), n); |
|
38292
e15b14c7fa14
needforspeed: make new upper/lower work properly for single-character
Fredrik Lundh <fredrik@pythonware.com>
parents:
38291
diff
changeset
|
2290 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2291 for (i = 0; i < n; i++) { |
|
38292
e15b14c7fa14
needforspeed: make new upper/lower work properly for single-character
Fredrik Lundh <fredrik@pythonware.com>
parents:
38291
diff
changeset
|
2292 int c = Py_CHARMASK(s[i]); |
|
38291
ad1e2438cfaa
needforspeed: speed up upper and lower for 8-bit string objects.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
2293 if (isupper(c)) |
|
ad1e2438cfaa
needforspeed: speed up upper and lower for 8-bit string objects.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
2294 s[i] = _tolower(c); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2295 } |
|
38291
ad1e2438cfaa
needforspeed: speed up upper and lower for 8-bit string objects.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
2296 |
|
37596
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2297 return newobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2298 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2299 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2300 PyDoc_STRVAR(upper__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2301 "S.upper() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2302 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2303 Return a copy of the string S converted to uppercase."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2304 |
|
38294
41768de17ead
needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents:
38292
diff
changeset
|
2305 #ifndef _toupper |
|
41768de17ead
needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents:
38292
diff
changeset
|
2306 #define _toupper toupper |
|
41768de17ead
needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents:
38292
diff
changeset
|
2307 #endif |
|
41768de17ead
needforspeed: _toupper/_tolower is a SUSv2 thing; fall back on ISO C
Fredrik Lundh <fredrik@pythonware.com>
parents:
38292
diff
changeset
|
2308 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2309 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2310 string_upper(PyStringObject *self) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2311 { |
|
38291
ad1e2438cfaa
needforspeed: speed up upper and lower for 8-bit string objects.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
2312 char *s; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2313 Py_ssize_t i, n = PyString_GET_SIZE(self); |
|
37596
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2314 PyObject *newobj; |
|
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2315 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2316 newobj = PyString_FromStringAndSize(NULL, n); |
|
38291
ad1e2438cfaa
needforspeed: speed up upper and lower for 8-bit string objects.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
2317 if (!newobj) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2318 return NULL; |
|
38291
ad1e2438cfaa
needforspeed: speed up upper and lower for 8-bit string objects.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
2319 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2320 s = PyString_AS_STRING(newobj); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2321 |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2322 Py_MEMCPY(s, PyString_AS_STRING(self), n); |
|
38292
e15b14c7fa14
needforspeed: make new upper/lower work properly for single-character
Fredrik Lundh <fredrik@pythonware.com>
parents:
38291
diff
changeset
|
2323 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2324 for (i = 0; i < n; i++) { |
|
38292
e15b14c7fa14
needforspeed: make new upper/lower work properly for single-character
Fredrik Lundh <fredrik@pythonware.com>
parents:
38291
diff
changeset
|
2325 int c = Py_CHARMASK(s[i]); |
|
38291
ad1e2438cfaa
needforspeed: speed up upper and lower for 8-bit string objects.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
2326 if (islower(c)) |
|
ad1e2438cfaa
needforspeed: speed up upper and lower for 8-bit string objects.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
2327 s[i] = _toupper(c); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2328 } |
|
38291
ad1e2438cfaa
needforspeed: speed up upper and lower for 8-bit string objects.
Fredrik Lundh <fredrik@pythonware.com>
parents:
38236
diff
changeset
|
2329 |
|
37596
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2330 return newobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2331 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2332 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2333 PyDoc_STRVAR(title__doc__, |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2334 "S.title() -> string\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2335 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2336 Return a titlecased version of S, i.e. words start with uppercase\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2337 characters, all remaining cased characters have lowercase."); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2338 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2339 static PyObject* |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2340 string_title(PyStringObject *self) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2341 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2342 char *s = PyString_AS_STRING(self), *s_new; |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2343 Py_ssize_t i, n = PyString_GET_SIZE(self); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2344 int previous_is_cased = 0; |
|
37596
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2345 PyObject *newobj; |
|
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2346 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2347 newobj = PyString_FromStringAndSize(NULL, n); |
|
37596
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2348 if (newobj == NULL) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2349 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2350 s_new = PyString_AsString(newobj); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2351 for (i = 0; i < n; i++) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2352 int c = Py_CHARMASK(*s++); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2353 if (islower(c)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2354 if (!previous_is_cased) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2355 c = toupper(c); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2356 previous_is_cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2357 } else if (isupper(c)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2358 if (previous_is_cased) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2359 c = tolower(c); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2360 previous_is_cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2361 } else |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2362 previous_is_cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2363 *s_new++ = c; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2364 } |
|
37596
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2365 return newobj; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2366 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2367 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2368 PyDoc_STRVAR(capitalize__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2369 "S.capitalize() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2370 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2371 Return a copy of the string S with only its first character\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2372 capitalized."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2373 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2374 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2375 string_capitalize(PyStringObject *self) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2376 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2377 char *s = PyString_AS_STRING(self), *s_new; |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2378 Py_ssize_t i, n = PyString_GET_SIZE(self); |
|
37596
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2379 PyObject *newobj; |
|
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2380 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2381 newobj = PyString_FromStringAndSize(NULL, n); |
|
37596
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2382 if (newobj == NULL) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2383 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2384 s_new = PyString_AsString(newobj); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2385 if (0 < n) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2386 int c = Py_CHARMASK(*s++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2387 if (islower(c)) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2388 *s_new = toupper(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2389 else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2390 *s_new = c; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2391 s_new++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2392 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2393 for (i = 1; i < n; i++) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2394 int c = Py_CHARMASK(*s++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2395 if (isupper(c)) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2396 *s_new = tolower(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2397 else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2398 *s_new = c; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2399 s_new++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2400 } |
|
37596
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2401 return newobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2402 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2403 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2404 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2405 PyDoc_STRVAR(count__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2406 "S.count(sub[, start[, end]]) -> int\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2407 \n\ |
|
38236
7e16fcce34a1
docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents:
38173
diff
changeset
|
2408 Return the number of non-overlapping occurrences of substring sub in\n\ |
|
7e16fcce34a1
docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents:
38173
diff
changeset
|
2409 string S[start:end]. Optional arguments start and end are interpreted\n\ |
|
7e16fcce34a1
docstring tweaks: count counts non-overlapping substrings, not
Fredrik Lundh <fredrik@pythonware.com>
parents:
38173
diff
changeset
|
2410 as in slice notation."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2411 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2412 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2413 string_count(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2414 { |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2415 PyObject *sub_obj; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2416 const char *str = PyString_AS_STRING(self), *sub; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2417 Py_ssize_t sub_len; |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2418 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2419 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2420 if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj, |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2421 _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
|
2422 return NULL; |
| 12615 | 2423 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2424 if (PyString_Check(sub_obj)) { |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2425 sub = PyString_AS_STRING(sub_obj); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2426 sub_len = PyString_GET_SIZE(sub_obj); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2427 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2428 #ifdef Py_USING_UNICODE |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2429 else if (PyUnicode_Check(sub_obj)) { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2430 Py_ssize_t count; |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2431 count = PyUnicode_Count((PyObject *)self, sub_obj, start, end); |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2432 if (count == -1) |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2433 return NULL; |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2434 else |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2435 return PyInt_FromSsize_t(count); |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2436 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2437 #endif |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2438 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
|
2439 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2440 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2441 string_adjust_indices(&start, &end, PyString_GET_SIZE(self)); |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2442 |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2443 return PyInt_FromSsize_t( |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2444 stringlib_count(str + start, end - start, sub, sub_len) |
|
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2445 ); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2446 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2447 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2448 PyDoc_STRVAR(swapcase__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2449 "S.swapcase() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2450 \n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2451 Return a copy of the string S with uppercase characters\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2452 converted to lowercase and vice versa."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2453 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2454 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2455 string_swapcase(PyStringObject *self) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2456 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2457 char *s = PyString_AS_STRING(self), *s_new; |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2458 Py_ssize_t i, n = PyString_GET_SIZE(self); |
|
37596
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2459 PyObject *newobj; |
|
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2460 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2461 newobj = PyString_FromStringAndSize(NULL, n); |
|
37596
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2462 if (newobj == NULL) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2463 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2464 s_new = PyString_AsString(newobj); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2465 for (i = 0; i < n; i++) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2466 int c = Py_CHARMASK(*s++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2467 if (islower(c)) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2468 *s_new = toupper(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2469 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2470 else if (isupper(c)) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2471 *s_new = tolower(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2472 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2473 else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2474 *s_new = c; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2475 s_new++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2476 } |
|
37596
1b078493d538
More low-hanging fruit. Still need to re-arrange some code (or find a better
Anthony Baxter <anthonybaxter@gmail.com>
parents:
37516
diff
changeset
|
2477 return newobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2478 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2479 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2480 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2481 PyDoc_STRVAR(translate__doc__, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2482 "S.translate(table [,deletechars]) -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2483 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2484 Return a copy of the string S, where all characters occurring\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2485 in the optional argument deletechars are removed, and the\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2486 remaining characters have been mapped through the given\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
2487 translation table, which must be a string of length 256."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2488 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2489 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2490 string_translate(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2491 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2492 register char *input, *output; |
|
41440
cedc8d7d1c7f
SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents:
40925
diff
changeset
|
2493 const char *table; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
2494 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
|
2495 PyObject *input_obj = (PyObject*)self; |
|
41440
cedc8d7d1c7f
SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents:
40925
diff
changeset
|
2496 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
|
2497 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
|
2498 PyObject *result; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2499 int trans_table[256]; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2500 PyObject *tableobj, *delobj = NULL; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2501 |
|
26818
e094a03a80ff
SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents:
26625
diff
changeset
|
2502 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
|
2503 &tableobj, &delobj)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2504 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2505 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2506 if (PyString_Check(tableobj)) { |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2507 table = PyString_AS_STRING(tableobj); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2508 tablen = PyString_GET_SIZE(tableobj); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2509 } |
|
41440
cedc8d7d1c7f
SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents:
40925
diff
changeset
|
2510 else if (tableobj == Py_None) { |
|
cedc8d7d1c7f
SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents:
40925
diff
changeset
|
2511 table = NULL; |
|
cedc8d7d1c7f
SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents:
40925
diff
changeset
|
2512 tablen = 256; |
|
cedc8d7d1c7f
SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents:
40925
diff
changeset
|
2513 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2514 #ifdef Py_USING_UNICODE |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2515 else if (PyUnicode_Check(tableobj)) { |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2516 /* Unicode .translate() does not support the deletechars |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2517 parameter; instead a mapping to None will cause characters |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2518 to be deleted. */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2519 if (delobj != NULL) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2520 PyErr_SetString(PyExc_TypeError, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2521 "deletions are implemented differently for unicode"); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2522 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2523 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2524 return PyUnicode_Translate((PyObject *)self, tableobj, NULL); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2525 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2526 #endif |
|
41440
cedc8d7d1c7f
SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents:
40925
diff
changeset
|
2527 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
|
2528 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2529 |
|
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
|
2530 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
|
2531 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
|
2532 "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
|
2533 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
|
2534 } |
|
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
|
2535 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2536 if (delobj != NULL) { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2537 if (PyString_Check(delobj)) { |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2538 del_table = PyString_AS_STRING(delobj); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2539 dellen = PyString_GET_SIZE(delobj); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2540 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2541 #ifdef Py_USING_UNICODE |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2542 else if (PyUnicode_Check(delobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2543 PyErr_SetString(PyExc_TypeError, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2544 "deletions are implemented differently for unicode"); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2545 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2546 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2547 #endif |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2548 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
|
2549 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2550 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2551 else { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2552 del_table = NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2553 dellen = 0; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2554 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2555 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2556 inlen = PyString_GET_SIZE(input_obj); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2557 result = PyString_FromStringAndSize((char *)NULL, inlen); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2558 if (result == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2559 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2560 output_start = output = PyString_AsString(result); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2561 input = PyString_AS_STRING(input_obj); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2562 |
|
41440
cedc8d7d1c7f
SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents:
40925
diff
changeset
|
2563 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
|
2564 /* 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
|
2565 for (i = inlen; --i >= 0; ) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2566 c = Py_CHARMASK(*input++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2567 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
|
2568 changed = 1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2569 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2570 if (changed || !PyString_CheckExact(input_obj)) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2571 return result; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2572 Py_DECREF(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2573 Py_INCREF(input_obj); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2574 return input_obj; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2575 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2576 |
|
41440
cedc8d7d1c7f
SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents:
40925
diff
changeset
|
2577 if (table == NULL) { |
|
cedc8d7d1c7f
SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents:
40925
diff
changeset
|
2578 for (i = 0; i < 256; i++) |
|
cedc8d7d1c7f
SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents:
40925
diff
changeset
|
2579 trans_table[i] = Py_CHARMASK(i); |
|
cedc8d7d1c7f
SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents:
40925
diff
changeset
|
2580 } else { |
|
cedc8d7d1c7f
SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents:
40925
diff
changeset
|
2581 for (i = 0; i < 256; i++) |
|
cedc8d7d1c7f
SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents:
40925
diff
changeset
|
2582 trans_table[i] = Py_CHARMASK(table[i]); |
|
cedc8d7d1c7f
SF 1193128: Let str.translate(None) be an identity transformation
Raymond Hettinger <python@rcn.com>
parents:
40925
diff
changeset
|
2583 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2584 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2585 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
|
2586 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
|
2587 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2588 for (i = inlen; --i >= 0; ) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2589 c = Py_CHARMASK(*input++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2590 if (trans_table[c] != -1) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2591 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
|
2592 continue; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2593 changed = 1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2594 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2595 if (!changed && PyString_CheckExact(input_obj)) { |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2596 Py_DECREF(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2597 Py_INCREF(input_obj); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2598 return input_obj; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2599 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2600 /* 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
|
2601 if (inlen > 0) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2602 _PyString_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
|
2603 return result; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2604 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2605 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2606 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2607 #define FORWARD 1 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2608 #define REVERSE -1 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2609 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2610 /* find and count characters and substrings */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2611 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2612 #define findchar(target, target_len, c) \ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2613 ((char *)memchr((const void *)(target), c, target_len)) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2614 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2615 /* String ops must return a string. */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2616 /* If the object is subclass of string, create a copy */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2617 Py_LOCAL(PyStringObject *) |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2618 return_self(PyStringObject *self) |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2619 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2620 if (PyString_CheckExact(self)) { |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2621 Py_INCREF(self); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2622 return self; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2623 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2624 return (PyStringObject *)PyString_FromStringAndSize( |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2625 PyString_AS_STRING(self), |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2626 PyString_GET_SIZE(self)); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2627 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2628 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
2629 Py_LOCAL_INLINE(Py_ssize_t) |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2630 countchar(const char *target, int target_len, char c, Py_ssize_t maxcount) |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2631 { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2632 Py_ssize_t count=0; |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2633 const char *start=target; |
|
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2634 const char *end=target+target_len; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2635 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2636 while ( (start=findchar(start, end-start, c)) != NULL ) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2637 count++; |
|
38410
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2638 if (count >= maxcount) |
|
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2639 break; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2640 start += 1; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2641 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2642 return count; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2643 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2644 |
|
38376
a16948e9915e
needforspeed: use Py_LOCAL on a few more locals in stringobject.c
Fredrik Lundh <fredrik@pythonware.com>
parents:
38372
diff
changeset
|
2645 Py_LOCAL(Py_ssize_t) |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2646 findstring(const char *target, Py_ssize_t target_len, |
|
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2647 const char *pattern, Py_ssize_t pattern_len, |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2648 Py_ssize_t start, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2649 Py_ssize_t end, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2650 int direction) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2651 { |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2652 if (start < 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2653 start += target_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2654 if (start < 0) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2655 start = 0; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2656 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2657 if (end > target_len) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2658 end = target_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2659 } else if (end < 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2660 end += target_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2661 if (end < 0) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2662 end = 0; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2663 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2664 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2665 /* zero-length substrings always match at the first attempt */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2666 if (pattern_len == 0) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2667 return (direction > 0) ? start : end; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2668 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2669 end -= pattern_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2670 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2671 if (direction < 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2672 for (; end >= start; end--) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2673 if (Py_STRING_MATCH(target, end, pattern, pattern_len)) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2674 return end; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2675 } else { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2676 for (; start <= end; start++) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2677 if (Py_STRING_MATCH(target, start, pattern, pattern_len)) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2678 return start; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2679 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2680 return -1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2681 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2682 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
2683 Py_LOCAL_INLINE(Py_ssize_t) |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2684 countstring(const char *target, Py_ssize_t target_len, |
|
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2685 const char *pattern, Py_ssize_t pattern_len, |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2686 Py_ssize_t start, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2687 Py_ssize_t end, |
|
38410
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2688 int direction, Py_ssize_t maxcount) |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2689 { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2690 Py_ssize_t count=0; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2691 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2692 if (start < 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2693 start += target_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2694 if (start < 0) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2695 start = 0; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2696 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2697 if (end > target_len) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2698 end = target_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2699 } else if (end < 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2700 end += target_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2701 if (end < 0) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2702 end = 0; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2703 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2704 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2705 /* zero-length substrings match everywhere */ |
|
38410
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2706 if (pattern_len == 0 || maxcount == 0) { |
|
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2707 if (target_len+1 < maxcount) |
|
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2708 return target_len+1; |
|
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2709 return maxcount; |
|
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2710 } |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2711 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2712 end -= pattern_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2713 if (direction < 0) { |
|
38410
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2714 for (; (end >= start); end--) |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2715 if (Py_STRING_MATCH(target, end, pattern, pattern_len)) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2716 count++; |
|
38410
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2717 if (--maxcount <= 0) break; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2718 end -= pattern_len-1; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2719 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2720 } else { |
|
38410
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2721 for (; (start <= end); start++) |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2722 if (Py_STRING_MATCH(target, start, pattern, pattern_len)) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2723 count++; |
|
38410
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2724 if (--maxcount <= 0) |
|
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2725 break; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2726 start += pattern_len-1; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2727 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2728 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2729 return count; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2730 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2731 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2732 |
|
38424
3d81faf47af7
needforspeed: more stringlib refactoring
Fredrik Lundh <fredrik@pythonware.com>
parents:
38417
diff
changeset
|
2733 /* Algorithms for different cases of string replacement */ |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2734 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2735 /* len(self)>=1, from="", len(to)>=1, maxcount>=1 */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2736 Py_LOCAL(PyStringObject *) |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2737 replace_interleave(PyStringObject *self, |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2738 const char *to_s, Py_ssize_t to_len, |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2739 Py_ssize_t maxcount) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2740 { |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2741 char *self_s, *result_s; |
|
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2742 Py_ssize_t self_len, result_len; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2743 Py_ssize_t count, i, product; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2744 PyStringObject *result; |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2745 |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2746 self_len = PyString_GET_SIZE(self); |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2747 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2748 /* 1 at the end plus 1 after every character */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2749 count = self_len+1; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2750 if (maxcount < count) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2751 count = maxcount; |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2752 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2753 /* Check for overflow */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2754 /* result_len = count * to_len + self_len; */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2755 product = count * to_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2756 if (product / to_len != count) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2757 PyErr_SetString(PyExc_OverflowError, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2758 "replace string is too long"); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2759 return NULL; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2760 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2761 result_len = product + self_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2762 if (result_len < 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2763 PyErr_SetString(PyExc_OverflowError, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2764 "replace string is too long"); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2765 return NULL; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2766 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2767 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2768 if (! (result = (PyStringObject *) |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2769 PyString_FromStringAndSize(NULL, result_len)) ) |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2770 return NULL; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2771 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2772 self_s = PyString_AS_STRING(self); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2773 result_s = PyString_AS_STRING(result); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2774 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2775 /* TODO: special case single character, which doesn't need memcpy */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2776 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2777 /* Lay the first one down (guaranteed this will occur) */ |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
2778 Py_MEMCPY(result_s, to_s, to_len); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2779 result_s += to_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2780 count -= 1; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2781 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2782 for (i=0; i<count; i++) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2783 *result_s++ = *self_s++; |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
2784 Py_MEMCPY(result_s, to_s, to_len); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2785 result_s += to_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2786 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2787 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2788 /* Copy the rest of the original string */ |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
2789 Py_MEMCPY(result_s, self_s, self_len-i); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2790 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2791 return result; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2792 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2793 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2794 /* Special case for deleting a single character */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2795 /* len(self)>=1, len(from)==1, to="", maxcount>=1 */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2796 Py_LOCAL(PyStringObject *) |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2797 replace_delete_single_character(PyStringObject *self, |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2798 char from_c, Py_ssize_t maxcount) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2799 { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2800 char *self_s, *result_s; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2801 char *start, *next, *end; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2802 Py_ssize_t self_len, result_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2803 Py_ssize_t count; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2804 PyStringObject *result; |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2805 |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2806 self_len = PyString_GET_SIZE(self); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2807 self_s = PyString_AS_STRING(self); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2808 |
|
38410
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2809 count = countchar(self_s, self_len, from_c, maxcount); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2810 if (count == 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2811 return return_self(self); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2812 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2813 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2814 result_len = self_len - count; /* from_len == 1 */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2815 assert(result_len>=0); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2816 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2817 if ( (result = (PyStringObject *) |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2818 PyString_FromStringAndSize(NULL, result_len)) == NULL) |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2819 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2820 result_s = PyString_AS_STRING(result); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2821 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2822 start = self_s; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2823 end = self_s + self_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2824 while (count-- > 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2825 next = findchar(start, end-start, from_c); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2826 if (next == NULL) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2827 break; |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
2828 Py_MEMCPY(result_s, start, next-start); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2829 result_s += (next-start); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2830 start = next+1; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2831 } |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
2832 Py_MEMCPY(result_s, start, end-start); |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2833 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2834 return result; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2835 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2836 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2837 /* len(self)>=1, len(from)>=2, to="", maxcount>=1 */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2838 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2839 Py_LOCAL(PyStringObject *) |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2840 replace_delete_substring(PyStringObject *self, |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2841 const char *from_s, Py_ssize_t from_len, |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2842 Py_ssize_t maxcount) { |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2843 char *self_s, *result_s; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2844 char *start, *next, *end; |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2845 Py_ssize_t self_len, result_len; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2846 Py_ssize_t count, offset; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2847 PyStringObject *result; |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2848 |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2849 self_len = PyString_GET_SIZE(self); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2850 self_s = PyString_AS_STRING(self); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2851 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2852 count = countstring(self_s, self_len, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2853 from_s, from_len, |
|
38410
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2854 0, self_len, 1, |
|
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
2855 maxcount); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2856 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2857 if (count == 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2858 /* no matches */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2859 return return_self(self); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2860 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2861 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2862 result_len = self_len - (count * from_len); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2863 assert (result_len>=0); |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2864 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2865 if ( (result = (PyStringObject *) |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2866 PyString_FromStringAndSize(NULL, result_len)) == NULL ) |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2867 return NULL; |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2868 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2869 result_s = PyString_AS_STRING(result); |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2870 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2871 start = self_s; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2872 end = self_s + self_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2873 while (count-- > 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2874 offset = findstring(start, end-start, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2875 from_s, from_len, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2876 0, end-start, FORWARD); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2877 if (offset == -1) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2878 break; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2879 next = start + offset; |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2880 |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
2881 Py_MEMCPY(result_s, start, next-start); |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2882 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2883 result_s += (next-start); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2884 start = next+from_len; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2885 } |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
2886 Py_MEMCPY(result_s, start, end-start); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2887 return result; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2888 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2889 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2890 /* len(self)>=1, len(from)==len(to)==1, maxcount>=1 */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2891 Py_LOCAL(PyStringObject *) |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2892 replace_single_character_in_place(PyStringObject *self, |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2893 char from_c, char to_c, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2894 Py_ssize_t maxcount) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2895 { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2896 char *self_s, *result_s, *start, *end, *next; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2897 Py_ssize_t self_len; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2898 PyStringObject *result; |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2899 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2900 /* The result string will be the same size */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2901 self_s = PyString_AS_STRING(self); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2902 self_len = PyString_GET_SIZE(self); |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2903 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2904 next = findchar(self_s, self_len, from_c); |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2905 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2906 if (next == NULL) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2907 /* No matches; return the original string */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2908 return return_self(self); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2909 } |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2910 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2911 /* Need to make a new string */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2912 result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2913 if (result == NULL) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2914 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2915 result_s = PyString_AS_STRING(result); |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
2916 Py_MEMCPY(result_s, self_s, self_len); |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2917 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2918 /* change everything in-place, starting with this one */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2919 start = result_s + (next-self_s); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2920 *start = to_c; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2921 start++; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2922 end = result_s + self_len; |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2923 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2924 while (--maxcount > 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2925 next = findchar(start, end-start, from_c); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2926 if (next == NULL) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2927 break; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2928 *next = to_c; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2929 start = next+1; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2930 } |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2931 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2932 return result; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2933 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2934 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2935 /* len(self)>=1, len(from)==len(to)>=2, maxcount>=1 */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2936 Py_LOCAL(PyStringObject *) |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2937 replace_substring_in_place(PyStringObject *self, |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2938 const char *from_s, Py_ssize_t from_len, |
|
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2939 const char *to_s, Py_ssize_t to_len, |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2940 Py_ssize_t maxcount) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2941 { |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2942 char *result_s, *start, *end; |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2943 char *self_s; |
|
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2944 Py_ssize_t self_len, offset; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2945 PyStringObject *result; |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2946 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2947 /* The result string will be the same size */ |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2948 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2949 self_s = PyString_AS_STRING(self); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2950 self_len = PyString_GET_SIZE(self); |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2951 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2952 offset = findstring(self_s, self_len, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2953 from_s, from_len, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2954 0, self_len, FORWARD); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2955 if (offset == -1) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2956 /* No matches; return the original string */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2957 return return_self(self); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2958 } |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2959 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2960 /* Need to make a new string */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2961 result = (PyStringObject *) PyString_FromStringAndSize(NULL, self_len); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2962 if (result == NULL) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2963 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2964 result_s = PyString_AS_STRING(result); |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
2965 Py_MEMCPY(result_s, self_s, self_len); |
|
38302
9c08a7bdbc31
Fixed problem identified by Georg. The special-case in-place code for replace
Andrew Dalke <dalke@dalkescientific.com>
parents:
38300
diff
changeset
|
2966 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2967 /* change everything in-place, starting with this one */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2968 start = result_s + offset; |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
2969 Py_MEMCPY(start, to_s, from_len); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2970 start += from_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2971 end = result_s + self_len; |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2972 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2973 while ( --maxcount > 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2974 offset = findstring(start, end-start, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2975 from_s, from_len, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2976 0, end-start, FORWARD); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2977 if (offset==-1) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2978 break; |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
2979 Py_MEMCPY(start+offset, to_s, from_len); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2980 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
|
2981 } |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
2982 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2983 return result; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2984 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2985 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2986 /* len(self)>=1, len(from)==1, len(to)>=2, maxcount>=1 */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2987 Py_LOCAL(PyStringObject *) |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2988 replace_single_character(PyStringObject *self, |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2989 char from_c, |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2990 const char *to_s, Py_ssize_t to_len, |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2991 Py_ssize_t maxcount) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2992 { |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2993 char *self_s, *result_s; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2994 char *start, *next, *end; |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
2995 Py_ssize_t self_len, result_len; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
2996 Py_ssize_t count, product; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2997 PyStringObject *result; |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2998 |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
2999 self_s = PyString_AS_STRING(self); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3000 self_len = PyString_GET_SIZE(self); |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
3001 |
|
38410
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
3002 count = countchar(self_s, self_len, from_c, maxcount); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3003 if (count == 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3004 /* no matches, return unchanged */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3005 return return_self(self); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3006 } |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3007 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3008 /* use the difference between current and new, hence the "-1" */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3009 /* result_len = self_len + count * (to_len-1) */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3010 product = count * (to_len-1); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3011 if (product / (to_len-1) != count) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3012 PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3013 return NULL; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3014 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3015 result_len = self_len + product; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3016 if (result_len < 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3017 PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3018 return NULL; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3019 } |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
3020 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3021 if ( (result = (PyStringObject *) |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3022 PyString_FromStringAndSize(NULL, result_len)) == NULL) |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3023 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3024 result_s = PyString_AS_STRING(result); |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
3025 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3026 start = self_s; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3027 end = self_s + self_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3028 while (count-- > 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3029 next = findchar(start, end-start, from_c); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3030 if (next == NULL) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3031 break; |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
3032 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3033 if (next == start) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3034 /* replace with the 'to' */ |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
3035 Py_MEMCPY(result_s, to_s, to_len); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3036 result_s += to_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3037 start += 1; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3038 } else { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3039 /* copy the unchanged old then the 'to' */ |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
3040 Py_MEMCPY(result_s, start, next-start); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3041 result_s += (next-start); |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
3042 Py_MEMCPY(result_s, to_s, to_len); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3043 result_s += to_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3044 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
|
3045 } |
|
17750
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
3046 } |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3047 /* Copy the remainder of the remaining string */ |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
3048 Py_MEMCPY(result_s, start, end-start); |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
3049 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3050 return result; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3051 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3052 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3053 /* len(self)>=1, len(from)>=2, len(to)>=2, maxcount>=1 */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3054 Py_LOCAL(PyStringObject *) |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3055 replace_substring(PyStringObject *self, |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3056 const char *from_s, Py_ssize_t from_len, |
|
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3057 const char *to_s, Py_ssize_t to_len, |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3058 Py_ssize_t maxcount) { |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3059 char *self_s, *result_s; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3060 char *start, *next, *end; |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3061 Py_ssize_t self_len, result_len; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3062 Py_ssize_t count, offset, product; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3063 PyStringObject *result; |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3064 |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3065 self_s = PyString_AS_STRING(self); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3066 self_len = PyString_GET_SIZE(self); |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3067 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3068 count = countstring(self_s, self_len, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3069 from_s, from_len, |
|
38410
4697d1958ccb
Added limits to the replace code so it does not count all of the matching
Andrew Dalke <dalke@dalkescientific.com>
parents:
38404
diff
changeset
|
3070 0, self_len, FORWARD, maxcount); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3071 if (count == 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3072 /* no matches, return unchanged */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3073 return return_self(self); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3074 } |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3075 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3076 /* Check for overflow */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3077 /* result_len = self_len + count * (to_len-from_len) */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3078 product = count * (to_len-from_len); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3079 if (product / (to_len-from_len) != count) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3080 PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3081 return NULL; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3082 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3083 result_len = self_len + product; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3084 if (result_len < 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3085 PyErr_SetString(PyExc_OverflowError, "replace string is too long"); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3086 return NULL; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3087 } |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
3088 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3089 if ( (result = (PyStringObject *) |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3090 PyString_FromStringAndSize(NULL, result_len)) == NULL) |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3091 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3092 result_s = PyString_AS_STRING(result); |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
3093 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3094 start = self_s; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3095 end = self_s + self_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3096 while (count-- > 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3097 offset = findstring(start, end-start, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3098 from_s, from_len, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3099 0, end-start, FORWARD); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3100 if (offset == -1) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3101 break; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3102 next = start+offset; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3103 if (next == start) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3104 /* replace with the 'to' */ |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
3105 Py_MEMCPY(result_s, to_s, to_len); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3106 result_s += to_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3107 start += from_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3108 } else { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3109 /* copy the unchanged old then the 'to' */ |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
3110 Py_MEMCPY(result_s, start, next-start); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3111 result_s += (next-start); |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
3112 Py_MEMCPY(result_s, to_s, to_len); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3113 result_s += to_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3114 start = next+from_len; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3115 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3116 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3117 /* Copy the remainder of the remaining string */ |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
3118 Py_MEMCPY(result_s, start, end-start); |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
3119 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3120 return result; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3121 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3122 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3123 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3124 Py_LOCAL(PyStringObject *) |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3125 replace(PyStringObject *self, |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3126 const char *from_s, Py_ssize_t from_len, |
|
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3127 const char *to_s, Py_ssize_t to_len, |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3128 Py_ssize_t maxcount) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3129 { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3130 if (maxcount < 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3131 maxcount = PY_SSIZE_T_MAX; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3132 } else if (maxcount == 0 || PyString_GET_SIZE(self) == 0) { |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3133 /* nothing to do; return the original string */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3134 return return_self(self); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3135 } |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3136 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3137 if (maxcount == 0 || |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3138 (from_len == 0 && to_len == 0)) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3139 /* nothing to do; return the original string */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3140 return return_self(self); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3141 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3142 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3143 /* Handle zero-length special cases */ |
|
39303
1b4e5eb21049
Whitespace normalization
Neal Norwitz <nnorwitz@gmail.com>
parents:
39302
diff
changeset
|
3144 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3145 if (from_len == 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3146 /* insert the 'to' string everywhere. */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3147 /* >>> "Python".replace("", ".") */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3148 /* '.P.y.t.h.o.n.' */ |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3149 return replace_interleave(self, to_s, to_len, maxcount); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3150 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3151 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3152 /* Except for "".replace("", "A") == "A" there is no way beyond this */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3153 /* point for an empty self string to generate a non-empty string */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3154 /* Special case so the remaining code always gets a non-empty string */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3155 if (PyString_GET_SIZE(self) == 0) { |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3156 return return_self(self); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3157 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3158 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3159 if (to_len == 0) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3160 /* delete all occurances of 'from' string */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3161 if (from_len == 1) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3162 return replace_delete_single_character( |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3163 self, from_s[0], maxcount); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3164 } else { |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3165 return replace_delete_substring(self, from_s, from_len, maxcount); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3166 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3167 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3168 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3169 /* Handle special case where both strings have the same length */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3170 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3171 if (from_len == to_len) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3172 if (from_len == 1) { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3173 return replace_single_character_in_place( |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3174 self, |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3175 from_s[0], |
|
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3176 to_s[0], |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3177 maxcount); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3178 } else { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3179 return replace_substring_in_place( |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3180 self, from_s, from_len, to_s, to_len, maxcount); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3181 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3182 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3183 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3184 /* Otherwise use the more generic algorithms */ |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3185 if (from_len == 1) { |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3186 return replace_single_character(self, from_s[0], |
|
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3187 to_s, to_len, maxcount); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3188 } else { |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3189 /* len('from')>=2, len('to')>=1 */ |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3190 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount); |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3191 } |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3192 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3193 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3194 PyDoc_STRVAR(replace__doc__, |
|
30751
6fdde96a7222
Avoid confusing name for the 3rd argument to str.replace().
Fred Drake <fdrake@acm.org>
parents:
30706
diff
changeset
|
3195 "S.replace (old, new[, count]) -> string\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3196 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3197 Return a copy of string S with all occurrences of substring\n\ |
|
30751
6fdde96a7222
Avoid confusing name for the 3rd argument to str.replace().
Fred Drake <fdrake@acm.org>
parents:
30706
diff
changeset
|
3198 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
|
3199 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
|
3200 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3201 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3202 string_replace(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3203 { |
|
37858
74f459a289be
Make s.replace() work with explicit counts exceeding 2Gb.
Thomas Wouters <thomas@python.org>
parents:
37855
diff
changeset
|
3204 Py_ssize_t count = -1; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3205 PyObject *from, *to; |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3206 const char *from_s, *to_s; |
|
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3207 Py_ssize_t from_len, to_len; |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3208 |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3209 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
|
3210 return NULL; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3211 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3212 if (PyString_Check(from)) { |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3213 from_s = PyString_AS_STRING(from); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3214 from_len = PyString_GET_SIZE(from); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3215 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
3216 #ifdef Py_USING_UNICODE |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3217 if (PyUnicode_Check(from)) |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
3218 return PyUnicode_Replace((PyObject *)self, |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3219 from, to, count); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
3220 #endif |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3221 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
|
3222 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3223 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3224 if (PyString_Check(to)) { |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3225 to_s = PyString_AS_STRING(to); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3226 to_len = PyString_GET_SIZE(to); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3227 } |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3228 #ifdef Py_USING_UNICODE |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3229 else if (PyUnicode_Check(to)) |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3230 return PyUnicode_Replace((PyObject *)self, |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3231 from, to, count); |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3232 #endif |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3233 else if (PyObject_AsCharBuffer(to, &to_s, &to_len)) |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3234 return NULL; |
|
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3235 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3236 return (PyObject *)replace((PyStringObject *) self, |
|
39302
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3237 from_s, from_len, |
|
041eb6231cdc
Bug #1515471: string.replace() accepts character buffers again.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38738
diff
changeset
|
3238 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
|
3239 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3240 |
|
38300
41809406a35e
needforspeed: new replace implementation by Andrew Dalke. replace is
Fredrik Lundh <fredrik@pythonware.com>
parents:
38299
diff
changeset
|
3241 /** End DALKE **/ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3242 |
|
38738
0a8a392e1fae
Update doc to make it agree with code.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38709
diff
changeset
|
3243 /* Matches the end (direction >= 0) or start (direction < 0) of self |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3244 * against substr, using the start and end arguments. Returns |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3245 * -1 on error, 0 if not found and 1 if found. |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3246 */ |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3247 Py_LOCAL(int) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3248 _string_tailmatch(PyStringObject *self, PyObject *substr, Py_ssize_t start, |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3249 Py_ssize_t end, int direction) |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3250 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3251 Py_ssize_t len = PyString_GET_SIZE(self); |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3252 Py_ssize_t slen; |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3253 const char* sub; |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3254 const char* str; |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3255 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3256 if (PyString_Check(substr)) { |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3257 sub = PyString_AS_STRING(substr); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3258 slen = PyString_GET_SIZE(substr); |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3259 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3260 #ifdef Py_USING_UNICODE |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3261 else if (PyUnicode_Check(substr)) |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3262 return PyUnicode_Tailmatch((PyObject *)self, |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3263 substr, start, end, direction); |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3264 #endif |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3265 else if (PyObject_AsCharBuffer(substr, &sub, &slen)) |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3266 return -1; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3267 str = PyString_AS_STRING(self); |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3268 |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3269 string_adjust_indices(&start, &end, len); |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3270 |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3271 if (direction < 0) { |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3272 /* startswith */ |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3273 if (start+slen > len) |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3274 return 0; |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3275 } else { |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3276 /* endswith */ |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3277 if (end-start < slen || start > len) |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3278 return 0; |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3279 |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3280 if (end-slen > start) |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3281 start = end - slen; |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3282 } |
|
38738
0a8a392e1fae
Update doc to make it agree with code.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38709
diff
changeset
|
3283 if (end-start >= slen) |
|
0a8a392e1fae
Update doc to make it agree with code.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38709
diff
changeset
|
3284 return ! memcmp(str+start, sub, slen); |
|
0a8a392e1fae
Update doc to make it agree with code.
Neal Norwitz <nnorwitz@gmail.com>
parents:
38709
diff
changeset
|
3285 return 0; |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3286 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3287 |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3288 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3289 PyDoc_STRVAR(startswith__doc__, |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3290 "S.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
|
3291 \n\ |
|
28559
aff080f2efb4
Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents:
28478
diff
changeset
|
3292 Return True if S starts with the specified prefix, False otherwise.\n\ |
|
aff080f2efb4
Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents:
28478
diff
changeset
|
3293 With optional start, test S beginning at that position.\n\ |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3294 With optional end, stop comparing S at that position.\n\ |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3295 prefix can also be a tuple of strings to try."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3296 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3297 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3298 string_startswith(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3299 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
3300 Py_ssize_t start = 0; |
|
37667
b1724e926e07
Replace most INT_MAX with PY_SSIZE_T_MAX.
Martin v. Löwis <martin@v.loewis.de>
parents:
37596
diff
changeset
|
3301 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
|
3302 PyObject *subobj; |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3303 int result; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3304 |
| 12615 | 3305 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 3306 _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
|
3307 return NULL; |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3308 if (PyTuple_Check(subobj)) { |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3309 Py_ssize_t i; |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3310 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3311 result = _string_tailmatch(self, |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3312 PyTuple_GET_ITEM(subobj, i), |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3313 start, end, -1); |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3314 if (result == -1) |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3315 return NULL; |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3316 else if (result) { |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3317 Py_RETURN_TRUE; |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3318 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3319 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3320 Py_RETURN_FALSE; |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
3321 } |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3322 result = _string_tailmatch(self, subobj, start, end, -1); |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3323 if (result == -1) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3324 return NULL; |
|
23850
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
3325 else |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3326 return PyBool_FromLong(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3327 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3328 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3329 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3330 PyDoc_STRVAR(endswith__doc__, |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3331 "S.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
|
3332 \n\ |
|
28559
aff080f2efb4
Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents:
28478
diff
changeset
|
3333 Return True if S ends with the specified suffix, False otherwise.\n\ |
|
aff080f2efb4
Reformat a few docstrings that caused line wraps in help() output.
Guido van Rossum <guido@python.org>
parents:
28478
diff
changeset
|
3334 With optional start, test S beginning at that position.\n\ |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3335 With optional end, stop comparing S at that position.\n\ |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3336 suffix can also be a tuple of strings to try."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3337 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3338 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3339 string_endswith(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3340 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
3341 Py_ssize_t start = 0; |
|
37667
b1724e926e07
Replace most INT_MAX with PY_SSIZE_T_MAX.
Martin v. Löwis <martin@v.loewis.de>
parents:
37596
diff
changeset
|
3342 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
|
3343 PyObject *subobj; |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3344 int result; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3345 |
| 12615 | 3346 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 3347 _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
|
3348 return NULL; |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3349 if (PyTuple_Check(subobj)) { |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3350 Py_ssize_t i; |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3351 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3352 result = _string_tailmatch(self, |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3353 PyTuple_GET_ITEM(subobj, i), |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3354 start, end, +1); |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3355 if (result == -1) |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3356 return NULL; |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3357 else if (result) { |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3358 Py_RETURN_TRUE; |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3359 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3360 } |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3361 Py_RETURN_FALSE; |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
3362 } |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3363 result = _string_tailmatch(self, subobj, start, end, +1); |
|
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3364 if (result == -1) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3365 return NULL; |
|
23850
854583cd63c7
SF bug # 493951 string.{starts,ends}with vs slices
Neal Norwitz <nnorwitz@gmail.com>
parents:
23832
diff
changeset
|
3366 else |
|
38697
1cafbcd7f55c
RFE #1491485: str/unicode.endswith()/startswith() now accept a tuple as first argument.
Georg Brandl <georg@python.org>
parents:
38531
diff
changeset
|
3367 return PyBool_FromLong(result); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3368 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3369 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
3370 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3371 PyDoc_STRVAR(encode__doc__, |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3372 "S.encode([encoding[,errors]]) -> object\n\ |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
3373 \n\ |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3374 Encodes S using the codec registered for encoding. encoding defaults\n\ |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3375 to the default encoding. errors may be given to set a different error\n\ |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
3376 handling scheme. Default is 'strict' meaning that encoding errors raise\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
|
3377 a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and\n\ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25017
diff
changeset
|
3378 'xmlcharrefreplace' as well as any other name registered with\n\ |
|
0337dad8403e
PEP 293 implemention (from SF patch http://www.python.org/sf/432401)
Walter Dörwald <walter@livinglogic.de>
parents:
25017
diff
changeset
|
3379 codecs.register_error that is able to handle UnicodeEncodeErrors."); |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
3380 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
3381 static PyObject * |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
3382 string_encode(PyStringObject *self, PyObject *args, PyObject *kwargs) |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
3383 { |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
3384 static char *kwlist[] = {"encoding", "errors", 0}; |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
3385 char *encoding = NULL; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
3386 char *errors = NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3387 PyObject *v; |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
3388 |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
3389 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:encode", |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
3390 kwlist, &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
|
3391 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3392 v = PyString_AsEncodedObject((PyObject *)self, encoding, errors); |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3393 if (v == NULL) |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3394 goto onError; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3395 if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3396 PyErr_Format(PyExc_TypeError, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3397 "encoder did not return a string/unicode object " |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3398 "(type=%.400s)", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
3399 Py_TYPE(v)->tp_name); |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3400 Py_DECREF(v); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3401 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3402 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3403 return v; |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3404 |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3405 onError: |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3406 return NULL; |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3407 } |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3408 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3409 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3410 PyDoc_STRVAR(decode__doc__, |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3411 "S.decode([encoding[,errors]]) -> object\n\ |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3412 \n\ |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3413 Decodes S using the codec registered for encoding. encoding defaults\n\ |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3414 to the default encoding. errors may be given to set a different error\n\ |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3415 handling scheme. Default is 'strict' meaning that encoding errors raise\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
|
3416 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ |
|
49431
2e794757bdc8
Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48935
diff
changeset
|
3417 as well as any other name registered with codecs.register_error that is\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
|
3418 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
|
3419 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3420 static PyObject * |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
3421 string_decode(PyStringObject *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
|
3422 { |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
3423 static char *kwlist[] = {"encoding", "errors", 0}; |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3424 char *encoding = NULL; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3425 char *errors = NULL; |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3426 PyObject *v; |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
3427 |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
3428 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
3429 kwlist, &encoding, &errors)) |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
3430 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3431 v = PyString_AsDecodedObject((PyObject *)self, encoding, errors); |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3432 if (v == NULL) |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3433 goto onError; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3434 if (!PyString_Check(v) && !PyUnicode_Check(v)) { |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3435 PyErr_Format(PyExc_TypeError, |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3436 "decoder did not return a string/unicode object " |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3437 "(type=%.400s)", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
3438 Py_TYPE(v)->tp_name); |
|
32551
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3439 Py_DECREF(v); |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3440 return NULL; |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3441 } |
|
f46d49e2e0f0
Allow string and unicode return types from .encode()/.decode()
Marc-André Lemburg <mal@egenix.com>
parents:
32423
diff
changeset
|
3442 return v; |
|
32552
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3443 |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3444 onError: |
|
3706a3704aea
.encode()/.decode() patch part 2.
Marc-André Lemburg <mal@egenix.com>
parents:
32551
diff
changeset
|
3445 return NULL; |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
3446 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
3447 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
3448 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3449 PyDoc_STRVAR(expandtabs__doc__, |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3450 "S.expandtabs([tabsize]) -> string\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3451 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3452 Return a copy of S where all tab characters are expanded using spaces.\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3453 If tabsize is not given, a tab size of 8 characters is assumed."); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3454 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3455 static PyObject* |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3456 string_expandtabs(PyStringObject *self, PyObject *args) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3457 { |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3458 const char *e, *p, *qe; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3459 char *q; |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3460 Py_ssize_t i, j, incr; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3461 PyObject *u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3462 int tabsize = 8; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3463 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3464 if (!PyArg_ParseTuple(args, "|i:expandtabs", &tabsize)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3465 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3466 |
|
13620
5b727ab37092
Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents:
13576
diff
changeset
|
3467 /* First pass: determine size of output string */ |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3468 i = 0; /* chars up to and including most recent \n or \r */ |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3469 j = 0; /* chars since most recent \n or \r (use in tab calculations) */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3470 e = PyString_AS_STRING(self) + PyString_GET_SIZE(self); /* end of input */ |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3471 for (p = PyString_AS_STRING(self); p < e; p++) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3472 if (*p == '\t') { |
|
42004
e62b606eff0b
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
41440
diff
changeset
|
3473 if (tabsize > 0) { |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3474 incr = tabsize - (j % tabsize); |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3475 if (j > PY_SSIZE_T_MAX - incr) |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3476 goto overflow1; |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3477 j += incr; |
|
42004
e62b606eff0b
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
41440
diff
changeset
|
3478 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3479 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3480 else { |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3481 if (j > PY_SSIZE_T_MAX - 1) |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3482 goto overflow1; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3483 j++; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3484 if (*p == '\n' || *p == '\r') { |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3485 if (i > PY_SSIZE_T_MAX - j) |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3486 goto overflow1; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3487 i += j; |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3488 j = 0; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3489 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3490 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3491 |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3492 if (i > PY_SSIZE_T_MAX - j) |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3493 goto overflow1; |
|
42004
e62b606eff0b
Prevent expandtabs() on string and unicode objects from causing a segfault when
Neal Norwitz <nnorwitz@gmail.com>
parents:
41440
diff
changeset
|
3494 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3495 /* Second pass: create output string and fill it */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3496 u = PyString_FromStringAndSize(NULL, i + j); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3497 if (!u) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3498 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3499 |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3500 j = 0; /* same as in first pass */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3501 q = PyString_AS_STRING(u); /* next output char */ |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3502 qe = PyString_AS_STRING(u) + PyString_GET_SIZE(u); /* end of output */ |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3503 |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3504 for (p = PyString_AS_STRING(self); p < e; p++) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3505 if (*p == '\t') { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3506 if (tabsize > 0) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3507 i = tabsize - (j % tabsize); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3508 j += i; |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3509 while (i--) { |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3510 if (q >= qe) |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3511 goto overflow2; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3512 *q++ = ' '; |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3513 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3514 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3515 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3516 else { |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3517 if (q >= qe) |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3518 goto overflow2; |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3519 *q++ = *p; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3520 j++; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3521 if (*p == '\n' || *p == '\r') |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3522 j = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3523 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3524 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3525 return u; |
|
45731
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3526 |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3527 overflow2: |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3528 Py_DECREF(u); |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3529 overflow1: |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3530 PyErr_SetString(PyExc_OverflowError, "new string is too long"); |
|
162c6952ea34
Fix the overflows in expandtabs(). "This time for sure!"
Guido van Rossum <guido@python.org>
parents:
45500
diff
changeset
|
3531 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3532 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3533 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
3534 Py_LOCAL_INLINE(PyObject *) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3535 pad(PyStringObject *self, Py_ssize_t left, Py_ssize_t right, char fill) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3536 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3537 PyObject *u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3538 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3539 if (left < 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3540 left = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3541 if (right < 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3542 right = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3543 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3544 if (left == 0 && right == 0 && PyString_CheckExact(self)) { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3545 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3546 return (PyObject *)self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3547 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3548 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3549 u = PyString_FromStringAndSize(NULL, |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3550 left + PyString_GET_SIZE(self) + right); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3551 if (u) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3552 if (left) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3553 memset(PyString_AS_STRING(u), fill, left); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3554 Py_MEMCPY(PyString_AS_STRING(u) + left, |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3555 PyString_AS_STRING(self), |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3556 PyString_GET_SIZE(self)); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3557 if (right) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3558 memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self), |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3559 fill, right); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3560 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3561 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3562 return u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3563 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3564 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3565 PyDoc_STRVAR(ljust__doc__, |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3566 "S.ljust(width[, fillchar]) -> string\n" |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
3567 "\n" |
|
49431
2e794757bdc8
Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48935
diff
changeset
|
3568 "Return S left-justified in a string of length width. Padding is\n" |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3569 "done using the specified fill character (default is a space)."); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3570 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3571 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3572 string_ljust(PyStringObject *self, PyObject *args) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3573 { |
|
37855
dc8efd70ab56
Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents:
37815
diff
changeset
|
3574 Py_ssize_t width; |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3575 char fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3576 |
|
37855
dc8efd70ab56
Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents:
37815
diff
changeset
|
3577 if (!PyArg_ParseTuple(args, "n|c:ljust", &width, &fillchar)) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3578 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3579 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3580 if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3581 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3582 return (PyObject*) self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3583 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3584 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3585 return pad(self, 0, width - PyString_GET_SIZE(self), fillchar); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3586 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3587 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3588 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3589 PyDoc_STRVAR(rjust__doc__, |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3590 "S.rjust(width[, fillchar]) -> string\n" |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
3591 "\n" |
|
49431
2e794757bdc8
Merged revisions 66766-66767,66771-66772,66774,66776,66783-66787,66790,66793,66797 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48935
diff
changeset
|
3592 "Return S right-justified in a string of length width. Padding is\n" |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3593 "done using the specified fill character (default is a space)"); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3594 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3595 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3596 string_rjust(PyStringObject *self, PyObject *args) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3597 { |
|
37855
dc8efd70ab56
Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents:
37815
diff
changeset
|
3598 Py_ssize_t width; |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3599 char fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3600 |
|
37855
dc8efd70ab56
Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents:
37815
diff
changeset
|
3601 if (!PyArg_ParseTuple(args, "n|c:rjust", &width, &fillchar)) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3602 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3603 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3604 if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3605 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3606 return (PyObject*) self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3607 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3608 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3609 return pad(self, width - PyString_GET_SIZE(self), 0, fillchar); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3610 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3611 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3612 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3613 PyDoc_STRVAR(center__doc__, |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3614 "S.center(width[, fillchar]) -> string\n" |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
3615 "\n" |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3616 "Return S centered in a string of length width. Padding is\n" |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3617 "done using the specified fill character (default is a space)"); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3618 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3619 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3620 string_center(PyStringObject *self, PyObject *args) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3621 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
3622 Py_ssize_t marg, left; |
|
37855
dc8efd70ab56
Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents:
37815
diff
changeset
|
3623 Py_ssize_t width; |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3624 char fillchar = ' '; |
|
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3625 |
|
37855
dc8efd70ab56
Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents:
37815
diff
changeset
|
3626 if (!PyArg_ParseTuple(args, "n|c:center", &width, &fillchar)) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3627 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3628 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3629 if (PyString_GET_SIZE(self) >= width && PyString_CheckExact(self)) { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3630 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3631 return (PyObject*) self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3632 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3633 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3634 marg = width - PyString_GET_SIZE(self); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3635 left = marg / 2 + (marg & width & 1); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3636 |
|
31021
21a10c3600b5
Add optional fillchar argument to ljust(), rjust(), and center() string methods.
Raymond Hettinger <python@rcn.com>
parents:
30751
diff
changeset
|
3637 return pad(self, left, marg - left, fillchar); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3638 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3639 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3640 PyDoc_STRVAR(zfill__doc__, |
|
23045
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3641 "S.zfill(width) -> string\n" |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3642 "\n" |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3643 "Pad a numeric string S with zeros on the left, to fill a field\n" |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3644 "of the specified width. The string S is never truncated."); |
|
23045
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3645 |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3646 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3647 string_zfill(PyStringObject *self, PyObject *args) |
|
23045
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3648 { |
|
36519
d6d415df0df0
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36510
diff
changeset
|
3649 Py_ssize_t fill; |
|
23045
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3650 PyObject *s; |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
3651 char *p; |
|
37855
dc8efd70ab56
Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents:
37815
diff
changeset
|
3652 Py_ssize_t width; |
|
dc8efd70ab56
Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents:
37815
diff
changeset
|
3653 |
|
dc8efd70ab56
Use Py_ssize_t to hold the 'width' argument to the ljust, rjust, center and
Thomas Wouters <thomas@python.org>
parents:
37815
diff
changeset
|
3654 if (!PyArg_ParseTuple(args, "n:zfill", &width)) |
|
23045
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3655 return NULL; |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3656 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3657 if (PyString_GET_SIZE(self) >= width) { |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3658 if (PyString_CheckExact(self)) { |
|
23051
19ea00feda33
Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents:
23046
diff
changeset
|
3659 Py_INCREF(self); |
|
19ea00feda33
Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents:
23046
diff
changeset
|
3660 return (PyObject*) self; |
|
19ea00feda33
Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents:
23046
diff
changeset
|
3661 } |
|
19ea00feda33
Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents:
23046
diff
changeset
|
3662 else |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3663 return PyString_FromStringAndSize( |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3664 PyString_AS_STRING(self), |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3665 PyString_GET_SIZE(self) |
|
23051
19ea00feda33
Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents:
23046
diff
changeset
|
3666 ); |
|
23045
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3667 } |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3668 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3669 fill = width - PyString_GET_SIZE(self); |
|
23045
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3670 |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3671 s = pad(self, fill, 0, '0'); |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3672 |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3673 if (s == NULL) |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3674 return NULL; |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3675 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3676 p = PyString_AS_STRING(s); |
|
23045
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3677 if (p[fill] == '+' || p[fill] == '-') { |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3678 /* move sign to beginning of string */ |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3679 p[0] = p[fill]; |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3680 p[fill] = '0'; |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3681 } |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3682 |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3683 return (PyObject*) s; |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3684 } |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
3685 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3686 PyDoc_STRVAR(isspace__doc__, |
|
30706
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3687 "S.isspace() -> bool\n\ |
|
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3688 \n\ |
|
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3689 Return True if all characters in S are whitespace\n\ |
|
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3690 and there is at least one character in S, False otherwise."); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3691 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3692 static PyObject* |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3693 string_isspace(PyStringObject *self) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3694 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3695 register const unsigned char *p |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3696 = (unsigned char *) PyString_AS_STRING(self); |
|
12588
54274beb244d
The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
3697 register const unsigned char *e; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3698 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3699 /* Shortcut for single character strings */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3700 if (PyString_GET_SIZE(self) == 1 && |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3701 isspace(*p)) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3702 return PyBool_FromLong(1); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3703 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3704 /* Special case for empty strings */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3705 if (PyString_GET_SIZE(self) == 0) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3706 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3707 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3708 e = p + PyString_GET_SIZE(self); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3709 for (; p < e; p++) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3710 if (!isspace(*p)) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3711 return PyBool_FromLong(0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3712 } |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3713 return PyBool_FromLong(1); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3714 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3715 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3716 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3717 PyDoc_STRVAR(isalpha__doc__, |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3718 "S.isalpha() -> bool\n\ |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3719 \n\ |
|
30706
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3720 Return True if all characters in S are alphabetic\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3721 and there is at least one character in S, False otherwise."); |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3722 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3723 static PyObject* |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3724 string_isalpha(PyStringObject *self) |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3725 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3726 register const unsigned char *p |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3727 = (unsigned char *) PyString_AS_STRING(self); |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3728 register const unsigned char *e; |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3729 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3730 /* Shortcut for single character strings */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3731 if (PyString_GET_SIZE(self) == 1 && |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3732 isalpha(*p)) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3733 return PyBool_FromLong(1); |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3734 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3735 /* Special case for empty strings */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3736 if (PyString_GET_SIZE(self) == 0) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3737 return PyBool_FromLong(0); |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3738 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3739 e = p + PyString_GET_SIZE(self); |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3740 for (; p < e; p++) { |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3741 if (!isalpha(*p)) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3742 return PyBool_FromLong(0); |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3743 } |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3744 return PyBool_FromLong(1); |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3745 } |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3746 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3747 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3748 PyDoc_STRVAR(isalnum__doc__, |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3749 "S.isalnum() -> bool\n\ |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3750 \n\ |
|
30706
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3751 Return True if all characters in S are alphanumeric\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3752 and there is at least one character in S, False otherwise."); |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3753 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3754 static PyObject* |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3755 string_isalnum(PyStringObject *self) |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3756 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3757 register const unsigned char *p |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3758 = (unsigned char *) PyString_AS_STRING(self); |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3759 register const unsigned char *e; |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3760 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3761 /* Shortcut for single character strings */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3762 if (PyString_GET_SIZE(self) == 1 && |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3763 isalnum(*p)) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3764 return PyBool_FromLong(1); |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3765 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3766 /* Special case for empty strings */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3767 if (PyString_GET_SIZE(self) == 0) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3768 return PyBool_FromLong(0); |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3769 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3770 e = p + PyString_GET_SIZE(self); |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3771 for (; p < e; p++) { |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3772 if (!isalnum(*p)) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3773 return PyBool_FromLong(0); |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3774 } |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3775 return PyBool_FromLong(1); |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3776 } |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3777 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
3778 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3779 PyDoc_STRVAR(isdigit__doc__, |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3780 "S.isdigit() -> bool\n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3781 \n\ |
|
30706
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3782 Return True if all characters in S are digits\n\ |
|
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3783 and there is at least one character in S, False otherwise."); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3784 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3785 static PyObject* |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3786 string_isdigit(PyStringObject *self) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3787 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3788 register const unsigned char *p |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3789 = (unsigned char *) PyString_AS_STRING(self); |
|
12588
54274beb244d
The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
3790 register const unsigned char *e; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3791 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3792 /* Shortcut for single character strings */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3793 if (PyString_GET_SIZE(self) == 1 && |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3794 isdigit(*p)) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3795 return PyBool_FromLong(1); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3796 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3797 /* Special case for empty strings */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3798 if (PyString_GET_SIZE(self) == 0) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3799 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3800 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3801 e = p + PyString_GET_SIZE(self); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3802 for (; p < e; p++) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3803 if (!isdigit(*p)) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3804 return PyBool_FromLong(0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3805 } |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3806 return PyBool_FromLong(1); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3807 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3808 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3809 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3810 PyDoc_STRVAR(islower__doc__, |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3811 "S.islower() -> bool\n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3812 \n\ |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3813 Return True if all cased characters in S are lowercase and there is\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3814 at least one cased character in S, False otherwise."); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3815 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3816 static PyObject* |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3817 string_islower(PyStringObject *self) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3818 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3819 register const unsigned char *p |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3820 = (unsigned char *) PyString_AS_STRING(self); |
|
12588
54274beb244d
The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
3821 register const unsigned char *e; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3822 int cased; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3823 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3824 /* Shortcut for single character strings */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3825 if (PyString_GET_SIZE(self) == 1) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3826 return PyBool_FromLong(islower(*p) != 0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3827 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3828 /* Special case for empty strings */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3829 if (PyString_GET_SIZE(self) == 0) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3830 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3831 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3832 e = p + PyString_GET_SIZE(self); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3833 cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3834 for (; p < e; p++) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3835 if (isupper(*p)) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3836 return PyBool_FromLong(0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3837 else if (!cased && islower(*p)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3838 cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3839 } |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3840 return PyBool_FromLong(cased); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3841 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3842 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3843 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3844 PyDoc_STRVAR(isupper__doc__, |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3845 "S.isupper() -> bool\n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3846 \n\ |
|
30706
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3847 Return True if all cased characters in S are uppercase and there is\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3848 at least one cased character in S, False otherwise."); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3849 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3850 static PyObject* |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3851 string_isupper(PyStringObject *self) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3852 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3853 register const unsigned char *p |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3854 = (unsigned char *) PyString_AS_STRING(self); |
|
12588
54274beb244d
The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
3855 register const unsigned char *e; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3856 int cased; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3857 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3858 /* Shortcut for single character strings */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3859 if (PyString_GET_SIZE(self) == 1) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3860 return PyBool_FromLong(isupper(*p) != 0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3861 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3862 /* Special case for empty strings */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3863 if (PyString_GET_SIZE(self) == 0) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3864 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3865 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3866 e = p + PyString_GET_SIZE(self); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3867 cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3868 for (; p < e; p++) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3869 if (islower(*p)) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3870 return PyBool_FromLong(0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3871 else if (!cased && isupper(*p)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3872 cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3873 } |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3874 return PyBool_FromLong(cased); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3875 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3876 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3877 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3878 PyDoc_STRVAR(istitle__doc__, |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3879 "S.istitle() -> bool\n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3880 \n\ |
|
30706
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3881 Return True if S is a titlecased string and there is at least one\n\ |
|
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3882 character in S, i.e. uppercase characters may only follow uncased\n\ |
|
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3883 characters and lowercase characters only cased ones. Return False\n\ |
|
be0ca26ad574
Patch #825679: Clarify semantics of .isfoo on empty strings.
Martin v. Löwis <martin@v.loewis.de>
parents:
30216
diff
changeset
|
3884 otherwise."); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3885 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3886 static PyObject* |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3887 string_istitle(PyStringObject *self, PyObject *uncased) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3888 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3889 register const unsigned char *p |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3890 = (unsigned char *) PyString_AS_STRING(self); |
|
12588
54274beb244d
The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
3891 register const unsigned char *e; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3892 int cased, previous_is_cased; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3893 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3894 /* Shortcut for single character strings */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3895 if (PyString_GET_SIZE(self) == 1) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3896 return PyBool_FromLong(isupper(*p) != 0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3897 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3898 /* Special case for empty strings */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3899 if (PyString_GET_SIZE(self) == 0) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3900 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
3901 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3902 e = p + PyString_GET_SIZE(self); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3903 cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3904 previous_is_cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3905 for (; p < e; p++) { |
|
12588
54274beb244d
The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
3906 register const unsigned char ch = *p; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3907 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3908 if (isupper(ch)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3909 if (previous_is_cased) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3910 return PyBool_FromLong(0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3911 previous_is_cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3912 cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3913 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3914 else if (islower(ch)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3915 if (!previous_is_cased) |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3916 return PyBool_FromLong(0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3917 previous_is_cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3918 cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3919 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3920 else |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3921 previous_is_cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3922 } |
|
22785
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
3923 return PyBool_FromLong(cased); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3924 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3925 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3926 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3927 PyDoc_STRVAR(splitlines__doc__, |
|
20415
621c7703e8b7
Remove extra "]" in splitlines() docstring.
Fred Drake <fdrake@acm.org>
parents:
20277
diff
changeset
|
3928 "S.splitlines([keepends]) -> list of strings\n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3929 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3930 Return a list of the lines in S, breaking at line boundaries.\n\ |
| 12371 | 3931 Line breaks are not included in the resulting list unless keepends\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
3932 is given and true."); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3933 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3934 static PyObject* |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3935 string_splitlines(PyStringObject *self, PyObject *args) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3936 { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
3937 register Py_ssize_t i; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
3938 register Py_ssize_t j; |
|
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
3939 Py_ssize_t len; |
| 12371 | 3940 int keepends = 0; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3941 PyObject *list; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3942 PyObject *str; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3943 char *data; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3944 |
| 12371 | 3945 if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3946 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3947 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3948 data = PyString_AS_STRING(self); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3949 len = PyString_GET_SIZE(self); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3950 |
|
38417
e0e43e561512
Added description of why splitlines doesn't use the prealloc strategy
Andrew Dalke <dalke@dalkescientific.com>
parents:
38410
diff
changeset
|
3951 /* This does not use the preallocated list because splitlines is |
|
e0e43e561512
Added description of why splitlines doesn't use the prealloc strategy
Andrew Dalke <dalke@dalkescientific.com>
parents:
38410
diff
changeset
|
3952 usually run with hundreds of newlines. The overhead of |
|
e0e43e561512
Added description of why splitlines doesn't use the prealloc strategy
Andrew Dalke <dalke@dalkescientific.com>
parents:
38410
diff
changeset
|
3953 switching between PyList_SET_ITEM and append causes about a |
|
e0e43e561512
Added description of why splitlines doesn't use the prealloc strategy
Andrew Dalke <dalke@dalkescientific.com>
parents:
38410
diff
changeset
|
3954 2-3% slowdown for that common case. A smarter implementation |
|
e0e43e561512
Added description of why splitlines doesn't use the prealloc strategy
Andrew Dalke <dalke@dalkescientific.com>
parents:
38410
diff
changeset
|
3955 could move the if check out, so the SET_ITEMs are done first |
|
e0e43e561512
Added description of why splitlines doesn't use the prealloc strategy
Andrew Dalke <dalke@dalkescientific.com>
parents:
38410
diff
changeset
|
3956 and the appends only done when the prealloc buffer is full. |
|
e0e43e561512
Added description of why splitlines doesn't use the prealloc strategy
Andrew Dalke <dalke@dalkescientific.com>
parents:
38410
diff
changeset
|
3957 That's too much work for little gain.*/ |
|
e0e43e561512
Added description of why splitlines doesn't use the prealloc strategy
Andrew Dalke <dalke@dalkescientific.com>
parents:
38410
diff
changeset
|
3958 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3959 list = PyList_New(0); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3960 if (!list) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3961 goto onError; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3962 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3963 for (i = j = 0; i < len; ) { |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
3964 Py_ssize_t eol; |
| 12371 | 3965 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3966 /* Find a line and append it */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3967 while (i < len && data[i] != '\n' && data[i] != '\r') |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3968 i++; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3969 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3970 /* Skip the line break reading CRLF as one line break */ |
| 12371 | 3971 eol = i; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3972 if (i < len) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3973 if (data[i] == '\r' && i + 1 < len && |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3974 data[i+1] == '\n') |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3975 i += 2; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3976 else |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3977 i++; |
| 12371 | 3978 if (keepends) |
| 3979 eol = i; | |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3980 } |
| 12371 | 3981 SPLIT_APPEND(data, j, eol); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3982 j = i; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3983 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3984 if (j < len) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3985 SPLIT_APPEND(data, j, len); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3986 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3987 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3988 return list; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3989 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3990 onError: |
|
36877
e9a7a2784e17
SF #1444030: Fix several potential defects found by Coverity.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
36866
diff
changeset
|
3991 Py_XDECREF(list); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3992 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3993 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
3994 |
|
47419
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47392
diff
changeset
|
3995 PyDoc_STRVAR(sizeof__doc__, |
|
47421
a65f095b27f3
Some style nits. Also clarify in the docstrings what __sizeof__ does.
Georg Brandl <georg@python.org>
parents:
47419
diff
changeset
|
3996 "S.__sizeof__() -> size of S in memory, in bytes"); |
|
47419
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47392
diff
changeset
|
3997 |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47392
diff
changeset
|
3998 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
3999 string_sizeof(PyStringObject *v) |
|
47419
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47392
diff
changeset
|
4000 { |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47392
diff
changeset
|
4001 Py_ssize_t res; |
| 56050 | 4002 res = PyStringObject_SIZE + PyString_GET_SIZE(v) * Py_TYPE(v)->tp_itemsize; |
|
47419
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47392
diff
changeset
|
4003 return PyInt_FromSsize_t(res); |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47392
diff
changeset
|
4004 } |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47392
diff
changeset
|
4005 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
4006 #undef SPLIT_APPEND |
|
38365
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
4007 #undef SPLIT_ADD |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
4008 #undef MAX_PREALLOC |
|
c0082705f2f2
Changes to string.split/rsplit on whitespace to preallocate space in the
Andrew Dalke <dalke@dalkescientific.com>
parents:
38347
diff
changeset
|
4009 #undef PREALLOC_SIZE |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
4010 |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27248
diff
changeset
|
4011 static PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4012 string_getnewargs(PyStringObject *v) |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27248
diff
changeset
|
4013 { |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
4014 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
|
4015 } |
|
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27248
diff
changeset
|
4016 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4017 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4018 #include "stringlib/string_format.h" |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4019 |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4020 PyDoc_STRVAR(format__doc__, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4021 "S.format(*args, **kwargs) -> unicode\n\ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4022 \n\ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4023 "); |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4024 |
|
47392
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4025 static PyObject * |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4026 string__format__(PyObject* self, PyObject* args) |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4027 { |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4028 PyObject *format_spec; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4029 PyObject *result = NULL; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4030 PyObject *tmp = NULL; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4031 |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4032 /* If 2.x, convert format_spec to the same type as value */ |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4033 /* This is to allow things like u''.format('') */ |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4034 if (!PyArg_ParseTuple(args, "O:__format__", &format_spec)) |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4035 goto done; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4036 if (!(PyString_Check(format_spec) || PyUnicode_Check(format_spec))) { |
|
47392
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4037 PyErr_Format(PyExc_TypeError, "__format__ arg must be str " |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4038 "or unicode, not %s", Py_TYPE(format_spec)->tp_name); |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4039 goto done; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4040 } |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4041 tmp = PyObject_Str(format_spec); |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4042 if (tmp == NULL) |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4043 goto done; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4044 format_spec = tmp; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4045 |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4046 result = _PyBytes_FormatAdvanced(self, |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4047 PyString_AS_STRING(format_spec), |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4048 PyString_GET_SIZE(format_spec)); |
|
47392
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4049 done: |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4050 Py_XDECREF(tmp); |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4051 return result; |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4052 } |
|
47ef9a806ce9
Refactor and clean up str.format() code (and helpers) in advance of optimizations.
Eric Smith <eric@trueblade.com>
parents:
47296
diff
changeset
|
4053 |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4054 PyDoc_STRVAR(p_format__doc__, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4055 "S.__format__(format_spec) -> unicode\n\ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4056 \n\ |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4057 "); |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4058 |
|
48221
b8c4bf393794
Issue #3139: Make buffer-interface thread-safe wrt. PyArg_ParseTuple,
Martin v. Löwis <martin@v.loewis.de>
parents:
48139
diff
changeset
|
4059 |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
4060 static PyMethodDef |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
4061 string_methods[] = { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
4062 /* Counterparts of the obsolete stropmodule functions; except |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
4063 string.maketrans(). */ |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4064 {"join", (PyCFunction)string_join, METH_O, join__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4065 {"split", (PyCFunction)string_split, METH_VARARGS, split__doc__}, |
|
31199
504943c89193
Add rsplit method for str and unicode builtin types.
Hye-Shik Chang <hyeshik@gmail.com>
parents:
31042
diff
changeset
|
4066 {"rsplit", (PyCFunction)string_rsplit, METH_VARARGS, rsplit__doc__}, |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4067 {"lower", (PyCFunction)string_lower, METH_NOARGS, lower__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4068 {"upper", (PyCFunction)string_upper, METH_NOARGS, upper__doc__}, |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
4069 {"islower", (PyCFunction)string_islower, METH_NOARGS, islower__doc__}, |
|
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
4070 {"isupper", (PyCFunction)string_isupper, METH_NOARGS, isupper__doc__}, |
|
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
4071 {"isspace", (PyCFunction)string_isspace, METH_NOARGS, isspace__doc__}, |
|
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
4072 {"isdigit", (PyCFunction)string_isdigit, METH_NOARGS, isdigit__doc__}, |
|
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
4073 {"istitle", (PyCFunction)string_istitle, METH_NOARGS, istitle__doc__}, |
|
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
4074 {"isalpha", (PyCFunction)string_isalpha, METH_NOARGS, isalpha__doc__}, |
|
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
4075 {"isalnum", (PyCFunction)string_isalnum, METH_NOARGS, isalnum__doc__}, |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4076 {"capitalize", (PyCFunction)string_capitalize, METH_NOARGS, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4077 capitalize__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4078 {"count", (PyCFunction)string_count, METH_VARARGS, count__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4079 {"endswith", (PyCFunction)string_endswith, METH_VARARGS, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4080 endswith__doc__}, |
|
38338
958f91684209
needforspeed: use METH_O for argument handling, which made partition some
Fredrik Lundh <fredrik@pythonware.com>
parents:
38335
diff
changeset
|
4081 {"partition", (PyCFunction)string_partition, METH_O, partition__doc__}, |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4082 {"find", (PyCFunction)string_find, METH_VARARGS, find__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4083 {"index", (PyCFunction)string_index, METH_VARARGS, index__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4084 {"lstrip", (PyCFunction)string_lstrip, METH_VARARGS, lstrip__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4085 {"replace", (PyCFunction)string_replace, METH_VARARGS, replace__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4086 {"rfind", (PyCFunction)string_rfind, METH_VARARGS, rfind__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4087 {"rindex", (PyCFunction)string_rindex, METH_VARARGS, rindex__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4088 {"rstrip", (PyCFunction)string_rstrip, METH_VARARGS, rstrip__doc__}, |
|
38390
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
4089 {"rpartition", (PyCFunction)string_rpartition, METH_O, |
|
76807902a2f5
needforspeed: added rpartition implementation
Fredrik Lundh <fredrik@pythonware.com>
parents:
38385
diff
changeset
|
4090 rpartition__doc__}, |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4091 {"startswith", (PyCFunction)string_startswith, METH_VARARGS, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4092 startswith__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4093 {"strip", (PyCFunction)string_strip, METH_VARARGS, strip__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4094 {"swapcase", (PyCFunction)string_swapcase, METH_NOARGS, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4095 swapcase__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4096 {"translate", (PyCFunction)string_translate, METH_VARARGS, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4097 translate__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4098 {"title", (PyCFunction)string_title, METH_NOARGS, title__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4099 {"ljust", (PyCFunction)string_ljust, METH_VARARGS, ljust__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4100 {"rjust", (PyCFunction)string_rjust, METH_VARARGS, rjust__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4101 {"center", (PyCFunction)string_center, METH_VARARGS, center__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4102 {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__}, |
|
45381
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4103 {"format", (PyCFunction) do_string_format, METH_VARARGS | METH_KEYWORDS, format__doc__}, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4104 {"__format__", (PyCFunction) string__format__, METH_VARARGS, p_format__doc__}, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4105 {"_formatter_field_name_split", (PyCFunction) formatter_field_name_split, METH_NOARGS}, |
|
b7231bf6bfb7
Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents:
44350
diff
changeset
|
4106 {"_formatter_parser", (PyCFunction) formatter_parser, METH_NOARGS}, |
|
56093
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
4107 {"encode", (PyCFunction)string_encode, METH_VARARGS | METH_KEYWORDS, encode__doc__}, |
|
f64c0eb8cc62
add keyword arguments support to str/unicode encode and decode #6300
Benjamin Peterson <benjamin@python.org>
parents:
56050
diff
changeset
|
4108 {"decode", (PyCFunction)string_decode, METH_VARARGS | METH_KEYWORDS, decode__doc__}, |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4109 {"expandtabs", (PyCFunction)string_expandtabs, METH_VARARGS, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4110 expandtabs__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4111 {"splitlines", (PyCFunction)string_splitlines, METH_VARARGS, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4112 splitlines__doc__}, |
|
47419
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47392
diff
changeset
|
4113 {"__sizeof__", (PyCFunction)string_sizeof, METH_NOARGS, |
|
88bd07c0a847
Issue #2898: Added sys.getsizeof() to retrieve size of objects in bytes.
Robert Schuppenies <okkotonushi@googlemail.com>
parents:
47392
diff
changeset
|
4114 sizeof__doc__}, |
|
27521
17bfd5dbc0f1
Implement appropriate __getnewargs__ for all immutable subclassable builtin
Guido van Rossum <guido@python.org>
parents:
27248
diff
changeset
|
4115 {"__getnewargs__", (PyCFunction)string_getnewargs, METH_NOARGS}, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
4116 {NULL, NULL} /* sentinel */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
4117 }; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
4118 |
|
24280
1598a1e473e2
staticforward bites the dust.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24184
diff
changeset
|
4119 static PyObject * |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
4120 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
|
4121 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
4122 static PyObject * |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4123 string_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
|
4124 { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4125 PyObject *x = NULL; |
|
36675
dba6494735d0
Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36528
diff
changeset
|
4126 static char *kwlist[] = {"object", 0}; |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4127 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4128 if (type != &PyString_Type) |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
4129 return str_subtype_new(type, args, kwds); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4130 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x)) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4131 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4132 if (x == NULL) |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4133 return PyString_FromString(""); |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4134 return PyObject_Str(x); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
4135 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
4136 |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
4137 static PyObject * |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
4138 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
|
4139 { |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
4140 PyObject *tmp, *pnew; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
4141 Py_ssize_t n; |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
4142 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4143 assert(PyType_IsSubtype(type, &PyString_Type)); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4144 tmp = string_new(&PyString_Type, args, kwds); |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
4145 if (tmp == NULL) |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
4146 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4147 assert(PyString_CheckExact(tmp)); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4148 n = PyString_GET_SIZE(tmp); |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
4149 pnew = type->tp_alloc(type, n); |
|
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
4150 if (pnew != NULL) { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4151 Py_MEMCPY(PyString_AS_STRING(pnew), PyString_AS_STRING(tmp), n+1); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4152 ((PyStringObject *)pnew)->ob_shash = |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4153 ((PyStringObject *)tmp)->ob_shash; |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4154 ((PyStringObject *)pnew)->ob_sstate = SSTATE_NOT_INTERNED; |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
4155 } |
|
19632
20fb7591f885
Fix a memory leak in str_subtype_new(). (All the other
Guido van Rossum <guido@python.org>
parents:
19592
diff
changeset
|
4156 Py_DECREF(tmp); |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
4157 return pnew; |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
4158 } |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
4159 |
|
23542
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4160 static PyObject * |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4161 basestring_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4162 { |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4163 PyErr_SetString(PyExc_TypeError, |
|
23597
ed007e47b3a1
Change name from string to basestring
Neal Norwitz <nnorwitz@gmail.com>
parents:
23542
diff
changeset
|
4164 "The basestring type cannot be instantiated"); |
|
23542
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4165 return NULL; |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4166 } |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4167 |
|
26273
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4168 static PyObject * |
|
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4169 string_mod(PyObject *v, PyObject *w) |
|
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4170 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4171 if (!PyString_Check(v)) { |
|
26273
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4172 Py_INCREF(Py_NotImplemented); |
|
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4173 return Py_NotImplemented; |
|
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4174 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4175 return PyString_Format(v, w); |
|
26273
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4176 } |
|
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4177 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
4178 PyDoc_STRVAR(basestring_doc, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
4179 "Type basestring cannot be instantiated; it is the base for str and unicode."); |
|
23542
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4180 |
|
26273
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4181 static PyNumberMethods string_as_number = { |
|
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4182 0, /*nb_add*/ |
|
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4183 0, /*nb_subtract*/ |
|
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4184 0, /*nb_multiply*/ |
|
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4185 0, /*nb_divide*/ |
|
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4186 string_mod, /*nb_remainder*/ |
|
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4187 }; |
|
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4188 |
|
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4189 |
|
23542
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4190 PyTypeObject PyBaseString_Type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
42216
diff
changeset
|
4191 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
23597
ed007e47b3a1
Change name from string to basestring
Neal Norwitz <nnorwitz@gmail.com>
parents:
23542
diff
changeset
|
4192 "basestring", |
|
23542
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4193 0, |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4194 0, |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4195 0, /* tp_dealloc */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4196 0, /* tp_print */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4197 0, /* tp_getattr */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4198 0, /* tp_setattr */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4199 0, /* tp_compare */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4200 0, /* tp_repr */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4201 0, /* tp_as_number */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4202 0, /* tp_as_sequence */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4203 0, /* tp_as_mapping */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4204 0, /* tp_hash */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4205 0, /* tp_call */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4206 0, /* tp_str */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4207 0, /* tp_getattro */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4208 0, /* tp_setattro */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4209 0, /* tp_as_buffer */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4210 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4211 basestring_doc, /* tp_doc */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4212 0, /* tp_traverse */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4213 0, /* tp_clear */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4214 0, /* tp_richcompare */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4215 0, /* tp_weaklistoffset */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4216 0, /* tp_iter */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4217 0, /* tp_iternext */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4218 0, /* tp_methods */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4219 0, /* tp_members */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4220 0, /* tp_getset */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4221 &PyBaseObject_Type, /* tp_base */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4222 0, /* tp_dict */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4223 0, /* tp_descr_get */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4224 0, /* tp_descr_set */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4225 0, /* tp_dictoffset */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4226 0, /* tp_init */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4227 0, /* tp_alloc */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4228 basestring_new, /* tp_new */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4229 0, /* tp_free */ |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4230 }; |
|
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4231 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
4232 PyDoc_STRVAR(string_doc, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4233 "str(object) -> string\n\ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4234 \n\ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4235 Return a nice string representation of the object.\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
23778
diff
changeset
|
4236 If the argument is a string, the return value is the same object."); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
4237 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4238 PyTypeObject PyString_Type = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
42216
diff
changeset
|
4239 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4240 "str", |
|
50036
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
4241 PyStringObject_SIZE, |
| 6 | 4242 sizeof(char), |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
37213
diff
changeset
|
4243 string_dealloc, /* tp_dealloc */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4244 (printfunc)string_print, /* tp_print */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4245 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4246 0, /* tp_setattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4247 0, /* tp_compare */ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
37213
diff
changeset
|
4248 string_repr, /* tp_repr */ |
|
26273
57c6e84f71ce
Add nb_remainder (i.e. __mod__) slot to str type. Fixes SF bug #615506.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
26164
diff
changeset
|
4249 &string_as_number, /* tp_as_number */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4250 &string_as_sequence, /* tp_as_sequence */ |
|
23778
245224d1b8c9
This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents:
23597
diff
changeset
|
4251 &string_as_mapping, /* tp_as_mapping */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4252 (hashfunc)string_hash, /* tp_hash */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4253 0, /* tp_call */ |
|
37304
79a0f080f898
Remove unnecessary casts in type object initializers.
Georg Brandl <georg@python.org>
parents:
37213
diff
changeset
|
4254 string_str, /* tp_str */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4255 PyObject_GenericGetAttr, /* tp_getattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4256 0, /* tp_setattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4257 &string_as_buffer, /* tp_as_buffer */ |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
4258 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES | |
|
46107
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45731
diff
changeset
|
4259 Py_TPFLAGS_BASETYPE | Py_TPFLAGS_STRING_SUBCLASS | |
|
5341b30b1812
Merged revisions 61750,61752,61754,61756,61760,61763,61768,61772,61775,61805,61809,61812,61819,61917,61920,61930,61933-61934 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45731
diff
changeset
|
4260 Py_TPFLAGS_HAVE_NEWBUFFER, /* tp_flags */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4261 string_doc, /* tp_doc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4262 0, /* tp_traverse */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4263 0, /* tp_clear */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4264 (richcmpfunc)string_richcompare, /* tp_richcompare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4265 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4266 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4267 0, /* tp_iternext */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4268 string_methods, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4269 0, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4270 0, /* tp_getset */ |
|
23542
220b98f06415
- A new type object, 'string', is added. This is a common base type
Guido van Rossum <guido@python.org>
parents:
23494
diff
changeset
|
4271 &PyBaseString_Type, /* tp_base */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4272 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4273 0, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4274 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4275 0, /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4276 0, /* tp_init */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4277 0, /* tp_alloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
4278 string_new, /* tp_new */ |
|
22977
4ba091cc70b8
Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22785
diff
changeset
|
4279 PyObject_Del, /* tp_free */ |
| 6 | 4280 }; |
| 4281 | |
| 4282 void | |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4283 PyString_Concat(register PyObject **pv, register PyObject *w) |
| 6 | 4284 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4285 register PyObject *v; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4286 if (*pv == NULL) |
| 6 | 4287 return; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4288 if (w == NULL || !PyString_Check(*pv)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4289 Py_DECREF(*pv); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4290 *pv = NULL; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4291 return; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4292 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4293 v = string_concat((PyStringObject *) *pv, w); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4294 Py_DECREF(*pv); |
| 6 | 4295 *pv = v; |
| 4296 } | |
| 4297 | |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4298 void |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4299 PyString_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
|
4300 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4301 PyString_Concat(pv, w); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4302 Py_XDECREF(w); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4303 } |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4304 |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4305 |
| 6 | 4306 /* The following function breaks the notion that strings are immutable: |
| 4307 it changes the size of a string. We get away with this only if there | |
| 4308 is only one module referencing the object. You can also think of it | |
| 4309 as creating a new string object and destroying the old one, only | |
| 4310 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
|
4311 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
|
4312 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
|
4313 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
|
4314 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
|
4315 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
|
4316 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
|
4317 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
|
4318 */ |
| 6 | 4319 |
| 4320 int | |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4321 _PyString_Resize(PyObject **pv, Py_ssize_t newsize) |
| 6 | 4322 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4323 register PyObject *v; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4324 register PyStringObject *sv; |
|
71
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
4325 v = *pv; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4326 if (!PyString_Check(v) || Py_REFCNT(v) != 1 || newsize < 0 || |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4327 PyString_CHECK_INTERNED(v)) { |
| 6 | 4328 *pv = 0; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4329 Py_DECREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4330 PyErr_BadInternalCall(); |
| 27 | 4331 return -1; |
| 6 | 4332 } |
|
71
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
4333 /* 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
|
4334 _Py_DEC_REFTOTAL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4335 _Py_ForgetReference(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4336 *pv = (PyObject *) |
|
50036
1f2a3b13e86d
Issue #4445: save 3 bytes (on average, on a typical machine) per
Mark Dickinson <dickinsm@gmail.com>
parents:
49431
diff
changeset
|
4337 PyObject_REALLOC((char *)v, PyStringObject_SIZE + newsize); |
| 6 | 4338 if (*pv == NULL) { |
|
22977
4ba091cc70b8
Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22785
diff
changeset
|
4339 PyObject_Del(v); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4340 PyErr_NoMemory(); |
| 27 | 4341 return -1; |
| 6 | 4342 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4343 _Py_NewReference(*pv); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4344 sv = (PyStringObject *) *pv; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
4345 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
|
4346 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
|
4347 sv->ob_shash = -1; /* invalidate cached hash value */ |
| 6 | 4348 return 0; |
| 4349 } | |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4350 |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4351 /* Helpers for formatstring */ |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4352 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
4353 Py_LOCAL_INLINE(PyObject *) |
|
36528
76ce880dce49
Use Py_ssize_t in helper function between Py_ssize_t-using functions.
Thomas Wouters <thomas@python.org>
parents:
36519
diff
changeset
|
4354 getnextarg(PyObject *args, Py_ssize_t arglen, Py_ssize_t *p_argidx) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4355 { |
|
36528
76ce880dce49
Use Py_ssize_t in helper function between Py_ssize_t-using functions.
Thomas Wouters <thomas@python.org>
parents:
36519
diff
changeset
|
4356 Py_ssize_t argidx = *p_argidx; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4357 if (argidx < arglen) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4358 (*p_argidx)++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4359 if (arglen < 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4360 return args; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4361 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4362 return PyTuple_GetItem(args, argidx); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4363 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4364 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4365 "not enough arguments for format string"); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4366 return NULL; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4367 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4368 |
|
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
|
4369 /* Format codes |
|
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
|
4370 * F_LJUST '-' |
|
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
|
4371 * F_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
|
4372 * F_BLANK ' ' |
|
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
|
4373 * F_ALT '#' |
|
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
|
4374 * F_ZERO '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
|
4375 */ |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4376 #define F_LJUST (1<<0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4377 #define F_SIGN (1<<1) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4378 #define F_BLANK (1<<2) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4379 #define F_ALT (1<<3) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4380 #define F_ZERO (1<<4) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4381 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
4382 Py_LOCAL_INLINE(int) |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
4383 formatfloat(char *buf, size_t buflen, int flags, |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
4384 int prec, int type, PyObject *v) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4385 { |
|
56758
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4386 char *tmp; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4387 double x; |
|
56758
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4388 Py_ssize_t len; |
|
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4389 |
|
24415
f5cfa0ee02cf
Fix the problem of not raising a TypeError exception when doing:
Neal Norwitz <nnorwitz@gmail.com>
parents:
24409
diff
changeset
|
4390 x = PyFloat_AsDouble(v); |
|
f5cfa0ee02cf
Fix the problem of not raising a TypeError exception when doing:
Neal Norwitz <nnorwitz@gmail.com>
parents:
24409
diff
changeset
|
4391 if (x == -1.0 && PyErr_Occurred()) { |
|
40357
b871e7bd8355
Patch [ 1586791 ] better error msgs for some TypeErrors
Georg Brandl <georg@python.org>
parents:
39995
diff
changeset
|
4392 PyErr_Format(PyExc_TypeError, "float argument required, " |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
4393 "not %.200s", Py_TYPE(v)->tp_name); |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
4394 return -1; |
|
24415
f5cfa0ee02cf
Fix the problem of not raising a TypeError exception when doing:
Neal Norwitz <nnorwitz@gmail.com>
parents:
24409
diff
changeset
|
4395 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4396 if (prec < 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4397 prec = 6; |
|
55750
606e97caeb77
Silence gcc 'comparison always false' warning
Mark Dickinson <dickinsm@gmail.com>
parents:
53442
diff
changeset
|
4398 #if SIZEOF_INT > 4 |
|
52408
1afb365f2448
Issue #532631: Add paranoid check to avoid potential buffer overflow
Mark Dickinson <dickinsm@gmail.com>
parents:
52404
diff
changeset
|
4399 /* make sure that the decimal representation of precision really does |
|
1afb365f2448
Issue #532631: Add paranoid check to avoid potential buffer overflow
Mark Dickinson <dickinsm@gmail.com>
parents:
52404
diff
changeset
|
4400 need at most 10 digits: platforms with sizeof(int) == 8 exist! */ |
|
55750
606e97caeb77
Silence gcc 'comparison always false' warning
Mark Dickinson <dickinsm@gmail.com>
parents:
53442
diff
changeset
|
4401 if (prec > 0x7fffffff) { |
|
52408
1afb365f2448
Issue #532631: Add paranoid check to avoid potential buffer overflow
Mark Dickinson <dickinsm@gmail.com>
parents:
52404
diff
changeset
|
4402 PyErr_SetString(PyExc_OverflowError, |
|
1afb365f2448
Issue #532631: Add paranoid check to avoid potential buffer overflow
Mark Dickinson <dickinsm@gmail.com>
parents:
52404
diff
changeset
|
4403 "outrageously large precision " |
|
1afb365f2448
Issue #532631: Add paranoid check to avoid potential buffer overflow
Mark Dickinson <dickinsm@gmail.com>
parents:
52404
diff
changeset
|
4404 "for formatted float"); |
|
1afb365f2448
Issue #532631: Add paranoid check to avoid potential buffer overflow
Mark Dickinson <dickinsm@gmail.com>
parents:
52404
diff
changeset
|
4405 return -1; |
|
1afb365f2448
Issue #532631: Add paranoid check to avoid potential buffer overflow
Mark Dickinson <dickinsm@gmail.com>
parents:
52404
diff
changeset
|
4406 } |
|
55750
606e97caeb77
Silence gcc 'comparison always false' warning
Mark Dickinson <dickinsm@gmail.com>
parents:
53442
diff
changeset
|
4407 #endif |
|
52408
1afb365f2448
Issue #532631: Add paranoid check to avoid potential buffer overflow
Mark Dickinson <dickinsm@gmail.com>
parents:
52404
diff
changeset
|
4408 |
|
52404
019815bc37ea
Issue #532631: Replace confusing fabs(x)/1e25 >= 1e25 test
Mark Dickinson <dickinsm@gmail.com>
parents:
50627
diff
changeset
|
4409 if (type == 'f' && fabs(x) >= 1e50) |
|
48003
5c33009994b4
Backed out r65069, pending fixing it in Windows.
Eric Smith <eric@trueblade.com>
parents:
48001
diff
changeset
|
4410 type = 'g'; |
|
26825
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26818
diff
changeset
|
4411 /* Worst case length calc to ensure no buffer overrun: |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26818
diff
changeset
|
4412 |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26818
diff
changeset
|
4413 'g' formats: |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4414 fmt = %#.<prec>g |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4415 buf = '-' + [0-9]*prec + '.' + 'e+' + (longest exp |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
4416 for any double rep.) |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4417 len = 1 + prec + 1 + 2 + 5 = 9 + prec |
|
26825
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26818
diff
changeset
|
4418 |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26818
diff
changeset
|
4419 'f' formats: |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26818
diff
changeset
|
4420 buf = '-' + [0-9]*x + '.' + [0-9]*prec (with x < 50) |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26818
diff
changeset
|
4421 len = 1 + 50 + 1 + prec = 52 + prec |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26818
diff
changeset
|
4422 |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4423 If prec=0 the effective precision is 1 (the leading digit is |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
4424 always given), therefore increase the length by one. |
|
26825
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26818
diff
changeset
|
4425 |
|
e153961f961f
Patch for bug #659709: bogus computation of float length
Marc-André Lemburg <mal@egenix.com>
parents:
26818
diff
changeset
|
4426 */ |
|
42216
86d1dbfc5833
Patch #1673759: add a missing overflow check when formatting floats
Georg Brandl <georg@python.org>
parents:
42024
diff
changeset
|
4427 if (((type == 'g' || type == 'G') && |
|
86d1dbfc5833
Patch #1673759: add a missing overflow check when formatting floats
Georg Brandl <georg@python.org>
parents:
42024
diff
changeset
|
4428 buflen <= (size_t)10 + (size_t)prec) || |
|
48003
5c33009994b4
Backed out r65069, pending fixing it in Windows.
Eric Smith <eric@trueblade.com>
parents:
48001
diff
changeset
|
4429 (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4430 PyErr_SetString(PyExc_OverflowError, |
| 15318 | 4431 "formatted float is too long (precision too large?)"); |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4432 return -1; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4433 } |
|
56758
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4434 tmp = PyOS_double_to_string(x, type, prec, |
|
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4435 (flags&F_ALT)?Py_DTSF_ALT:0, NULL); |
|
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4436 if (!tmp) |
|
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4437 return -1; |
|
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4438 len = strlen(tmp); |
|
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4439 if (len >= buflen) { |
|
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4440 PyErr_SetString(PyExc_OverflowError, |
|
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4441 "formatted float is too long (precision too large?)"); |
|
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4442 PyMem_Free(tmp); |
|
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4443 return -1; |
|
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4444 } |
|
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4445 strcpy(buf, tmp); |
|
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4446 PyMem_Free(tmp); |
|
74472da09f6c
Finished removing _PyOS_double_to_string, as mentioned in issue 7117.
Eric Smith <eric@trueblade.com>
parents:
56526
diff
changeset
|
4447 return (int)len; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4448 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4449 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4450 /* _PyString_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
|
4451 * 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
|
4452 * 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
|
4453 * 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
|
4454 * . *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
|
4455 * *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
|
4456 * 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
|
4457 * 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
|
4458 * "-"? ("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
|
4459 * "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
|
4460 * 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
|
4461 * 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
|
4462 * 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
|
4463 * 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
|
4464 * 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
|
4465 * 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
|
4466 * 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
|
4467 * |
|
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
|
4468 * 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
|
4469 * 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
|
4470 */ |
|
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
|
4471 PyObject* |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4472 _PyString_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
|
4473 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
|
4474 { |
|
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
|
4475 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
|
4476 char *buf; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
4477 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
|
4478 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
|
4479 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
|
4480 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
|
4481 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
|
4482 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
|
4483 |
|
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
|
4484 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
|
4485 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
|
4486 case 'u': |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
4487 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
|
4488 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
|
4489 case 'o': |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
4490 result = Py_TYPE(val)->tp_as_number->nb_oct(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
|
4491 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
|
4492 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
|
4493 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
|
4494 numnondigits = 2; |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
4495 result = Py_TYPE(val)->tp_as_number->nb_hex(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
|
4496 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
|
4497 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
|
4498 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
|
4499 } |
|
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
|
4500 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
|
4501 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
|
4502 |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4503 buf = PyString_AsString(result); |
|
39494
a95090281032
Fix refleak introduced in rev. 51248.
Georg Brandl <georg@python.org>
parents:
39469
diff
changeset
|
4504 if (!buf) { |
|
a95090281032
Fix refleak introduced in rev. 51248.
Georg Brandl <georg@python.org>
parents:
39469
diff
changeset
|
4505 Py_DECREF(result); |
|
39469
71baa06767dd
Fix segfault when doing string formatting on subclasses of long if
Neal Norwitz <nnorwitz@gmail.com>
parents:
39463
diff
changeset
|
4506 return NULL; |
|
39494
a95090281032
Fix refleak introduced in rev. 51248.
Georg Brandl <georg@python.org>
parents:
39469
diff
changeset
|
4507 } |
|
39469
71baa06767dd
Fix segfault when doing string formatting on subclasses of long if
Neal Norwitz <nnorwitz@gmail.com>
parents:
39463
diff
changeset
|
4508 |
|
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
|
4509 /* To modify the string in-place, there can only be one reference. */ |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
4510 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
|
4511 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
|
4512 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
|
4513 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4514 llen = PyString_Size(result); |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
4515 if (llen > INT_MAX) { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4516 PyErr_SetString(PyExc_ValueError, "string too large in _PyString_FormatLong"); |
|
36866
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36675
diff
changeset
|
4517 return NULL; |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36675
diff
changeset
|
4518 } |
|
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36675
diff
changeset
|
4519 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
|
4520 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
|
4521 --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
|
4522 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
|
4523 } |
|
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
|
4524 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
|
4525 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
|
4526 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
|
4527 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
|
4528 |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
4529 /* Get rid of base marker unless F_ALT */ |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
4530 if ((flags & F_ALT) == 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
|
4531 /* Need to skip 0x, 0X or 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
|
4532 int skipped = 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
|
4533 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
|
4534 case 'o': |
|
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
|
4535 assert(buf[sign] == '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
|
4536 /* If 0 is only digit, leave it alone. */ |
|
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
|
4537 if (numdigits > 1) { |
|
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
|
4538 skipped = 1; |
|
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
|
4539 --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
|
4540 } |
|
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
|
4541 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
|
4542 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
|
4543 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
|
4544 assert(buf[sign] == '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
|
4545 assert(buf[sign + 1] == '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
|
4546 skipped = 2; |
|
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
|
4547 numnondigits -= 2; |
|
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
|
4548 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
|
4549 } |
|
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
|
4550 if (skipped) { |
|
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
|
4551 buf += skipped; |
|
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
|
4552 len -= skipped; |
|
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
|
4553 if (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
|
4554 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
|
4555 } |
|
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
|
4556 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
|
4557 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
|
4558 } |
|
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
|
4559 |
|
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
|
4560 /* 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
|
4561 if (prec > numdigits) { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4562 PyObject *r1 = PyString_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
|
4563 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
|
4564 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
|
4565 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
|
4566 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
|
4567 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
|
4568 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4569 b1 = PyString_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
|
4570 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
|
4571 *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
|
4572 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
|
4573 *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
|
4574 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
|
4575 *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
|
4576 *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
|
4577 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
|
4578 result = r1; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4579 buf = PyString_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
|
4580 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
|
4581 } |
|
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
|
4582 |
|
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
|
4583 /* 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
|
4584 if (type == 'X') { |
|
27a718221e72
SF bug #1224347: int/long unification and hex()
Raymond Hettinger <python@rcn.com>
parents:
34621
diff
changeset
|
4585 /* 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
|
4586 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
|
4587 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
|
4588 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
|
4589 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
|
4590 } |
|
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
|
4591 *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
|
4592 *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
|
4593 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
|
4594 } |
|
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
|
4595 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
4596 Py_LOCAL_INLINE(int) |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
4597 formatint(char *buf, size_t buflen, int flags, |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
4598 int prec, int type, PyObject *v) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4599 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4600 /* fmt = '%#.' + `prec` + 'l' + `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
|
4601 worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine) |
|
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
|
4602 + 1 + 1 = 24 */ |
|
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
|
4603 char fmt[64]; /* plenty big enough! */ |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4604 char *sign; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4605 long x; |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4606 |
|
24415
f5cfa0ee02cf
Fix the problem of not raising a TypeError exception when doing:
Neal Norwitz <nnorwitz@gmail.com>
parents:
24409
diff
changeset
|
4607 x = PyInt_AsLong(v); |
|
f5cfa0ee02cf
Fix the problem of not raising a TypeError exception when doing:
Neal Norwitz <nnorwitz@gmail.com>
parents:
24409
diff
changeset
|
4608 if (x == -1 && PyErr_Occurred()) { |
|
40357
b871e7bd8355
Patch [ 1586791 ] better error msgs for some TypeErrors
Georg Brandl <georg@python.org>
parents:
39995
diff
changeset
|
4609 PyErr_Format(PyExc_TypeError, "int argument required, not %.200s", |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
4610 Py_TYPE(v)->tp_name); |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
4611 return -1; |
|
24415
f5cfa0ee02cf
Fix the problem of not raising a TypeError exception when doing:
Neal Norwitz <nnorwitz@gmail.com>
parents:
24409
diff
changeset
|
4612 } |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4613 if (x < 0 && type == 'u') { |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4614 type = 'd'; |
|
24736
ff05eef3a6d1
Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents:
24598
diff
changeset
|
4615 } |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4616 if (x < 0 && (type == 'x' || type == 'X' || type == 'o')) |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4617 sign = "-"; |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4618 else |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4619 sign = ""; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4620 if (prec < 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4621 prec = 1; |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4622 |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4623 if ((flags & F_ALT) && |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4624 (type == 'x' || type == 'X')) { |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4625 /* When converting under %#x or %#X, there are a number |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4626 * of issues that cause pain: |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4627 * - when 0 is being converted, the C standard leaves off |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4628 * the '0x' or '0X', which is inconsistent with other |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4629 * %#x/%#X conversions and inconsistent with Python's |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4630 * hex() function |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4631 * - there are platforms that violate the standard and |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4632 * convert 0 with the '0x' or '0X' |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4633 * (Metrowerks, Compaq Tru64) |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4634 * - there are platforms that give '0x' when converting |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4635 * under %#X, but convert 0 in accordance with the |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4636 * standard (OS/2 EMX) |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4637 * |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4638 * We can achieve the desired consistency by inserting our |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4639 * own '0x' or '0X' prefix, and substituting %x/%X in place |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4640 * of %#x/%#X. |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4641 * |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4642 * Note that this is the same approach as used in |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4643 * formatint() in unicodeobject.c |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4644 */ |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4645 PyOS_snprintf(fmt, sizeof(fmt), "%s0%c%%.%dl%c", |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4646 sign, type, prec, type); |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4647 } |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4648 else { |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4649 PyOS_snprintf(fmt, sizeof(fmt), "%s%%%s.%dl%c", |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4650 sign, (flags&F_ALT) ? "#" : "", |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4651 prec, type); |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4652 } |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4653 |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4654 /* buf = '+'/'-'/'' + '0'/'0x'/'' + '[0-9]'*max(prec, len(x in octal)) |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4655 * worst case buf = '-0x' + [0-9]*prec, where prec >= 11 |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
4656 */ |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4657 if (buflen <= 14 || buflen <= (size_t)3 + (size_t)prec) { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4658 PyErr_SetString(PyExc_OverflowError, |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4659 "formatted integer is too long (precision too large?)"); |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4660 return -1; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4661 } |
|
31042
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4662 if (sign[0]) |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4663 PyOS_snprintf(buf, buflen, fmt, -x); |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4664 else |
|
9c846c3df6f3
- Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents:
31021
diff
changeset
|
4665 PyOS_snprintf(buf, buflen, fmt, x); |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
4666 return (int)strlen(buf); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4667 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4668 |
|
38440
5b0753b9d16a
needforspeed: replace improvements, changed to Py_LOCAL_INLINE
Fredrik Lundh <fredrik@pythonware.com>
parents:
38438
diff
changeset
|
4669 Py_LOCAL_INLINE(int) |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
4670 formatchar(char *buf, size_t buflen, PyObject *v) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4671 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4672 /* presume that the buffer is at least 2 characters long */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4673 if (PyString_Check(v)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4674 if (!PyArg_Parse(v, "c;%c requires int or char", &buf[0])) |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
4675 return -1; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4676 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4677 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4678 if (!PyArg_Parse(v, "b;%c requires int or char", &buf[0])) |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
4679 return -1; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4680 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4681 buf[1] = '\0'; |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
4682 return 1; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4683 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4684 |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4685 /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...) |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4686 |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4687 FORMATBUFLEN is the length of the buffer in which the floats, ints, & |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4688 chars are formatted. XXX This is a magic number. Each formatting |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4689 routine does bounds checking to ensure no overflow, but a better |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4690 solution may be to malloc a buffer of appropriate size for each |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4691 format. For now, the current solution is sufficient. |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4692 */ |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4693 #define FORMATBUFLEN (size_t)120 |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4694 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4695 PyObject * |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4696 PyString_Format(PyObject *format, PyObject *args) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4697 { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4698 char *fmt, *res; |
|
36519
d6d415df0df0
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36510
diff
changeset
|
4699 Py_ssize_t arglen, argidx; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
4700 Py_ssize_t reslen, rescnt, fmtcnt; |
|
3570
977ff1ace070
Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
4701 int args_owned = 0; |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
4702 PyObject *result, *orig_args; |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
4703 #ifdef Py_USING_UNICODE |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
4704 PyObject *v, *w; |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
4705 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4706 PyObject *dict = NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4707 if (format == NULL || !PyString_Check(format) || args == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4708 PyErr_BadInternalCall(); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4709 return NULL; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4710 } |
| 12328 | 4711 orig_args = args; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4712 fmt = PyString_AS_STRING(format); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4713 fmtcnt = PyString_GET_SIZE(format); |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
4714 reslen = rescnt = fmtcnt + 100; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4715 result = PyString_FromStringAndSize((char *)NULL, reslen); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4716 if (result == NULL) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4717 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4718 res = PyString_AsString(result); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4719 if (PyTuple_Check(args)) { |
| 21254 | 4720 arglen = PyTuple_GET_SIZE(args); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4721 argidx = 0; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4722 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4723 else { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4724 arglen = -1; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4725 argidx = -2; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4726 } |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
4727 if (Py_TYPE(args)->tp_as_mapping && !PyTuple_Check(args) && |
|
26164
63a08639dae2
Fix SF # 635969, No error "not all arguments converted"
Neal Norwitz <nnorwitz@gmail.com>
parents:
25721
diff
changeset
|
4728 !PyObject_TypeCheck(args, &PyBaseString_Type)) |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4729 dict = args; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4730 while (--fmtcnt >= 0) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4731 if (*fmt != '%') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4732 if (--rescnt < 0) { |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
4733 rescnt = fmtcnt + 100; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
4734 reslen += rescnt; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4735 if (_PyString_Resize(&result, reslen) < 0) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4736 return NULL; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4737 res = PyString_AS_STRING(result) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4738 + reslen - rescnt; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4739 --rescnt; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4740 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4741 *res++ = *fmt++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4742 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4743 else { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4744 /* Got a format specifier */ |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4745 int flags = 0; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
4746 Py_ssize_t width = -1; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4747 int prec = -1; |
|
1408
18df674539da
Three micro fixes to formatstring
Guido van Rossum <guido@python.org>
parents:
1380
diff
changeset
|
4748 int c = '\0'; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4749 int fill; |
|
45500
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4750 int isnumok; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4751 PyObject *v = NULL; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4752 PyObject *temp = NULL; |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4753 char *pbuf; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4754 int sign; |
|
36866
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36675
diff
changeset
|
4755 Py_ssize_t len; |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4756 char formatbuf[FORMATBUFLEN]; |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
4757 /* For format{float,int,char}() */ |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
4758 #ifdef Py_USING_UNICODE |
| 12328 | 4759 char *fmt_start = fmt; |
|
36866
9eb24f92a784
Change int to Py_ssize_t in several places.
Martin v. Löwis <martin@v.loewis.de>
parents:
36675
diff
changeset
|
4760 Py_ssize_t argidx_start = argidx; |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
4761 #endif |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
4762 |
| 4588 | 4763 fmt++; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4764 if (*fmt == '(') { |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4765 char *keystart; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
4766 Py_ssize_t keylen; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4767 PyObject *key; |
|
6286
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
4768 int pcount = 1; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4769 |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4770 if (dict == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4771 PyErr_SetString(PyExc_TypeError, |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
4772 "format requires a mapping"); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4773 goto error; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4774 } |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4775 ++fmt; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4776 --fmtcnt; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4777 keystart = fmt; |
|
6286
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
4778 /* Skip over balanced parentheses */ |
|
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
4779 while (pcount > 0 && --fmtcnt >= 0) { |
|
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
4780 if (*fmt == ')') |
|
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
4781 --pcount; |
|
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
4782 else if (*fmt == '(') |
|
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
4783 ++pcount; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4784 fmt++; |
|
6286
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
4785 } |
|
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
4786 keylen = fmt - keystart - 1; |
|
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
4787 if (fmtcnt < 0 || pcount > 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4788 PyErr_SetString(PyExc_ValueError, |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4789 "incomplete format key"); |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4790 goto error; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4791 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4792 key = PyString_FromStringAndSize(keystart, |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4793 keylen); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4794 if (key == NULL) |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4795 goto error; |
|
3570
977ff1ace070
Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
4796 if (args_owned) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4797 Py_DECREF(args); |
|
3570
977ff1ace070
Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
4798 args_owned = 0; |
|
977ff1ace070
Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
4799 } |
|
977ff1ace070
Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
4800 args = PyObject_GetItem(dict, key); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4801 Py_DECREF(key); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4802 if (args == NULL) { |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4803 goto error; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4804 } |
|
3570
977ff1ace070
Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
4805 args_owned = 1; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4806 arglen = -1; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4807 argidx = -2; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4808 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4809 while (--fmtcnt >= 0) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4810 switch (c = *fmt++) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4811 case '-': flags |= F_LJUST; continue; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4812 case '+': flags |= F_SIGN; continue; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4813 case ' ': flags |= F_BLANK; continue; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4814 case '#': flags |= F_ALT; continue; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4815 case '0': flags |= F_ZERO; continue; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4816 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4817 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4818 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4819 if (c == '*') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4820 v = getnextarg(args, arglen, &argidx); |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4821 if (v == NULL) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4822 goto error; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4823 if (!PyInt_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4824 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4825 "* wants int"); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4826 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4827 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4828 width = PyInt_AsLong(v); |
|
10923
8234bb36fb8c
Fix bug discovered by John W. Shipman -- when the width of a format
Guido van Rossum <guido@python.org>
parents:
9305
diff
changeset
|
4829 if (width < 0) { |
|
8234bb36fb8c
Fix bug discovered by John W. Shipman -- when the width of a format
Guido van Rossum <guido@python.org>
parents:
9305
diff
changeset
|
4830 flags |= F_LJUST; |
|
8234bb36fb8c
Fix bug discovered by John W. Shipman -- when the width of a format
Guido van Rossum <guido@python.org>
parents:
9305
diff
changeset
|
4831 width = -width; |
|
8234bb36fb8c
Fix bug discovered by John W. Shipman -- when the width of a format
Guido van Rossum <guido@python.org>
parents:
9305
diff
changeset
|
4832 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4833 if (--fmtcnt >= 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4834 c = *fmt++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4835 } |
|
2308
14b126d0b5be
use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
4836 else if (c >= 0 && isdigit(c)) { |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4837 width = c - '0'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4838 while (--fmtcnt >= 0) { |
|
2308
14b126d0b5be
use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
4839 c = Py_CHARMASK(*fmt++); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4840 if (!isdigit(c)) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4841 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4842 if ((width*10) / 10 != width) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4843 PyErr_SetString( |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4844 PyExc_ValueError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4845 "width too big"); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4846 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4847 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4848 width = width*10 + (c - '0'); |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4849 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4850 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4851 if (c == '.') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4852 prec = 0; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4853 if (--fmtcnt >= 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4854 c = *fmt++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4855 if (c == '*') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4856 v = getnextarg(args, arglen, &argidx); |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4857 if (v == NULL) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4858 goto error; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4859 if (!PyInt_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4860 PyErr_SetString( |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4861 PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4862 "* wants int"); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4863 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4864 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4865 prec = PyInt_AsLong(v); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4866 if (prec < 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4867 prec = 0; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4868 if (--fmtcnt >= 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4869 c = *fmt++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4870 } |
|
2308
14b126d0b5be
use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
4871 else if (c >= 0 && isdigit(c)) { |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4872 prec = c - '0'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4873 while (--fmtcnt >= 0) { |
|
2308
14b126d0b5be
use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
4874 c = Py_CHARMASK(*fmt++); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4875 if (!isdigit(c)) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4876 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4877 if ((prec*10) / 10 != prec) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4878 PyErr_SetString( |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4879 PyExc_ValueError, |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4880 "prec too big"); |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4881 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4882 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4883 prec = prec*10 + (c - '0'); |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4884 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4885 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4886 } /* prec */ |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4887 if (fmtcnt >= 0) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4888 if (c == 'h' || c == 'l' || c == 'L') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4889 if (--fmtcnt >= 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4890 c = *fmt++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4891 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4892 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4893 if (fmtcnt < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4894 PyErr_SetString(PyExc_ValueError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4895 "incomplete format"); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4896 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4897 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4898 if (c != '%') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4899 v = getnextarg(args, arglen, &argidx); |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4900 if (v == NULL) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4901 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4902 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4903 sign = 0; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4904 fill = ' '; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4905 switch (c) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4906 case '%': |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
4907 pbuf = "%"; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4908 len = 1; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4909 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4910 case 's': |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
4911 #ifdef Py_USING_UNICODE |
|
35503
f63e6960c5cf
Fix bug in last checkin (2.231). To match previous behavior, unicode
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35331
diff
changeset
|
4912 if (PyUnicode_Check(v)) { |
|
f63e6960c5cf
Fix bug in last checkin (2.231). To match previous behavior, unicode
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35331
diff
changeset
|
4913 fmt = fmt_start; |
|
f63e6960c5cf
Fix bug in last checkin (2.231). To match previous behavior, unicode
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35331
diff
changeset
|
4914 argidx = argidx_start; |
|
f63e6960c5cf
Fix bug in last checkin (2.231). To match previous behavior, unicode
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35331
diff
changeset
|
4915 goto unicode; |
|
f63e6960c5cf
Fix bug in last checkin (2.231). To match previous behavior, unicode
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35331
diff
changeset
|
4916 } |
|
35665
dc5d706283d6
Fix PyString_Format so that the "%s" format works again when Unicode is not
Georg Brandl <georg@python.org>
parents:
35503
diff
changeset
|
4917 #endif |
|
35331
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35177
diff
changeset
|
4918 temp = _PyObject_Str(v); |
|
35665
dc5d706283d6
Fix PyString_Format so that the "%s" format works again when Unicode is not
Georg Brandl <georg@python.org>
parents:
35503
diff
changeset
|
4919 #ifdef Py_USING_UNICODE |
|
35331
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35177
diff
changeset
|
4920 if (temp != NULL && PyUnicode_Check(temp)) { |
|
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35177
diff
changeset
|
4921 Py_DECREF(temp); |
| 12328 | 4922 fmt = fmt_start; |
|
17655
0470babdae49
Fix for bug #417030: "print '%*s' fails for unicode string"
Marc-André Lemburg <mal@egenix.com>
parents:
17645
diff
changeset
|
4923 argidx = argidx_start; |
| 12328 | 4924 goto unicode; |
| 4925 } | |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
4926 #endif |
|
25682
ce49f12a1769
The string formatting code has a test to switch to Unicode when %s
Guido van Rossum <guido@python.org>
parents:
25659
diff
changeset
|
4927 /* Fall through */ |
| 29529 | 4928 case 'r': |
|
35331
ef11cbc08dab
Change the %s format specifier for str objects so that it returns a
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
35177
diff
changeset
|
4929 if (c == 'r') |
| 12371 | 4930 temp = PyObject_Repr(v); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
4931 if (temp == NULL) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4932 goto error; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4933 if (!PyString_Check(temp)) { |
|
8468
c27cd5fc46b5
Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
4934 PyErr_SetString(PyExc_TypeError, |
|
25683
38843d35b646
Undo this part of the previous checkin:
Guido van Rossum <guido@python.org>
parents:
25682
diff
changeset
|
4935 "%s argument has non-string str()"); |
| 21254 | 4936 Py_DECREF(temp); |
|
8468
c27cd5fc46b5
Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
4937 goto error; |
|
c27cd5fc46b5
Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
4938 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4939 pbuf = PyString_AS_STRING(temp); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4940 len = PyString_GET_SIZE(temp); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4941 if (prec >= 0 && len > prec) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4942 len = prec; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4943 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4944 case 'i': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4945 case 'd': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4946 case 'u': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4947 case 'o': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4948 case 'x': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4949 case 'X': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4950 if (c == 'i') |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
4951 c = 'd'; |
|
45500
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4952 isnumok = 0; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4953 if (PyNumber_Check(v)) { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4954 PyObject *iobj=NULL; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4955 |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4956 if (PyInt_Check(v) || (PyLong_Check(v))) { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4957 iobj = v; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4958 Py_INCREF(iobj); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4959 } |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4960 else { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4961 iobj = PyNumber_Int(v); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4962 if (iobj==NULL) iobj = PyNumber_Long(v); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4963 } |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4964 if (iobj!=NULL) { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4965 if (PyInt_Check(iobj)) { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4966 isnumok = 1; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4967 pbuf = formatbuf; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4968 len = formatint(pbuf, |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4969 sizeof(formatbuf), |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4970 flags, prec, c, iobj); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4971 Py_DECREF(iobj); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4972 if (len < 0) |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4973 goto error; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4974 sign = 1; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4975 } |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4976 else if (PyLong_Check(iobj)) { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4977 int ilen; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4978 |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4979 isnumok = 1; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
4980 temp = _PyString_FormatLong(iobj, flags, |
|
45500
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4981 prec, c, &pbuf, &ilen); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4982 Py_DECREF(iobj); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4983 len = ilen; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4984 if (!temp) |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4985 goto error; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4986 sign = 1; |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4987 } |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4988 else { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4989 Py_DECREF(iobj); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4990 } |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4991 } |
|
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
|
4992 } |
|
45500
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4993 if (!isnumok) { |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4994 PyErr_Format(PyExc_TypeError, |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4995 "%%%c format: a number is required, " |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4996 "not %.200s", c, Py_TYPE(v)->tp_name); |
|
c03aff6d07f5
Issue 1742669. Now %d accepts very big float numbers.
Facundo Batista <facundobatista@gmail.com>
parents:
45381
diff
changeset
|
4997 goto error; |
|
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
|
4998 } |
|
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
|
4999 if (flags & F_ZERO) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5000 fill = '0'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5001 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5002 case 'e': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5003 case 'E': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5004 case 'f': |
|
30216
7ab1a41f16da
SF bug #795506: Wrong handling of string format code for float values.
Raymond Hettinger <python@rcn.com>
parents:
29529
diff
changeset
|
5005 case 'F': |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5006 case 'g': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5007 case 'G': |
|
48003
5c33009994b4
Backed out r65069, pending fixing it in Windows.
Eric Smith <eric@trueblade.com>
parents:
48001
diff
changeset
|
5008 if (c == 'F') |
|
5c33009994b4
Backed out r65069, pending fixing it in Windows.
Eric Smith <eric@trueblade.com>
parents:
48001
diff
changeset
|
5009 c = 'f'; |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
5010 pbuf = formatbuf; |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
5011 len = formatfloat(pbuf, sizeof(formatbuf), |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
5012 flags, prec, c, v); |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
5013 if (len < 0) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5014 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5015 sign = 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
|
5016 if (flags & F_ZERO) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5017 fill = '0'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5018 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5019 case 'c': |
|
28478
8802de0da533
Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
5020 #ifdef Py_USING_UNICODE |
|
8802de0da533
Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
5021 if (PyUnicode_Check(v)) { |
|
8802de0da533
Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
5022 fmt = fmt_start; |
|
8802de0da533
Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
5023 argidx = argidx_start; |
|
8802de0da533
Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
5024 goto unicode; |
|
8802de0da533
Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
5025 } |
|
8802de0da533
Fix PyString_Format() so that '%c' % u'a' returns u'a'
Walter Dörwald <walter@livinglogic.de>
parents:
27521
diff
changeset
|
5026 #endif |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
5027 pbuf = formatbuf; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
5028 len = formatchar(pbuf, sizeof(formatbuf), v); |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
5029 if (len < 0) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5030 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5031 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5032 default: |
|
6286
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
5033 PyErr_Format(PyExc_ValueError, |
|
15579
31d78f361530
[ Patch #102852 ] Make % error a bit more informative by indicates the
Andrew M. Kuchling <amk@amk.ca>
parents:
15484
diff
changeset
|
5034 "unsupported format character '%c' (0x%x) " |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
5035 "at index %zd", |
|
25233
8d751b62ecc4
Fix warnings on 64-bit platforms about casts from pointers to ints.
Guido van Rossum <guido@python.org>
parents:
25173
diff
changeset
|
5036 c, c, |
|
39995
6ba402b60041
Forward-port of r52136,52138: a review of overflow-detecting code.
Armin Rigo <arigo@tunes.org>
parents:
39734
diff
changeset
|
5037 (Py_ssize_t)(fmt - 1 - |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5038 PyString_AsString(format))); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5039 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5040 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5041 if (sign) { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
5042 if (*pbuf == '-' || *pbuf == '+') { |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
5043 sign = *pbuf++; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5044 len--; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5045 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5046 else if (flags & F_SIGN) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5047 sign = '+'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5048 else if (flags & F_BLANK) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5049 sign = ' '; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5050 else |
|
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
|
5051 sign = 0; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5052 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5053 if (width < len) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5054 width = len; |
|
25719
f532e9b69551
Fix a nasty endcase reported by Armin Rigo in SF bug 618623:
Guido van Rossum <guido@python.org>
parents:
25683
diff
changeset
|
5055 if (rescnt - (sign != 0) < width) { |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
5056 reslen -= rescnt; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
5057 rescnt = width + fmtcnt + 100; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
5058 reslen += rescnt; |
|
25719
f532e9b69551
Fix a nasty endcase reported by Armin Rigo in SF bug 618623:
Guido van Rossum <guido@python.org>
parents:
25683
diff
changeset
|
5059 if (reslen < 0) { |
|
f532e9b69551
Fix a nasty endcase reported by Armin Rigo in SF bug 618623:
Guido van Rossum <guido@python.org>
parents:
25683
diff
changeset
|
5060 Py_DECREF(result); |
|
40925
621443fc01bb
Backport from Py3k branch: fix refleak in PyString_Format.
Georg Brandl <georg@python.org>
parents:
40908
diff
changeset
|
5061 Py_XDECREF(temp); |
|
25719
f532e9b69551
Fix a nasty endcase reported by Armin Rigo in SF bug 618623:
Guido van Rossum <guido@python.org>
parents:
25683
diff
changeset
|
5062 return PyErr_NoMemory(); |
|
f532e9b69551
Fix a nasty endcase reported by Armin Rigo in SF bug 618623:
Guido van Rossum <guido@python.org>
parents:
25683
diff
changeset
|
5063 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5064 if (_PyString_Resize(&result, reslen) < 0) { |
|
40925
621443fc01bb
Backport from Py3k branch: fix refleak in PyString_Format.
Georg Brandl <georg@python.org>
parents:
40908
diff
changeset
|
5065 Py_XDECREF(temp); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5066 return NULL; |
|
40925
621443fc01bb
Backport from Py3k branch: fix refleak in PyString_Format.
Georg Brandl <georg@python.org>
parents:
40908
diff
changeset
|
5067 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5068 res = PyString_AS_STRING(result) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
5069 + reslen - rescnt; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5070 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5071 if (sign) { |
| 1409 | 5072 if (fill != ' ') |
| 5073 *res++ = sign; | |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5074 rescnt--; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5075 if (width > len) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5076 width--; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5077 } |
|
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
|
5078 if ((flags & F_ALT) && (c == 'x' || c == '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
|
5079 assert(pbuf[0] == '0'); |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
5080 assert(pbuf[1] == c); |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
5081 if (fill != ' ') { |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
5082 *res++ = *pbuf++; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
5083 *res++ = *pbuf++; |
|
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
|
5084 } |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
5085 rescnt -= 2; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
5086 width -= 2; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
5087 if (width < 0) |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
5088 width = 0; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
5089 len -= 2; |
|
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
|
5090 } |
|
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
|
5091 if (width > len && !(flags & F_LJUST)) { |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5092 do { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5093 --rescnt; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5094 *res++ = fill; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5095 } while (--width > len); |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5096 } |
|
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
|
5097 if (fill == ' ') { |
|
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
|
5098 if (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
|
5099 *res++ = 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
|
5100 if ((flags & F_ALT) && |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
5101 (c == 'x' || c == 'X')) { |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
5102 assert(pbuf[0] == '0'); |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
5103 assert(pbuf[1] == c); |
|
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
|
5104 *res++ = *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
|
5105 *res++ = *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
|
5106 } |
|
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
|
5107 } |
|
38457
551814e4b471
needforspeed: added Py_MEMCPY macro (currently tuned for Visual C only),
Fredrik Lundh <fredrik@pythonware.com>
parents:
38442
diff
changeset
|
5108 Py_MEMCPY(res, pbuf, len); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5109 res += len; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5110 rescnt -= len; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5111 while (--width >= len) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5112 --rescnt; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5113 *res++ = ' '; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5114 } |
|
2308
14b126d0b5be
use Py_CHARMASK; and don't check for neg. float to the float power here
Guido van Rossum <guido@python.org>
parents:
2059
diff
changeset
|
5115 if (dict && (argidx < arglen) && c != '%') { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
5116 PyErr_SetString(PyExc_TypeError, |
|
23494
76f376bea5e5
Patch 549187. Improve string formatting error message.
Raymond Hettinger <python@rcn.com>
parents:
23430
diff
changeset
|
5117 "not all arguments converted during string formatting"); |
|
40925
621443fc01bb
Backport from Py3k branch: fix refleak in PyString_Format.
Georg Brandl <georg@python.org>
parents:
40908
diff
changeset
|
5118 Py_XDECREF(temp); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
5119 goto error; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
5120 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
5121 Py_XDECREF(temp); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5122 } /* '%' */ |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5123 } /* until end */ |
|
2432
445bea8ceab1
don't complain about too many args if arg is a dict
Guido van Rossum <guido@python.org>
parents:
2308
diff
changeset
|
5124 if (argidx < arglen && !dict) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
5125 PyErr_SetString(PyExc_TypeError, |
|
23494
76f376bea5e5
Patch 549187. Improve string formatting error message.
Raymond Hettinger <python@rcn.com>
parents:
23430
diff
changeset
|
5126 "not all arguments converted during string formatting"); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5127 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5128 } |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
6286
diff
changeset
|
5129 if (args_owned) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
5130 Py_DECREF(args); |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
6286
diff
changeset
|
5131 } |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5132 _PyString_Resize(&result, reslen - rescnt); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5133 return result; |
| 12328 | 5134 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
5135 #ifdef Py_USING_UNICODE |
| 12328 | 5136 unicode: |
| 5137 if (args_owned) { | |
| 5138 Py_DECREF(args); | |
| 5139 args_owned = 0; | |
| 5140 } | |
|
17655
0470babdae49
Fix for bug #417030: "print '%*s' fails for unicode string"
Marc-André Lemburg <mal@egenix.com>
parents:
17645
diff
changeset
|
5141 /* Fiddle args right (remove the first argidx arguments) */ |
| 12328 | 5142 if (PyTuple_Check(orig_args) && argidx > 0) { |
| 5143 PyObject *v; | |
|
36519
d6d415df0df0
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36510
diff
changeset
|
5144 Py_ssize_t n = PyTuple_GET_SIZE(orig_args) - argidx; |
| 12328 | 5145 v = PyTuple_New(n); |
| 5146 if (v == NULL) | |
| 5147 goto error; | |
| 5148 while (--n >= 0) { | |
| 5149 PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx); | |
| 5150 Py_INCREF(w); | |
| 5151 PyTuple_SET_ITEM(v, n, w); | |
| 5152 } | |
| 5153 args = v; | |
| 5154 } else { | |
| 5155 Py_INCREF(orig_args); | |
| 5156 args = orig_args; | |
| 5157 } | |
|
15082
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5158 args_owned = 1; |
|
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5159 /* Take what we have of the result and let the Unicode formatting |
|
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5160 function format the rest of the input. */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5161 rescnt = res - PyString_AS_STRING(result); |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5162 if (_PyString_Resize(&result, rescnt)) |
|
15082
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5163 goto error; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5164 fmtcnt = PyString_GET_SIZE(format) - \ |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5165 (fmt - PyString_AS_STRING(format)); |
|
15082
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5166 format = PyUnicode_Decode(fmt, fmtcnt, NULL, NULL); |
|
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5167 if (format == NULL) |
|
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5168 goto error; |
|
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5169 v = PyUnicode_Format(format, args); |
|
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5170 Py_DECREF(format); |
|
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5171 if (v == NULL) |
| 12328 | 5172 goto error; |
|
15082
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5173 /* Paste what we have (result) to what the Unicode formatting |
|
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5174 function returned (v) and return the result (or error) */ |
|
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5175 w = PyUnicode_Concat(result, v); |
|
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5176 Py_DECREF(result); |
|
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5177 Py_DECREF(v); |
| 12328 | 5178 Py_DECREF(args); |
|
15082
c2f13c395b9a
[ Bug #116174 ] using %% in cstrings sometimes fails with unicode paramsFix for the bug reported in Bug #116174: "%% %s" % u"abc" failed due
Marc-André Lemburg <mal@egenix.com>
parents:
14869
diff
changeset
|
5179 return w; |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
5180 #endif /* Py_USING_UNICODE */ |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
5181 |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5182 error: |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
5183 Py_DECREF(result); |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
6286
diff
changeset
|
5184 if (args_owned) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
5185 Py_DECREF(args); |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
6286
diff
changeset
|
5186 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5187 return NULL; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
5188 } |
| 5005 | 5189 |
| 5190 void | |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
5191 PyString_InternInPlace(PyObject **p) |
| 5005 | 5192 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5193 register PyStringObject *s = (PyStringObject *)(*p); |
| 5005 | 5194 PyObject *t; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5195 if (s == NULL || !PyString_Check(s)) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
5196 Py_FatalError("PyString_InternInPlace: strings only please!"); |
|
33019
5218bff56449
Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32552
diff
changeset
|
5197 /* If it's a string subclass, we don't really know what putting |
|
5218bff56449
Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32552
diff
changeset
|
5198 it in the interned dict might do. */ |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5199 if (!PyString_CheckExact(s)) |
|
33019
5218bff56449
Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32552
diff
changeset
|
5200 return; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5201 if (PyString_CHECK_INTERNED(s)) |
| 5005 | 5202 return; |
| 5203 if (interned == NULL) { | |
| 5204 interned = PyDict_New(); | |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5205 if (interned == NULL) { |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5206 PyErr_Clear(); /* Don't leave an exception */ |
| 5005 | 5207 return; |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5208 } |
| 5005 | 5209 } |
|
33019
5218bff56449
Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32552
diff
changeset
|
5210 t = PyDict_GetItem(interned, (PyObject *)s); |
|
5218bff56449
Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32552
diff
changeset
|
5211 if (t) { |
| 5005 | 5212 Py_INCREF(t); |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5213 Py_DECREF(*p); |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5214 *p = t; |
| 5005 | 5215 return; |
| 5216 } | |
|
33019
5218bff56449
Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32552
diff
changeset
|
5217 |
|
33023
4f588f57a93b
Fixed some compiler warnings.
Armin Rigo <arigo@tunes.org>
parents:
33019
diff
changeset
|
5218 if (PyDict_SetItem(interned, (PyObject *)s, (PyObject *)s) < 0) { |
|
33019
5218bff56449
Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32552
diff
changeset
|
5219 PyErr_Clear(); |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5220 return; |
| 5005 | 5221 } |
|
33019
5218bff56449
Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32552
diff
changeset
|
5222 /* The two references in interned are not counted by refcnt. |
|
5218bff56449
Subclasses of string can no longer be interned. The semantics of
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
32552
diff
changeset
|
5223 The string deallocator will take care of this */ |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
5224 Py_REFCNT(s) -= 2; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5225 PyString_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL; |
| 5005 | 5226 } |
| 5227 | |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5228 void |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
5229 PyString_InternImmortal(PyObject **p) |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5230 { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
5231 PyString_InternInPlace(p); |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5232 if (PyString_CHECK_INTERNED(*p) != SSTATE_INTERNED_IMMORTAL) { |
|
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5233 PyString_CHECK_INTERNED(*p) = SSTATE_INTERNED_IMMORTAL; |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5234 Py_INCREF(*p); |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5235 } |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5236 } |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5237 |
| 5005 | 5238 |
| 5239 PyObject * | |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
5240 PyString_InternFromString(const char *cp) |
| 5005 | 5241 { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5242 PyObject *s = PyString_FromString(cp); |
| 5005 | 5243 if (s == NULL) |
| 5244 return NULL; | |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
5245 PyString_InternInPlace(&s); |
| 5005 | 5246 return s; |
| 5247 } | |
| 5248 | |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
5249 void |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47421
diff
changeset
|
5250 PyString_Fini(void) |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
5251 { |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
5252 int i; |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
5253 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
|
5254 Py_XDECREF(characters[i]); |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
5255 characters[i] = NULL; |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
5256 } |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
5257 Py_XDECREF(nullstring); |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
5258 nullstring = NULL; |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
5259 } |
|
16751
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
5260 |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
5261 void _Py_ReleaseInternedStrings(void) |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
5262 { |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5263 PyObject *keys; |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5264 PyStringObject *s; |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35999
diff
changeset
|
5265 Py_ssize_t i, n; |
|
40901
2e4dac059cc5
Add more details when releasing interned strings
Neal Norwitz <nnorwitz@gmail.com>
parents:
40357
diff
changeset
|
5266 Py_ssize_t immortal_size = 0, mortal_size = 0; |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5267 |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5268 if (interned == NULL || !PyDict_Check(interned)) |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5269 return; |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5270 keys = PyDict_Keys(interned); |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5271 if (keys == NULL || !PyList_Check(keys)) { |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5272 PyErr_Clear(); |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5273 return; |
|
16751
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
5274 } |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5275 |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5276 /* Since _Py_ReleaseInternedStrings() is intended to help a leak |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5277 detector, interned strings are not forcibly deallocated; rather, we |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5278 give them their stolen references back, and then clear and DECREF |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5279 the interned dict. */ |
|
37037
bfb74a420ac8
Introduced symbol PY_FORMAT_SIZE_T. See the new comments
Tim Peters <tim.peters@gmail.com>
parents:
36882
diff
changeset
|
5280 |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5281 n = PyList_GET_SIZE(keys); |
|
40901
2e4dac059cc5
Add more details when releasing interned strings
Neal Norwitz <nnorwitz@gmail.com>
parents:
40357
diff
changeset
|
5282 fprintf(stderr, "releasing %" PY_FORMAT_SIZE_T "d interned strings\n", |
|
2e4dac059cc5
Add more details when releasing interned strings
Neal Norwitz <nnorwitz@gmail.com>
parents:
40357
diff
changeset
|
5283 n); |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5284 for (i = 0; i < n; i++) { |
|
47560
acda43252710
More reverting of r63675 per the mailing list discussions. This restores
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
5285 s = (PyStringObject *) PyList_GET_ITEM(keys, i); |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5286 switch (s->ob_sstate) { |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5287 case SSTATE_NOT_INTERNED: |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5288 /* XXX Shouldn't happen */ |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5289 break; |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5290 case SSTATE_INTERNED_IMMORTAL: |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
5291 Py_REFCNT(s) += 1; |
|
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
5292 immortal_size += Py_SIZE(s); |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5293 break; |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5294 case SSTATE_INTERNED_MORTAL: |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
5295 Py_REFCNT(s) += 2; |
|
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
44225
diff
changeset
|
5296 mortal_size += Py_SIZE(s); |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5297 break; |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5298 default: |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5299 Py_FatalError("Inconsistent interned string state."); |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5300 } |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5301 s->ob_sstate = SSTATE_NOT_INTERNED; |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5302 } |
|
40901
2e4dac059cc5
Add more details when releasing interned strings
Neal Norwitz <nnorwitz@gmail.com>
parents:
40357
diff
changeset
|
5303 fprintf(stderr, "total size of all interned strings: " |
|
2e4dac059cc5
Add more details when releasing interned strings
Neal Norwitz <nnorwitz@gmail.com>
parents:
40357
diff
changeset
|
5304 "%" PY_FORMAT_SIZE_T "d/%" PY_FORMAT_SIZE_T "d " |
|
2e4dac059cc5
Add more details when releasing interned strings
Neal Norwitz <nnorwitz@gmail.com>
parents:
40357
diff
changeset
|
5305 "mortal/immortal\n", mortal_size, immortal_size); |
|
24901
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5306 Py_DECREF(keys); |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5307 PyDict_Clear(interned); |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5308 Py_DECREF(interned); |
|
bc660f73795c
SF patch 576101, by Oren Tirosh: alternative implementation of
Guido van Rossum <guido@python.org>
parents:
24898
diff
changeset
|
5309 interned = NULL; |
|
16751
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
5310 } |
