Mercurial > cpython-lru_cache-weakref
annotate Objects/stringobject.c @ 23295:6fa76b4e59fe legacy-trunk
Repair widespread misuse of _PyString_Resize. Since it's clear people
don't understand how this function works, also beefed up the docs. The
most common usage error is of this form (often spread out across gotos):
if (_PyString_Resize(&s, n) < 0) {
Py_DECREF(s);
s = NULL;
goto outtahere;
}
The error is that if _PyString_Resize runs out of memory, it automatically
decrefs the input string object s (which also deallocates it, since its
refcount must be 1 upon entry), and sets s to NULL. So if the "if"
branch ever triggers, it's an error to call Py_DECREF(s): s is already
NULL! A correct way to write the above is the simpler (and intended)
if (_PyString_Resize(&s, n) < 0)
goto outtahere;
Bugfix candidate.
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Sat, 27 Apr 2002 18:44:32 +0000 |
| parents | e639e34ef901 |
| children | 1be6e79b1d46 |
| rev | line source |
|---|---|
| 6 | 1 /* String object implementation */ |
| 2 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3 #include "Python.h" |
| 6 | 4 |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
5 #include <ctype.h> |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
6 |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
7 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
8 int null_strings, one_strings; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
9 #endif |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
10 |
|
14869
25ddd3dc7a7a
Rationalize use of limits.h, moving the inclusion to Python.h.
Fred Drake <fdrake@acm.org>
parents:
14713
diff
changeset
|
11 #if !defined(HAVE_LIMITS_H) && !defined(UCHAR_MAX) |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
12 #define UCHAR_MAX 255 |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
13 #endif |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
14 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
15 static PyStringObject *characters[UCHAR_MAX + 1]; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
16 static PyStringObject *nullstring; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
17 |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
18 /* |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
19 For both PyString_FromString() and PyString_FromStringAndSize(), the |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
20 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
|
21 null terminating character. |
|
30a817038af0
Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents:
21327
diff
changeset
|
22 |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
23 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
|
24 string containing exactly `size' bytes. |
|
30a817038af0
Updated patch #487906: Revise inline docs.
Martin v. Löwis <martin@v.loewis.de>
parents:
21327
diff
changeset
|
25 |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
26 For PyString_FromStringAndSize(), the parameter the parameter `str' is |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
27 either NULL or else points to a string containing at least `size' bytes. |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
28 For PyString_FromStringAndSize(), the string in the `str' parameter does |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
29 not have to be null-terminated. (Therefore it is safe to construct a |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
30 substring by calling `PyString_FromStringAndSize(origstring, substrlen)'.) |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
31 If `str' is NULL then PyString_FromStringAndSize() will allocate `size+1' |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
32 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
|
33 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
|
34 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
|
35 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
|
36 |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
37 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
|
38 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
|
39 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
|
40 is therefore equal to the equal to the `size' parameter (for |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
41 PyString_FromStringAndSize()) or the length of the string in the `str' |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
42 parameter (for PyString_FromString()). |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
43 */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
44 PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
45 PyString_FromStringAndSize(const char *str, int size) |
| 6 | 46 { |
|
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
|
47 register PyStringObject *op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
48 if (size == 0 && (op = nullstring) != NULL) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
49 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
50 null_strings++; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
51 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
52 Py_INCREF(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
53 return (PyObject *)op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
54 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
55 if (size == 1 && str != NULL && |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
56 (op = characters[*str & UCHAR_MAX]) != NULL) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
57 { |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
58 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
59 one_strings++; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
60 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
61 Py_INCREF(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
62 return (PyObject *)op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
63 } |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
64 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
65 /* PyObject_NewVar is inlined */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
66 op = (PyStringObject *) |
|
22977
4ba091cc70b8
Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22785
diff
changeset
|
67 PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char)); |
| 27 | 68 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
69 return PyErr_NoMemory(); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
70 PyObject_INIT_VAR(op, &PyString_Type, size); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
71 op->ob_shash = -1; |
| 5005 | 72 op->ob_sinterned = NULL; |
| 27 | 73 if (str != NULL) |
| 74 memcpy(op->ob_sval, str, size); | |
| 75 op->ob_sval[size] = '\0'; | |
|
22689
614167df2096
Eliminate DONT_SHARE_SHORT_STRINGS.
Tim Peters <tim.peters@gmail.com>
parents:
22651
diff
changeset
|
76 /* share short strings */ |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
77 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
|
78 PyObject *t = (PyObject *)op; |
|
ede29ad6440a
Mark Favas reported that gcc caught me using casts as lvalues. Dodge it.
Tim Peters <tim.peters@gmail.com>
parents:
17712
diff
changeset
|
79 PyString_InternInPlace(&t); |
|
17721
1dea431fb0a0
Sheesh -- repair the dodge around "cast isn't an lvalue" complaints to
Tim Peters <tim.peters@gmail.com>
parents:
17720
diff
changeset
|
80 op = (PyStringObject *)t; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
81 nullstring = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
82 Py_INCREF(op); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
83 } 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
|
84 PyObject *t = (PyObject *)op; |
|
ede29ad6440a
Mark Favas reported that gcc caught me using casts as lvalues. Dodge it.
Tim Peters <tim.peters@gmail.com>
parents:
17712
diff
changeset
|
85 PyString_InternInPlace(&t); |
|
17721
1dea431fb0a0
Sheesh -- repair the dodge around "cast isn't an lvalue" complaints to
Tim Peters <tim.peters@gmail.com>
parents:
17720
diff
changeset
|
86 op = (PyStringObject *)t; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
87 characters[*str & UCHAR_MAX] = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
88 Py_INCREF(op); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
89 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
90 return (PyObject *) op; |
| 6 | 91 } |
| 92 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
93 PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
94 PyString_FromString(const char *str) |
| 6 | 95 { |
|
21263
2f2abeeefd87
PyString_FromString: this requires its argument be non-NULL, but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21254
diff
changeset
|
96 register size_t size; |
|
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
|
97 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
|
98 |
|
2f2abeeefd87
PyString_FromString: this requires its argument be non-NULL, but doesn't
Tim Peters <tim.peters@gmail.com>
parents:
21254
diff
changeset
|
99 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
|
100 size = strlen(str); |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
101 if (size > INT_MAX) { |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
102 PyErr_SetString(PyExc_OverflowError, |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
103 "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
|
104 return NULL; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
105 } |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
106 if (size == 0 && (op = nullstring) != NULL) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
107 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
108 null_strings++; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
109 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
110 Py_INCREF(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
111 return (PyObject *)op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
112 } |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
113 if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
114 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
115 one_strings++; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
116 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
117 Py_INCREF(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
118 return (PyObject *)op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
119 } |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
120 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
121 /* PyObject_NewVar is inlined */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
122 op = (PyStringObject *) |
|
22977
4ba091cc70b8
Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22785
diff
changeset
|
123 PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char)); |
| 27 | 124 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
125 return PyErr_NoMemory(); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
126 PyObject_INIT_VAR(op, &PyString_Type, size); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
127 op->ob_shash = -1; |
| 5005 | 128 op->ob_sinterned = NULL; |
|
21327
34a1a50e6ab6
SF patch #491049 (David Jacobs): Small PyString_FromString optimization
Guido van Rossum <guido@python.org>
parents:
21263
diff
changeset
|
129 memcpy(op->ob_sval, str, size+1); |
|
22689
614167df2096
Eliminate DONT_SHARE_SHORT_STRINGS.
Tim Peters <tim.peters@gmail.com>
parents:
22651
diff
changeset
|
130 /* share short strings */ |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
131 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
|
132 PyObject *t = (PyObject *)op; |
|
ede29ad6440a
Mark Favas reported that gcc caught me using casts as lvalues. Dodge it.
Tim Peters <tim.peters@gmail.com>
parents:
17712
diff
changeset
|
133 PyString_InternInPlace(&t); |
|
17721
1dea431fb0a0
Sheesh -- repair the dodge around "cast isn't an lvalue" complaints to
Tim Peters <tim.peters@gmail.com>
parents:
17720
diff
changeset
|
134 op = (PyStringObject *)t; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
135 nullstring = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
136 Py_INCREF(op); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
137 } 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
|
138 PyObject *t = (PyObject *)op; |
|
ede29ad6440a
Mark Favas reported that gcc caught me using casts as lvalues. Dodge it.
Tim Peters <tim.peters@gmail.com>
parents:
17712
diff
changeset
|
139 PyString_InternInPlace(&t); |
|
17721
1dea431fb0a0
Sheesh -- repair the dodge around "cast isn't an lvalue" complaints to
Tim Peters <tim.peters@gmail.com>
parents:
17720
diff
changeset
|
140 op = (PyStringObject *)t; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
141 characters[*str & UCHAR_MAX] = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
142 Py_INCREF(op); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
143 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
144 return (PyObject *) op; |
| 6 | 145 } |
| 146 | |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
147 PyObject * |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
148 PyString_FromFormatV(const char *format, va_list vargs) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
149 { |
|
20221
abaf9ea2e560
SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents:
20140
diff
changeset
|
150 va_list count; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
151 int n = 0; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
152 const char* f; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
153 char *s; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
154 PyObject* string; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
155 |
|
20221
abaf9ea2e560
SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents:
20140
diff
changeset
|
156 #ifdef VA_LIST_IS_ARRAY |
|
abaf9ea2e560
SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents:
20140
diff
changeset
|
157 memcpy(count, vargs, sizeof(va_list)); |
|
abaf9ea2e560
SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents:
20140
diff
changeset
|
158 #else |
|
abaf9ea2e560
SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents:
20140
diff
changeset
|
159 count = vargs; |
|
abaf9ea2e560
SF bug [#467265] Compile errors on SuSe Linux on IBM/s390.
Tim Peters <tim.peters@gmail.com>
parents:
20140
diff
changeset
|
160 #endif |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
161 /* 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
|
162 for (f = format; *f; f++) { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
163 if (*f == '%') { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
164 const char* p = f; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
165 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
|
166 ; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
167 |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
168 /* skip the 'l' in %ld, since it doesn't change the |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
169 width. although only %d is supported (see |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
170 "expand" section below), others can be easily |
|
21147
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
171 added */ |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
172 if (*f == 'l' && *(f+1) == 'd') |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
173 ++f; |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
174 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
175 switch (*f) { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
176 case 'c': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
177 (void)va_arg(count, int); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
178 /* fall through... */ |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
179 case '%': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
180 n++; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
181 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
182 case 'd': case 'i': case 'x': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
183 (void) va_arg(count, int); |
|
21147
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
184 /* 20 bytes is enough to hold a 64-bit |
|
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
185 integer. Decimal takes the most space. |
|
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
186 This isn't enough for octal. */ |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
187 n += 20; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
188 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
189 case 's': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
190 s = va_arg(count, char*); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
191 n += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
192 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
193 case 'p': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
194 (void) va_arg(count, int); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
195 /* maximum 64-bit pointer representation: |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
196 * 0xffffffffffffffff |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
197 * 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
|
198 * 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
|
199 */ |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
200 n += 19; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
201 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
202 default: |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
203 /* if we stumble upon an unknown |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
204 formatting code, copy the rest of |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
205 the format string to the output |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
206 string. (we cannot just skip the |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
207 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
|
208 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
|
209 n += strlen(p); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
210 goto expand; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
211 } |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
212 } else |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
213 n++; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
214 } |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
215 expand: |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
216 /* 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
|
217 /* 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
|
218 use sprintf directly instead of the slower PyOS_snprintf. */ |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
219 string = PyString_FromStringAndSize(NULL, n); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
220 if (!string) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
221 return NULL; |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
222 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
223 s = PyString_AsString(string); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
224 |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
225 for (f = format; *f; f++) { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
226 if (*f == '%') { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
227 const char* p = f++; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
228 int i, longflag = 0; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
229 /* 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
|
230 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
|
231 n = 0; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
232 while (isdigit(Py_CHARMASK(*f))) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
233 n = (n*10) + *f++ - '0'; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
234 if (*f == '.') { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
235 f++; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
236 n = 0; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
237 while (isdigit(Py_CHARMASK(*f))) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
238 n = (n*10) + *f++ - '0'; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
239 } |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
240 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
|
241 f++; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
242 /* handle the long flag, but only for %ld. others |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
243 can be added when necessary. */ |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
244 if (*f == 'l' && *(f+1) == 'd') { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
245 longflag = 1; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
246 ++f; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
247 } |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
248 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
249 switch (*f) { |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
250 case 'c': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
251 *s++ = va_arg(vargs, int); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
252 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
253 case 'd': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
254 if (longflag) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
255 sprintf(s, "%ld", va_arg(vargs, long)); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
256 else |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
257 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
|
258 s += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
259 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
260 case 'i': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
261 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
|
262 s += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
263 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
264 case 'x': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
265 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
|
266 s += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
267 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
268 case 's': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
269 p = va_arg(vargs, char*); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
270 i = strlen(p); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
271 if (n > 0 && i > n) |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
272 i = n; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
273 memcpy(s, p, i); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
274 s += i; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
275 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
276 case 'p': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
277 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
|
278 /* %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
|
279 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
|
280 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
|
281 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
|
282 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
|
283 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
|
284 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
|
285 } |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
286 s += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
287 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
288 case '%': |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
289 *s++ = '%'; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
290 break; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
291 default: |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
292 strcpy(s, p); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
293 s += strlen(s); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
294 goto end; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
295 } |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
296 } else |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
297 *s++ = *f; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
298 } |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
299 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
300 end: |
|
19489
3e74318a1b76
Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents:
19486
diff
changeset
|
301 _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
|
302 return string; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
303 } |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
304 |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
305 PyObject * |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
306 PyString_FromFormat(const char *format, ...) |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
307 { |
|
19489
3e74318a1b76
Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents:
19486
diff
changeset
|
308 PyObject* ret; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
309 va_list vargs; |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
310 |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
311 #ifdef HAVE_STDARG_PROTOTYPES |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
312 va_start(vargs, format); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
313 #else |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
314 va_start(vargs); |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
315 #endif |
|
19489
3e74318a1b76
Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents:
19486
diff
changeset
|
316 ret = PyString_FromFormatV(format, vargs); |
|
3e74318a1b76
Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents:
19486
diff
changeset
|
317 va_end(vargs); |
|
3e74318a1b76
Two improvements suggested by Greg Stein:
Barry Warsaw <barry@python.org>
parents:
19486
diff
changeset
|
318 return ret; |
|
19475
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
319 } |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
320 |
|
bc63afc701ef
PyString_FromFormat() and PyString_FromFormatV(): Largely ripped from
Barry Warsaw <barry@python.org>
parents:
19343
diff
changeset
|
321 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
322 PyObject *PyString_Decode(const char *s, |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
323 int size, |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
324 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
|
325 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
|
326 { |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
327 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
|
328 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
329 str = PyString_FromStringAndSize(s, size); |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
330 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
|
331 return NULL; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
332 v = PyString_AsDecodedString(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
|
333 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
|
334 return v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
335 } |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
336 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
337 PyObject *PyString_AsDecodedObject(PyObject *str, |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
338 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
|
339 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
|
340 { |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
341 PyObject *v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
342 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
343 if (!PyString_Check(str)) { |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
344 PyErr_BadArgument(); |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
345 goto onError; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
346 } |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
347 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
348 if (encoding == NULL) { |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
349 #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
|
350 encoding = PyUnicode_GetDefaultEncoding(); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
351 #else |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
352 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
|
353 goto onError; |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
354 #endif |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
355 } |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
356 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
357 /* 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
|
358 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
|
359 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
|
360 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
|
361 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
362 return v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
363 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
364 onError: |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
365 return NULL; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
366 } |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
367 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
368 PyObject *PyString_AsDecodedString(PyObject *str, |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
369 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
|
370 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
|
371 { |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
372 PyObject *v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
373 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
374 v = PyString_AsDecodedObject(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
|
375 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
|
376 goto onError; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
377 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
378 #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
|
379 /* 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
|
380 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
|
381 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
|
382 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
|
383 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
|
384 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
|
385 goto onError; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
386 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
387 #endif |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
388 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
|
389 PyErr_Format(PyExc_TypeError, |
|
13561
ea732719b3fd
Fix typo in error message
Andrew M. Kuchling <amk@amk.ca>
parents:
13544
diff
changeset
|
390 "decoder did not return a string object (type=%.400s)", |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
391 v->ob_type->tp_name); |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
392 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
|
393 goto onError; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
394 } |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
395 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
396 return v; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
397 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
398 onError: |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
399 return NULL; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
400 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
401 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
402 PyObject *PyString_Encode(const char *s, |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
403 int size, |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
404 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
|
405 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
|
406 { |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
407 PyObject *v, *str; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
408 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
409 str = PyString_FromStringAndSize(s, size); |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
410 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
|
411 return NULL; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
412 v = PyString_AsEncodedString(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
|
413 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
|
414 return v; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
415 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
416 |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
417 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
|
418 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
|
419 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
|
420 { |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
421 PyObject *v; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
422 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
423 if (!PyString_Check(str)) { |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
424 PyErr_BadArgument(); |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
425 goto onError; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
426 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
427 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
428 if (encoding == NULL) { |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
429 #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
|
430 encoding = PyUnicode_GetDefaultEncoding(); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
431 #else |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
432 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
|
433 goto onError; |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
434 #endif |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
435 } |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
436 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
437 /* 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
|
438 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
|
439 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
|
440 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
|
441 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
442 return v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
443 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
444 onError: |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
445 return NULL; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
446 } |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
447 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
448 PyObject *PyString_AsEncodedString(PyObject *str, |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
449 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
|
450 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
|
451 { |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
452 PyObject *v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
453 |
|
18086
592109f2a6fd
Fix for bug #432384: Recursion in PyString_AsEncodedString?
Marc-André Lemburg <mal@egenix.com>
parents:
17964
diff
changeset
|
454 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
|
455 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
|
456 goto onError; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
457 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
458 #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
|
459 /* 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
|
460 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
|
461 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
|
462 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
|
463 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
|
464 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
|
465 goto onError; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
466 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
467 #endif |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
468 if (!PyString_Check(v)) { |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
469 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
|
470 "encoder did not return a string object (type=%.400s)", |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
471 v->ob_type->tp_name); |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
472 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
|
473 goto onError; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
474 } |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
475 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
476 return v; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
477 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
478 onError: |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
479 return NULL; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
480 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
481 |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
482 static void |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
483 string_dealloc(PyObject *op) |
| 750 | 484 { |
|
20277
2983b54256c7
Enable GC for new-style instances. This touches lots of files, since
Guido van Rossum <guido@python.org>
parents:
20221
diff
changeset
|
485 op->ob_type->tp_free(op); |
| 750 | 486 } |
| 487 | |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
488 static int |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
489 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
|
490 { |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
491 char *s; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
492 int len; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
493 if (PyString_AsStringAndSize(op, &s, &len)) |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
494 return -1; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
495 return len; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
496 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
497 |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
498 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
|
499 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
|
500 { |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
501 char *s; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
502 int len; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
503 if (PyString_AsStringAndSize(op, &s, &len)) |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
504 return NULL; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
505 return s; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
506 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
507 |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1908
diff
changeset
|
508 int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
509 PyString_Size(register PyObject *op) |
| 6 | 510 { |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
511 if (!PyString_Check(op)) |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
512 return string_getsize(op); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
513 return ((PyStringObject *)op) -> ob_size; |
| 6 | 514 } |
| 515 | |
| 516 /*const*/ char * | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
517 PyString_AsString(register PyObject *op) |
| 6 | 518 { |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
519 if (!PyString_Check(op)) |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
520 return string_getbuffer(op); |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
521 return ((PyStringObject *)op) -> ob_sval; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
522 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
523 |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
524 int |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
525 PyString_AsStringAndSize(register PyObject *obj, |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
526 register char **s, |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
527 register int *len) |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
528 { |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
529 if (s == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
530 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
|
531 return -1; |
| 6 | 532 } |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
533 |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
534 if (!PyString_Check(obj)) { |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
535 #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
|
536 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
|
537 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
|
538 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
|
539 return -1; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
540 } |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
541 else |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
542 #endif |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
543 { |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
544 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
|
545 "expected string or Unicode object, " |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
546 "%.200s found", obj->ob_type->tp_name); |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
547 return -1; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
548 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
549 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
550 |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
551 *s = PyString_AS_STRING(obj); |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
552 if (len != NULL) |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
553 *len = PyString_GET_SIZE(obj); |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
554 else if ((int)strlen(*s) != PyString_GET_SIZE(obj)) { |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
555 PyErr_SetString(PyExc_TypeError, |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
556 "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
|
557 return -1; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
558 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
559 return 0; |
| 6 | 560 } |
| 561 | |
| 562 /* Methods */ | |
| 563 | |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
564 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
565 string_print(PyStringObject *op, FILE *fp, int flags) |
| 6 | 566 { |
| 567 int i; | |
| 568 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
|
569 int quote; |
|
20464
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
570 |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
571 /* XXX Ought to check for interrupts when writing long strings */ |
|
20464
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
572 if (! PyString_CheckExact(op)) { |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
573 int ret; |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
574 /* A str subclass may have its own __str__ method. */ |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
575 op = (PyStringObject *) PyObject_Str((PyObject *)op); |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
576 if (op == NULL) |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
577 return -1; |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
578 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
|
579 Py_DECREF(op); |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
580 return ret; |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
581 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
582 if (flags & Py_PRINT_RAW) { |
| 6 | 583 fwrite(op->ob_sval, 1, (int) op->ob_size, fp); |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
584 return 0; |
| 6 | 585 } |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
586 |
|
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
|
587 /* 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
|
588 quote = '\''; |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
589 if (memchr(op->ob_sval, '\'', op->ob_size) && |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
590 !memchr(op->ob_sval, '"', op->ob_size)) |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
591 quote = '"'; |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
592 |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
593 fputc(quote, fp); |
| 6 | 594 for (i = 0; i < op->ob_size; i++) { |
| 595 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
|
596 if (c == quote || c == '\\') |
| 6 | 597 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
|
598 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
|
599 fprintf(fp, "\\t"); |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
600 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
|
601 fprintf(fp, "\\n"); |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
602 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
|
603 fprintf(fp, "\\r"); |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
604 else if (c < ' ' || c >= 0x7f) |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
605 fprintf(fp, "\\x%02x", c & 0xff); |
| 6 | 606 else |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
607 fputc(c, fp); |
| 6 | 608 } |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
609 fputc(quote, fp); |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
610 return 0; |
| 6 | 611 } |
| 612 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
613 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
614 string_repr(register PyStringObject *op) |
| 6 | 615 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
616 size_t newsize = 2 + 4 * op->ob_size * sizeof(char); |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
617 PyObject *v; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
618 if (newsize > INT_MAX) { |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
619 PyErr_SetString(PyExc_OverflowError, |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
620 "string is too large to make repr"); |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
621 } |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
622 v = PyString_FromStringAndSize((char *)NULL, newsize); |
| 6 | 623 if (v == NULL) { |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
624 return NULL; |
| 6 | 625 } |
| 626 else { | |
| 627 register int i; | |
| 628 register char c; | |
| 629 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
|
630 int quote; |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
631 |
|
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
|
632 /* 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
|
633 quote = '\''; |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
634 if (memchr(op->ob_sval, '\'', op->ob_size) && |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
635 !memchr(op->ob_sval, '"', op->ob_size)) |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
636 quote = '"'; |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
637 |
|
21147
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
638 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
|
639 *p++ = quote; |
| 6 | 640 for (i = 0; i < op->ob_size; i++) { |
|
21147
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
641 /* 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
|
642 and a closing quote. */ |
|
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
643 assert(newsize - (p - PyString_AS_STRING(v)) >= 5); |
| 6 | 644 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
|
645 if (c == quote || c == '\\') |
| 6 | 646 *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
|
647 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
|
648 *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
|
649 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
|
650 *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
|
651 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
|
652 *p++ = '\\', *p++ = 'r'; |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
653 else if (c < ' ' || c >= 0x7f) { |
|
21147
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
654 /* For performance, we don't want to call |
|
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
655 PyOS_snprintf here (extra layers of |
|
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
656 function call). */ |
|
16226
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
657 sprintf(p, "\\x%02x", c & 0xff); |
|
c7c404b35f33
Show '\011', '\012', and '\015' as '\t', '\n', '\r' in strings.
Ka-Ping Yee <ping@zesty.ca>
parents:
16014
diff
changeset
|
658 p += 4; |
| 6 | 659 } |
| 660 else | |
| 661 *p++ = c; | |
| 662 } | |
|
21147
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
663 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
|
664 *p++ = quote; |
| 6 | 665 *p = '\0'; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
666 _PyString_Resize( |
|
21147
90a0d80033b4
PyString_FromFormatV, string_repr: document why these use sprintf
Tim Peters <tim.peters@gmail.com>
parents:
21138
diff
changeset
|
667 &v, (int) (p - PyString_AS_STRING(v))); |
| 27 | 668 return v; |
| 6 | 669 } |
| 670 } | |
| 671 | |
|
17645
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
672 static PyObject * |
|
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
673 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
|
674 { |
|
20464
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
675 assert(PyString_Check(s)); |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
676 if (PyString_CheckExact(s)) { |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
677 Py_INCREF(s); |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
678 return s; |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
679 } |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
680 else { |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
681 /* Subtype -- return genuine string with the same value. */ |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
682 PyStringObject *t = (PyStringObject *) s; |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
683 return PyString_FromStringAndSize(t->ob_sval, t->ob_size); |
|
98df0115ecf9
SF bug [#468061] __str__ ignored in str subclass.
Tim Peters <tim.peters@gmail.com>
parents:
20415
diff
changeset
|
684 } |
|
17645
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
685 } |
|
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
686 |
| 6 | 687 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
688 string_length(PyStringObject *a) |
| 6 | 689 { |
| 690 return a->ob_size; | |
| 691 } | |
| 692 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
693 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
694 string_concat(register PyStringObject *a, register PyObject *bb) |
| 6 | 695 { |
| 696 register unsigned int size; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
697 register PyStringObject *op; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
698 if (!PyString_Check(bb)) { |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
699 #ifdef Py_USING_UNICODE |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
700 if (PyUnicode_Check(bb)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
701 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
|
702 #endif |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
703 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
|
704 "cannot concatenate 'str' and '%.200s' objects", |
|
12816
4272b368bf69
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12615
diff
changeset
|
705 bb->ob_type->tp_name); |
| 6 | 706 return NULL; |
| 707 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
708 #define b ((PyStringObject *)bb) |
| 6 | 709 /* Optimize cases with empty left or right operand */ |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
710 if ((a->ob_size == 0 || b->ob_size == 0) && |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
711 PyString_CheckExact(a) && PyString_CheckExact(b)) { |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
712 if (a->ob_size == 0) { |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
713 Py_INCREF(bb); |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
714 return bb; |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
715 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
716 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
717 return (PyObject *)a; |
| 6 | 718 } |
| 719 size = a->ob_size + b->ob_size; | |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
720 /* PyObject_NewVar is inlined */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
721 op = (PyStringObject *) |
|
22977
4ba091cc70b8
Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22785
diff
changeset
|
722 PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char)); |
| 27 | 723 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
724 return PyErr_NoMemory(); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
725 PyObject_INIT_VAR(op, &PyString_Type, size); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
726 op->ob_shash = -1; |
| 5005 | 727 op->ob_sinterned = NULL; |
| 27 | 728 memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size); |
| 729 memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size); | |
| 730 op->ob_sval[size] = '\0'; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
731 return (PyObject *) op; |
| 6 | 732 #undef b |
| 733 } | |
| 734 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
735 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
736 string_repeat(register PyStringObject *a, register int n) |
| 6 | 737 { |
| 738 register int i; | |
|
5288
0f1711b7d367
Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents:
5098
diff
changeset
|
739 register int size; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
740 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
|
741 size_t nbytes; |
| 6 | 742 if (n < 0) |
| 743 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
|
744 /* 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
|
745 * 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
|
746 */ |
| 6 | 747 size = a->ob_size * n; |
|
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
|
748 if (n && size / n != a->ob_size) { |
|
7728b0ea9344
Fix for bug 113934. string*n and unicode*n did no overflow checking at
Tim Peters <tim.peters@gmail.com>
parents:
14375
diff
changeset
|
749 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
|
750 "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
|
751 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
|
752 } |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
753 if (size == a->ob_size && PyString_CheckExact(a)) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
754 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
755 return (PyObject *)a; |
| 6 | 756 } |
|
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
|
757 nbytes = size * sizeof(char); |
|
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
|
758 if (nbytes / sizeof(char) != (size_t)size || |
|
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
|
759 nbytes + sizeof(PyStringObject) <= nbytes) { |
|
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
|
760 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
|
761 "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
|
762 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
|
763 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
764 op = (PyStringObject *) |
|
22977
4ba091cc70b8
Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22785
diff
changeset
|
765 PyObject_MALLOC(sizeof(PyStringObject) + nbytes); |
| 27 | 766 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
767 return PyErr_NoMemory(); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
768 PyObject_INIT_VAR(op, &PyString_Type, size); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
769 op->ob_shash = -1; |
| 5005 | 770 op->ob_sinterned = NULL; |
| 27 | 771 for (i = 0; i < size; i += a->ob_size) |
| 772 memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size); | |
| 773 op->ob_sval[size] = '\0'; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
774 return (PyObject *) op; |
| 6 | 775 } |
| 776 | |
| 777 /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ | |
| 778 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
779 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
780 string_slice(register PyStringObject *a, register int i, register int j) |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
781 /* j -- may be negative! */ |
| 6 | 782 { |
| 783 if (i < 0) | |
| 784 i = 0; | |
| 785 if (j < 0) | |
| 786 j = 0; /* Avoid signed/unsigned bug in next line */ | |
| 787 if (j > a->ob_size) | |
| 788 j = a->ob_size; | |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
789 if (i == 0 && j == a->ob_size && PyString_CheckExact(a)) { |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
790 /* It's the same as a */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
791 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
792 return (PyObject *)a; |
| 6 | 793 } |
| 794 if (j < i) | |
| 795 j = i; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
796 return PyString_FromStringAndSize(a->ob_sval + i, (int) (j-i)); |
| 6 | 797 } |
| 798 | |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
799 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
800 string_contains(PyObject *a, PyObject *el) |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
801 { |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
802 register char *s, *end; |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
803 register char c; |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
804 #ifdef Py_USING_UNICODE |
| 12328 | 805 if (PyUnicode_Check(el)) |
|
11998
f94d10b6d0f4
Fix typo in replace() detected by Mark Hammond and fixed by Marc-Andre.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
806 return PyUnicode_Contains(a, el); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
807 #endif |
| 12328 | 808 if (!PyString_Check(el) || PyString_Size(el) != 1) { |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
809 PyErr_SetString(PyExc_TypeError, |
|
12902
98eb3059ae76
Patch from Michael Hudson: improve unclear error message
Andrew M. Kuchling <amk@amk.ca>
parents:
12816
diff
changeset
|
810 "'in <string>' requires character as left operand"); |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
811 return -1; |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
812 } |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
813 c = PyString_AsString(el)[0]; |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
814 s = PyString_AsString(a); |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
815 end = s + PyString_Size(a); |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
816 while (s < end) { |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
817 if (c == *s++) |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
818 return 1; |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
819 } |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
820 return 0; |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
821 } |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
822 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
823 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
824 string_item(PyStringObject *a, register int i) |
| 6 | 825 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
826 PyObject *v; |
|
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
|
827 char *pchar; |
| 6 | 828 if (i < 0 || i >= a->ob_size) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
829 PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6 | 830 return NULL; |
| 831 } | |
|
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
|
832 pchar = a->ob_sval + i; |
|
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
|
833 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
|
834 if (v == NULL) |
|
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
|
835 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
|
836 else { |
|
2a7cfc172a68
Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents:
17711
diff
changeset
|
837 #ifdef COUNT_ALLOCS |
|
2a7cfc172a68
Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents:
17711
diff
changeset
|
838 one_strings++; |
|
2a7cfc172a68
Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents:
17711
diff
changeset
|
839 #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
|
840 Py_INCREF(v); |
|
17712
2a7cfc172a68
Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents:
17711
diff
changeset
|
841 } |
|
180
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
842 return v; |
| 6 | 843 } |
| 844 | |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
845 static PyObject* |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
846 string_richcompare(PyStringObject *a, PyStringObject *b, int op) |
| 6 | 847 { |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
848 int c; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
849 int len_a, len_b; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
850 int min_len; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
851 PyObject *result; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
852 |
|
20140
225f17ec8932
Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents:
20057
diff
changeset
|
853 /* Make sure both arguments are strings. */ |
|
225f17ec8932
Merge branch changes (coercion, rich comparisons) into trunk.
Guido van Rossum <guido@python.org>
parents:
20057
diff
changeset
|
854 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
|
855 result = Py_NotImplemented; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
856 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
857 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
858 if (a == b) { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
859 switch (op) { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
860 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
|
861 result = Py_True; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
862 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
863 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
|
864 result = Py_False; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
865 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
866 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
867 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
868 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
|
869 /* 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
|
870 much time, since Py_NE is rarely used. */ |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
871 if (a->ob_size == b->ob_size |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
872 && (a->ob_sval[0] == b->ob_sval[0] |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
873 && memcmp(a->ob_sval, b->ob_sval, |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
874 a->ob_size) == 0)) { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
875 result = Py_True; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
876 } else { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
877 result = Py_False; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
878 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
879 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
880 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
881 len_a = a->ob_size; len_b = b->ob_size; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
882 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
|
883 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
|
884 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
|
885 if (c==0) |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
886 c = memcmp(a->ob_sval, b->ob_sval, min_len); |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
887 }else |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
888 c = 0; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
889 if (c == 0) |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
890 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
|
891 switch (op) { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
892 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
|
893 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
|
894 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
|
895 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
|
896 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
|
897 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
|
898 default: |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
899 result = Py_NotImplemented; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
900 goto out; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
901 } |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
902 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
|
903 out: |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
904 Py_INCREF(result); |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
905 return result; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
906 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
907 |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
908 int |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
909 _PyString_Eq(PyObject *o1, PyObject *o2) |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
910 { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
911 PyStringObject *a, *b; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
912 a = (PyStringObject*)o1; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
913 b = (PyStringObject*)o2; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
914 return a->ob_size == b->ob_size |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
915 && *a->ob_sval == *b->ob_sval |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
916 && memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0; |
| 6 | 917 } |
| 918 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
919 static long |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
920 string_hash(PyStringObject *a) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
921 { |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
922 register int len; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
923 register unsigned char *p; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
924 register long x; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
925 |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
926 if (a->ob_shash != -1) |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
927 return a->ob_shash; |
|
5098
a44586bd9fab
Slight tweak: in string_hash(), if the hash hasn't been computed yet,
Guido van Rossum <guido@python.org>
parents:
5057
diff
changeset
|
928 if (a->ob_sinterned != NULL) |
|
a44586bd9fab
Slight tweak: in string_hash(), if the hash hasn't been computed yet,
Guido van Rossum <guido@python.org>
parents:
5057
diff
changeset
|
929 return (a->ob_shash = |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
930 ((PyStringObject *)(a->ob_sinterned))->ob_shash); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
931 len = a->ob_size; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
932 p = (unsigned char *) a->ob_sval; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
933 x = *p << 7; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
934 while (--len >= 0) |
|
4283
839f72610ae1
Multiply by 1000003 instead of 3 in string hach
Guido van Rossum <guido@python.org>
parents:
3586
diff
changeset
|
935 x = (1000003*x) ^ *p++; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
936 x ^= a->ob_size; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
937 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
938 x = -2; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
939 a->ob_shash = x; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
940 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
941 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
942 |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
943 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
944 string_buffer_getreadbuf(PyStringObject *self, int index, const void **ptr) |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
945 { |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
946 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
|
947 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
|
948 "accessing non-existent string segment"); |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
949 return -1; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
950 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
951 *ptr = (void *)self->ob_sval; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
952 return self->ob_size; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
953 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
954 |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
955 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
956 string_buffer_getwritebuf(PyStringObject *self, int index, const void **ptr) |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
957 { |
|
6286
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
958 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
|
959 "Cannot use string as modifiable buffer"); |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
960 return -1; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
961 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
962 |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
963 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
964 string_buffer_getsegcount(PyStringObject *self, int *lenp) |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
965 { |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
966 if ( lenp ) |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
967 *lenp = self->ob_size; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
968 return 1; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
969 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
970 |
|
9305
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
971 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
972 string_buffer_getcharbuf(PyStringObject *self, int 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
|
973 { |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
974 if ( index != 0 ) { |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
975 PyErr_SetString(PyExc_SystemError, |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
976 "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
|
977 return -1; |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
978 } |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
979 *ptr = self->ob_sval; |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
980 return self->ob_size; |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
981 } |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
982 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
983 static PySequenceMethods string_as_sequence = { |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
984 (inquiry)string_length, /*sq_length*/ |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
985 (binaryfunc)string_concat, /*sq_concat*/ |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
986 (intargfunc)string_repeat, /*sq_repeat*/ |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
987 (intargfunc)string_item, /*sq_item*/ |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
988 (intintargfunc)string_slice, /*sq_slice*/ |
|
259
6321d3dc57e0
Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents:
180
diff
changeset
|
989 0, /*sq_ass_item*/ |
|
6321d3dc57e0
Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents:
180
diff
changeset
|
990 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
|
991 (objobjproc)string_contains /*sq_contains*/ |
| 6 | 992 }; |
| 993 | |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
994 static PyBufferProcs string_as_buffer = { |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
995 (getreadbufferproc)string_buffer_getreadbuf, |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
996 (getwritebufferproc)string_buffer_getwritebuf, |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
997 (getsegcountproc)string_buffer_getsegcount, |
|
9305
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
998 (getcharbufferproc)string_buffer_getcharbuf, |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
999 }; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
1000 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1001 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1002 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1003 #define LEFTSTRIP 0 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1004 #define RIGHTSTRIP 1 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1005 #define BOTHSTRIP 2 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1006 |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1007 /* Arrays indexed by above */ |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1008 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
|
1009 |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1010 #define STRIPNAME(i) (stripformat[i]+3) |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1011 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1012 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1013 static PyObject * |
|
13460
e6d17f15ae6a
Somebody started playing with const, so of course the outcome
Tim Peters <tim.peters@gmail.com>
parents:
13459
diff
changeset
|
1014 split_whitespace(const char *s, int len, int maxsplit) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1015 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1016 int i, j, err; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1017 PyObject* item; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1018 PyObject *list = PyList_New(0); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1019 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1020 if (list == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1021 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1022 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1023 for (i = j = 0; i < len; ) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1024 while (i < len && isspace(Py_CHARMASK(s[i]))) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1025 i++; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1026 j = i; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1027 while (i < len && !isspace(Py_CHARMASK(s[i]))) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1028 i++; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1029 if (j < i) { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1030 if (maxsplit-- <= 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1031 break; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1032 item = PyString_FromStringAndSize(s+j, (int)(i-j)); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1033 if (item == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1034 goto finally; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1035 err = PyList_Append(list, item); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1036 Py_DECREF(item); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1037 if (err < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1038 goto finally; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1039 while (i < len && isspace(Py_CHARMASK(s[i]))) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1040 i++; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1041 j = i; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1042 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1043 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1044 if (j < len) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1045 item = PyString_FromStringAndSize(s+j, (int)(len - j)); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1046 if (item == NULL) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1047 goto finally; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1048 err = PyList_Append(list, item); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1049 Py_DECREF(item); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1050 if (err < 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1051 goto finally; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1052 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1053 return list; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1054 finally: |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1055 Py_DECREF(list); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1056 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1057 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1058 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1059 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1060 static char split__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1061 "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
|
1062 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1063 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
|
1064 delimiter string. If maxsplit is given, at most maxsplit\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1065 splits are done. If sep is not specified, any whitespace string\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1066 is a separator."; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1067 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1068 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1069 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
|
1070 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1071 int len = PyString_GET_SIZE(self), n, i, j, err; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1072 int maxsplit = -1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1073 const char *s = PyString_AS_STRING(self), *sub; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1074 PyObject *list, *item, *subobj = Py_None; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1075 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1076 if (!PyArg_ParseTuple(args, "|Oi:split", &subobj, &maxsplit)) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1077 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1078 if (maxsplit < 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1079 maxsplit = INT_MAX; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1080 if (subobj == Py_None) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1081 return split_whitespace(s, len, maxsplit); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1082 if (PyString_Check(subobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1083 sub = PyString_AS_STRING(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1084 n = PyString_GET_SIZE(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1085 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1086 #ifdef Py_USING_UNICODE |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1087 else if (PyUnicode_Check(subobj)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1088 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
|
1089 #endif |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1090 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
|
1091 return NULL; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1092 if (n == 0) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1093 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
|
1094 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1095 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1096 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1097 list = PyList_New(0); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1098 if (list == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1099 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1100 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1101 i = j = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1102 while (i+n <= len) { |
|
12966
447efb64c72d
Fredrik Lundh <effbot@telia.com>:
Fred Drake <fdrake@acm.org>
parents:
12924
diff
changeset
|
1103 if (s[i] == sub[0] && memcmp(s+i, sub, n) == 0) { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1104 if (maxsplit-- <= 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1105 break; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1106 item = PyString_FromStringAndSize(s+j, (int)(i-j)); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1107 if (item == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1108 goto fail; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1109 err = PyList_Append(list, item); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1110 Py_DECREF(item); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1111 if (err < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1112 goto fail; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1113 i = j = i + n; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1114 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1115 else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1116 i++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1117 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1118 item = PyString_FromStringAndSize(s+j, (int)(len-j)); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1119 if (item == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1120 goto fail; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1121 err = PyList_Append(list, item); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1122 Py_DECREF(item); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1123 if (err < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1124 goto fail; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1125 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1126 return list; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1127 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1128 fail: |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1129 Py_DECREF(list); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1130 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1131 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1132 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1133 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1134 static char join__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1135 "S.join(sequence) -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1136 \n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1137 Return a string which is the concatenation of the strings in the\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1138 sequence. 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
|
1139 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1140 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
1141 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
|
1142 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1143 char *sep = PyString_AS_STRING(self); |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1144 const int 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
|
1145 PyObject *res = NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1146 char *p; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1147 int seqlen = 0; |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1148 size_t sz = 0; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1149 int 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
|
1150 PyObject *seq, *item; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1151 |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1152 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
|
1153 if (seq == NULL) { |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1154 if (PyErr_ExceptionMatches(PyExc_TypeError)) |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1155 PyErr_Format(PyExc_TypeError, |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1156 "sequence expected, %.80s found", |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1157 orig->ob_type->tp_name); |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1158 return NULL; |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1159 } |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1160 |
|
13576
61e4c29d53d8
replace PyXXX_Length calls with PyXXX_Size calls
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13561
diff
changeset
|
1161 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
|
1162 if (seqlen == 0) { |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1163 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1164 return PyString_FromString(""); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1165 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1166 if (seqlen == 1) { |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1167 item = PySequence_Fast_GET_ITEM(seq, 0); |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1168 if (!PyString_Check(item) && !PyUnicode_Check(item)) { |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1169 PyErr_Format(PyExc_TypeError, |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1170 "sequence item 0: expected string," |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1171 " %.80s found", |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1172 item->ob_type->tp_name); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1173 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1174 return NULL; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1175 } |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1176 Py_INCREF(item); |
|
13528
e848d6ed3eb9
string_join(): Some cleaning up of reference counting. In the
Barry Warsaw <barry@python.org>
parents:
13526
diff
changeset
|
1177 Py_DECREF(seq); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1178 return item; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1179 } |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1180 |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1181 /* There are at least two things to join. Do a pre-pass to figure out |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1182 * the total amount of space we'll need (sz), see whether any argument |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1183 * is absurd, and defer to the Unicode join if appropriate. |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1184 */ |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1185 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
|
1186 const size_t old_sz = sz; |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1187 item = PySequence_Fast_GET_ITEM(seq, i); |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1188 if (!PyString_Check(item)){ |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1189 #ifdef Py_USING_UNICODE |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1190 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
|
1191 /* 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
|
1192 * 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
|
1193 * 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
|
1194 * 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
|
1195 */ |
|
9e093bf8f6a9
Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents:
17655
diff
changeset
|
1196 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
|
1197 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
|
1198 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
|
1199 return result; |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1200 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1201 #endif |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1202 PyErr_Format(PyExc_TypeError, |
|
13544
3cf4f5aee00e
small updates to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13528
diff
changeset
|
1203 "sequence item %i: expected string," |
|
3cf4f5aee00e
small updates to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13528
diff
changeset
|
1204 " %.80s found", |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1205 i, item->ob_type->tp_name); |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1206 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1207 return NULL; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1208 } |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1209 sz += PyString_GET_SIZE(item); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1210 if (i != 0) |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1211 sz += seplen; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1212 if (sz < old_sz || sz > INT_MAX) { |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1213 PyErr_SetString(PyExc_OverflowError, |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1214 "join() is too long for a Python string"); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1215 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1216 return NULL; |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1217 } |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1218 } |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1219 |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1220 /* Allocate result space. */ |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1221 res = PyString_FromStringAndSize((char*)NULL, (int)sz); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1222 if (res == NULL) { |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1223 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1224 return NULL; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1225 } |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1226 |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1227 /* Catenate everything. */ |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1228 p = PyString_AS_STRING(res); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1229 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
|
1230 size_t n; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1231 item = PySequence_Fast_GET_ITEM(seq, i); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1232 n = PyString_GET_SIZE(item); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1233 memcpy(p, PyString_AS_STRING(item), n); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1234 p += n; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1235 if (i < seqlen - 1) { |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1236 memcpy(p, sep, seplen); |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1237 p += seplen; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1238 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1239 } |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1240 |
|
13526
3686d12362b8
fix two refcount bugs in new string_join implementation:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13524
diff
changeset
|
1241 Py_DECREF(seq); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1242 return res; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1243 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1244 |
|
18130
8eea2efb34cd
Reformat decl of new _PyString_Join. Add NEWS blurb about repr() speedup.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
1245 PyObject * |
|
8eea2efb34cd
Reformat decl of new _PyString_Join. Add NEWS blurb about repr() speedup.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
1246 _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
|
1247 { |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1248 assert(sep != NULL && PyString_Check(sep)); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1249 assert(x != NULL); |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
1250 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
|
1251 } |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1252 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1253 static long |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1254 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
|
1255 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1256 const char *s = PyString_AS_STRING(self), *sub; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1257 int len = PyString_GET_SIZE(self); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1258 int n, i = 0, last = INT_MAX; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1259 PyObject *subobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1260 |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
1261 if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", |
| 12615 | 1262 &subobj, _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last)) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1263 return -2; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1264 if (PyString_Check(subobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1265 sub = PyString_AS_STRING(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1266 n = PyString_GET_SIZE(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1267 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1268 #ifdef Py_USING_UNICODE |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1269 else if (PyUnicode_Check(subobj)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1270 return PyUnicode_Find((PyObject *)self, subobj, i, last, 1); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1271 #endif |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1272 else if (PyObject_AsCharBuffer(subobj, &sub, &n)) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1273 return -2; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1274 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1275 if (last > len) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1276 last = len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1277 if (last < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1278 last += len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1279 if (last < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1280 last = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1281 if (i < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1282 i += len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1283 if (i < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1284 i = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1285 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1286 if (dir > 0) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1287 if (n == 0 && i <= last) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1288 return (long)i; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1289 last -= n; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1290 for (; i <= last; ++i) |
|
12966
447efb64c72d
Fredrik Lundh <effbot@telia.com>:
Fred Drake <fdrake@acm.org>
parents:
12924
diff
changeset
|
1291 if (s[i] == sub[0] && memcmp(&s[i], sub, n) == 0) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1292 return (long)i; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1293 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1294 else { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1295 int j; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
1296 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1297 if (n == 0 && i <= last) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1298 return (long)last; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1299 for (j = last-n; j >= i; --j) |
|
12966
447efb64c72d
Fredrik Lundh <effbot@telia.com>:
Fred Drake <fdrake@acm.org>
parents:
12924
diff
changeset
|
1300 if (s[j] == sub[0] && memcmp(&s[j], sub, n) == 0) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1301 return (long)j; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1302 } |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
1303 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1304 return -1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1305 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1306 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1307 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1308 static char find__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1309 "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
|
1310 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1311 Return the lowest index in S where substring sub is found,\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1312 such that sub is contained within s[start,end]. Optional\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1313 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
|
1314 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1315 Return -1 on failure."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1316 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1317 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1318 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
|
1319 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1320 long 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
|
1321 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1322 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1323 return PyInt_FromLong(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1324 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1325 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1326 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1327 static char index__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1328 "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
|
1329 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1330 Like S.find() but raise ValueError when the substring is not found."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1331 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1332 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1333 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
|
1334 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1335 long 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
|
1336 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1337 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1338 if (result == -1) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1339 PyErr_SetString(PyExc_ValueError, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1340 "substring not found in string.index"); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1341 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1342 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1343 return PyInt_FromLong(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1344 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1345 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1346 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1347 static char rfind__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1348 "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
|
1349 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1350 Return the highest index in S where substring sub is found,\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1351 such that sub is contained within s[start,end]. Optional\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1352 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
|
1353 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1354 Return -1 on failure."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1355 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1356 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1357 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
|
1358 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1359 long 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
|
1360 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1361 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1362 return PyInt_FromLong(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1363 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1364 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1365 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1366 static char rindex__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1367 "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
|
1368 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1369 Like S.rfind() but raise ValueError when the substring is not found."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1370 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1371 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1372 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
|
1373 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1374 long 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
|
1375 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1376 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1377 if (result == -1) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1378 PyErr_SetString(PyExc_ValueError, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1379 "substring not found in string.rindex"); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1380 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1381 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1382 return PyInt_FromLong(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1383 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1384 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1385 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1386 static PyObject * |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1387 do_xstrip(PyStringObject *self, int striptype, PyObject *sepobj) |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1388 { |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1389 char *s = PyString_AS_STRING(self); |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1390 int len = PyString_GET_SIZE(self); |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1391 char *sep = PyString_AS_STRING(sepobj); |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1392 int seplen = PyString_GET_SIZE(sepobj); |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1393 int i, j; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1394 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1395 i = 0; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1396 if (striptype != RIGHTSTRIP) { |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1397 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
|
1398 i++; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1399 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1400 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1401 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1402 j = len; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1403 if (striptype != LEFTSTRIP) { |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1404 do { |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1405 j--; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1406 } 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
|
1407 j++; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1408 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1409 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1410 if (i == 0 && j == len && PyString_CheckExact(self)) { |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1411 Py_INCREF(self); |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1412 return (PyObject*)self; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1413 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1414 else |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1415 return PyString_FromStringAndSize(s+i, j-i); |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1416 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1417 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1418 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1419 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
1420 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
|
1421 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1422 char *s = PyString_AS_STRING(self); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1423 int len = PyString_GET_SIZE(self), i, j; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1424 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1425 i = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1426 if (striptype != RIGHTSTRIP) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1427 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
|
1428 i++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1429 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1430 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1431 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1432 j = len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1433 if (striptype != LEFTSTRIP) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1434 do { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1435 j--; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1436 } 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
|
1437 j++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1438 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1439 |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
1440 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
|
1441 Py_INCREF(self); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1442 return (PyObject*)self; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1443 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1444 else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1445 return PyString_FromStringAndSize(s+i, j-i); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1446 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1447 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1448 |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1449 static PyObject * |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1450 do_argstrip(PyStringObject *self, int striptype, PyObject *args) |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1451 { |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1452 PyObject *sep = NULL; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1453 |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1454 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
|
1455 return NULL; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1456 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1457 if (sep != NULL && sep != Py_None) { |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1458 if (PyString_Check(sep)) |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1459 return do_xstrip(self, striptype, sep); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1460 else if (PyUnicode_Check(sep)) { |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1461 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
|
1462 PyObject *res; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1463 if (uniself==NULL) |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1464 return NULL; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1465 res = _PyUnicode_XStrip((PyUnicodeObject *)uniself, |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1466 striptype, sep); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1467 Py_DECREF(uniself); |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1468 return res; |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1469 } |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1470 else { |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1471 PyErr_Format(PyExc_TypeError, |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1472 "%s arg must be None, str or unicode", |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1473 STRIPNAME(striptype)); |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1474 return NULL; |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1475 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1476 return do_xstrip(self, striptype, sep); |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1477 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1478 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1479 return do_strip(self, striptype); |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1480 } |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1481 |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1482 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1483 static char strip__doc__[] = |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1484 "S.strip([sep]) -> string or unicode\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1485 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1486 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
|
1487 whitespace removed.\n\ |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1488 If sep is given and not None, remove characters in sep instead.\n\ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1489 If sep 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
|
1490 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1491 static PyObject * |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1492 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
|
1493 { |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1494 if (PyTuple_GET_SIZE(args) == 0) |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1495 return do_strip(self, BOTHSTRIP); /* Common case */ |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1496 else |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1497 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
|
1498 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1499 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1500 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1501 static char lstrip__doc__[] = |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1502 "S.lstrip([sep]) -> string or unicode\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1503 \n\ |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1504 Return a copy of the string S with leading whitespace removed.\n\ |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1505 If sep is given and not None, remove characters in sep instead.\n\ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1506 If sep 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
|
1507 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1508 static PyObject * |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1509 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
|
1510 { |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1511 if (PyTuple_GET_SIZE(args) == 0) |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1512 return do_strip(self, LEFTSTRIP); /* Common case */ |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1513 else |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1514 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
|
1515 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1516 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1517 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1518 static char rstrip__doc__[] = |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1519 "S.rstrip([sep]) -> string or unicode\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1520 \n\ |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1521 Return a copy of the string S with trailing whitespace removed.\n\ |
|
23225
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1522 If sep is given and not None, remove characters in sep instead.\n\ |
|
e639e34ef901
Apply patch diff.txt from SF feature request
Walter Dörwald <walter@livinglogic.de>
parents:
23051
diff
changeset
|
1523 If sep 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
|
1524 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1525 static PyObject * |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1526 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
|
1527 { |
|
23015
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1528 if (PyTuple_GET_SIZE(args) == 0) |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1529 return do_strip(self, RIGHTSTRIP); /* Common case */ |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1530 else |
|
202ae5b75cca
Partially implement SF feature request 444708.
Guido van Rossum <guido@python.org>
parents:
22977
diff
changeset
|
1531 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
|
1532 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1533 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1534 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1535 static char lower__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1536 "S.lower() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1537 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1538 Return a copy of the string S converted to lowercase."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1539 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1540 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
1541 string_lower(PyStringObject *self) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1542 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1543 char *s = PyString_AS_STRING(self), *s_new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1544 int i, n = PyString_GET_SIZE(self); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1545 PyObject *new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1546 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1547 new = PyString_FromStringAndSize(NULL, n); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1548 if (new == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1549 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1550 s_new = PyString_AsString(new); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1551 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
|
1552 int c = Py_CHARMASK(*s++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1553 if (isupper(c)) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1554 *s_new = tolower(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1555 } else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1556 *s_new = c; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1557 s_new++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1558 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1559 return new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1560 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1561 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1562 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1563 static char upper__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1564 "S.upper() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1565 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1566 Return a copy of the string S converted to uppercase."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1567 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1568 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
1569 string_upper(PyStringObject *self) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1570 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1571 char *s = PyString_AS_STRING(self), *s_new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1572 int i, n = PyString_GET_SIZE(self); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1573 PyObject *new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1574 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1575 new = PyString_FromStringAndSize(NULL, n); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1576 if (new == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1577 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1578 s_new = PyString_AsString(new); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1579 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
|
1580 int c = Py_CHARMASK(*s++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1581 if (islower(c)) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1582 *s_new = toupper(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1583 } else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1584 *s_new = c; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1585 s_new++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1586 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1587 return new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1588 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1589 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1590 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1591 static char title__doc__[] = |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1592 "S.title() -> string\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1593 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1594 Return a titlecased version of S, i.e. words start with uppercase\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1595 characters, all remaining cased characters have lowercase."; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1596 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1597 static PyObject* |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
1598 string_title(PyStringObject *self) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1599 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1600 char *s = PyString_AS_STRING(self), *s_new; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1601 int i, n = PyString_GET_SIZE(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1602 int previous_is_cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1603 PyObject *new; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1604 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1605 new = PyString_FromStringAndSize(NULL, n); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1606 if (new == NULL) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1607 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1608 s_new = PyString_AsString(new); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1609 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
|
1610 int c = Py_CHARMASK(*s++); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1611 if (islower(c)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1612 if (!previous_is_cased) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1613 c = toupper(c); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1614 previous_is_cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1615 } else if (isupper(c)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1616 if (previous_is_cased) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1617 c = tolower(c); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1618 previous_is_cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1619 } else |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1620 previous_is_cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1621 *s_new++ = c; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1622 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1623 return new; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1624 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1625 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1626 static char capitalize__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1627 "S.capitalize() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1628 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1629 Return a copy of the string S with only its first character\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1630 capitalized."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1631 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1632 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
1633 string_capitalize(PyStringObject *self) |
|
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 char *s = PyString_AS_STRING(self), *s_new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1636 int i, n = PyString_GET_SIZE(self); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1637 PyObject *new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1638 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1639 new = PyString_FromStringAndSize(NULL, n); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1640 if (new == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1641 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1642 s_new = PyString_AsString(new); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1643 if (0 < n) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1644 int c = Py_CHARMASK(*s++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1645 if (islower(c)) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1646 *s_new = toupper(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1647 else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1648 *s_new = c; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1649 s_new++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1650 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1651 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
|
1652 int c = Py_CHARMASK(*s++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1653 if (isupper(c)) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1654 *s_new = tolower(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1655 else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1656 *s_new = c; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1657 s_new++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1658 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1659 return new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1660 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1661 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1662 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1663 static char count__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1664 "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
|
1665 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1666 Return the number of occurrences of substring sub in string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1667 S[start:end]. Optional arguments start and end are\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1668 interpreted as in slice notation."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1669 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1670 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1671 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
|
1672 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1673 const char *s = PyString_AS_STRING(self), *sub; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1674 int len = PyString_GET_SIZE(self), n; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1675 int i = 0, last = INT_MAX; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1676 int m, r; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1677 PyObject *subobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1678 |
| 12615 | 1679 if (!PyArg_ParseTuple(args, "O|O&O&:count", &subobj, |
| 1680 _PyEval_SliceIndex, &i, _PyEval_SliceIndex, &last)) | |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1681 return NULL; |
| 12615 | 1682 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1683 if (PyString_Check(subobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1684 sub = PyString_AS_STRING(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1685 n = PyString_GET_SIZE(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1686 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1687 #ifdef Py_USING_UNICODE |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1688 else if (PyUnicode_Check(subobj)) { |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1689 int count; |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1690 count = PyUnicode_Count((PyObject *)self, subobj, i, last); |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1691 if (count == -1) |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1692 return NULL; |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1693 else |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1694 return PyInt_FromLong((long) count); |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1695 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1696 #endif |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1697 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
|
1698 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1699 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1700 if (last > len) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1701 last = len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1702 if (last < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1703 last += len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1704 if (last < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1705 last = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1706 if (i < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1707 i += len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1708 if (i < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1709 i = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1710 m = last + 1 - n; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1711 if (n == 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1712 return PyInt_FromLong((long) (m-i)); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1713 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1714 r = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1715 while (i < m) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1716 if (!memcmp(s+i, sub, n)) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1717 r++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1718 i += n; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1719 } else { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1720 i++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1721 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1722 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1723 return PyInt_FromLong((long) r); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1724 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1725 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1726 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1727 static char swapcase__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1728 "S.swapcase() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1729 \n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1730 Return a copy of the string S with uppercase characters\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1731 converted to lowercase and vice versa."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1732 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1733 static PyObject * |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
1734 string_swapcase(PyStringObject *self) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1735 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1736 char *s = PyString_AS_STRING(self), *s_new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1737 int i, n = PyString_GET_SIZE(self); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1738 PyObject *new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1739 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1740 new = PyString_FromStringAndSize(NULL, n); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1741 if (new == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1742 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1743 s_new = PyString_AsString(new); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1744 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
|
1745 int c = Py_CHARMASK(*s++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1746 if (islower(c)) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1747 *s_new = toupper(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1748 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1749 else if (isupper(c)) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1750 *s_new = tolower(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1751 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1752 else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1753 *s_new = c; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1754 s_new++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1755 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1756 return new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1757 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1758 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1759 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1760 static char translate__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1761 "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
|
1762 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1763 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
|
1764 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
|
1765 remaining characters have been mapped through the given\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1766 translation table, which must be a string of length 256."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1767 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1768 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1769 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
|
1770 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1771 register char *input, *output; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1772 register const char *table; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1773 register int i, c, changed = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1774 PyObject *input_obj = (PyObject*)self; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1775 const char *table1, *output_start, *del_table=NULL; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1776 int inlen, tablen, dellen = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1777 PyObject *result; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1778 int trans_table[256]; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1779 PyObject *tableobj, *delobj = NULL; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1780 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1781 if (!PyArg_ParseTuple(args, "O|O:translate", |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1782 &tableobj, &delobj)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1783 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1784 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1785 if (PyString_Check(tableobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1786 table1 = PyString_AS_STRING(tableobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1787 tablen = PyString_GET_SIZE(tableobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1788 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1789 #ifdef Py_USING_UNICODE |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1790 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
|
1791 /* 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
|
1792 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
|
1793 to be deleted. */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1794 if (delobj != NULL) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1795 PyErr_SetString(PyExc_TypeError, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1796 "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
|
1797 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1798 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1799 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
|
1800 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1801 #endif |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1802 else if (PyObject_AsCharBuffer(tableobj, &table1, &tablen)) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1803 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1804 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1805 if (delobj != NULL) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1806 if (PyString_Check(delobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1807 del_table = PyString_AS_STRING(delobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1808 dellen = PyString_GET_SIZE(delobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1809 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1810 #ifdef Py_USING_UNICODE |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1811 else if (PyUnicode_Check(delobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1812 PyErr_SetString(PyExc_TypeError, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1813 "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
|
1814 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1815 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
1816 #endif |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1817 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
|
1818 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1819 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1820 if (tablen != 256) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1821 PyErr_SetString(PyExc_ValueError, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1822 "translation table must be 256 characters long"); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1823 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1824 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1825 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1826 else { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1827 del_table = NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1828 dellen = 0; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1829 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1830 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1831 table = table1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1832 inlen = PyString_Size(input_obj); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1833 result = PyString_FromStringAndSize((char *)NULL, inlen); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1834 if (result == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1835 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1836 output_start = output = PyString_AsString(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1837 input = PyString_AsString(input_obj); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1838 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1839 if (dellen == 0) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1840 /* 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
|
1841 for (i = inlen; --i >= 0; ) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1842 c = Py_CHARMASK(*input++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1843 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
|
1844 changed = 1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1845 } |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
1846 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
|
1847 return result; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1848 Py_DECREF(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1849 Py_INCREF(input_obj); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1850 return input_obj; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1851 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1852 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1853 for (i = 0; i < 256; i++) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1854 trans_table[i] = Py_CHARMASK(table[i]); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1855 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1856 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
|
1857 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
|
1858 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1859 for (i = inlen; --i >= 0; ) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1860 c = Py_CHARMASK(*input++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1861 if (trans_table[c] != -1) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1862 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
|
1863 continue; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1864 changed = 1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1865 } |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
1866 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
|
1867 Py_DECREF(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1868 Py_INCREF(input_obj); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1869 return input_obj; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1870 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1871 /* 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
|
1872 if (inlen > 0) |
|
6fa76b4e59fe
Repair widespread misuse of _PyString_Resize. Since it's clear people
Tim Peters <tim.peters@gmail.com>
parents:
23225
diff
changeset
|
1873 _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
|
1874 return result; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1875 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1876 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1877 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1878 /* What follows is used for implementing replace(). Perry Stoll. */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1879 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1880 /* |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1881 mymemfind |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1882 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1883 strstr replacement for arbitrary blocks of memory. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1884 |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11998
diff
changeset
|
1885 Locates the first occurrence in the memory pointed to by MEM of the |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1886 contents of memory pointed to by PAT. Returns the index into MEM if |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1887 found, or -1 if not found. If len of PAT is greater than length of |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1888 MEM, the function returns -1. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1889 */ |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
1890 static int |
|
13460
e6d17f15ae6a
Somebody started playing with const, so of course the outcome
Tim Peters <tim.peters@gmail.com>
parents:
13459
diff
changeset
|
1891 mymemfind(const char *mem, int len, const char *pat, int pat_len) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1892 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1893 register int ii; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1894 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1895 /* pattern can not occur in the last pat_len-1 chars */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1896 len -= pat_len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1897 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1898 for (ii = 0; ii <= len; ii++) { |
|
12966
447efb64c72d
Fredrik Lundh <effbot@telia.com>:
Fred Drake <fdrake@acm.org>
parents:
12924
diff
changeset
|
1899 if (mem[ii] == pat[0] && memcmp(&mem[ii], pat, pat_len) == 0) { |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1900 return ii; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1901 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1902 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1903 return -1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1904 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1905 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1906 /* |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1907 mymemcnt |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1908 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1909 Return the number of distinct times PAT is found in MEM. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1910 meaning mem=1111 and pat==11 returns 2. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1911 mem=11111 and pat==11 also return 2. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1912 */ |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
1913 static int |
|
13460
e6d17f15ae6a
Somebody started playing with const, so of course the outcome
Tim Peters <tim.peters@gmail.com>
parents:
13459
diff
changeset
|
1914 mymemcnt(const char *mem, int len, const char *pat, int pat_len) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1915 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1916 register int offset = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1917 int nfound = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1918 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1919 while (len >= 0) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1920 offset = mymemfind(mem, len, pat, pat_len); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1921 if (offset == -1) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1922 break; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1923 mem += offset + pat_len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1924 len -= offset + pat_len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1925 nfound++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1926 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1927 return nfound; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1928 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1929 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1930 /* |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1931 mymemreplace |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1932 |
|
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
|
1933 Return a string in which all occurrences of PAT in memory STR are |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1934 replaced with SUB. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1935 |
|
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
|
1936 If length of PAT is less than length of STR or there are no occurrences |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1937 of PAT in STR, then the original string is returned. Otherwise, a new |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1938 string is allocated here and returned. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1939 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1940 on return, out_len is: |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1941 the length of output string, or |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1942 -1 if the input string is returned, or |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1943 unchanged if an error occurs (no memory). |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1944 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1945 return value is: |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1946 the new string allocated locally, or |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1947 NULL if an error occurred. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1948 */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1949 static char * |
|
13460
e6d17f15ae6a
Somebody started playing with const, so of course the outcome
Tim Peters <tim.peters@gmail.com>
parents:
13459
diff
changeset
|
1950 mymemreplace(const char *str, int len, /* input string */ |
|
e6d17f15ae6a
Somebody started playing with const, so of course the outcome
Tim Peters <tim.peters@gmail.com>
parents:
13459
diff
changeset
|
1951 const char *pat, int pat_len, /* pattern string to find */ |
|
e6d17f15ae6a
Somebody started playing with const, so of course the outcome
Tim Peters <tim.peters@gmail.com>
parents:
13459
diff
changeset
|
1952 const char *sub, int sub_len, /* substitution string */ |
|
e6d17f15ae6a
Somebody started playing with const, so of course the outcome
Tim Peters <tim.peters@gmail.com>
parents:
13459
diff
changeset
|
1953 int count, /* number of replacements */ |
|
17750
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1954 int *out_len) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1955 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1956 char *out_s; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1957 char *new_s; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1958 int nfound, offset, new_len; |
|
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 if (len == 0 || pat_len > len) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1961 goto return_same; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1962 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1963 /* find length of output string */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1964 nfound = mymemcnt(str, len, pat, pat_len); |
|
17751
c886cd1c2184
Heh. I need a break. After this: stropmodule & stringobject were more
Tim Peters <tim.peters@gmail.com>
parents:
17750
diff
changeset
|
1965 if (count < 0) |
|
c886cd1c2184
Heh. I need a break. After this: stropmodule & stringobject were more
Tim Peters <tim.peters@gmail.com>
parents:
17750
diff
changeset
|
1966 count = INT_MAX; |
|
c886cd1c2184
Heh. I need a break. After this: stropmodule & stringobject were more
Tim Peters <tim.peters@gmail.com>
parents:
17750
diff
changeset
|
1967 else if (nfound > count) |
|
c886cd1c2184
Heh. I need a break. After this: stropmodule & stringobject were more
Tim Peters <tim.peters@gmail.com>
parents:
17750
diff
changeset
|
1968 nfound = count; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1969 if (nfound == 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1970 goto return_same; |
|
17750
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1971 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1972 new_len = len + nfound*(sub_len - pat_len); |
|
17750
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1973 if (new_len == 0) { |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1974 /* Have to allocate something for the caller to free(). */ |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1975 out_s = (char *)PyMem_MALLOC(1); |
|
17751
c886cd1c2184
Heh. I need a break. After this: stropmodule & stringobject were more
Tim Peters <tim.peters@gmail.com>
parents:
17750
diff
changeset
|
1976 if (out_s == NULL) |
|
17750
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1977 return NULL; |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1978 out_s[0] = '\0'; |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1979 } |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1980 else { |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1981 assert(new_len > 0); |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1982 new_s = (char *)PyMem_MALLOC(new_len); |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1983 if (new_s == NULL) |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1984 return NULL; |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1985 out_s = new_s; |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1986 |
|
17751
c886cd1c2184
Heh. I need a break. After this: stropmodule & stringobject were more
Tim Peters <tim.peters@gmail.com>
parents:
17750
diff
changeset
|
1987 for (; count > 0 && len > 0; --count) { |
|
17750
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1988 /* find index of next instance of pattern */ |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1989 offset = mymemfind(str, len, pat, pat_len); |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1990 if (offset == -1) |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1991 break; |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1992 |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1993 /* copy non matching part of input string */ |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1994 memcpy(new_s, str, offset); |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1995 str += offset + pat_len; |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1996 len -= offset + pat_len; |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1997 |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1998 /* copy substitute into the output string */ |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1999 new_s += offset; |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
2000 memcpy(new_s, sub, sub_len); |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
2001 new_s += sub_len; |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
2002 } |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
2003 /* copy any remaining values into output string */ |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
2004 if (len > 0) |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
2005 memcpy(new_s, str, len); |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
2006 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2007 *out_len = new_len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2008 return out_s; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2009 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2010 return_same: |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2011 *out_len = -1; |
|
17750
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
2012 return (char *)str; /* cast away const */ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2013 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2014 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2015 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2016 static char replace__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2017 "S.replace (old, new[, maxsplit]) -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2018 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2019 Return a copy of string S with all occurrences of substring\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2020 old replaced by new. If the optional argument maxsplit is\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2021 given, only the first maxsplit occurrences are replaced."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2022 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2023 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2024 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
|
2025 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2026 const char *str = PyString_AS_STRING(self), *sub, *repl; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2027 char *new_s; |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2028 const int len = PyString_GET_SIZE(self); |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2029 int sub_len, repl_len, out_len; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2030 int count = -1; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2031 PyObject *new; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2032 PyObject *subobj, *replobj; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2033 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2034 if (!PyArg_ParseTuple(args, "OO|i:replace", |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2035 &subobj, &replobj, &count)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2036 return NULL; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2037 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2038 if (PyString_Check(subobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2039 sub = PyString_AS_STRING(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2040 sub_len = PyString_GET_SIZE(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2041 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2042 #ifdef Py_USING_UNICODE |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2043 else if (PyUnicode_Check(subobj)) |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2044 return PyUnicode_Replace((PyObject *)self, |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2045 subobj, replobj, count); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2046 #endif |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2047 else if (PyObject_AsCharBuffer(subobj, &sub, &sub_len)) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2048 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2049 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2050 if (PyString_Check(replobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2051 repl = PyString_AS_STRING(replobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2052 repl_len = PyString_GET_SIZE(replobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2053 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2054 #ifdef Py_USING_UNICODE |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2055 else if (PyUnicode_Check(replobj)) |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2056 return PyUnicode_Replace((PyObject *)self, |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2057 subobj, replobj, count); |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2058 #endif |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2059 else if (PyObject_AsCharBuffer(replobj, &repl, &repl_len)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2060 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2061 |
|
11998
f94d10b6d0f4
Fix typo in replace() detected by Mark Hammond and fixed by Marc-Andre.
Guido van Rossum <guido@python.org>
parents:
11969
diff
changeset
|
2062 if (sub_len <= 0) { |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11998
diff
changeset
|
2063 PyErr_SetString(PyExc_ValueError, "empty pattern string"); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2064 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2065 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2066 new_s = mymemreplace(str,len,sub,sub_len,repl,repl_len,count,&out_len); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2067 if (new_s == NULL) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2068 PyErr_NoMemory(); |
|
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 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2071 if (out_len == -1) { |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2072 if (PyString_CheckExact(self)) { |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2073 /* we're returning another reference to self */ |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2074 new = (PyObject*)self; |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2075 Py_INCREF(new); |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2076 } |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2077 else { |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2078 new = PyString_FromStringAndSize(str, len); |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2079 if (new == NULL) |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2080 return NULL; |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2081 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2082 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2083 else { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2084 new = PyString_FromStringAndSize(new_s, out_len); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
2085 PyMem_FREE(new_s); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2086 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2087 return new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2088 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2089 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2090 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2091 static char 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
|
2092 "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
|
2093 \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
|
2094 Return True if S starts with the specified prefix, False otherwise. With\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2095 optional start, test S beginning at that position. With optional end, stop\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2096 comparing S at that position."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2097 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2098 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2099 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
|
2100 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2101 const char* str = PyString_AS_STRING(self); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2102 int 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
|
2103 const char* prefix; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2104 int plen; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2105 int start = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2106 int end = -1; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2107 PyObject *subobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2108 |
| 12615 | 2109 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 2110 _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
|
2111 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2112 if (PyString_Check(subobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2113 prefix = PyString_AS_STRING(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2114 plen = PyString_GET_SIZE(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2115 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2116 #ifdef Py_USING_UNICODE |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2117 else if (PyUnicode_Check(subobj)) { |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2118 int rc; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2119 rc = PyUnicode_Tailmatch((PyObject *)self, |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2120 subobj, start, end, -1); |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2121 if (rc == -1) |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2122 return NULL; |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2123 else |
|
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
|
2124 return PyBool_FromLong((long) rc); |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2125 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2126 #endif |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2127 else if (PyObject_AsCharBuffer(subobj, &prefix, &plen)) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2128 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2129 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2130 /* adopt Java semantics for index out of range. it is legal for |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2131 * offset to be == plen, but this only returns true if prefix is |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2132 * the empty string. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2133 */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2134 if (start < 0 || start+plen > len) |
|
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
|
2135 return PyBool_FromLong(0); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2136 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2137 if (!memcmp(str+start, prefix, plen)) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2138 /* did the match end after the specified end? */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2139 if (end < 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
|
2140 return PyBool_FromLong(1); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2141 else if (end - start < plen) |
|
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
|
2142 return PyBool_FromLong(0); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2143 else |
|
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
|
2144 return PyBool_FromLong(1); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2145 } |
|
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
|
2146 else return PyBool_FromLong(0); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2147 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2148 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2149 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2150 static char 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
|
2151 "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
|
2152 \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
|
2153 Return True if S ends with the specified suffix, False otherwise. With\n\ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2154 optional start, test S beginning at that position. With optional end, stop\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2155 comparing S at that position."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2156 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2157 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2158 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
|
2159 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2160 const char* str = PyString_AS_STRING(self); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2161 int 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
|
2162 const char* suffix; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2163 int slen; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2164 int start = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2165 int end = -1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2166 int lower, upper; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2167 PyObject *subobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2168 |
| 12615 | 2169 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 2170 _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
|
2171 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2172 if (PyString_Check(subobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2173 suffix = PyString_AS_STRING(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2174 slen = PyString_GET_SIZE(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2175 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2176 #ifdef Py_USING_UNICODE |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2177 else if (PyUnicode_Check(subobj)) { |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2178 int rc; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2179 rc = PyUnicode_Tailmatch((PyObject *)self, |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2180 subobj, start, end, +1); |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2181 if (rc == -1) |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2182 return NULL; |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2183 else |
|
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
|
2184 return PyBool_FromLong((long) rc); |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
2185 } |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
2186 #endif |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2187 else if (PyObject_AsCharBuffer(subobj, &suffix, &slen)) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2188 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2189 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2190 if (start < 0 || start > len || slen > len) |
|
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
|
2191 return PyBool_FromLong(0); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2192 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2193 upper = (end >= 0 && end <= len) ? end : len; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2194 lower = (upper - slen) > start ? (upper - slen) : start; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2195 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2196 if (upper-lower >= slen && !memcmp(str+lower, suffix, slen)) |
|
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
|
2197 return PyBool_FromLong(1); |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
2198 else return PyBool_FromLong(0); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2199 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2200 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2201 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2202 static char 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
|
2203 "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
|
2204 \n\ |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2205 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
|
2206 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
|
2207 handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2208 a ValueError. Other possible values are 'ignore' and 'replace'."; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2209 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2210 static PyObject * |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2211 string_encode(PyStringObject *self, PyObject *args) |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2212 { |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2213 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
|
2214 char *errors = NULL; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2215 if (!PyArg_ParseTuple(args, "|ss:encode", &encoding, &errors)) |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2216 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
|
2217 return PyString_AsEncodedObject((PyObject *)self, encoding, errors); |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2218 } |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2219 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2220 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2221 static char decode__doc__[] = |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2222 "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
|
2223 \n\ |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2224 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
|
2225 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
|
2226 handling scheme. Default is 'strict' meaning that encoding errors raise\n\ |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2227 a ValueError. Other possible values are 'ignore' and 'replace'."; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2228 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2229 static PyObject * |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2230 string_decode(PyStringObject *self, PyObject *args) |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2231 { |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2232 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
|
2233 char *errors = NULL; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2234 if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2235 return NULL; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
2236 return PyString_AsDecodedObject((PyObject *)self, encoding, errors); |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2237 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2238 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2239 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2240 static char expandtabs__doc__[] = |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2241 "S.expandtabs([tabsize]) -> string\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2242 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2243 Return a copy of S where all tab characters are expanded using spaces.\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2244 If tabsize is not given, a tab size of 8 characters is assumed."; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2245 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2246 static PyObject* |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2247 string_expandtabs(PyStringObject *self, PyObject *args) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2248 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2249 const char *e, *p; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2250 char *q; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2251 int i, j; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2252 PyObject *u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2253 int tabsize = 8; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2254 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2255 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
|
2256 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2257 |
|
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
|
2258 /* First pass: determine size of output string */ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2259 i = j = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2260 e = PyString_AS_STRING(self) + PyString_GET_SIZE(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2261 for (p = PyString_AS_STRING(self); p < e; p++) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2262 if (*p == '\t') { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2263 if (tabsize > 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2264 j += tabsize - (j % tabsize); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2265 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2266 else { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2267 j++; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2268 if (*p == '\n' || *p == '\r') { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2269 i += j; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2270 j = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2271 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2272 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2273 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2274 /* Second pass: create output string and fill it */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2275 u = PyString_FromStringAndSize(NULL, i + j); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2276 if (!u) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2277 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2278 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2279 j = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2280 q = PyString_AS_STRING(u); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2281 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2282 for (p = PyString_AS_STRING(self); p < e; p++) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2283 if (*p == '\t') { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2284 if (tabsize > 0) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2285 i = tabsize - (j % tabsize); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2286 j += i; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2287 while (i--) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2288 *q++ = ' '; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2289 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2290 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2291 else { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2292 j++; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2293 *q++ = *p; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2294 if (*p == '\n' || *p == '\r') |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2295 j = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2296 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2297 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2298 return u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2299 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2300 |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2301 static PyObject * |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2302 pad(PyStringObject *self, int left, int right, char fill) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2303 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2304 PyObject *u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2305 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2306 if (left < 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2307 left = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2308 if (right < 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2309 right = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2310 |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2311 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
|
2312 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2313 return (PyObject *)self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2314 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2315 |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2316 u = PyString_FromStringAndSize(NULL, |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2317 left + PyString_GET_SIZE(self) + right); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2318 if (u) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2319 if (left) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2320 memset(PyString_AS_STRING(u), fill, left); |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2321 memcpy(PyString_AS_STRING(u) + left, |
|
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2322 PyString_AS_STRING(self), |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2323 PyString_GET_SIZE(self)); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2324 if (right) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2325 memset(PyString_AS_STRING(u) + left + PyString_GET_SIZE(self), |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2326 fill, right); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2327 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2328 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2329 return u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2330 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2331 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2332 static char ljust__doc__[] = |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2333 "S.ljust(width) -> string\n" |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2334 "\n" |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2335 "Return S left justified in a string of length width. Padding is\n" |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2336 "done using spaces."; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2337 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2338 static PyObject * |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2339 string_ljust(PyStringObject *self, PyObject *args) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2340 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2341 int width; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2342 if (!PyArg_ParseTuple(args, "i:ljust", &width)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2343 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2344 |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2345 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
|
2346 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2347 return (PyObject*) self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2348 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2349 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2350 return pad(self, 0, width - PyString_GET_SIZE(self), ' '); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2351 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2352 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2353 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2354 static char rjust__doc__[] = |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2355 "S.rjust(width) -> string\n" |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2356 "\n" |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2357 "Return S right justified in a string of length width. Padding is\n" |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2358 "done using spaces."; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2359 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2360 static PyObject * |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2361 string_rjust(PyStringObject *self, PyObject *args) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2362 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2363 int width; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2364 if (!PyArg_ParseTuple(args, "i:rjust", &width)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2365 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2366 |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2367 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
|
2368 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2369 return (PyObject*) self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2370 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2371 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2372 return pad(self, width - PyString_GET_SIZE(self), 0, ' '); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2373 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2374 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2375 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2376 static char center__doc__[] = |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2377 "S.center(width) -> string\n" |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2378 "\n" |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2379 "Return S centered in a string of length width. Padding is done\n" |
|
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2380 "using spaces."; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2381 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2382 static PyObject * |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2383 string_center(PyStringObject *self, PyObject *args) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2384 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2385 int marg, left; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2386 int width; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2387 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2388 if (!PyArg_ParseTuple(args, "i:center", &width)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2389 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2390 |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2391 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
|
2392 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2393 return (PyObject*) self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2394 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2395 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2396 marg = width - PyString_GET_SIZE(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2397 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
|
2398 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2399 return pad(self, left, marg - left, ' '); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2400 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2401 |
|
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
|
2402 static char zfill__doc__[] = |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2403 "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
|
2404 "\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
|
2405 "Pad a numeric string S with zeros on the left, to fill a field\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
|
2406 "of the specified width. The string S is never truncated."; |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2407 |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2408 static PyObject * |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2409 string_zfill(PyStringObject *self, PyObject *args) |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2410 { |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2411 int 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
|
2412 PyObject *s; |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
2413 char *p; |
|
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
|
2414 |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2415 int width; |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2416 if (!PyArg_ParseTuple(args, "i:zfill", &width)) |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2417 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
|
2418 |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2419 if (PyString_GET_SIZE(self) >= width) { |
|
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
|
2420 if (PyString_CheckExact(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
|
2421 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
|
2422 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
|
2423 } |
|
19ea00feda33
Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents:
23046
diff
changeset
|
2424 else |
|
19ea00feda33
Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents:
23046
diff
changeset
|
2425 return PyString_FromStringAndSize( |
|
19ea00feda33
Return the orginal string only if it's a real str or unicode
Walter Dörwald <walter@livinglogic.de>
parents:
23046
diff
changeset
|
2426 PyString_AS_STRING(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
|
2427 PyString_GET_SIZE(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
|
2428 ); |
|
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
|
2429 } |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2430 |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2431 fill = width - PyString_GET_SIZE(self); |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2432 |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2433 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
|
2434 |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2435 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
|
2436 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
|
2437 |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2438 p = PyString_AS_STRING(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
|
2439 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
|
2440 /* 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
|
2441 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
|
2442 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
|
2443 } |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2444 |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2445 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
|
2446 } |
|
8b1268bc371a
Apply the second version of SF patch http://www.python.org/sf/536241
Walter Dörwald <walter@livinglogic.de>
parents:
23015
diff
changeset
|
2447 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2448 static char isspace__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
|
2449 "S.isspace() -> bool\n" |
|
19884
df66b4dd0b0d
More bug 460020: lots of string optimizations inhibited for string
Tim Peters <tim.peters@gmail.com>
parents:
19846
diff
changeset
|
2450 "\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
|
2451 "Return True if there are only whitespace characters in S,\n" |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
2452 "False otherwise."; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2453 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2454 static PyObject* |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
2455 string_isspace(PyStringObject *self) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2456 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2457 register const unsigned char *p |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2458 = (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
|
2459 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
|
2460 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2461 /* Shortcut for single character strings */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2462 if (PyString_GET_SIZE(self) == 1 && |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2463 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
|
2464 return PyBool_FromLong(1); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2465 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2466 /* Special case for empty strings */ |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2467 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
|
2468 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2469 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2470 e = p + PyString_GET_SIZE(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2471 for (; p < e; p++) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2472 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
|
2473 return PyBool_FromLong(0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2474 } |
|
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
|
2475 return PyBool_FromLong(1); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2476 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2477 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2478 |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2479 static char 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
|
2480 "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
|
2481 \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
|
2482 Return True if all characters in S are alphabetic\n\ |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
2483 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
|
2484 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2485 static PyObject* |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
2486 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
|
2487 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2488 register const unsigned char *p |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2489 = (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
|
2490 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
|
2491 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2492 /* Shortcut for single character strings */ |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2493 if (PyString_GET_SIZE(self) == 1 && |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2494 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
|
2495 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
|
2496 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2497 /* Special case for empty strings */ |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2498 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
|
2499 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
|
2500 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2501 e = p + PyString_GET_SIZE(self); |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2502 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
|
2503 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
|
2504 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
|
2505 } |
|
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
|
2506 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
|
2507 } |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2508 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2509 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2510 static char 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
|
2511 "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
|
2512 \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
|
2513 Return True if all characters in S are alphanumeric\n\ |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
2514 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
|
2515 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2516 static PyObject* |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
2517 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
|
2518 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2519 register const unsigned char *p |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2520 = (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
|
2521 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
|
2522 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2523 /* Shortcut for single character strings */ |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2524 if (PyString_GET_SIZE(self) == 1 && |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2525 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
|
2526 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
|
2527 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2528 /* Special case for empty strings */ |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2529 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
|
2530 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
|
2531 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2532 e = p + PyString_GET_SIZE(self); |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2533 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
|
2534 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
|
2535 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
|
2536 } |
|
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
|
2537 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
|
2538 } |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2539 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2540 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2541 static char 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
|
2542 "S.isdigit() -> bool\n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2543 \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
|
2544 Return True if there are only digit characters in S,\n\ |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
2545 False otherwise."; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2546 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2547 static PyObject* |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
2548 string_isdigit(PyStringObject *self) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2549 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2550 register const unsigned char *p |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2551 = (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
|
2552 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
|
2553 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2554 /* Shortcut for single character strings */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2555 if (PyString_GET_SIZE(self) == 1 && |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2556 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
|
2557 return PyBool_FromLong(1); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2558 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2559 /* Special case for empty strings */ |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2560 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
|
2561 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2562 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2563 e = p + PyString_GET_SIZE(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2564 for (; p < e; p++) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2565 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
|
2566 return PyBool_FromLong(0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2567 } |
|
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
|
2568 return PyBool_FromLong(1); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2569 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2570 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2571 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2572 static char 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
|
2573 "S.islower() -> bool\n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2574 \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
|
2575 Return True if all cased characters in S are lowercase and there is\n\ |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
2576 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
|
2577 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2578 static PyObject* |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
2579 string_islower(PyStringObject *self) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2580 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2581 register const unsigned char *p |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2582 = (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
|
2583 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
|
2584 int cased; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2585 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2586 /* Shortcut for single character strings */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2587 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
|
2588 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
|
2589 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2590 /* Special case for empty strings */ |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2591 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
|
2592 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2593 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2594 e = p + PyString_GET_SIZE(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2595 cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2596 for (; p < e; p++) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2597 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
|
2598 return PyBool_FromLong(0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2599 else if (!cased && islower(*p)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2600 cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2601 } |
|
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
|
2602 return PyBool_FromLong(cased); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2603 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2604 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2605 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2606 static char 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
|
2607 "S.isupper() -> bool\n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2608 \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
|
2609 Return True if all cased characters in S are uppercase and there is\n\ |
|
aa8d59823add
Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents:
22689
diff
changeset
|
2610 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
|
2611 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2612 static PyObject* |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
2613 string_isupper(PyStringObject *self) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2614 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2615 register const unsigned char *p |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2616 = (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
|
2617 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
|
2618 int cased; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2619 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2620 /* Shortcut for single character strings */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2621 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
|
2622 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
|
2623 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2624 /* Special case for empty strings */ |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2625 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
|
2626 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2627 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2628 e = p + PyString_GET_SIZE(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2629 cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2630 for (; p < e; p++) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2631 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
|
2632 return PyBool_FromLong(0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2633 else if (!cased && isupper(*p)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2634 cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2635 } |
|
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
|
2636 return PyBool_FromLong(cased); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2637 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2638 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2639 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2640 static char 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
|
2641 "S.istitle() -> bool\n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2642 \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
|
2643 Return True if S is a titlecased string, i.e. uppercase characters\n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2644 may only follow uncased characters and lowercase characters only cased\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
|
2645 ones. Return False otherwise."; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2646 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2647 static PyObject* |
|
19301
b9701538f827
Patch #427190: Implement and use METH_NOARGS and METH_O.
Martin v. Löwis <martin@v.loewis.de>
parents:
18933
diff
changeset
|
2648 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
|
2649 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2650 register const unsigned char *p |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2651 = (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
|
2652 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
|
2653 int cased, previous_is_cased; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2654 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2655 /* Shortcut for single character strings */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2656 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
|
2657 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
|
2658 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2659 /* Special case for empty strings */ |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2660 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
|
2661 return PyBool_FromLong(0); |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2662 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2663 e = p + PyString_GET_SIZE(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2664 cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2665 previous_is_cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2666 for (; p < e; p++) { |
|
12588
54274beb244d
The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
2667 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
|
2668 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2669 if (isupper(ch)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2670 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
|
2671 return PyBool_FromLong(0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2672 previous_is_cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2673 cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2674 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2675 else if (islower(ch)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2676 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
|
2677 return PyBool_FromLong(0); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2678 previous_is_cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2679 cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2680 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2681 else |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2682 previous_is_cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2683 } |
|
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
|
2684 return PyBool_FromLong(cased); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2685 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2686 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2687 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2688 static char splitlines__doc__[] = |
|
20415
621c7703e8b7
Remove extra "]" in splitlines() docstring.
Fred Drake <fdrake@acm.org>
parents:
20277
diff
changeset
|
2689 "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
|
2690 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2691 Return a list of the lines in S, breaking at line boundaries.\n\ |
| 12371 | 2692 Line breaks are not included in the resulting list unless keepends\n\ |
| 2693 is given and true."; | |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2694 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2695 #define SPLIT_APPEND(data, left, right) \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2696 str = PyString_FromStringAndSize(data + left, right - left); \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2697 if (!str) \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2698 goto onError; \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2699 if (PyList_Append(list, str)) { \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2700 Py_DECREF(str); \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2701 goto onError; \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2702 } \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2703 else \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2704 Py_DECREF(str); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2705 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2706 static PyObject* |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2707 string_splitlines(PyStringObject *self, PyObject *args) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2708 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2709 register int i; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2710 register int j; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2711 int len; |
| 12371 | 2712 int keepends = 0; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2713 PyObject *list; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2714 PyObject *str; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2715 char *data; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2716 |
| 12371 | 2717 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
|
2718 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2719 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2720 data = PyString_AS_STRING(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2721 len = PyString_GET_SIZE(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2722 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2723 list = PyList_New(0); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2724 if (!list) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2725 goto onError; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2726 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2727 for (i = j = 0; i < len; ) { |
| 12371 | 2728 int eol; |
| 2729 | |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2730 /* 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
|
2731 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
|
2732 i++; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2733 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2734 /* Skip the line break reading CRLF as one line break */ |
| 12371 | 2735 eol = i; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2736 if (i < len) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2737 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
|
2738 data[i+1] == '\n') |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2739 i += 2; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2740 else |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2741 i++; |
| 12371 | 2742 if (keepends) |
| 2743 eol = i; | |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2744 } |
| 12371 | 2745 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
|
2746 j = i; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2747 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2748 if (j < len) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2749 SPLIT_APPEND(data, j, len); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2750 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2751 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2752 return list; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2753 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2754 onError: |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2755 Py_DECREF(list); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2756 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2757 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2758 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2759 #undef SPLIT_APPEND |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2760 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2761 |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2762 static PyMethodDef |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2763 string_methods[] = { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2764 /* 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
|
2765 string.maketrans(). */ |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
2766 {"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
|
2767 {"split", (PyCFunction)string_split, METH_VARARGS, split__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
2768 {"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
|
2769 {"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
|
2770 {"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
|
2771 {"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
|
2772 {"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
|
2773 {"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
|
2774 {"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
|
2775 {"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
|
2776 {"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
|
2777 {"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
|
2778 capitalize__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
2779 {"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
|
2780 {"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
|
2781 endswith__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
2782 {"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
|
2783 {"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
|
2784 {"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
|
2785 {"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
|
2786 {"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
|
2787 {"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
|
2788 {"rstrip", (PyCFunction)string_rstrip, METH_VARARGS, rstrip__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
2789 {"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
|
2790 startswith__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
2791 {"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
|
2792 {"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
|
2793 swapcase__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
2794 {"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
|
2795 translate__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
2796 {"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
|
2797 {"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
|
2798 {"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
|
2799 {"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
|
2800 {"zfill", (PyCFunction)string_zfill, METH_VARARGS, zfill__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
2801 {"encode", (PyCFunction)string_encode, METH_VARARGS, encode__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
2802 {"decode", (PyCFunction)string_decode, METH_VARARGS, decode__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
2803 {"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
|
2804 expandtabs__doc__}, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
2805 {"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
|
2806 splitlines__doc__}, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2807 {NULL, NULL} /* sentinel */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2808 }; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2809 |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2810 staticforward PyObject * |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2811 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
|
2812 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2813 static PyObject * |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2814 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
|
2815 { |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2816 PyObject *x = NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2817 static char *kwlist[] = {"object", 0}; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2818 |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2819 if (type != &PyString_Type) |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2820 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
|
2821 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
|
2822 return NULL; |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2823 if (x == NULL) |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2824 return PyString_FromString(""); |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2825 return PyObject_Str(x); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2826 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2827 |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2828 static PyObject * |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2829 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
|
2830 { |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
2831 PyObject *tmp, *pnew; |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2832 int n; |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2833 |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2834 assert(PyType_IsSubtype(type, &PyString_Type)); |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2835 tmp = string_new(&PyString_Type, args, kwds); |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2836 if (tmp == NULL) |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2837 return NULL; |
|
19846
662e22f99639
More on SF bug [#460020] bug or feature: unicode() and subclasses.
Tim Peters <tim.peters@gmail.com>
parents:
19632
diff
changeset
|
2838 assert(PyString_CheckExact(tmp)); |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
2839 n = PyString_GET_SIZE(tmp); |
|
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
2840 pnew = type->tp_alloc(type, n); |
|
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
2841 if (pnew != NULL) { |
|
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
2842 memcpy(PyString_AS_STRING(pnew), PyString_AS_STRING(tmp), n+1); |
|
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
2843 ((PyStringObject *)pnew)->ob_shash = |
|
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
2844 ((PyStringObject *)tmp)->ob_shash; |
|
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
2845 ((PyStringObject *)pnew)->ob_sinterned = |
|
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
2846 ((PyStringObject *)tmp)->ob_sinterned; |
|
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
2847 } |
|
19632
20fb7591f885
Fix a memory leak in str_subtype_new(). (All the other
Guido van Rossum <guido@python.org>
parents:
19592
diff
changeset
|
2848 Py_DECREF(tmp); |
|
19886
3b0da13ace48
str_subtype_new, unicode_subtype_new:
Tim Peters <tim.peters@gmail.com>
parents:
19884
diff
changeset
|
2849 return pnew; |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2850 } |
|
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2851 |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2852 static char string_doc[] = |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2853 "str(object) -> string\n\ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2854 \n\ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2855 Return a nice string representation of the object.\n\ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2856 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
|
2857 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2858 PyTypeObject PyString_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2859 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 2860 0, |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2861 "str", |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2862 sizeof(PyStringObject), |
| 6 | 2863 sizeof(char), |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2864 (destructor)string_dealloc, /* tp_dealloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2865 (printfunc)string_print, /* tp_print */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2866 0, /* tp_getattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2867 0, /* tp_setattr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2868 0, /* tp_compare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2869 (reprfunc)string_repr, /* tp_repr */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2870 0, /* tp_as_number */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2871 &string_as_sequence, /* tp_as_sequence */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2872 0, /* tp_as_mapping */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2873 (hashfunc)string_hash, /* tp_hash */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2874 0, /* tp_call */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2875 (reprfunc)string_str, /* tp_str */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2876 PyObject_GenericGetAttr, /* tp_getattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2877 0, /* tp_setattro */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2878 &string_as_buffer, /* tp_as_buffer */ |
|
19592
85765e61deb0
Make str and tuple types subclassable.
Guido van Rossum <guido@python.org>
parents:
19489
diff
changeset
|
2879 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|
18933
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2880 string_doc, /* tp_doc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2881 0, /* tp_traverse */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2882 0, /* tp_clear */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2883 (richcmpfunc)string_richcompare, /* tp_richcompare */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2884 0, /* tp_weaklistoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2885 0, /* tp_iter */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2886 0, /* tp_iternext */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2887 string_methods, /* tp_methods */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2888 0, /* tp_members */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2889 0, /* tp_getset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2890 0, /* tp_base */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2891 0, /* tp_dict */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2892 0, /* tp_descr_get */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2893 0, /* tp_descr_set */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2894 0, /* tp_dictoffset */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2895 0, /* tp_init */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2896 0, /* tp_alloc */ |
|
09df3254b49d
Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents:
18887
diff
changeset
|
2897 string_new, /* tp_new */ |
|
22977
4ba091cc70b8
Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22785
diff
changeset
|
2898 PyObject_Del, /* tp_free */ |
| 6 | 2899 }; |
| 2900 | |
| 2901 void | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2902 PyString_Concat(register PyObject **pv, register PyObject *w) |
| 6 | 2903 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2904 register PyObject *v; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2905 if (*pv == NULL) |
| 6 | 2906 return; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2907 if (w == NULL || !PyString_Check(*pv)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2908 Py_DECREF(*pv); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2909 *pv = NULL; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2910 return; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2911 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2912 v = string_concat((PyStringObject *) *pv, w); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2913 Py_DECREF(*pv); |
| 6 | 2914 *pv = v; |
| 2915 } | |
| 2916 | |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2917 void |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2918 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
|
2919 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2920 PyString_Concat(pv, w); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2921 Py_XDECREF(w); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2922 } |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2923 |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2924 |
| 6 | 2925 /* The following function breaks the notion that strings are immutable: |
| 2926 it changes the size of a string. We get away with this only if there | |
| 2927 is only one module referencing the object. You can also think of it | |
| 2928 as creating a new string object and destroying the old one, only | |
| 2929 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
|
2930 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
|
2931 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
|
2932 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
|
2933 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
|
2934 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
|
2935 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
|
2936 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
|
2937 */ |
| 6 | 2938 |
| 2939 int | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2940 _PyString_Resize(PyObject **pv, int newsize) |
| 6 | 2941 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2942 register PyObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2943 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
|
2944 v = *pv; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2945 if (!PyString_Check(v) || v->ob_refcnt != 1) { |
| 6 | 2946 *pv = 0; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2947 Py_DECREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2948 PyErr_BadInternalCall(); |
| 27 | 2949 return -1; |
| 6 | 2950 } |
|
71
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
2951 /* XXX UNREF/NEWREF interface should be more symmetrical */ |
|
3586
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
3571
diff
changeset
|
2952 #ifdef Py_REF_DEBUG |
|
2612
ca6b60d4ea90
fix dusty debugging macros
Guido van Rossum <guido@python.org>
parents:
2504
diff
changeset
|
2953 --_Py_RefTotal; |
|
71
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
2954 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2955 _Py_ForgetReference(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2956 *pv = (PyObject *) |
|
22977
4ba091cc70b8
Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22785
diff
changeset
|
2957 PyObject_REALLOC((char *)v, |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2958 sizeof(PyStringObject) + newsize * sizeof(char)); |
| 6 | 2959 if (*pv == NULL) { |
|
22977
4ba091cc70b8
Remove PyMalloc_New, _PyMalloc_MALLOC, and PyMalloc_Del.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22785
diff
changeset
|
2960 PyObject_Del(v); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2961 PyErr_NoMemory(); |
| 27 | 2962 return -1; |
| 6 | 2963 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2964 _Py_NewReference(*pv); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2965 sv = (PyStringObject *) *pv; |
|
71
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
2966 sv->ob_size = newsize; |
|
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
2967 sv->ob_sval[newsize] = '\0'; |
| 6 | 2968 return 0; |
| 2969 } | |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2970 |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2971 /* Helpers for formatstring */ |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2972 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2973 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2974 getnextarg(PyObject *args, int arglen, int *p_argidx) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2975 { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2976 int argidx = *p_argidx; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2977 if (argidx < arglen) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2978 (*p_argidx)++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2979 if (arglen < 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2980 return args; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2981 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2982 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
|
2983 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2984 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2985 "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
|
2986 return NULL; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2987 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2988 |
|
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
|
2989 /* 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
|
2990 * 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
|
2991 * 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
|
2992 * 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
|
2993 * 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
|
2994 * 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
|
2995 */ |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2996 #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
|
2997 #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
|
2998 #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
|
2999 #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
|
3000 #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
|
3001 |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
3002 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3003 formatfloat(char *buf, size_t buflen, int flags, |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3004 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
|
3005 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3006 /* fmt = '%#.' + `prec` + `type` |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3007 worst case length = 3 + 10 (len of INT_MAX) + 1 = 14 (use 20)*/ |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3008 char fmt[20]; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3009 double x; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3010 if (!PyArg_Parse(v, "d;float argument required", &x)) |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
3011 return -1; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3012 if (prec < 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3013 prec = 6; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3014 if (type == 'f' && fabs(x)/1e25 >= 1e25) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3015 type = 'g'; |
|
21073
304b34023876
sprintf -> PyOS_snprintf in some "obviously safe" cases.
Tim Peters <tim.peters@gmail.com>
parents:
20604
diff
changeset
|
3016 PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%d%c", |
|
304b34023876
sprintf -> PyOS_snprintf in some "obviously safe" cases.
Tim Peters <tim.peters@gmail.com>
parents:
20604
diff
changeset
|
3017 (flags&F_ALT) ? "#" : "", |
|
304b34023876
sprintf -> PyOS_snprintf in some "obviously safe" cases.
Tim Peters <tim.peters@gmail.com>
parents:
20604
diff
changeset
|
3018 prec, type); |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3019 /* worst case length calc to ensure no buffer overrun: |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3020 fmt = %#.<prec>g |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3021 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
|
3022 for any double rep.) |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3023 len = 1 + prec + 1 + 2 + 5 = 9 + prec |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3024 If prec=0 the effective precision is 1 (the leading digit is |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3025 always given), therefore increase by one to 10+prec. */ |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3026 if (buflen <= (size_t)10 + (size_t)prec) { |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3027 PyErr_SetString(PyExc_OverflowError, |
| 15318 | 3028 "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
|
3029 return -1; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3030 } |
|
21073
304b34023876
sprintf -> PyOS_snprintf in some "obviously safe" cases.
Tim Peters <tim.peters@gmail.com>
parents:
20604
diff
changeset
|
3031 PyOS_snprintf(buf, buflen, fmt, x); |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
3032 return strlen(buf); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3033 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3034 |
|
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
|
3035 /* _PyString_FormatLong emulates the format codes d, u, o, x and X, and |
|
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
|
3036 * 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
|
3037 * 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
|
3038 * 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
|
3039 * . *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
|
3040 * *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
|
3041 * 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
|
3042 * 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
|
3043 * "-"? ("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
|
3044 * "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
|
3045 * 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
|
3046 * 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
|
3047 * 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
|
3048 * 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
|
3049 * 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
|
3050 * 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
|
3051 * 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
|
3052 * |
|
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
|
3053 * 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
|
3054 * 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
|
3055 */ |
|
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
|
3056 PyObject* |
|
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
|
3057 _PyString_FormatLong(PyObject *val, int flags, int prec, int 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
|
3058 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
|
3059 { |
|
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
|
3060 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
|
3061 char *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
|
3062 int 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
|
3063 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
|
3064 int len; /* number of characters */ |
|
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
|
3065 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
|
3066 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
|
3067 |
|
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
|
3068 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
|
3069 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
|
3070 case 'u': |
|
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
|
3071 result = val->ob_type->tp_str(val); |
|
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
|
3072 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
|
3073 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
|
3074 result = val->ob_type->tp_as_number->nb_oct(val); |
|
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
|
3075 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
|
3076 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
|
3077 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
|
3078 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
|
3079 result = val->ob_type->tp_as_number->nb_hex(val); |
|
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
|
3080 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
|
3081 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
|
3082 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
|
3083 } |
|
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
|
3084 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
|
3085 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
|
3086 |
|
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
|
3087 /* To modify the string in-place, there can only be one reference. */ |
|
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
|
3088 if (result->ob_refcnt != 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
|
3089 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
|
3090 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
|
3091 } |
|
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
|
3092 buf = PyString_AsString(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
|
3093 len = PyString_Size(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
|
3094 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
|
3095 --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
|
3096 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
|
3097 } |
|
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
|
3098 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
|
3099 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
|
3100 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
|
3101 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
|
3102 |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3103 /* 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
|
3104 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
|
3105 /* 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
|
3106 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
|
3107 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
|
3108 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
|
3109 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
|
3110 /* 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
|
3111 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
|
3112 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
|
3113 --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
|
3114 } |
|
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
|
3115 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
|
3116 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
|
3117 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
|
3118 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
|
3119 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
|
3120 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
|
3121 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
|
3122 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
|
3123 } |
|
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
|
3124 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
|
3125 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
|
3126 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
|
3127 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
|
3128 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
|
3129 } |
|
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
|
3130 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
|
3131 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
|
3132 } |
|
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
|
3133 |
|
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
|
3134 /* 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
|
3135 if (prec > 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
|
3136 PyObject *r1 = PyString_FromStringAndSize(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
|
3137 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
|
3138 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
|
3139 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
|
3140 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
|
3141 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
|
3142 } |
|
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
|
3143 b1 = PyString_AS_STRING(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
|
3144 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
|
3145 *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
|
3146 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
|
3147 *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
|
3148 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
|
3149 *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
|
3150 *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
|
3151 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
|
3152 result = 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
|
3153 buf = PyString_AS_STRING(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
|
3154 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
|
3155 } |
|
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
|
3156 |
|
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
|
3157 /* Fix up case for hex conversions. */ |
|
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
|
3158 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
|
3159 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
|
3160 /* Need to convert all upper case letters to lower case. */ |
|
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
|
3161 for (i = 0; i < len; 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
|
3162 if (buf[i] >= 'A' && buf[i] <= 'F') |
|
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
|
3163 buf[i] += 'a'-'A'; |
|
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
|
3164 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
|
3165 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
|
3166 /* Need to convert 0x to 0X (and -0x to -0X). */ |
|
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
|
3167 if (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
|
3168 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
|
3169 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
|
3170 } |
|
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
|
3171 *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
|
3172 *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
|
3173 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
|
3174 } |
|
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
|
3175 |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
3176 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3177 formatint(char *buf, size_t buflen, int flags, |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3178 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
|
3179 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3180 /* 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
|
3181 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
|
3182 + 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
|
3183 char fmt[64]; /* plenty big enough! */ |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3184 long x; |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3185 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3186 if (!PyArg_Parse(v, "l;int argument required", &x)) |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
3187 return -1; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3188 if (prec < 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3189 prec = 1; |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3190 |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3191 if ((flags & F_ALT) && |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3192 (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
|
3193 /* 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
|
3194 * 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
|
3195 * - 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
|
3196 * 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
|
3197 * %#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
|
3198 * hex() function |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3199 * - 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
|
3200 * 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
|
3201 * (Metrowerks, Compaq Tru64) |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3202 * - 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
|
3203 * 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
|
3204 * 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
|
3205 * |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3206 * 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
|
3207 * 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
|
3208 * of %#x/%#X. |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3209 * |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3210 * 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
|
3211 * formatint() in unicodeobject.c |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3212 */ |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
3213 PyOS_snprintf(fmt, sizeof(fmt), "0%c%%.%dl%c", |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3214 type, prec, type); |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3215 } |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3216 else { |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3217 PyOS_snprintf(fmt, sizeof(fmt), "%%%s.%dl%c", |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
3218 (flags&F_ALT) ? "#" : "", |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3219 prec, type); |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3220 } |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3221 |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3222 /* buf = '+'/'-'/'0'/'0x' + '[0-9]'*max(prec, len(x in octal)) |
|
22087
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3223 * worst case buf = '0x' + [0-9]*prec, where prec >= 11 |
|
5b24a6ae2dc4
%#x/%#X format conversion cleanup (see patch #450267):
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents:
22053
diff
changeset
|
3224 */ |
|
14713
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3225 if (buflen <= 13 || buflen <= (size_t)2 + (size_t)prec) { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3226 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
|
3227 "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
|
3228 return -1; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3229 } |
|
21073
304b34023876
sprintf -> PyOS_snprintf in some "obviously safe" cases.
Tim Peters <tim.peters@gmail.com>
parents:
20604
diff
changeset
|
3230 PyOS_snprintf(buf, buflen, fmt, x); |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
3231 return strlen(buf); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3232 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3233 |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
3234 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3235 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
|
3236 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3237 /* presume that the buffer is at least 2 characters long */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3238 if (PyString_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3239 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
|
3240 return -1; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3241 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3242 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3243 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
|
3244 return -1; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3245 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3246 buf[1] = '\0'; |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
3247 return 1; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3248 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3249 |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3250 |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3251 /* 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
|
3252 |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3253 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
|
3254 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
|
3255 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
|
3256 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
|
3257 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
|
3258 */ |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3259 #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
|
3260 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3261 PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3262 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
|
3263 { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3264 char *fmt, *res; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3265 int fmtcnt, rescnt, reslen, arglen, argidx; |
|
3570
977ff1ace070
Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
3266 int args_owned = 0; |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
3267 PyObject *result, *orig_args; |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
3268 #ifdef Py_USING_UNICODE |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
3269 PyObject *v, *w; |
|
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
3270 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3271 PyObject *dict = NULL; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3272 if (format == NULL || !PyString_Check(format) || args == NULL) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3273 PyErr_BadInternalCall(); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3274 return NULL; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3275 } |
| 12328 | 3276 orig_args = args; |
| 21254 | 3277 fmt = PyString_AS_STRING(format); |
| 3278 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
|
3279 reslen = rescnt = fmtcnt + 100; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3280 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
|
3281 if (result == NULL) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3282 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3283 res = PyString_AsString(result); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3284 if (PyTuple_Check(args)) { |
| 21254 | 3285 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
|
3286 argidx = 0; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3287 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3288 else { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3289 arglen = -1; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3290 argidx = -2; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3291 } |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3292 if (args->ob_type->tp_as_mapping) |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3293 dict = args; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3294 while (--fmtcnt >= 0) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3295 if (*fmt != '%') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3296 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
|
3297 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
|
3298 reslen += rescnt; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3299 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
|
3300 return NULL; |
| 21254 | 3301 res = PyString_AS_STRING(result) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3302 + reslen - rescnt; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3303 --rescnt; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3304 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3305 *res++ = *fmt++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3306 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3307 else { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3308 /* Got a format specifier */ |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3309 int flags = 0; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3310 int width = -1; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3311 int prec = -1; |
|
1408
18df674539da
Three micro fixes to formatstring
Guido van Rossum <guido@python.org>
parents:
1380
diff
changeset
|
3312 int c = '\0'; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3313 int fill; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3314 PyObject *v = NULL; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3315 PyObject *temp = NULL; |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3316 char *pbuf; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3317 int sign; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3318 int len; |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
3319 char formatbuf[FORMATBUFLEN]; |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
3320 /* For format{float,int,char}() */ |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
3321 #ifdef Py_USING_UNICODE |
| 12328 | 3322 char *fmt_start = fmt; |
|
17655
0470babdae49
Fix for bug #417030: "print '%*s' fails for unicode string"
Marc-André Lemburg <mal@egenix.com>
parents:
17645
diff
changeset
|
3323 int argidx_start = argidx; |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
3324 #endif |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
3325 |
| 4588 | 3326 fmt++; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3327 if (*fmt == '(') { |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3328 char *keystart; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3329 int keylen; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3330 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
|
3331 int pcount = 1; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3332 |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3333 if (dict == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3334 PyErr_SetString(PyExc_TypeError, |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
3335 "format requires a mapping"); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3336 goto error; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3337 } |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3338 ++fmt; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3339 --fmtcnt; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3340 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
|
3341 /* 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
|
3342 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
|
3343 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
|
3344 --pcount; |
|
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
3345 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
|
3346 ++pcount; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3347 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
|
3348 } |
|
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
3349 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
|
3350 if (fmtcnt < 0 || pcount > 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3351 PyErr_SetString(PyExc_ValueError, |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3352 "incomplete format key"); |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3353 goto error; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3354 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3355 key = PyString_FromStringAndSize(keystart, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3356 keylen); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3357 if (key == NULL) |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3358 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
|
3359 if (args_owned) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3360 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
|
3361 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
|
3362 } |
|
977ff1ace070
Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
3363 args = PyObject_GetItem(dict, key); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3364 Py_DECREF(key); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3365 if (args == NULL) { |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3366 goto error; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3367 } |
|
3570
977ff1ace070
Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
3368 args_owned = 1; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3369 arglen = -1; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3370 argidx = -2; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3371 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3372 while (--fmtcnt >= 0) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3373 switch (c = *fmt++) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3374 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
|
3375 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
|
3376 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
|
3377 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
|
3378 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
|
3379 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3380 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3381 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3382 if (c == '*') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3383 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
|
3384 if (v == NULL) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3385 goto error; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3386 if (!PyInt_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3387 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3388 "* wants int"); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3389 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3390 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3391 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
|
3392 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
|
3393 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
|
3394 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
|
3395 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3396 if (--fmtcnt >= 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3397 c = *fmt++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3398 } |
|
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
|
3399 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
|
3400 width = c - '0'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3401 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
|
3402 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
|
3403 if (!isdigit(c)) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3404 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3405 if ((width*10) / 10 != width) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3406 PyErr_SetString( |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3407 PyExc_ValueError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3408 "width too big"); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3409 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3410 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3411 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
|
3412 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3413 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3414 if (c == '.') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3415 prec = 0; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3416 if (--fmtcnt >= 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3417 c = *fmt++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3418 if (c == '*') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3419 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
|
3420 if (v == NULL) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3421 goto error; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3422 if (!PyInt_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3423 PyErr_SetString( |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3424 PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3425 "* wants int"); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3426 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3427 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3428 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
|
3429 if (prec < 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3430 prec = 0; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3431 if (--fmtcnt >= 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3432 c = *fmt++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3433 } |
|
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
|
3434 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
|
3435 prec = c - '0'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3436 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
|
3437 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
|
3438 if (!isdigit(c)) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3439 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3440 if ((prec*10) / 10 != prec) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3441 PyErr_SetString( |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3442 PyExc_ValueError, |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3443 "prec too big"); |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3444 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3445 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3446 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
|
3447 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3448 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3449 } /* prec */ |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3450 if (fmtcnt >= 0) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3451 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
|
3452 if (--fmtcnt >= 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3453 c = *fmt++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3454 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3455 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3456 if (fmtcnt < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3457 PyErr_SetString(PyExc_ValueError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3458 "incomplete format"); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3459 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3460 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3461 if (c != '%') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3462 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
|
3463 if (v == NULL) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3464 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3465 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3466 sign = 0; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3467 fill = ' '; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3468 switch (c) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3469 case '%': |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3470 pbuf = "%"; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3471 len = 1; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3472 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3473 case 's': |
| 12328 | 3474 case 'r': |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
3475 #ifdef Py_USING_UNICODE |
| 12328 | 3476 if (PyUnicode_Check(v)) { |
| 3477 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
|
3478 argidx = argidx_start; |
| 12328 | 3479 goto unicode; |
| 3480 } | |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
3481 #endif |
| 12371 | 3482 if (c == 's') |
| 21254 | 3483 temp = PyObject_Str(v); |
| 12371 | 3484 else |
| 3485 temp = PyObject_Repr(v); | |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3486 if (temp == NULL) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3487 goto error; |
|
8468
c27cd5fc46b5
Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
3488 if (!PyString_Check(temp)) { |
|
c27cd5fc46b5
Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
3489 PyErr_SetString(PyExc_TypeError, |
|
c27cd5fc46b5
Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
3490 "%s argument has non-string str()"); |
| 21254 | 3491 Py_DECREF(temp); |
|
8468
c27cd5fc46b5
Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
3492 goto error; |
|
c27cd5fc46b5
Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
3493 } |
| 21254 | 3494 pbuf = PyString_AS_STRING(temp); |
| 3495 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
|
3496 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
|
3497 len = prec; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3498 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3499 case 'i': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3500 case 'd': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3501 case 'u': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3502 case 'o': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3503 case 'x': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3504 case 'X': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3505 if (c == 'i') |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3506 c = 'd'; |
|
15455
d0146d128897
Fox for SF bug #123859: %[duxXo] long formats inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
15435
diff
changeset
|
3507 if (PyLong_Check(v)) { |
|
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
|
3508 temp = _PyString_FormatLong(v, flags, |
|
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
|
3509 prec, c, &pbuf, &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
|
3510 if (!temp) |
|
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
|
3511 goto 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
|
3512 /* unbounded ints can always produce |
|
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
|
3513 a sign character! */ |
|
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
|
3514 sign = 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
|
3515 } |
|
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
|
3516 else { |
|
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
|
3517 pbuf = formatbuf; |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
3518 len = formatint(pbuf, |
|
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
3519 sizeof(formatbuf), |
|
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
|
3520 flags, prec, c, v); |
|
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
|
3521 if (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
|
3522 goto 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
|
3523 /* only d conversion is signed */ |
|
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
|
3524 sign = c == '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
|
3525 } |
|
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
|
3526 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
|
3527 fill = '0'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3528 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3529 case 'e': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3530 case 'E': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3531 case 'f': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3532 case 'g': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3533 case 'G': |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3534 pbuf = formatbuf; |
|
23046
769e0a538e54
Remove 'const' from local variable declaration in string_zfill() -- it
Guido van Rossum <guido@python.org>
parents:
23045
diff
changeset
|
3535 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
|
3536 flags, prec, c, v); |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
3537 if (len < 0) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3538 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3539 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
|
3540 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
|
3541 fill = '0'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3542 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3543 case 'c': |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3544 pbuf = formatbuf; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3545 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
|
3546 if (len < 0) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3547 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3548 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3549 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
|
3550 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
|
3551 "unsupported format character '%c' (0x%x) " |
|
31d78f361530
[ Patch #102852 ] Make % error a bit more informative by indicates the
Andrew M. Kuchling <amk@amk.ca>
parents:
15484
diff
changeset
|
3552 "at index %i", |
|
31d78f361530
[ Patch #102852 ] Make % error a bit more informative by indicates the
Andrew M. Kuchling <amk@amk.ca>
parents:
15484
diff
changeset
|
3553 c, c, fmt - 1 - PyString_AsString(format)); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3554 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3555 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3556 if (sign) { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3557 if (*pbuf == '-' || *pbuf == '+') { |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3558 sign = *pbuf++; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3559 len--; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3560 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3561 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
|
3562 sign = '+'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3563 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
|
3564 sign = ' '; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3565 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
|
3566 sign = 0; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3567 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3568 if (width < len) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3569 width = len; |
|
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
|
3570 if (rescnt < width + (sign != 0)) { |
|
1282
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
3571 reslen -= rescnt; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
3572 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
|
3573 reslen += rescnt; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3574 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
|
3575 return NULL; |
| 21254 | 3576 res = PyString_AS_STRING(result) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3577 + reslen - rescnt; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3578 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3579 if (sign) { |
| 1409 | 3580 if (fill != ' ') |
| 3581 *res++ = sign; | |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3582 rescnt--; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3583 if (width > len) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3584 width--; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3585 } |
|
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
|
3586 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
|
3587 assert(pbuf[0] == '0'); |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3588 assert(pbuf[1] == c); |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3589 if (fill != ' ') { |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3590 *res++ = *pbuf++; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3591 *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
|
3592 } |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3593 rescnt -= 2; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3594 width -= 2; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3595 if (width < 0) |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3596 width = 0; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3597 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
|
3598 } |
|
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
|
3599 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
|
3600 do { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3601 --rescnt; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3602 *res++ = fill; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3603 } while (--width > len); |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3604 } |
|
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
|
3605 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
|
3606 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
|
3607 *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
|
3608 if ((flags & F_ALT) && |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3609 (c == 'x' || c == 'X')) { |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3610 assert(pbuf[0] == '0'); |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3611 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
|
3612 *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
|
3613 *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
|
3614 } |
|
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
|
3615 } |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3616 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
|
3617 res += len; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3618 rescnt -= len; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3619 while (--width >= len) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3620 --rescnt; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3621 *res++ = ' '; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3622 } |
|
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
|
3623 if (dict && (argidx < arglen) && c != '%') { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3624 PyErr_SetString(PyExc_TypeError, |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3625 "not all arguments converted"); |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3626 goto error; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3627 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3628 Py_XDECREF(temp); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3629 } /* '%' */ |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3630 } /* 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
|
3631 if (argidx < arglen && !dict) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3632 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3633 "not all arguments converted"); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3634 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3635 } |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
6286
diff
changeset
|
3636 if (args_owned) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3637 Py_DECREF(args); |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
6286
diff
changeset
|
3638 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3639 _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
|
3640 return result; |
| 12328 | 3641 |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
3642 #ifdef Py_USING_UNICODE |
| 12328 | 3643 unicode: |
| 3644 if (args_owned) { | |
| 3645 Py_DECREF(args); | |
| 3646 args_owned = 0; | |
| 3647 } | |
|
17655
0470babdae49
Fix for bug #417030: "print '%*s' fails for unicode string"
Marc-André Lemburg <mal@egenix.com>
parents:
17645
diff
changeset
|
3648 /* Fiddle args right (remove the first argidx arguments) */ |
| 12328 | 3649 if (PyTuple_Check(orig_args) && argidx > 0) { |
| 3650 PyObject *v; | |
| 3651 int n = PyTuple_GET_SIZE(orig_args) - argidx; | |
| 3652 v = PyTuple_New(n); | |
| 3653 if (v == NULL) | |
| 3654 goto error; | |
| 3655 while (--n >= 0) { | |
| 3656 PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx); | |
| 3657 Py_INCREF(w); | |
| 3658 PyTuple_SET_ITEM(v, n, w); | |
| 3659 } | |
| 3660 args = v; | |
| 3661 } else { | |
| 3662 Py_INCREF(orig_args); | |
| 3663 args = orig_args; | |
| 3664 } | |
|
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
|
3665 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
|
3666 /* 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
|
3667 function format the rest of the input. */ |
| 12328 | 3668 rescnt = res - PyString_AS_STRING(result); |
|
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
|
3669 if (_PyString_Resize(&result, rescnt)) |
|
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
|
3670 goto error; |
| 12328 | 3671 fmtcnt = PyString_GET_SIZE(format) - \ |
| 3672 (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
|
3673 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
|
3674 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
|
3675 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
|
3676 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
|
3677 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
|
3678 if (v == NULL) |
| 12328 | 3679 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
|
3680 /* 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
|
3681 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
|
3682 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
|
3683 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
|
3684 Py_DECREF(v); |
| 12328 | 3685 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
|
3686 return w; |
|
19343
7257e87e0720
Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents:
19301
diff
changeset
|
3687 #endif /* Py_USING_UNICODE */ |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
3688 |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3689 error: |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3690 Py_DECREF(result); |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
6286
diff
changeset
|
3691 if (args_owned) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3692 Py_DECREF(args); |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
6286
diff
changeset
|
3693 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3694 return NULL; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3695 } |
| 5005 | 3696 |
| 3697 | |
| 3698 | |
|
14018
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3699 /* This dictionary will leak at PyString_Fini() time. That's acceptable |
|
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3700 * because PyString_Fini() specifically frees interned strings that are |
|
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3701 * only referenced by this dictionary. The CVS log entry for revision 2.45 |
|
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3702 * says: |
|
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3703 * |
|
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3704 * Change the Fini function to only remove otherwise unreferenced |
|
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3705 * strings from the interned table. There are references in |
|
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3706 * hard-to-find static variables all over the interpreter, and it's not |
|
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3707 * worth trying to get rid of all those; but "uninterning" isn't fair |
|
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3708 * either and may cause subtle failures later -- so we have to keep them |
|
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3709 * in the interned table. |
|
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3710 */ |
| 5005 | 3711 static PyObject *interned; |
| 3712 | |
| 3713 void | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3714 PyString_InternInPlace(PyObject **p) |
| 5005 | 3715 { |
| 3716 register PyStringObject *s = (PyStringObject *)(*p); | |
| 3717 PyObject *t; | |
| 3718 if (s == NULL || !PyString_Check(s)) | |
| 3719 Py_FatalError("PyString_InternInPlace: strings only please!"); | |
| 3720 if ((t = s->ob_sinterned) != NULL) { | |
| 3721 if (t == (PyObject *)s) | |
| 3722 return; | |
| 3723 Py_INCREF(t); | |
| 3724 *p = t; | |
| 3725 Py_DECREF(s); | |
| 3726 return; | |
| 3727 } | |
| 3728 if (interned == NULL) { | |
| 3729 interned = PyDict_New(); | |
| 3730 if (interned == NULL) | |
| 3731 return; | |
| 3732 } | |
| 3733 if ((t = PyDict_GetItem(interned, (PyObject *)s)) != NULL) { | |
| 3734 Py_INCREF(t); | |
| 3735 *p = s->ob_sinterned = t; | |
| 3736 Py_DECREF(s); | |
| 3737 return; | |
| 3738 } | |
|
19887
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3739 /* Ensure that only true string objects appear in the intern dict, |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3740 and as the value of ob_sinterned. */ |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3741 if (PyString_CheckExact(s)) { |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3742 t = (PyObject *)s; |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3743 if (PyDict_SetItem(interned, t, t) == 0) { |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3744 s->ob_sinterned = t; |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3745 return; |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3746 } |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3747 } |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3748 else { |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3749 t = PyString_FromStringAndSize(PyString_AS_STRING(s), |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3750 PyString_GET_SIZE(s)); |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3751 if (t != NULL) { |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3752 if (PyDict_SetItem(interned, t, t) == 0) { |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3753 *p = s->ob_sinterned = t; |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3754 Py_DECREF(s); |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3755 return; |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3756 } |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3757 Py_DECREF(t); |
|
b28fc8c36e3d
If interning an instance of a string subclass, intern a real string object
Tim Peters <tim.peters@gmail.com>
parents:
19886
diff
changeset
|
3758 } |
| 5005 | 3759 } |
| 3760 PyErr_Clear(); | |
| 3761 } | |
| 3762 | |
| 3763 | |
| 3764 PyObject * | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3765 PyString_InternFromString(const char *cp) |
| 5005 | 3766 { |
| 3767 PyObject *s = PyString_FromString(cp); | |
| 3768 if (s == NULL) | |
| 3769 return NULL; | |
| 3770 PyString_InternInPlace(&s); | |
| 3771 return s; | |
| 3772 } | |
| 3773 | |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3774 void |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3775 PyString_Fini(void) |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3776 { |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3777 int i; |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3778 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
|
3779 Py_XDECREF(characters[i]); |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3780 characters[i] = NULL; |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3781 } |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3782 Py_XDECREF(nullstring); |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3783 nullstring = NULL; |
|
5935
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3784 if (interned) { |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3785 int pos, changed; |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3786 PyObject *key, *value; |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3787 do { |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3788 changed = 0; |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3789 pos = 0; |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3790 while (PyDict_Next(interned, &pos, &key, &value)) { |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3791 if (key->ob_refcnt == 2 && key == value) { |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3792 PyDict_DelItem(interned, key); |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3793 changed = 1; |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3794 } |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3795 } |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3796 } while (changed); |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3797 } |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3798 } |
|
16751
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3799 |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3800 void _Py_ReleaseInternedStrings(void) |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3801 { |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3802 if (interned) { |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17450
diff
changeset
|
3803 fprintf(stderr, "releasing interned strings\n"); |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17450
diff
changeset
|
3804 PyDict_Clear(interned); |
|
16751
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3805 Py_DECREF(interned); |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3806 interned = NULL; |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3807 } |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3808 } |
