Mercurial > cpython-lru_cache-weakref
annotate Objects/stringobject.c @ 18130:8eea2efb34cd legacy-trunk
Reformat decl of new _PyString_Join. Add NEWS blurb about repr() speedup.
| author | Tim Peters <tim.peters@gmail.com> |
|---|---|
| date | Sat, 16 Jun 2001 05:42:57 +0000 |
| parents | 9d533ae574d2 |
| children | 9b291ca6546c |
| rev | line source |
|---|---|
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
136
diff
changeset
|
1 |
| 6 | 2 /* String object implementation */ |
| 3 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
4 #include "Python.h" |
| 6 | 5 |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
6 #include <ctype.h> |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
7 |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
8 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
9 int null_strings, one_strings; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
10 #endif |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
11 |
|
14869
25ddd3dc7a7a
Rationalize use of limits.h, moving the inclusion to Python.h.
Fred Drake <fdrake@acm.org>
parents:
14713
diff
changeset
|
12 #if !defined(HAVE_LIMITS_H) && !defined(UCHAR_MAX) |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
13 #define UCHAR_MAX 255 |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
14 #endif |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
15 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
16 static PyStringObject *characters[UCHAR_MAX + 1]; |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1368
diff
changeset
|
17 #ifndef DONT_SHARE_SHORT_STRINGS |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
18 static PyStringObject *nullstring; |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1368
diff
changeset
|
19 #endif |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
20 |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
21 /* |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
22 Newsizedstringobject() and newstringobject() try in certain cases |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
23 to share string objects. When the size of the string is zero, |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
24 these routines always return a pointer to the same string object; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
25 when the size is one, they return a pointer to an already existing |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
26 object if the contents of the string is known. For |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
27 newstringobject() this is always the case, for |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
28 newsizedstringobject() this is the case when the first argument in |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
29 not NULL. |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
30 A common practice to allocate a string and then fill it in or |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
31 change it must be done carefully. It is only allowed to change the |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
32 contents of the string if the obect was gotten from |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
33 newsizedstringobject() with a NULL first argument, because in the |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
34 future these routines may try to do even more sharing of objects. |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
35 */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
36 PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
37 PyString_FromStringAndSize(const char *str, int size) |
| 6 | 38 { |
|
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
|
39 register PyStringObject *op; |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1368
diff
changeset
|
40 #ifndef DONT_SHARE_SHORT_STRINGS |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
41 if (size == 0 && (op = nullstring) != NULL) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
42 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
43 null_strings++; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
44 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
45 Py_INCREF(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
46 return (PyObject *)op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
47 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
48 if (size == 1 && str != NULL && |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
49 (op = characters[*str & UCHAR_MAX]) != NULL) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
50 { |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
51 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
52 one_strings++; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
53 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
54 Py_INCREF(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
55 return (PyObject *)op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
56 } |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1368
diff
changeset
|
57 #endif /* DONT_SHARE_SHORT_STRINGS */ |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
58 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
59 /* PyObject_NewVar is inlined */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
60 op = (PyStringObject *) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
61 PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char)); |
| 27 | 62 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
63 return PyErr_NoMemory(); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
64 PyObject_INIT_VAR(op, &PyString_Type, size); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
65 #ifdef CACHE_HASH |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
66 op->ob_shash = -1; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
67 #endif |
| 5005 | 68 #ifdef INTERN_STRINGS |
| 69 op->ob_sinterned = NULL; | |
| 70 #endif | |
| 27 | 71 if (str != NULL) |
| 72 memcpy(op->ob_sval, str, size); | |
| 73 op->ob_sval[size] = '\0'; | |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1368
diff
changeset
|
74 #ifndef DONT_SHARE_SHORT_STRINGS |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
75 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
|
76 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
|
77 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
|
78 op = (PyStringObject *)t; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
79 nullstring = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
80 Py_INCREF(op); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
81 } 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
|
82 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
|
83 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
|
84 op = (PyStringObject *)t; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
85 characters[*str & UCHAR_MAX] = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
86 Py_INCREF(op); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
87 } |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1368
diff
changeset
|
88 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
89 return (PyObject *) op; |
| 6 | 90 } |
| 91 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
92 PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
93 PyString_FromString(const char *str) |
| 6 | 94 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
95 register size_t size = strlen(str); |
|
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
|
96 register PyStringObject *op; |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
97 if (size > INT_MAX) { |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
98 PyErr_SetString(PyExc_OverflowError, |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
99 "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
|
100 return NULL; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
101 } |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1368
diff
changeset
|
102 #ifndef DONT_SHARE_SHORT_STRINGS |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
103 if (size == 0 && (op = nullstring) != NULL) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
104 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
105 null_strings++; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
106 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
107 Py_INCREF(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
108 return (PyObject *)op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
109 } |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
110 if (size == 1 && (op = characters[*str & UCHAR_MAX]) != NULL) { |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
111 #ifdef COUNT_ALLOCS |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
112 one_strings++; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
113 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
114 Py_INCREF(op); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
115 return (PyObject *)op; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
116 } |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1368
diff
changeset
|
117 #endif /* DONT_SHARE_SHORT_STRINGS */ |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
118 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
119 /* PyObject_NewVar is inlined */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
120 op = (PyStringObject *) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
121 PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char)); |
| 27 | 122 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
123 return PyErr_NoMemory(); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
124 PyObject_INIT_VAR(op, &PyString_Type, size); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
125 #ifdef CACHE_HASH |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
126 op->ob_shash = -1; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
127 #endif |
| 5005 | 128 #ifdef INTERN_STRINGS |
| 129 op->ob_sinterned = NULL; | |
| 130 #endif | |
| 27 | 131 strcpy(op->ob_sval, str); |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1368
diff
changeset
|
132 #ifndef DONT_SHARE_SHORT_STRINGS |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
133 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
|
134 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
|
135 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
|
136 op = (PyStringObject *)t; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
137 nullstring = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
138 Py_INCREF(op); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
139 } 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
|
140 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
|
141 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
|
142 op = (PyStringObject *)t; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
143 characters[*str & UCHAR_MAX] = op; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
144 Py_INCREF(op); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
145 } |
|
1380
a4aa83012af3
Fixed bugs in resizetuple and extended the interface.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1368
diff
changeset
|
146 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
147 return (PyObject *) op; |
| 6 | 148 } |
| 149 | |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
150 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
|
151 int size, |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
152 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
|
153 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
|
154 { |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
155 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
|
156 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
157 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
|
158 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
|
159 return NULL; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
160 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
|
161 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
|
162 return v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
163 } |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
164 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
165 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
|
166 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
|
167 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
|
168 { |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
169 PyObject *v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
170 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
171 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
|
172 PyErr_BadArgument(); |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
173 goto onError; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
174 } |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
175 |
|
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
176 if (encoding == NULL) |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
177 encoding = PyUnicode_GetDefaultEncoding(); |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
178 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
179 /* 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
|
180 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
|
181 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
|
182 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
|
183 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
184 return v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
185 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
186 onError: |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
187 return NULL; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
188 } |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
189 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
190 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
|
191 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
|
192 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
|
193 { |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
194 PyObject *v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
195 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
196 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
|
197 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
|
198 goto onError; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
199 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
200 /* 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
|
201 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
|
202 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
|
203 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
|
204 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
|
205 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
|
206 goto onError; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
207 } |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
208 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
|
209 PyErr_Format(PyExc_TypeError, |
|
13561
ea732719b3fd
Fix typo in error message
Andrew M. Kuchling <amk@amk.ca>
parents:
13544
diff
changeset
|
210 "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
|
211 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
|
212 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
|
213 goto onError; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
214 } |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
215 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
216 return v; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
217 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
218 onError: |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
219 return NULL; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
220 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
221 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
222 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
|
223 int size, |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
224 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
|
225 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
|
226 { |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
227 PyObject *v, *str; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
228 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
229 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
|
230 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
|
231 return NULL; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
232 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
|
233 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
|
234 return v; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
235 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
236 |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
237 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
|
238 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
|
239 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
|
240 { |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
241 PyObject *v; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
242 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
243 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
|
244 PyErr_BadArgument(); |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
245 goto onError; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
246 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
247 |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
248 if (encoding == NULL) |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
249 encoding = PyUnicode_GetDefaultEncoding(); |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
250 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
251 /* 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
|
252 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
|
253 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
|
254 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
|
255 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
256 return v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
257 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
258 onError: |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
259 return NULL; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
260 } |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
261 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
262 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
|
263 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
|
264 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
|
265 { |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
266 PyObject *v; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
267 |
|
18086
592109f2a6fd
Fix for bug #432384: Recursion in PyString_AsEncodedString?
Marc-André Lemburg <mal@egenix.com>
parents:
17964
diff
changeset
|
268 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
|
269 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
|
270 goto onError; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
271 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
272 /* 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
|
273 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
|
274 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
|
275 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
|
276 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
|
277 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
|
278 goto onError; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
279 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
280 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
|
281 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
|
282 "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
|
283 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
|
284 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
|
285 goto onError; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
286 } |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
287 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
288 return v; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
289 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
290 onError: |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
291 return NULL; |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
292 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
293 |
|
1313
6ee380349c84
* Added gmtime/localtime/mktime and SYSV timezone globals to timemodule.c.
Guido van Rossum <guido@python.org>
parents:
1282
diff
changeset
|
294 static void |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
295 string_dealloc(PyObject *op) |
| 750 | 296 { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
297 PyObject_DEL(op); |
| 750 | 298 } |
| 299 | |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
300 static int |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
301 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
|
302 { |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
303 char *s; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
304 int len; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
305 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
|
306 return -1; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
307 return len; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
308 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
309 |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
310 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
|
311 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
|
312 { |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
313 char *s; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
314 int len; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
315 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
|
316 return NULL; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
317 return s; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
318 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
319 |
|
2053
de49d49044d7
Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents:
1908
diff
changeset
|
320 int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
321 PyString_Size(register PyObject *op) |
| 6 | 322 { |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
323 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
|
324 return string_getsize(op); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
325 return ((PyStringObject *)op) -> ob_size; |
| 6 | 326 } |
| 327 | |
| 328 /*const*/ char * | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
329 PyString_AsString(register PyObject *op) |
| 6 | 330 { |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
331 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
|
332 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
|
333 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
|
334 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
335 |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
336 /* Internal API needed by PyString_AsStringAndSize(): */ |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
337 extern |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
338 PyObject *_PyUnicode_AsDefaultEncodedString(PyObject *unicode, |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
339 const char *errors); |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
340 |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
341 int |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
342 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
|
343 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
|
344 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
|
345 { |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
346 if (s == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
347 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
|
348 return -1; |
| 6 | 349 } |
|
14696
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
350 |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
351 if (!PyString_Check(obj)) { |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
352 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
|
353 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
|
354 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
|
355 return -1; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
356 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
357 else { |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
358 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
|
359 "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
|
360 "%.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
|
361 return -1; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
362 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
363 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
364 |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
365 *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
|
366 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
|
367 *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
|
368 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
|
369 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
|
370 "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
|
371 return -1; |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
372 } |
|
85038a024b19
This patch adds a new Python C API called PyString_AsStringAndSize()
Marc-André Lemburg <mal@egenix.com>
parents:
14492
diff
changeset
|
373 return 0; |
| 6 | 374 } |
| 375 | |
| 376 /* Methods */ | |
| 377 | |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
378 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
379 string_print(PyStringObject *op, FILE *fp, int flags) |
| 6 | 380 { |
| 381 int i; | |
| 382 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
|
383 int quote; |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
384 /* XXX Ought to check for interrupts when writing long strings */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
385 if (flags & Py_PRINT_RAW) { |
| 6 | 386 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
|
387 return 0; |
| 6 | 388 } |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
389 |
|
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
|
390 /* 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
|
391 quote = '\''; |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
392 if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"')) |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
393 quote = '"'; |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
394 |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
395 fputc(quote, fp); |
| 6 | 396 for (i = 0; i < op->ob_size; i++) { |
| 397 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
|
398 if (c == quote || c == '\\') |
| 6 | 399 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
|
400 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
|
401 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
|
402 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
|
403 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
|
404 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
|
405 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
|
406 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
|
407 fprintf(fp, "\\x%02x", c & 0xff); |
| 6 | 408 else |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
409 fputc(c, fp); |
| 6 | 410 } |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
411 fputc(quote, fp); |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
412 return 0; |
| 6 | 413 } |
| 414 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
415 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
416 string_repr(register PyStringObject *op) |
| 6 | 417 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
418 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
|
419 PyObject *v; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
420 if (newsize > INT_MAX) { |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
421 PyErr_SetString(PyExc_OverflowError, |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
422 "string is too large to make repr"); |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
423 } |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
424 v = PyString_FromStringAndSize((char *)NULL, newsize); |
| 6 | 425 if (v == NULL) { |
|
287
07efda2951a8
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
259
diff
changeset
|
426 return NULL; |
| 6 | 427 } |
| 428 else { | |
| 429 register int i; | |
| 430 register char c; | |
| 431 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
|
432 int quote; |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
433 |
|
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
|
434 /* 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
|
435 quote = '\''; |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
436 if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"')) |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
437 quote = '"'; |
|
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
438 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
439 p = ((PyStringObject *)v)->ob_sval; |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
440 *p++ = quote; |
| 6 | 441 for (i = 0; i < op->ob_size; i++) { |
| 442 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
|
443 if (c == quote || c == '\\') |
| 6 | 444 *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
|
445 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
|
446 *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
|
447 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
|
448 *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
|
449 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
|
450 *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
|
451 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
|
452 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
|
453 p += 4; |
| 6 | 454 } |
| 455 else | |
| 456 *p++ = c; | |
| 457 } | |
|
1368
4c8e5fcb3301
Add some necessary casts; use double quotes to represent strings in
Guido van Rossum <guido@python.org>
parents:
1356
diff
changeset
|
458 *p++ = quote; |
| 6 | 459 *p = '\0'; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
460 _PyString_Resize( |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
461 &v, (int) (p - ((PyStringObject *)v)->ob_sval)); |
| 27 | 462 return v; |
| 6 | 463 } |
| 464 } | |
| 465 | |
|
17645
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
466 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
|
467 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
|
468 { |
|
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
469 Py_INCREF(s); |
|
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
470 return s; |
|
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
471 } |
|
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
472 |
| 6 | 473 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
474 string_length(PyStringObject *a) |
| 6 | 475 { |
| 476 return a->ob_size; | |
| 477 } | |
| 478 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
479 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
480 string_concat(register PyStringObject *a, register PyObject *bb) |
| 6 | 481 { |
| 482 register unsigned int size; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
483 register PyStringObject *op; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
484 if (!PyString_Check(bb)) { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
485 if (PyUnicode_Check(bb)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
486 return PyUnicode_Concat((PyObject *)a, bb); |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
487 PyErr_Format(PyExc_TypeError, |
|
12816
4272b368bf69
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12615
diff
changeset
|
488 "cannot add type \"%.200s\" to string", |
|
4272b368bf69
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
12615
diff
changeset
|
489 bb->ob_type->tp_name); |
| 6 | 490 return NULL; |
| 491 } | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
492 #define b ((PyStringObject *)bb) |
| 6 | 493 /* Optimize cases with empty left or right operand */ |
| 494 if (a->ob_size == 0) { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
495 Py_INCREF(bb); |
| 6 | 496 return bb; |
| 497 } | |
| 498 if (b->ob_size == 0) { | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
499 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
500 return (PyObject *)a; |
| 6 | 501 } |
| 502 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
|
503 /* PyObject_NewVar is inlined */ |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
504 op = (PyStringObject *) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
505 PyObject_MALLOC(sizeof(PyStringObject) + size * sizeof(char)); |
| 27 | 506 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
507 return PyErr_NoMemory(); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
508 PyObject_INIT_VAR(op, &PyString_Type, size); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
509 #ifdef CACHE_HASH |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
510 op->ob_shash = -1; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
511 #endif |
| 5005 | 512 #ifdef INTERN_STRINGS |
| 513 op->ob_sinterned = NULL; | |
| 514 #endif | |
| 27 | 515 memcpy(op->ob_sval, a->ob_sval, (int) a->ob_size); |
| 516 memcpy(op->ob_sval + a->ob_size, b->ob_sval, (int) b->ob_size); | |
| 517 op->ob_sval[size] = '\0'; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
518 return (PyObject *) op; |
| 6 | 519 #undef b |
| 520 } | |
| 521 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
522 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
523 string_repeat(register PyStringObject *a, register int n) |
| 6 | 524 { |
| 525 register int i; | |
|
5288
0f1711b7d367
Tweaks to keep the Microsoft compiler quiet.
Guido van Rossum <guido@python.org>
parents:
5098
diff
changeset
|
526 register int size; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
527 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
|
528 size_t nbytes; |
| 6 | 529 if (n < 0) |
| 530 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
|
531 /* 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
|
532 * 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
|
533 */ |
| 6 | 534 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
|
535 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
|
536 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
|
537 "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
|
538 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
|
539 } |
| 6 | 540 if (size == a->ob_size) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
541 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
542 return (PyObject *)a; |
| 6 | 543 } |
|
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
|
544 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
|
545 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
|
546 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
|
547 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
|
548 "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
|
549 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
|
550 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
551 op = (PyStringObject *) |
|
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
|
552 PyObject_MALLOC(sizeof(PyStringObject) + nbytes); |
| 27 | 553 if (op == NULL) |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
554 return PyErr_NoMemory(); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
555 PyObject_INIT_VAR(op, &PyString_Type, size); |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
556 #ifdef CACHE_HASH |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
557 op->ob_shash = -1; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
558 #endif |
| 5005 | 559 #ifdef INTERN_STRINGS |
| 560 op->ob_sinterned = NULL; | |
| 561 #endif | |
| 27 | 562 for (i = 0; i < size; i += a->ob_size) |
| 563 memcpy(op->ob_sval+i, a->ob_sval, (int) a->ob_size); | |
| 564 op->ob_sval[size] = '\0'; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
565 return (PyObject *) op; |
| 6 | 566 } |
| 567 | |
| 568 /* String slice a[i:j] consists of characters a[i] ... a[j-1] */ | |
| 569 | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
570 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
571 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
|
572 /* j -- may be negative! */ |
| 6 | 573 { |
| 574 if (i < 0) | |
| 575 i = 0; | |
| 576 if (j < 0) | |
| 577 j = 0; /* Avoid signed/unsigned bug in next line */ | |
| 578 if (j > a->ob_size) | |
| 579 j = a->ob_size; | |
| 580 if (i == 0 && j == a->ob_size) { /* It's the same as a */ | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
581 Py_INCREF(a); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
582 return (PyObject *)a; |
| 6 | 583 } |
| 584 if (j < i) | |
| 585 j = i; | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
586 return PyString_FromStringAndSize(a->ob_sval + i, (int) (j-i)); |
| 6 | 587 } |
| 588 | |
|
11942
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
589 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
590 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
|
591 { |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
592 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
|
593 register char c; |
| 12328 | 594 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
|
595 return PyUnicode_Contains(a, el); |
| 12328 | 596 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
|
597 PyErr_SetString(PyExc_TypeError, |
|
12902
98eb3059ae76
Patch from Michael Hudson: improve unclear error message
Andrew M. Kuchling <amk@amk.ca>
parents:
12816
diff
changeset
|
598 "'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
|
599 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
|
600 } |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
601 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
|
602 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
|
603 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
|
604 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
|
605 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
|
606 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
|
607 } |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
608 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
|
609 } |
|
421b1620f582
Patch by Moshe Zadka: move the string special case from abstract.c
Guido van Rossum <guido@python.org>
parents:
11919
diff
changeset
|
610 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
611 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
612 string_item(PyStringObject *a, register int i) |
| 6 | 613 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
614 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
|
615 char *pchar; |
| 6 | 616 if (i < 0 || i >= a->ob_size) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
617 PyErr_SetString(PyExc_IndexError, "string index out of range"); |
| 6 | 618 return NULL; |
| 619 } | |
|
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
|
620 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
|
621 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
|
622 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
|
623 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
|
624 else { |
|
2a7cfc172a68
Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents:
17711
diff
changeset
|
625 #ifdef COUNT_ALLOCS |
|
2a7cfc172a68
Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents:
17711
diff
changeset
|
626 one_strings++; |
|
2a7cfc172a68
Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents:
17711
diff
changeset
|
627 #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
|
628 Py_INCREF(v); |
|
17712
2a7cfc172a68
Ack! Restore the COUNT_ALLOCS one_strings code.
Tim Peters <tim.peters@gmail.com>
parents:
17711
diff
changeset
|
629 } |
|
180
a405a31242e7
Optimized single-character strings gotten from s[i].
Guido van Rossum <guido@python.org>
parents:
158
diff
changeset
|
630 return v; |
| 6 | 631 } |
| 632 | |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
633 static PyObject* |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
634 string_richcompare(PyStringObject *a, PyStringObject *b, int op) |
| 6 | 635 { |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
636 int c; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
637 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
|
638 int min_len; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
639 PyObject *result; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
640 |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
641 /* One of the objects is a string object. Make sure the |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
642 other one is one, too. */ |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
643 if (a->ob_type != b->ob_type) { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
644 result = Py_NotImplemented; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
645 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
646 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
647 if (a == b) { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
648 switch (op) { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
649 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
|
650 result = Py_True; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
651 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
652 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
|
653 result = Py_False; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
654 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
655 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
656 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
657 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
|
658 /* 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
|
659 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
|
660 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
|
661 && (a->ob_sval[0] == b->ob_sval[0] |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
662 && memcmp(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
|
663 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
|
664 result = Py_True; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
665 } else { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
666 result = Py_False; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
667 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
668 goto out; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
669 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
670 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
|
671 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
|
672 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
|
673 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
|
674 if (c==0) |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
675 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
|
676 }else |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
677 c = 0; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
678 if (c == 0) |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
679 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
|
680 switch (op) { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
681 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
|
682 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
|
683 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
|
684 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
|
685 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
|
686 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
|
687 default: |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
688 result = Py_NotImplemented; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
689 goto out; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
690 } |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
691 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
|
692 out: |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
693 Py_INCREF(result); |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
694 return result; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
695 } |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
696 |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
697 int |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
698 _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
|
699 { |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
700 PyStringObject *a, *b; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
701 a = (PyStringObject*)o1; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
702 b = (PyStringObject*)o2; |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
703 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
|
704 && *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
|
705 && memcmp(a->ob_sval, b->ob_sval, a->ob_size) == 0; |
| 6 | 706 } |
| 707 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
708 static long |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
709 string_hash(PyStringObject *a) |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
710 { |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
711 register int len; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
712 register unsigned char *p; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
713 register long x; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
714 |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
715 #ifdef CACHE_HASH |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
716 if (a->ob_shash != -1) |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
717 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
|
718 #ifdef INTERN_STRINGS |
|
a44586bd9fab
Slight tweak: in string_hash(), if the hash hasn't been computed yet,
Guido van Rossum <guido@python.org>
parents:
5057
diff
changeset
|
719 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
|
720 return (a->ob_shash = |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
721 ((PyStringObject *)(a->ob_sinterned))->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
|
722 #endif |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
723 #endif |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
724 len = a->ob_size; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
725 p = (unsigned char *) a->ob_sval; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
726 x = *p << 7; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
727 while (--len >= 0) |
|
4283
839f72610ae1
Multiply by 1000003 instead of 3 in string hach
Guido van Rossum <guido@python.org>
parents:
3586
diff
changeset
|
728 x = (1000003*x) ^ *p++; |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
729 x ^= a->ob_size; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
730 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
731 x = -2; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
732 #ifdef CACHE_HASH |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
733 a->ob_shash = x; |
|
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1347
diff
changeset
|
734 #endif |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
735 return x; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
736 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
737 |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
738 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
739 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
|
740 { |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
741 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
|
742 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
|
743 "accessing non-existent string segment"); |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
744 return -1; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
745 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
746 *ptr = (void *)self->ob_sval; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
747 return self->ob_size; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
748 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
749 |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
750 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
751 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
|
752 { |
|
6286
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
753 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
|
754 "Cannot use string as modifiable buffer"); |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
755 return -1; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
756 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
757 |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
758 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
759 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
|
760 { |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
761 if ( lenp ) |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
762 *lenp = self->ob_size; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
763 return 1; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
764 } |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
765 |
|
9305
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
766 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
767 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
|
768 { |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
769 if ( index != 0 ) { |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
770 PyErr_SetString(PyExc_SystemError, |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
771 "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
|
772 return -1; |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
773 } |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
774 *ptr = self->ob_sval; |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
775 return self->ob_size; |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
776 } |
|
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
777 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
778 static PySequenceMethods string_as_sequence = { |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
779 (inquiry)string_length, /*sq_length*/ |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
780 (binaryfunc)string_concat, /*sq_concat*/ |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
781 (intargfunc)string_repeat, /*sq_repeat*/ |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
782 (intargfunc)string_item, /*sq_item*/ |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
783 (intintargfunc)string_slice, /*sq_slice*/ |
|
259
6321d3dc57e0
Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents:
180
diff
changeset
|
784 0, /*sq_ass_item*/ |
|
6321d3dc57e0
Fix comments in string_as_sequence
Guido van Rossum <guido@python.org>
parents:
180
diff
changeset
|
785 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
|
786 (objobjproc)string_contains /*sq_contains*/ |
| 6 | 787 }; |
| 788 | |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
789 static PyBufferProcs string_as_buffer = { |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
790 (getreadbufferproc)string_buffer_getreadbuf, |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
791 (getwritebufferproc)string_buffer_getwritebuf, |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
792 (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
|
793 (getcharbufferproc)string_buffer_getcharbuf, |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
794 }; |
|
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
795 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
796 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
797 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
798 #define LEFTSTRIP 0 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
799 #define RIGHTSTRIP 1 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
800 #define BOTHSTRIP 2 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
801 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
802 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
803 static PyObject * |
|
13460
e6d17f15ae6a
Somebody started playing with const, so of course the outcome
Tim Peters <tim.peters@gmail.com>
parents:
13459
diff
changeset
|
804 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
|
805 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
806 int i, j, err; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
807 PyObject* item; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
808 PyObject *list = PyList_New(0); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
809 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
810 if (list == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
811 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
812 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
813 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
|
814 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
|
815 i++; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
816 j = i; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
817 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
|
818 i++; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
819 if (j < i) { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
820 if (maxsplit-- <= 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
821 break; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
822 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
|
823 if (item == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
824 goto finally; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
825 err = PyList_Append(list, item); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
826 Py_DECREF(item); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
827 if (err < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
828 goto finally; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
829 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
|
830 i++; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
831 j = i; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
832 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
833 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
834 if (j < len) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
835 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
|
836 if (item == NULL) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
837 goto finally; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
838 err = PyList_Append(list, item); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
839 Py_DECREF(item); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
840 if (err < 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
841 goto finally; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
842 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
843 return list; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
844 finally: |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
845 Py_DECREF(list); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
846 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
847 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
848 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
849 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
850 static char split__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
851 "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
|
852 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
853 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
|
854 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
|
855 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
|
856 is a separator."; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
857 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
858 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
859 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
|
860 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
861 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
|
862 int maxsplit = -1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
863 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
|
864 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
|
865 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
866 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
|
867 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
868 if (maxsplit < 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
869 maxsplit = INT_MAX; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
870 if (subobj == Py_None) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
871 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
|
872 if (PyString_Check(subobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
873 sub = PyString_AS_STRING(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
874 n = PyString_GET_SIZE(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
875 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
876 else if (PyUnicode_Check(subobj)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
877 return PyUnicode_Split((PyObject *)self, subobj, maxsplit); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
878 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
|
879 return NULL; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
880 if (n == 0) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
881 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
|
882 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
883 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
884 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
885 list = PyList_New(0); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
886 if (list == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
887 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
888 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
889 i = j = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
890 while (i+n <= len) { |
|
12966
447efb64c72d
Fredrik Lundh <effbot@telia.com>:
Fred Drake <fdrake@acm.org>
parents:
12924
diff
changeset
|
891 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
|
892 if (maxsplit-- <= 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
893 break; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
894 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
|
895 if (item == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
896 goto fail; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
897 err = PyList_Append(list, item); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
898 Py_DECREF(item); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
899 if (err < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
900 goto fail; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
901 i = j = i + n; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
902 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
903 else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
904 i++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
905 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
906 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
|
907 if (item == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
908 goto fail; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
909 err = PyList_Append(list, item); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
910 Py_DECREF(item); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
911 if (err < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
912 goto fail; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
913 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
914 return list; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
915 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
916 fail: |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
917 Py_DECREF(list); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
918 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
919 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
920 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
921 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
922 static char join__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
923 "S.join(sequence) -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
924 \n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
925 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
|
926 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
|
927 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
928 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
929 string_join(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
930 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
931 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
|
932 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
|
933 PyObject *res = NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
934 char *p; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
935 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
|
936 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
|
937 int i; |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
938 PyObject *orig, *seq, *item; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
939 |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
940 if (!PyArg_ParseTuple(args, "O:join", &orig)) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
941 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
942 |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
943 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
|
944 if (seq == NULL) { |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
945 if (PyErr_ExceptionMatches(PyExc_TypeError)) |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
946 PyErr_Format(PyExc_TypeError, |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
947 "sequence expected, %.80s found", |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
948 orig->ob_type->tp_name); |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
949 return NULL; |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
950 } |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
951 |
|
13576
61e4c29d53d8
replace PyXXX_Length calls with PyXXX_Size calls
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13561
diff
changeset
|
952 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
|
953 if (seqlen == 0) { |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
954 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
955 return PyString_FromString(""); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
956 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
957 if (seqlen == 1) { |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
958 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
|
959 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
|
960 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
|
961 "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
|
962 " %.80s found", |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
963 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
|
964 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
965 return NULL; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
966 } |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
967 Py_INCREF(item); |
|
13528
e848d6ed3eb9
string_join(): Some cleaning up of reference counting. In the
Barry Warsaw <barry@python.org>
parents:
13526
diff
changeset
|
968 Py_DECREF(seq); |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
969 return item; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
970 } |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
971 |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
972 /* 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
|
973 * 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
|
974 * 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
|
975 */ |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
976 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
|
977 const size_t old_sz = sz; |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
978 item = PySequence_Fast_GET_ITEM(seq, i); |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
979 if (!PyString_Check(item)){ |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
980 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
|
981 /* 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
|
982 * 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
|
983 * 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
|
984 * 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
|
985 */ |
|
9e093bf8f6a9
Make unicode.join() work nice with iterators. This also required a change
Tim Peters <tim.peters@gmail.com>
parents:
17655
diff
changeset
|
986 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
|
987 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
|
988 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
|
989 return result; |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
990 } |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
991 PyErr_Format(PyExc_TypeError, |
|
13544
3cf4f5aee00e
small updates to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13528
diff
changeset
|
992 "sequence item %i: expected string," |
|
3cf4f5aee00e
small updates to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13528
diff
changeset
|
993 " %.80s found", |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
994 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
|
995 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
996 return NULL; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
997 } |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
998 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
|
999 if (i != 0) |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1000 sz += seplen; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1001 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
|
1002 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
|
1003 "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
|
1004 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1005 return NULL; |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1006 } |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1007 } |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1008 |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1009 /* Allocate result space. */ |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1010 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
|
1011 if (res == NULL) { |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1012 Py_DECREF(seq); |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1013 return NULL; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1014 } |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1015 |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1016 /* Catenate everything. */ |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1017 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
|
1018 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
|
1019 size_t n; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1020 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
|
1021 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
|
1022 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
|
1023 p += n; |
|
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1024 if (i < seqlen - 1) { |
|
13524
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1025 memcpy(p, sep, seplen); |
|
16e490462e6b
two changes to string_join:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13460
diff
changeset
|
1026 p += seplen; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1027 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1028 } |
|
16014
754ecd21fbef
Derivative of patch #102549, "simpler, faster(!) implementation of string.join".
Tim Peters <tim.peters@gmail.com>
parents:
15910
diff
changeset
|
1029 |
|
13526
3686d12362b8
fix two refcount bugs in new string_join implementation:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
13524
diff
changeset
|
1030 Py_DECREF(seq); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1031 return res; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1032 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1033 |
|
18130
8eea2efb34cd
Reformat decl of new _PyString_Join. Add NEWS blurb about repr() speedup.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
1034 PyObject * |
|
8eea2efb34cd
Reformat decl of new _PyString_Join. Add NEWS blurb about repr() speedup.
Tim Peters <tim.peters@gmail.com>
parents:
18129
diff
changeset
|
1035 _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
|
1036 { |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1037 PyObject* args; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1038 PyObject* result = NULL; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1039 |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1040 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
|
1041 assert(x != NULL); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1042 args = PyTuple_New(1); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1043 if (args != NULL) { |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1044 Py_INCREF(x); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1045 PyTuple_SET_ITEM(args, 0, x); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1046 result = string_join((PyStringObject *)sep, args); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1047 Py_DECREF(args); |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1048 } |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1049 return result; |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1050 } |
|
9d533ae574d2
SF bug 433228: repr(list) woes when len(list) big.
Tim Peters <tim.peters@gmail.com>
parents:
18086
diff
changeset
|
1051 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1052 static long |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1053 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
|
1054 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1055 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
|
1056 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
|
1057 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
|
1058 PyObject *subobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1059 |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
1060 if (!PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", |
| 12615 | 1061 &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
|
1062 return -2; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1063 if (PyString_Check(subobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1064 sub = PyString_AS_STRING(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1065 n = PyString_GET_SIZE(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1066 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1067 else if (PyUnicode_Check(subobj)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1068 return PyUnicode_Find((PyObject *)self, subobj, i, last, 1); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1069 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
|
1070 return -2; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1071 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1072 if (last > len) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1073 last = len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1074 if (last < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1075 last += len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1076 if (last < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1077 last = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1078 if (i < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1079 i += len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1080 if (i < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1081 i = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1082 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1083 if (dir > 0) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1084 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
|
1085 return (long)i; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1086 last -= n; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1087 for (; i <= last; ++i) |
|
12966
447efb64c72d
Fredrik Lundh <effbot@telia.com>:
Fred Drake <fdrake@acm.org>
parents:
12924
diff
changeset
|
1088 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
|
1089 return (long)i; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1090 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1091 else { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1092 int j; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
1093 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1094 if (n == 0 && i <= last) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1095 return (long)last; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1096 for (j = last-n; j >= i; --j) |
|
12966
447efb64c72d
Fredrik Lundh <effbot@telia.com>:
Fred Drake <fdrake@acm.org>
parents:
12924
diff
changeset
|
1097 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
|
1098 return (long)j; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1099 } |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
1100 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1101 return -1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1102 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1103 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1104 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1105 static char find__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1106 "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
|
1107 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1108 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
|
1109 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
|
1110 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
|
1111 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1112 Return -1 on failure."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1113 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1114 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1115 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
|
1116 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1117 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
|
1118 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1119 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1120 return PyInt_FromLong(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1121 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1122 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1123 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1124 static char index__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1125 "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
|
1126 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1127 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
|
1128 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1129 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1130 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
|
1131 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1132 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
|
1133 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1134 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1135 if (result == -1) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1136 PyErr_SetString(PyExc_ValueError, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1137 "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
|
1138 return NULL; |
|
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 return PyInt_FromLong(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1141 } |
|
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 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1144 static char rfind__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1145 "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
|
1146 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1147 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
|
1148 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
|
1149 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
|
1150 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1151 Return -1 on failure."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1152 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1153 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1154 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
|
1155 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1156 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
|
1157 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1158 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1159 return PyInt_FromLong(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1160 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1161 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1162 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1163 static char rindex__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1164 "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
|
1165 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1166 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
|
1167 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1168 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1169 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
|
1170 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1171 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
|
1172 if (result == -2) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1173 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1174 if (result == -1) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1175 PyErr_SetString(PyExc_ValueError, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1176 "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
|
1177 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1178 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1179 return PyInt_FromLong(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1180 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1181 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1182 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1183 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1184 do_strip(PyStringObject *self, PyObject *args, int striptype) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1185 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1186 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
|
1187 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
|
1188 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1189 if (!PyArg_ParseTuple(args, ":strip")) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1190 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1191 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1192 i = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1193 if (striptype != RIGHTSTRIP) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1194 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
|
1195 i++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1196 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1197 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1198 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1199 j = len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1200 if (striptype != LEFTSTRIP) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1201 do { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1202 j--; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1203 } 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
|
1204 j++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1205 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1206 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1207 if (i == 0 && j == len) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1208 Py_INCREF(self); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1209 return (PyObject*)self; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1210 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1211 else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1212 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
|
1213 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1214 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1215 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1216 static char strip__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1217 "S.strip() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1218 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1219 Return a copy of the string S with leading and trailing\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1220 whitespace removed."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1221 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1222 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1223 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
|
1224 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1225 return do_strip(self, args, BOTHSTRIP); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1226 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1227 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1228 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1229 static char lstrip__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1230 "S.lstrip() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1231 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1232 Return a copy of the string S with leading whitespace removed."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1233 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1234 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1235 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
|
1236 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1237 return do_strip(self, args, LEFTSTRIP); |
|
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 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1240 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1241 static char rstrip__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1242 "S.rstrip() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1243 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1244 Return a copy of the string S with trailing whitespace removed."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1245 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1246 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1247 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
|
1248 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1249 return do_strip(self, args, RIGHTSTRIP); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1250 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1251 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1252 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1253 static char lower__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1254 "S.lower() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1255 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1256 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
|
1257 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1258 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1259 string_lower(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1260 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1261 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
|
1262 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
|
1263 PyObject *new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1264 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1265 if (!PyArg_ParseTuple(args, ":lower")) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1266 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1267 new = PyString_FromStringAndSize(NULL, n); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1268 if (new == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1269 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1270 s_new = PyString_AsString(new); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1271 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
|
1272 int c = Py_CHARMASK(*s++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1273 if (isupper(c)) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1274 *s_new = tolower(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1275 } else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1276 *s_new = c; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1277 s_new++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1278 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1279 return new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1280 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1281 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1282 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1283 static char upper__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1284 "S.upper() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1285 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1286 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
|
1287 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1288 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1289 string_upper(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1290 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1291 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
|
1292 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
|
1293 PyObject *new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1294 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1295 if (!PyArg_ParseTuple(args, ":upper")) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1296 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1297 new = PyString_FromStringAndSize(NULL, n); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1298 if (new == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1299 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1300 s_new = PyString_AsString(new); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1301 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
|
1302 int c = Py_CHARMASK(*s++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1303 if (islower(c)) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1304 *s_new = toupper(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1305 } else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1306 *s_new = c; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1307 s_new++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1308 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1309 return new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1310 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1311 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1312 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1313 static char title__doc__[] = |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1314 "S.title() -> string\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1315 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1316 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
|
1317 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
|
1318 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1319 static PyObject* |
|
15484
07705177ca6b
Jeffrey D. Collins <tokeneater@users.sourceforge.net>:
Fred Drake <fdrake@acm.org>
parents:
15455
diff
changeset
|
1320 string_title(PyStringObject *self, PyObject *args) |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1321 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1322 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
|
1323 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
|
1324 int previous_is_cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1325 PyObject *new; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1326 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1327 if (!PyArg_ParseTuple(args, ":title")) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1328 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1329 new = PyString_FromStringAndSize(NULL, n); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1330 if (new == NULL) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1331 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1332 s_new = PyString_AsString(new); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1333 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
|
1334 int c = Py_CHARMASK(*s++); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1335 if (islower(c)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1336 if (!previous_is_cased) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1337 c = toupper(c); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1338 previous_is_cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1339 } else if (isupper(c)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1340 if (previous_is_cased) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1341 c = tolower(c); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1342 previous_is_cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1343 } else |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1344 previous_is_cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1345 *s_new++ = c; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1346 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1347 return new; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1348 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1349 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1350 static char capitalize__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1351 "S.capitalize() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1352 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1353 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
|
1354 capitalized."; |
|
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_capitalize(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 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1359 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
|
1360 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
|
1361 PyObject *new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1362 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1363 if (!PyArg_ParseTuple(args, ":capitalize")) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1364 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1365 new = PyString_FromStringAndSize(NULL, n); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1366 if (new == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1367 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1368 s_new = PyString_AsString(new); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1369 if (0 < n) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1370 int c = Py_CHARMASK(*s++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1371 if (islower(c)) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1372 *s_new = toupper(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1373 else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1374 *s_new = c; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1375 s_new++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1376 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1377 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
|
1378 int c = Py_CHARMASK(*s++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1379 if (isupper(c)) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1380 *s_new = tolower(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1381 else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1382 *s_new = c; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1383 s_new++; |
|
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 return new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1386 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1387 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1388 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1389 static char count__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1390 "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
|
1391 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1392 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
|
1393 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
|
1394 interpreted as in slice notation."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1395 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1396 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1397 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
|
1398 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1399 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
|
1400 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
|
1401 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
|
1402 int m, r; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1403 PyObject *subobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1404 |
| 12615 | 1405 if (!PyArg_ParseTuple(args, "O|O&O&:count", &subobj, |
| 1406 _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
|
1407 return NULL; |
| 12615 | 1408 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1409 if (PyString_Check(subobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1410 sub = PyString_AS_STRING(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1411 n = PyString_GET_SIZE(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1412 } |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1413 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
|
1414 int count; |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1415 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
|
1416 if (count == -1) |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1417 return NULL; |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1418 else |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1419 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
|
1420 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1421 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
|
1422 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1423 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1424 if (last > len) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1425 last = len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1426 if (last < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1427 last += len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1428 if (last < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1429 last = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1430 if (i < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1431 i += len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1432 if (i < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1433 i = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1434 m = last + 1 - n; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1435 if (n == 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1436 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
|
1437 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1438 r = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1439 while (i < m) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1440 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
|
1441 r++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1442 i += n; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1443 } else { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1444 i++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1445 } |
|
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 return PyInt_FromLong((long) r); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1448 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1449 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1450 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1451 static char swapcase__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1452 "S.swapcase() -> string\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1453 \n\ |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1454 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
|
1455 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
|
1456 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1457 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1458 string_swapcase(PyStringObject *self, PyObject *args) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1459 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1460 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
|
1461 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
|
1462 PyObject *new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1463 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
11673
diff
changeset
|
1464 if (!PyArg_ParseTuple(args, ":swapcase")) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1465 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1466 new = PyString_FromStringAndSize(NULL, n); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1467 if (new == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1468 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1469 s_new = PyString_AsString(new); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1470 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
|
1471 int c = Py_CHARMASK(*s++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1472 if (islower(c)) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1473 *s_new = toupper(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1474 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1475 else if (isupper(c)) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1476 *s_new = tolower(c); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1477 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1478 else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1479 *s_new = c; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1480 s_new++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1481 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1482 return new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1483 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1484 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1485 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1486 static char translate__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1487 "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
|
1488 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1489 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
|
1490 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
|
1491 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
|
1492 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
|
1493 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1494 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1495 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
|
1496 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1497 register char *input, *output; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1498 register const char *table; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1499 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
|
1500 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
|
1501 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
|
1502 int inlen, tablen, dellen = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1503 PyObject *result; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1504 int trans_table[256]; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1505 PyObject *tableobj, *delobj = NULL; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1506 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1507 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
|
1508 &tableobj, &delobj)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1509 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1510 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1511 if (PyString_Check(tableobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1512 table1 = PyString_AS_STRING(tableobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1513 tablen = PyString_GET_SIZE(tableobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1514 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1515 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
|
1516 /* 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
|
1517 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
|
1518 to be deleted. */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1519 if (delobj != NULL) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1520 PyErr_SetString(PyExc_TypeError, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1521 "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
|
1522 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1523 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1524 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
|
1525 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1526 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
|
1527 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1528 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1529 if (delobj != NULL) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1530 if (PyString_Check(delobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1531 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
|
1532 dellen = PyString_GET_SIZE(delobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1533 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1534 else if (PyUnicode_Check(delobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1535 PyErr_SetString(PyExc_TypeError, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1536 "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
|
1537 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1538 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1539 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
|
1540 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1541 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1542 if (tablen != 256) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1543 PyErr_SetString(PyExc_ValueError, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1544 "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
|
1545 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1546 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1547 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1548 else { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1549 del_table = NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1550 dellen = 0; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1551 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1552 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1553 table = table1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1554 inlen = PyString_Size(input_obj); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1555 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
|
1556 if (result == NULL) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1557 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1558 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
|
1559 input = PyString_AsString(input_obj); |
|
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 if (dellen == 0) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1562 /* 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
|
1563 for (i = inlen; --i >= 0; ) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1564 c = Py_CHARMASK(*input++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1565 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
|
1566 changed = 1; |
|
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 if (changed) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1569 return result; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1570 Py_DECREF(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1571 Py_INCREF(input_obj); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1572 return input_obj; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1573 } |
|
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 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
|
1576 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
|
1577 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1578 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
|
1579 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
|
1580 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1581 for (i = inlen; --i >= 0; ) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1582 c = Py_CHARMASK(*input++); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1583 if (trans_table[c] != -1) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1584 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
|
1585 continue; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1586 changed = 1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1587 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1588 if (!changed) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1589 Py_DECREF(result); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1590 Py_INCREF(input_obj); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1591 return input_obj; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1592 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1593 /* Fix the size of the resulting string */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1594 if (inlen > 0 &&_PyString_Resize(&result, output-output_start)) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1595 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1596 return result; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1597 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1598 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1599 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1600 /* 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
|
1601 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1602 /* |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1603 mymemfind |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1604 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1605 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
|
1606 |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11998
diff
changeset
|
1607 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
|
1608 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
|
1609 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
|
1610 MEM, the function returns -1. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1611 */ |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
1612 static int |
|
13460
e6d17f15ae6a
Somebody started playing with const, so of course the outcome
Tim Peters <tim.peters@gmail.com>
parents:
13459
diff
changeset
|
1613 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
|
1614 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1615 register int ii; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1616 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1617 /* 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
|
1618 len -= pat_len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1619 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1620 for (ii = 0; ii <= len; ii++) { |
|
12966
447efb64c72d
Fredrik Lundh <effbot@telia.com>:
Fred Drake <fdrake@acm.org>
parents:
12924
diff
changeset
|
1621 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
|
1622 return ii; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1623 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1624 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1625 return -1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1626 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1627 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1628 /* |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1629 mymemcnt |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1630 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1631 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
|
1632 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
|
1633 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
|
1634 */ |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
1635 static int |
|
13460
e6d17f15ae6a
Somebody started playing with const, so of course the outcome
Tim Peters <tim.peters@gmail.com>
parents:
13459
diff
changeset
|
1636 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
|
1637 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1638 register int offset = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1639 int nfound = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1640 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1641 while (len >= 0) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1642 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
|
1643 if (offset == -1) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1644 break; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1645 mem += offset + pat_len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1646 len -= offset + pat_len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1647 nfound++; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1648 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1649 return nfound; |
|
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 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1652 /* |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1653 mymemreplace |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1654 |
|
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
|
1655 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
|
1656 replaced with SUB. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1657 |
|
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
|
1658 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
|
1659 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
|
1660 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
|
1661 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1662 on return, out_len is: |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1663 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
|
1664 -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
|
1665 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
|
1666 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1667 return value is: |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1668 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
|
1669 NULL if an error occurred. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1670 */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1671 static char * |
|
13460
e6d17f15ae6a
Somebody started playing with const, so of course the outcome
Tim Peters <tim.peters@gmail.com>
parents:
13459
diff
changeset
|
1672 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
|
1673 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
|
1674 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
|
1675 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
|
1676 int *out_len) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1677 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1678 char *out_s; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1679 char *new_s; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1680 int nfound, offset, new_len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1681 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1682 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
|
1683 goto return_same; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1684 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1685 /* find length of output string */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1686 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
|
1687 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
|
1688 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
|
1689 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
|
1690 nfound = count; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1691 if (nfound == 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1692 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
|
1693 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1694 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
|
1695 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
|
1696 /* 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
|
1697 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
|
1698 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
|
1699 return NULL; |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1700 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
|
1701 } |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1702 else { |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1703 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
|
1704 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
|
1705 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
|
1706 return NULL; |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1707 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
|
1708 |
|
17751
c886cd1c2184
Heh. I need a break. After this: stropmodule & stringobject were more
Tim Peters <tim.peters@gmail.com>
parents:
17750
diff
changeset
|
1709 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
|
1710 /* 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
|
1711 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
|
1712 if (offset == -1) |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1713 break; |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1714 |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1715 /* 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
|
1716 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
|
1717 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
|
1718 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
|
1719 |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1720 /* 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
|
1721 new_s += offset; |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1722 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
|
1723 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
|
1724 } |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1725 /* 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
|
1726 if (len > 0) |
|
b39266f7484c
Fudge. stropmodule and stringobject both had copies of the buggy
Tim Peters <tim.peters@gmail.com>
parents:
17741
diff
changeset
|
1727 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
|
1728 } |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1729 *out_len = new_len; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1730 return out_s; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1731 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1732 return_same: |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1733 *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
|
1734 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
|
1735 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1736 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1737 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1738 static char replace__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1739 "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
|
1740 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1741 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
|
1742 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
|
1743 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
|
1744 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1745 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1746 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
|
1747 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1748 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
|
1749 char *new_s; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1750 int len = PyString_GET_SIZE(self), sub_len, repl_len, out_len; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1751 int count = -1; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1752 PyObject *new; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1753 PyObject *subobj, *replobj; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1754 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1755 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
|
1756 &subobj, &replobj, &count)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1757 return NULL; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1758 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1759 if (PyString_Check(subobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1760 sub = PyString_AS_STRING(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1761 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
|
1762 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1763 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
|
1764 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
|
1765 subobj, replobj, count); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1766 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
|
1767 return NULL; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1768 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1769 if (PyString_Check(replobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1770 repl = PyString_AS_STRING(replobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1771 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
|
1772 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1773 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
|
1774 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
|
1775 subobj, replobj, count); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1776 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
|
1777 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1778 |
|
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
|
1779 if (sub_len <= 0) { |
|
12025
7994d9db7f4d
On 17-Mar-2000, Marc-Andre Lemburg said:
Barry Warsaw <barry@python.org>
parents:
11998
diff
changeset
|
1780 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
|
1781 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1782 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1783 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
|
1784 if (new_s == NULL) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1785 PyErr_NoMemory(); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1786 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1787 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1788 if (out_len == -1) { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1789 /* we're returning another reference to self */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1790 new = (PyObject*)self; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1791 Py_INCREF(new); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1792 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1793 else { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1794 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
|
1795 PyMem_FREE(new_s); |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1796 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1797 return new; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1798 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1799 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1800 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1801 static char startswith__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1802 "S.startswith(prefix[, start[, end]]) -> int\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1803 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1804 Return 1 if S starts with the specified prefix, otherwise return 0. With\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1805 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
|
1806 comparing S at that position."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1807 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1808 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1809 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
|
1810 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1811 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
|
1812 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
|
1813 const char* prefix; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1814 int plen; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1815 int start = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1816 int end = -1; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1817 PyObject *subobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1818 |
| 12615 | 1819 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, |
| 1820 _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
|
1821 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1822 if (PyString_Check(subobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1823 prefix = PyString_AS_STRING(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1824 plen = PyString_GET_SIZE(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1825 } |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1826 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
|
1827 int rc; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
1828 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
|
1829 subobj, start, end, -1); |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1830 if (rc == -1) |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1831 return NULL; |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1832 else |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1833 return PyInt_FromLong((long) rc); |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1834 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1835 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
|
1836 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1837 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1838 /* 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
|
1839 * 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
|
1840 * the empty string. |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1841 */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1842 if (start < 0 || start+plen > len) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1843 return PyInt_FromLong(0); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1844 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1845 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
|
1846 /* 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
|
1847 if (end < 0) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1848 return PyInt_FromLong(1); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1849 else if (end - start < plen) |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1850 return PyInt_FromLong(0); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1851 else |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1852 return PyInt_FromLong(1); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1853 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1854 else return PyInt_FromLong(0); |
|
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 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1857 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1858 static char endswith__doc__[] = |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1859 "S.endswith(suffix[, start[, end]]) -> int\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1860 \n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1861 Return 1 if S ends with the specified suffix, otherwise return 0. With\n\ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1862 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
|
1863 comparing S at that position."; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1864 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1865 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
1866 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
|
1867 { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1868 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
|
1869 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
|
1870 const char* suffix; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1871 int slen; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1872 int start = 0; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1873 int end = -1; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1874 int lower, upper; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1875 PyObject *subobj; |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1876 |
| 12615 | 1877 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, |
| 1878 _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
|
1879 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1880 if (PyString_Check(subobj)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1881 suffix = PyString_AS_STRING(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1882 slen = PyString_GET_SIZE(subobj); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1883 } |
|
15910
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1884 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
|
1885 int rc; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
1886 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
|
1887 subobj, start, end, +1); |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1888 if (rc == -1) |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1889 return NULL; |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1890 else |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1891 return PyInt_FromLong((long) rc); |
|
f7f3be3d00b9
Added checks to prevent PyUnicode_Count() from dumping core
Marc-André Lemburg <mal@egenix.com>
parents:
15579
diff
changeset
|
1892 } |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1893 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
|
1894 return NULL; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1895 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1896 if (start < 0 || start > len || slen > len) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1897 return PyInt_FromLong(0); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1898 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1899 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
|
1900 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
|
1901 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1902 if (upper-lower >= slen && !memcmp(str+lower, suffix, slen)) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1903 return PyInt_FromLong(1); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
1904 else return PyInt_FromLong(0); |
|
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 |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
1908 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
|
1909 "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
|
1910 \n\ |
|
17835
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
1911 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
|
1912 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
|
1913 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
|
1914 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
|
1915 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
1916 static PyObject * |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
1917 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
|
1918 { |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
1919 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
|
1920 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
|
1921 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
|
1922 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
|
1923 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
|
1924 } |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
1925 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
1926 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
1927 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
|
1928 "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
|
1929 \n\ |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
1930 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
|
1931 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
|
1932 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
|
1933 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
|
1934 |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
1935 static PyObject * |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
1936 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
|
1937 { |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
1938 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
|
1939 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
|
1940 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
|
1941 return NULL; |
|
9ea87c0a7c04
This patch changes the way the string .encode() method works slightly
Marc-André Lemburg <mal@egenix.com>
parents:
17751
diff
changeset
|
1942 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
|
1943 } |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
1944 |
|
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
1945 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1946 static char expandtabs__doc__[] = |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1947 "S.expandtabs([tabsize]) -> string\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1948 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1949 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
|
1950 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
|
1951 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1952 static PyObject* |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1953 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
|
1954 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1955 const char *e, *p; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1956 char *q; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1957 int i, j; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1958 PyObject *u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1959 int tabsize = 8; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1960 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1961 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
|
1962 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1963 |
|
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
|
1964 /* 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
|
1965 i = j = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1966 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
|
1967 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
|
1968 if (*p == '\t') { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1969 if (tabsize > 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1970 j += tabsize - (j % tabsize); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1971 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1972 else { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1973 j++; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1974 if (*p == '\n' || *p == '\r') { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1975 i += j; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1976 j = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1977 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1978 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1979 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1980 /* 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
|
1981 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
|
1982 if (!u) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1983 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1984 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1985 j = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1986 q = PyString_AS_STRING(u); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1987 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1988 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
|
1989 if (*p == '\t') { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1990 if (tabsize > 0) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1991 i = tabsize - (j % tabsize); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1992 j += i; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1993 while (i--) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1994 *q++ = ' '; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1995 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1996 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1997 else { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1998 j++; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
1999 *q++ = *p; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2000 if (*p == '\n' || *p == '\r') |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2001 j = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2002 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2003 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2004 return u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2005 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2006 |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2007 static |
|
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2008 PyObject *pad(PyStringObject *self, |
|
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2009 int left, |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2010 int right, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2011 char fill) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2012 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2013 PyObject *u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2014 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2015 if (left < 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2016 left = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2017 if (right < 0) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2018 right = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2019 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2020 if (left == 0 && right == 0) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2021 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2022 return (PyObject *)self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2023 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2024 |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2025 u = PyString_FromStringAndSize(NULL, |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2026 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
|
2027 if (u) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2028 if (left) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2029 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
|
2030 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
|
2031 PyString_AS_STRING(self), |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2032 PyString_GET_SIZE(self)); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2033 if (right) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2034 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
|
2035 fill, right); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2036 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2037 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2038 return u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2039 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2040 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2041 static char ljust__doc__[] = |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2042 "S.ljust(width) -> string\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2043 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2044 Return S left justified in a string of length width. Padding is\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2045 done using spaces."; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2046 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2047 static PyObject * |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2048 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
|
2049 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2050 int width; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2051 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
|
2052 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2053 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2054 if (PyString_GET_SIZE(self) >= width) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2055 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2056 return (PyObject*) self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2057 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2058 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2059 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
|
2060 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2061 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2062 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2063 static char rjust__doc__[] = |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2064 "S.rjust(width) -> string\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2065 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2066 Return S right justified in a string of length width. Padding is\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2067 done using spaces."; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2068 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2069 static PyObject * |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2070 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
|
2071 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2072 int width; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2073 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
|
2074 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2075 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2076 if (PyString_GET_SIZE(self) >= width) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2077 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2078 return (PyObject*) self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2079 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2080 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2081 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
|
2082 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2083 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2084 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2085 static char center__doc__[] = |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2086 "S.center(width) -> string\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2087 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2088 Return S centered in a string of length width. Padding is done\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2089 using spaces."; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2090 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2091 static PyObject * |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2092 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
|
2093 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2094 int marg, left; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2095 int width; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2096 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2097 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
|
2098 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2099 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2100 if (PyString_GET_SIZE(self) >= width) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2101 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2102 return (PyObject*) self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2103 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2104 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2105 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
|
2106 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
|
2107 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2108 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
|
2109 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2110 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2111 #if 0 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2112 static char zfill__doc__[] = |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2113 "S.zfill(width) -> string\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2114 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2115 Pad a numeric string x with zeros on the left, to fill a field\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2116 of the specified width. The string x is never truncated."; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2117 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2118 static PyObject * |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2119 string_zfill(PyStringObject *self, PyObject *args) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2120 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2121 int fill; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2122 PyObject *u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2123 char *str; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2124 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2125 int width; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2126 if (!PyArg_ParseTuple(args, "i:zfill", &width)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2127 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2128 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2129 if (PyString_GET_SIZE(self) >= width) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2130 Py_INCREF(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2131 return (PyObject*) self; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2132 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2133 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2134 fill = width - PyString_GET_SIZE(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2135 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2136 u = pad(self, fill, 0, '0'); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2137 if (u == NULL) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2138 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2139 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2140 str = PyString_AS_STRING(u); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2141 if (str[fill] == '+' || str[fill] == '-') { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2142 /* move sign to beginning of string */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2143 str[0] = str[fill]; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2144 str[fill] = '0'; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2145 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2146 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2147 return u; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2148 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2149 #endif |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2150 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2151 static char isspace__doc__[] = |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2152 "S.isspace() -> int\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2153 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2154 Return 1 if there are only whitespace characters in S,\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2155 0 otherwise."; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2156 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2157 static PyObject* |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2158 string_isspace(PyStringObject *self, PyObject *args) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2159 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2160 register const unsigned char *p |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2161 = (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
|
2162 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
|
2163 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2164 if (!PyArg_NoArgs(args)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2165 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2166 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2167 /* Shortcut for single character strings */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2168 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
|
2169 isspace(*p)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2170 return PyInt_FromLong(1); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2171 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2172 /* Special case for empty strings */ |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2173 if (PyString_GET_SIZE(self) == 0) |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2174 return PyInt_FromLong(0); |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2175 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2176 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
|
2177 for (; p < e; p++) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2178 if (!isspace(*p)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2179 return PyInt_FromLong(0); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2180 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2181 return PyInt_FromLong(1); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2182 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2183 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2184 |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2185 static char isalpha__doc__[] = |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2186 "S.isalpha() -> int\n\ |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2187 \n\ |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2188 Return 1 if all characters in S are alphabetic\n\ |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2189 and there is at least one character in S, 0 otherwise."; |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2190 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2191 static PyObject* |
|
15484
07705177ca6b
Jeffrey D. Collins <tokeneater@users.sourceforge.net>:
Fred Drake <fdrake@acm.org>
parents:
15455
diff
changeset
|
2192 string_isalpha(PyStringObject *self, PyObject *args) |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2193 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2194 register const unsigned char *p |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2195 = (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
|
2196 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
|
2197 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2198 if (!PyArg_NoArgs(args)) |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2199 return NULL; |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2200 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2201 /* 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
|
2202 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
|
2203 isalpha(*p)) |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2204 return PyInt_FromLong(1); |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2205 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2206 /* 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
|
2207 if (PyString_GET_SIZE(self) == 0) |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2208 return PyInt_FromLong(0); |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2209 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2210 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
|
2211 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
|
2212 if (!isalpha(*p)) |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2213 return PyInt_FromLong(0); |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2214 } |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2215 return PyInt_FromLong(1); |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2216 } |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2217 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2218 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2219 static char isalnum__doc__[] = |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2220 "S.isalnum() -> int\n\ |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2221 \n\ |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2222 Return 1 if all characters in S are alphanumeric\n\ |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2223 and there is at least one character in S, 0 otherwise."; |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2224 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2225 static PyObject* |
|
15484
07705177ca6b
Jeffrey D. Collins <tokeneater@users.sourceforge.net>:
Fred Drake <fdrake@acm.org>
parents:
15455
diff
changeset
|
2226 string_isalnum(PyStringObject *self, PyObject *args) |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2227 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2228 register const unsigned char *p |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2229 = (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
|
2230 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
|
2231 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2232 if (!PyArg_NoArgs(args)) |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2233 return NULL; |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2234 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2235 /* 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
|
2236 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
|
2237 isalnum(*p)) |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2238 return PyInt_FromLong(1); |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2239 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2240 /* 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
|
2241 if (PyString_GET_SIZE(self) == 0) |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2242 return PyInt_FromLong(0); |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2243 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2244 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
|
2245 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
|
2246 if (!isalnum(*p)) |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2247 return PyInt_FromLong(0); |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2248 } |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2249 return PyInt_FromLong(1); |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2250 } |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2251 |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2252 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2253 static char isdigit__doc__[] = |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2254 "S.isdigit() -> int\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2255 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2256 Return 1 if there are only digit characters in S,\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2257 0 otherwise."; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2258 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2259 static PyObject* |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2260 string_isdigit(PyStringObject *self, PyObject *args) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2261 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2262 register const unsigned char *p |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2263 = (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
|
2264 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
|
2265 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2266 if (!PyArg_NoArgs(args)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2267 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2268 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2269 /* Shortcut for single character strings */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2270 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
|
2271 isdigit(*p)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2272 return PyInt_FromLong(1); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2273 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2274 /* Special case for empty strings */ |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2275 if (PyString_GET_SIZE(self) == 0) |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2276 return PyInt_FromLong(0); |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2277 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2278 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
|
2279 for (; p < e; p++) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2280 if (!isdigit(*p)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2281 return PyInt_FromLong(0); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2282 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2283 return PyInt_FromLong(1); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2284 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2285 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2286 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2287 static char islower__doc__[] = |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2288 "S.islower() -> int\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2289 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2290 Return 1 if all cased characters in S are lowercase and there is\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2291 at least one cased character in S, 0 otherwise."; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2292 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2293 static PyObject* |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2294 string_islower(PyStringObject *self, PyObject *args) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2295 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2296 register const unsigned char *p |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2297 = (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
|
2298 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
|
2299 int cased; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2300 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2301 if (!PyArg_NoArgs(args)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2302 return NULL; |
|
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 /* Shortcut for single character strings */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2305 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
|
2306 return PyInt_FromLong(islower(*p) != 0); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2307 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2308 /* Special case for empty strings */ |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2309 if (PyString_GET_SIZE(self) == 0) |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2310 return PyInt_FromLong(0); |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2311 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2312 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
|
2313 cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2314 for (; p < e; p++) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2315 if (isupper(*p)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2316 return PyInt_FromLong(0); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2317 else if (!cased && islower(*p)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2318 cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2319 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2320 return PyInt_FromLong(cased); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2321 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2322 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2323 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2324 static char isupper__doc__[] = |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2325 "S.isupper() -> int\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2326 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2327 Return 1 if all cased characters in S are uppercase and there is\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2328 at least one cased character in S, 0 otherwise."; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2329 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2330 static PyObject* |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2331 string_isupper(PyStringObject *self, PyObject *args) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2332 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2333 register const unsigned char *p |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2334 = (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
|
2335 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
|
2336 int cased; |
|
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 if (!PyArg_NoArgs(args)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2339 return NULL; |
|
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 /* Shortcut for single character strings */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2342 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
|
2343 return PyInt_FromLong(isupper(*p) != 0); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2344 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2345 /* Special case for empty strings */ |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2346 if (PyString_GET_SIZE(self) == 0) |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2347 return PyInt_FromLong(0); |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2348 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2349 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
|
2350 cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2351 for (; p < e; p++) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2352 if (islower(*p)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2353 return PyInt_FromLong(0); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2354 else if (!cased && isupper(*p)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2355 cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2356 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2357 return PyInt_FromLong(cased); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2358 } |
|
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 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2361 static char istitle__doc__[] = |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2362 "S.istitle() -> int\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2363 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2364 Return 1 if S is a titlecased string, i.e. uppercase characters\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2365 may only follow uncased characters and lowercase characters only cased\n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2366 ones. Return 0 otherwise."; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2367 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2368 static PyObject* |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2369 string_istitle(PyStringObject *self, PyObject *args) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2370 { |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2371 register const unsigned char *p |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2372 = (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
|
2373 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
|
2374 int cased, previous_is_cased; |
|
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 if (!PyArg_NoArgs(args)) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2377 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2378 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2379 /* Shortcut for single character strings */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2380 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
|
2381 return PyInt_FromLong(isupper(*p) != 0); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2382 |
|
12924
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2383 /* Special case for empty strings */ |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2384 if (PyString_GET_SIZE(self) == 0) |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2385 return PyInt_FromLong(0); |
|
2d0b99778edf
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12902
diff
changeset
|
2386 |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2387 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
|
2388 cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2389 previous_is_cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2390 for (; p < e; p++) { |
|
12588
54274beb244d
The methods islower(), isupper(), isspace(), isdigit() and istitle()
Guido van Rossum <guido@python.org>
parents:
12576
diff
changeset
|
2391 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
|
2392 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2393 if (isupper(ch)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2394 if (previous_is_cased) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2395 return PyInt_FromLong(0); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2396 previous_is_cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2397 cased = 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 else if (islower(ch)) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2400 if (!previous_is_cased) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2401 return PyInt_FromLong(0); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2402 previous_is_cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2403 cased = 1; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2404 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2405 else |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2406 previous_is_cased = 0; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2407 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2408 return PyInt_FromLong(cased); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2409 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2410 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2411 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2412 static char splitlines__doc__[] = |
| 12371 | 2413 "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
|
2414 \n\ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2415 Return a list of the lines in S, breaking at line boundaries.\n\ |
| 12371 | 2416 Line breaks are not included in the resulting list unless keepends\n\ |
| 2417 is given and true."; | |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2418 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2419 #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
|
2420 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
|
2421 if (!str) \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2422 goto onError; \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2423 if (PyList_Append(list, str)) { \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2424 Py_DECREF(str); \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2425 goto onError; \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2426 } \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2427 else \ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2428 Py_DECREF(str); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2429 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2430 static PyObject* |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2431 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
|
2432 { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2433 register int i; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2434 register int j; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2435 int len; |
| 12371 | 2436 int keepends = 0; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2437 PyObject *list; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2438 PyObject *str; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2439 char *data; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2440 |
| 12371 | 2441 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
|
2442 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2443 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2444 data = PyString_AS_STRING(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2445 len = PyString_GET_SIZE(self); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2446 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2447 list = PyList_New(0); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2448 if (!list) |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2449 goto onError; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2450 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2451 for (i = j = 0; i < len; ) { |
| 12371 | 2452 int eol; |
| 2453 | |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2454 /* 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
|
2455 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
|
2456 i++; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2457 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2458 /* Skip the line break reading CRLF as one line break */ |
| 12371 | 2459 eol = i; |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2460 if (i < len) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2461 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
|
2462 data[i+1] == '\n') |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2463 i += 2; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2464 else |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2465 i++; |
| 12371 | 2466 if (keepends) |
| 2467 eol = i; | |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2468 } |
| 12371 | 2469 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
|
2470 j = i; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2471 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2472 if (j < len) { |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2473 SPLIT_APPEND(data, j, len); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2474 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2475 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2476 return list; |
|
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 onError: |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2479 Py_DECREF(list); |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2480 return NULL; |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2481 } |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2482 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2483 #undef SPLIT_APPEND |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2484 |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2485 |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2486 static PyMethodDef |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2487 string_methods[] = { |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2488 /* 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
|
2489 string.maketrans(). */ |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2490 {"join", (PyCFunction)string_join, 1, join__doc__}, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2491 {"split", (PyCFunction)string_split, 1, split__doc__}, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2492 {"lower", (PyCFunction)string_lower, 1, lower__doc__}, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2493 {"upper", (PyCFunction)string_upper, 1, upper__doc__}, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2494 {"islower", (PyCFunction)string_islower, 0, islower__doc__}, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2495 {"isupper", (PyCFunction)string_isupper, 0, isupper__doc__}, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2496 {"isspace", (PyCFunction)string_isspace, 0, isspace__doc__}, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2497 {"isdigit", (PyCFunction)string_isdigit, 0, isdigit__doc__}, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2498 {"istitle", (PyCFunction)string_istitle, 0, istitle__doc__}, |
|
13354
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2499 {"isalpha", (PyCFunction)string_isalpha, 0, isalpha__doc__}, |
|
99f341a97fde
Added new .isalpha() and .isalnum() methods to match the same
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
2500 {"isalnum", (PyCFunction)string_isalnum, 0, isalnum__doc__}, |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2501 {"capitalize", (PyCFunction)string_capitalize, 1, capitalize__doc__}, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2502 {"count", (PyCFunction)string_count, 1, count__doc__}, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2503 {"endswith", (PyCFunction)string_endswith, 1, endswith__doc__}, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2504 {"find", (PyCFunction)string_find, 1, find__doc__}, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2505 {"index", (PyCFunction)string_index, 1, index__doc__}, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2506 {"lstrip", (PyCFunction)string_lstrip, 1, lstrip__doc__}, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2507 {"replace", (PyCFunction)string_replace, 1, replace__doc__}, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2508 {"rfind", (PyCFunction)string_rfind, 1, rfind__doc__}, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2509 {"rindex", (PyCFunction)string_rindex, 1, rindex__doc__}, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2510 {"rstrip", (PyCFunction)string_rstrip, 1, rstrip__doc__}, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2511 {"startswith", (PyCFunction)string_startswith, 1, startswith__doc__}, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2512 {"strip", (PyCFunction)string_strip, 1, strip__doc__}, |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2513 {"swapcase", (PyCFunction)string_swapcase, 1, swapcase__doc__}, |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2514 {"translate", (PyCFunction)string_translate, 1, translate__doc__}, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2515 {"title", (PyCFunction)string_title, 1, title__doc__}, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2516 {"ljust", (PyCFunction)string_ljust, 1, ljust__doc__}, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2517 {"rjust", (PyCFunction)string_rjust, 1, rjust__doc__}, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2518 {"center", (PyCFunction)string_center, 1, center__doc__}, |
|
13377
2017f54b0325
Added new codec APIs and a new interface method .encode() which
Marc-André Lemburg <mal@egenix.com>
parents:
13354
diff
changeset
|
2519 {"encode", (PyCFunction)string_encode, 1, 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
|
2520 {"decode", (PyCFunction)string_decode, 1, decode__doc__}, |
|
11969
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2521 {"expandtabs", (PyCFunction)string_expandtabs, 1, expandtabs__doc__}, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2522 {"splitlines", (PyCFunction)string_splitlines, 1, splitlines__doc__}, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2523 #if 0 |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2524 {"zfill", (PyCFunction)string_zfill, 1, zfill__doc__}, |
|
74b7213fb609
Many changes for Unicode, by Marc-Andre Lemburg.
Guido van Rossum <guido@python.org>
parents:
11942
diff
changeset
|
2525 #endif |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2526 {NULL, NULL} /* sentinel */ |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2527 }; |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2528 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2529 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2530 string_getattr(PyStringObject *s, char *name) |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2531 { |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2532 return Py_FindMethod(string_methods, (PyObject*)s, name); |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2533 } |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2534 |
|
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2535 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2536 PyTypeObject PyString_Type = { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2537 PyObject_HEAD_INIT(&PyType_Type) |
| 6 | 2538 0, |
| 2539 "string", | |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2540 sizeof(PyStringObject), |
| 6 | 2541 sizeof(char), |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2542 (destructor)string_dealloc, /*tp_dealloc*/ |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2543 (printfunc)string_print, /*tp_print*/ |
|
11383
aca7b5eaf5e8
Mainlining the string_methods branch. See branch revision log
Barry Warsaw <barry@python.org>
parents:
10923
diff
changeset
|
2544 (getattrfunc)string_getattr, /*tp_getattr*/ |
| 6 | 2545 0, /*tp_setattr*/ |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
2546 0, /*tp_compare*/ |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2547 (reprfunc)string_repr, /*tp_repr*/ |
| 6 | 2548 0, /*tp_as_number*/ |
| 2549 &string_as_sequence, /*tp_as_sequence*/ | |
| 2550 0, /*tp_as_mapping*/ | |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2551 (hashfunc)string_hash, /*tp_hash*/ |
| 5005 | 2552 0, /*tp_call*/ |
|
17645
b5f583eecb20
Add a proper implementation for the tp_str slot (returning self, of
Guido van Rossum <guido@python.org>
parents:
17640
diff
changeset
|
2553 (reprfunc)string_str, /*tp_str*/ |
| 5005 | 2554 0, /*tp_getattro*/ |
| 2555 0, /*tp_setattro*/ | |
|
5410
3b2fda31bf1b
Checkin of Jack's buffer mods.
Guido van Rossum <guido@python.org>
parents:
5396
diff
changeset
|
2556 &string_as_buffer, /*tp_as_buffer*/ |
|
9305
083efa076366
Greg Stein: Implement the new bf_getcharbuffer function, indicating
Guido van Rossum <guido@python.org>
parents:
9191
diff
changeset
|
2557 Py_TPFLAGS_DEFAULT, /*tp_flags*/ |
| 5005 | 2558 0, /*tp_doc*/ |
|
17964
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
2559 0, /*tp_traverse*/ |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
2560 0, /*tp_clear*/ |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
2561 (richcmpfunc)string_richcompare, /*tp_richcompare*/ |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
2562 0, /*tp_weaklistoffset*/ |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
2563 0, /*tp_iter*/ |
|
5e1141157d53
Patch #424335: Implement string_richcompare, remove string_compare.
Martin v. Löwis <martin@v.loewis.de>
parents:
17835
diff
changeset
|
2564 0, /*tp_iternext*/ |
| 6 | 2565 }; |
| 2566 | |
| 2567 void | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2568 PyString_Concat(register PyObject **pv, register PyObject *w) |
| 6 | 2569 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2570 register PyObject *v; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2571 if (*pv == NULL) |
| 6 | 2572 return; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2573 if (w == NULL || !PyString_Check(*pv)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2574 Py_DECREF(*pv); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2575 *pv = NULL; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2576 return; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2577 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2578 v = string_concat((PyStringObject *) *pv, w); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2579 Py_DECREF(*pv); |
| 6 | 2580 *pv = v; |
| 2581 } | |
| 2582 | |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2583 void |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2584 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
|
2585 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2586 PyString_Concat(pv, w); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2587 Py_XDECREF(w); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2588 } |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2589 |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2590 |
| 6 | 2591 /* The following function breaks the notion that strings are immutable: |
| 2592 it changes the size of a string. We get away with this only if there | |
| 2593 is only one module referencing the object. You can also think of it | |
| 2594 as creating a new string object and destroying the old one, only | |
| 2595 more efficiently. In any case, don't use this if the string may | |
| 2596 already be known to some other part of the code... */ | |
| 2597 | |
| 2598 int | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2599 _PyString_Resize(PyObject **pv, int newsize) |
| 6 | 2600 { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2601 register PyObject *v; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2602 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
|
2603 v = *pv; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2604 if (!PyString_Check(v) || v->ob_refcnt != 1) { |
| 6 | 2605 *pv = 0; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2606 Py_DECREF(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2607 PyErr_BadInternalCall(); |
| 27 | 2608 return -1; |
| 6 | 2609 } |
|
71
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
2610 /* XXX UNREF/NEWREF interface should be more symmetrical */ |
|
3586
36cc3ee54f28
new debugger symbol names
Guido van Rossum <guido@python.org>
parents:
3571
diff
changeset
|
2611 #ifdef Py_REF_DEBUG |
|
2612
ca6b60d4ea90
fix dusty debugging macros
Guido van Rossum <guido@python.org>
parents:
2504
diff
changeset
|
2612 --_Py_RefTotal; |
|
71
ac4522e99ca9
Fixed resizestring() to work if reference tracing is turned on.
Guido van Rossum <guido@python.org>
parents:
27
diff
changeset
|
2613 #endif |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2614 _Py_ForgetReference(v); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2615 *pv = (PyObject *) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
2616 PyObject_REALLOC((char *)v, |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2617 sizeof(PyStringObject) + newsize * sizeof(char)); |
| 6 | 2618 if (*pv == NULL) { |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
12371
diff
changeset
|
2619 PyObject_DEL(v); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2620 PyErr_NoMemory(); |
| 27 | 2621 return -1; |
| 6 | 2622 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2623 _Py_NewReference(*pv); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2624 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
|
2625 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
|
2626 sv->ob_sval[newsize] = '\0'; |
| 6 | 2627 return 0; |
| 2628 } | |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2629 |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2630 /* Helpers for formatstring */ |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2631 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2632 static PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2633 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
|
2634 { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2635 int argidx = *p_argidx; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2636 if (argidx < arglen) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2637 (*p_argidx)++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2638 if (arglen < 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2639 return args; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2640 else |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2641 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
|
2642 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2643 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2644 "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
|
2645 return NULL; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2646 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2647 |
|
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
|
2648 /* 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
|
2649 * 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
|
2650 * 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
|
2651 * 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
|
2652 * 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
|
2653 * 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
|
2654 */ |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2655 #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
|
2656 #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
|
2657 #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
|
2658 #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
|
2659 #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
|
2660 |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
2661 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2662 formatfloat(char *buf, size_t buflen, int flags, |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2663 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
|
2664 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
2665 /* fmt = '%#.' + `prec` + `type` |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
2666 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
|
2667 char fmt[20]; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2668 double x; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2669 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
|
2670 return -1; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2671 if (prec < 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2672 prec = 6; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2673 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
|
2674 type = 'g'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2675 sprintf(fmt, "%%%s.%d%c", (flags&F_ALT) ? "#" : "", prec, type); |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
2676 /* 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
|
2677 fmt = %#.<prec>g |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
2678 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
|
2679 for any double rep.) |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
2680 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
|
2681 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
|
2682 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
|
2683 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
|
2684 PyErr_SetString(PyExc_OverflowError, |
| 15318 | 2685 "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
|
2686 return -1; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
2687 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2688 sprintf(buf, fmt, x); |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
2689 return strlen(buf); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2690 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2691 |
|
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
|
2692 /* _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
|
2693 * 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
|
2694 * 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
|
2695 * 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
|
2696 * . *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
|
2697 * *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
|
2698 * 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
|
2699 * 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
|
2700 * "-"? ("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
|
2701 * "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
|
2702 * 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
|
2703 * 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
|
2704 * 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
|
2705 * 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
|
2706 * 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
|
2707 * 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
|
2708 * 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
|
2709 * |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2710 * 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
|
2711 * 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
|
2712 */ |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2713 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
|
2714 _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
|
2715 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
|
2716 { |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2717 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
|
2718 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
|
2719 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
|
2720 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
|
2721 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
|
2722 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
|
2723 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
|
2724 |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2725 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
|
2726 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
|
2727 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
|
2728 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
|
2729 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
|
2730 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
|
2731 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
|
2732 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
|
2733 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
|
2734 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
|
2735 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
|
2736 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
|
2737 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
|
2738 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
|
2739 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
|
2740 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2741 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
|
2742 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
|
2743 |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2744 /* 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
|
2745 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
|
2746 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
|
2747 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
|
2748 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2749 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
|
2750 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
|
2751 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
|
2752 --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
|
2753 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
|
2754 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2755 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
|
2756 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
|
2757 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
|
2758 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
|
2759 |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
2760 /* 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
|
2761 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
|
2762 /* 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
|
2763 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
|
2764 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
|
2765 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
|
2766 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
|
2767 /* 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
|
2768 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
|
2769 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
|
2770 --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
|
2771 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2772 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
|
2773 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
|
2774 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
|
2775 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
|
2776 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
|
2777 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
|
2778 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
|
2779 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
|
2780 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2781 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
|
2782 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
|
2783 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
|
2784 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
|
2785 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
|
2786 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2787 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
|
2788 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
|
2789 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2790 |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2791 /* 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
|
2792 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
|
2793 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
|
2794 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
|
2795 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
|
2796 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
|
2797 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
|
2798 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
|
2799 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2800 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
|
2801 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
|
2802 *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
|
2803 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
|
2804 *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
|
2805 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
|
2806 *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
|
2807 *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
|
2808 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
|
2809 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
|
2810 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
|
2811 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
|
2812 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2813 |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2814 /* 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
|
2815 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
|
2816 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
|
2817 /* 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
|
2818 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
|
2819 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
|
2820 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
|
2821 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
|
2822 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
|
2823 /* 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
|
2824 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
|
2825 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
|
2826 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
|
2827 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2828 *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
|
2829 *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
|
2830 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
|
2831 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
2832 |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
2833 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2834 formatint(char *buf, size_t buflen, int flags, |
|
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2835 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
|
2836 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
2837 /* 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
|
2838 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
|
2839 + 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
|
2840 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
|
2841 long x; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2842 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
|
2843 return -1; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2844 if (prec < 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2845 prec = 1; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2846 sprintf(fmt, "%%%s.%dl%c", (flags&F_ALT) ? "#" : "", prec, 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
|
2847 /* buf = '+'/'-'/'0'/'0x' + '[0-9]'*max(prec, len(x in octal)) |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
2848 worst case buf = '0x' + [0-9]*prec, where prec >= 11 */ |
|
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
|
2849 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
|
2850 PyErr_SetString(PyExc_OverflowError, |
| 15318 | 2851 "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
|
2852 return -1; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
2853 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2854 sprintf(buf, fmt, x); |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
2855 /* When converting 0 under %#x or %#X, C leaves off the base marker, |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
2856 * but we want it (for consistency with other %#x conversions, and |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
2857 * for consistency with Python's hex() function). |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2858 * BUG 28-Apr-2001 tim: At least two platform Cs (Metrowerks & |
|
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2859 * Compaq Tru64) violate the std by converting 0 w/ leading 0x anyway. |
|
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2860 * So add it only if the platform didn't already. |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
2861 */ |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2862 if (x == 0 && |
|
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2863 (flags & F_ALT) && |
|
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2864 (type == 'x' || type == 'X') && |
|
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2865 buf[1] != (char)type) /* this last always true under std C */ |
|
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2866 { |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
2867 memmove(buf+2, buf, strlen(buf) + 1); |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
2868 buf[0] = '0'; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
2869 buf[1] = (char)type; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
2870 } |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
2871 return strlen(buf); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2872 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2873 |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
2874 static int |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2875 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
|
2876 { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
2877 /* 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
|
2878 if (PyString_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2879 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
|
2880 return -1; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2881 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2882 else { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2883 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
|
2884 return -1; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2885 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2886 buf[1] = '\0'; |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
2887 return 1; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2888 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2889 |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2890 |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
2891 /* 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
|
2892 |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
2893 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
|
2894 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
|
2895 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
|
2896 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
|
2897 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
|
2898 */ |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
2899 #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
|
2900 |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2901 PyObject * |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
2902 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
|
2903 { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2904 char *fmt, *res; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2905 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
|
2906 int args_owned = 0; |
|
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
|
2907 PyObject *result, *orig_args, *v, *w; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2908 PyObject *dict = NULL; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2909 if (format == NULL || !PyString_Check(format) || args == NULL) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2910 PyErr_BadInternalCall(); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2911 return NULL; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2912 } |
| 12328 | 2913 orig_args = args; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2914 fmt = PyString_AsString(format); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2915 fmtcnt = PyString_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
|
2916 reslen = rescnt = fmtcnt + 100; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2917 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
|
2918 if (result == NULL) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2919 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2920 res = PyString_AsString(result); |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2921 if (PyTuple_Check(args)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2922 arglen = PyTuple_Size(args); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2923 argidx = 0; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2924 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2925 else { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2926 arglen = -1; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2927 argidx = -2; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2928 } |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2929 if (args->ob_type->tp_as_mapping) |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2930 dict = args; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2931 while (--fmtcnt >= 0) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2932 if (*fmt != '%') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2933 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
|
2934 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
|
2935 reslen += rescnt; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2936 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
|
2937 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2938 res = PyString_AsString(result) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2939 + reslen - rescnt; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2940 --rescnt; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2941 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2942 *res++ = *fmt++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2943 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2944 else { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2945 /* Got a format specifier */ |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2946 int flags = 0; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2947 int width = -1; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2948 int prec = -1; |
|
1408
18df674539da
Three micro fixes to formatstring
Guido van Rossum <guido@python.org>
parents:
1380
diff
changeset
|
2949 int c = '\0'; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2950 int fill; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2951 PyObject *v = NULL; |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2952 PyObject *temp = NULL; |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
2953 char *pbuf; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2954 int sign; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
2955 int len; |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
2956 char formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */ |
| 12328 | 2957 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
|
2958 int argidx_start = argidx; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2959 |
| 4588 | 2960 fmt++; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2961 if (*fmt == '(') { |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2962 char *keystart; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2963 int keylen; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2964 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
|
2965 int pcount = 1; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2966 |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2967 if (dict == NULL) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2968 PyErr_SetString(PyExc_TypeError, |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
2969 "format requires a mapping"); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2970 goto error; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2971 } |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2972 ++fmt; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2973 --fmtcnt; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2974 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
|
2975 /* 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
|
2976 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
|
2977 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
|
2978 --pcount; |
|
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
2979 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
|
2980 ++pcount; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2981 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
|
2982 } |
|
82cd2baa4136
Patch submitted by Brad Howes (with one bug fixed by me): allow
Guido van Rossum <guido@python.org>
parents:
5935
diff
changeset
|
2983 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
|
2984 if (fmtcnt < 0 || pcount > 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2985 PyErr_SetString(PyExc_ValueError, |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2986 "incomplete format key"); |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2987 goto error; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2988 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2989 key = PyString_FromStringAndSize(keystart, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2990 keylen); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2991 if (key == NULL) |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2992 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
|
2993 if (args_owned) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2994 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
|
2995 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
|
2996 } |
|
977ff1ace070
Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
2997 args = PyObject_GetItem(dict, key); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
2998 Py_DECREF(key); |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
2999 if (args == NULL) { |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3000 goto error; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3001 } |
|
3570
977ff1ace070
Fix obscure bug in string%mapping where the mapping creates its items
Guido van Rossum <guido@python.org>
parents:
2612
diff
changeset
|
3002 args_owned = 1; |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3003 arglen = -1; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3004 argidx = -2; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3005 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3006 while (--fmtcnt >= 0) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3007 switch (c = *fmt++) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3008 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
|
3009 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
|
3010 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
|
3011 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
|
3012 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
|
3013 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3014 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3015 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3016 if (c == '*') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3017 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
|
3018 if (v == NULL) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3019 goto error; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3020 if (!PyInt_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3021 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3022 "* wants int"); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3023 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3024 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3025 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
|
3026 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
|
3027 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
|
3028 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
|
3029 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3030 if (--fmtcnt >= 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3031 c = *fmt++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3032 } |
|
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
|
3033 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
|
3034 width = c - '0'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3035 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
|
3036 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
|
3037 if (!isdigit(c)) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3038 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3039 if ((width*10) / 10 != width) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3040 PyErr_SetString( |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3041 PyExc_ValueError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3042 "width too big"); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3043 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3044 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3045 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
|
3046 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3047 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3048 if (c == '.') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3049 prec = 0; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3050 if (--fmtcnt >= 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3051 c = *fmt++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3052 if (c == '*') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3053 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
|
3054 if (v == NULL) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3055 goto error; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3056 if (!PyInt_Check(v)) { |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3057 PyErr_SetString( |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3058 PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3059 "* wants int"); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3060 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3061 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3062 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
|
3063 if (prec < 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3064 prec = 0; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3065 if (--fmtcnt >= 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3066 c = *fmt++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3067 } |
|
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
|
3068 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
|
3069 prec = c - '0'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3070 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
|
3071 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
|
3072 if (!isdigit(c)) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3073 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3074 if ((prec*10) / 10 != prec) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3075 PyErr_SetString( |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3076 PyExc_ValueError, |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3077 "prec too big"); |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3078 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3079 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3080 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
|
3081 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3082 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3083 } /* prec */ |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3084 if (fmtcnt >= 0) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3085 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
|
3086 if (--fmtcnt >= 0) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3087 c = *fmt++; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3088 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3089 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3090 if (fmtcnt < 0) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3091 PyErr_SetString(PyExc_ValueError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3092 "incomplete format"); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3093 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3094 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3095 if (c != '%') { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3096 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
|
3097 if (v == NULL) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3098 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3099 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3100 sign = 0; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3101 fill = ' '; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3102 switch (c) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3103 case '%': |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3104 pbuf = "%"; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3105 len = 1; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3106 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3107 case 's': |
| 12328 | 3108 case 'r': |
| 3109 if (PyUnicode_Check(v)) { | |
| 3110 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
|
3111 argidx = argidx_start; |
| 12328 | 3112 goto unicode; |
| 3113 } | |
| 12371 | 3114 if (c == 's') |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3115 temp = PyObject_Str(v); |
| 12371 | 3116 else |
| 3117 temp = PyObject_Repr(v); | |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3118 if (temp == NULL) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3119 goto error; |
|
8468
c27cd5fc46b5
Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
3120 if (!PyString_Check(temp)) { |
|
c27cd5fc46b5
Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
3121 PyErr_SetString(PyExc_TypeError, |
|
c27cd5fc46b5
Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
3122 "%s argument has non-string str()"); |
|
c27cd5fc46b5
Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
3123 goto error; |
|
c27cd5fc46b5
Should check that PyObject_Str() really returned a string!
Guido van Rossum <guido@python.org>
parents:
7981
diff
changeset
|
3124 } |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3125 pbuf = PyString_AsString(temp); |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3126 len = PyString_Size(temp); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3127 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
|
3128 len = prec; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3129 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3130 case 'i': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3131 case 'd': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3132 case 'u': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3133 case 'o': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3134 case 'x': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3135 case 'X': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3136 if (c == 'i') |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3137 c = 'd'; |
|
15455
d0146d128897
Fox for SF bug #123859: %[duxXo] long formats inconsistent.
Tim Peters <tim.peters@gmail.com>
parents:
15435
diff
changeset
|
3138 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
|
3139 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
|
3140 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
|
3141 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
|
3142 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
|
3143 /* 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
|
3144 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
|
3145 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
|
3146 } |
|
58b832b8745e
Derived from Martin's 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 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
|
3148 pbuf = formatbuf; |
|
58b832b8745e
Derived from Martin's 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 len = formatint(pbuf, sizeof(formatbuf), |
|
58b832b8745e
Derived from Martin's 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 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
|
3151 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
|
3152 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
|
3153 /* 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
|
3154 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
|
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 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
|
3157 fill = '0'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3158 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3159 case 'e': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3160 case 'E': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3161 case 'f': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3162 case 'g': |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3163 case 'G': |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3164 pbuf = formatbuf; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3165 len = formatfloat(pbuf, sizeof(formatbuf), flags, prec, c, v); |
|
5022
62645787fbc8
Don't use static buffers internally for formatstring().
Guido van Rossum <guido@python.org>
parents:
5005
diff
changeset
|
3166 if (len < 0) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3167 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3168 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
|
3169 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
|
3170 fill = '0'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3171 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3172 case 'c': |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3173 pbuf = formatbuf; |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3174 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
|
3175 if (len < 0) |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3176 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3177 break; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3178 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
|
3179 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
|
3180 "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
|
3181 "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
|
3182 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
|
3183 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3184 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3185 if (sign) { |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3186 if (*pbuf == '-' || *pbuf == '+') { |
|
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3187 sign = *pbuf++; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3188 len--; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3189 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3190 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
|
3191 sign = '+'; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3192 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
|
3193 sign = ' '; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3194 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
|
3195 sign = 0; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3196 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3197 if (width < len) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3198 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
|
3199 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
|
3200 reslen -= rescnt; |
|
e0464e7115ca
* pythonrun.c: Print exception type+arg *after* stack trace instead of
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
3201 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
|
3202 reslen += rescnt; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3203 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
|
3204 return NULL; |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3205 res = PyString_AsString(result) |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3206 + reslen - rescnt; |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3207 } |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3208 if (sign) { |
| 1409 | 3209 if (fill != ' ') |
| 3210 *res++ = sign; | |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3211 rescnt--; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3212 if (width > len) |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3213 width--; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3214 } |
|
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
|
3215 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
|
3216 assert(pbuf[0] == '0'); |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3217 assert(pbuf[1] == c); |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3218 if (fill != ' ') { |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3219 *res++ = *pbuf++; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3220 *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
|
3221 } |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3222 rescnt -= 2; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3223 width -= 2; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3224 if (width < 0) |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3225 width = 0; |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3226 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
|
3227 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3228 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
|
3229 do { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3230 --rescnt; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3231 *res++ = fill; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3232 } while (--width > len); |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3233 } |
|
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
|
3234 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
|
3235 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
|
3236 *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
|
3237 if ((flags & F_ALT) && |
|
17450
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3238 (c == 'x' || c == 'X')) { |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3239 assert(pbuf[0] == '0'); |
|
f74f5aafdc7e
Bug 415514 reported that e.g.
Tim Peters <tim.peters@gmail.com>
parents:
17425
diff
changeset
|
3240 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
|
3241 *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
|
3242 *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
|
3243 } |
|
58b832b8745e
Derived from Martin's SF patch 110609: support unbounded ints in %d,i,u,x,X,o formats.
Tim Peters <tim.peters@gmail.com>
parents:
14696
diff
changeset
|
3244 } |
|
13196
18920eaa1226
Marc-Andre Lemburg <mal@lemburg.com>:
Marc-André Lemburg <mal@egenix.com>
parents:
12966
diff
changeset
|
3245 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
|
3246 res += len; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3247 rescnt -= len; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3248 while (--width >= len) { |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3249 --rescnt; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3250 *res++ = ' '; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3251 } |
|
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
|
3252 if (dict && (argidx < arglen) && c != '%') { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3253 PyErr_SetString(PyExc_TypeError, |
|
1847
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3254 "not all arguments converted"); |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3255 goto error; |
|
0d318475f9e3
fix nasty bug in resizing (formatstring)
Guido van Rossum <guido@python.org>
parents:
1409
diff
changeset
|
3256 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3257 Py_XDECREF(temp); |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3258 } /* '%' */ |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3259 } /* 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
|
3260 if (argidx < arglen && !dict) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3261 PyErr_SetString(PyExc_TypeError, |
|
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3262 "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
|
3263 goto error; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3264 } |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
6286
diff
changeset
|
3265 if (args_owned) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3266 Py_DECREF(args); |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
6286
diff
changeset
|
3267 } |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3268 _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
|
3269 return result; |
| 12328 | 3270 |
| 3271 unicode: | |
| 3272 if (args_owned) { | |
| 3273 Py_DECREF(args); | |
| 3274 args_owned = 0; | |
| 3275 } | |
|
17655
0470babdae49
Fix for bug #417030: "print '%*s' fails for unicode string"
Marc-André Lemburg <mal@egenix.com>
parents:
17645
diff
changeset
|
3276 /* Fiddle args right (remove the first argidx arguments) */ |
| 12328 | 3277 if (PyTuple_Check(orig_args) && argidx > 0) { |
| 3278 PyObject *v; | |
| 3279 int n = PyTuple_GET_SIZE(orig_args) - argidx; | |
| 3280 v = PyTuple_New(n); | |
| 3281 if (v == NULL) | |
| 3282 goto error; | |
| 3283 while (--n >= 0) { | |
| 3284 PyObject *w = PyTuple_GET_ITEM(orig_args, n + argidx); | |
| 3285 Py_INCREF(w); | |
| 3286 PyTuple_SET_ITEM(v, n, w); | |
| 3287 } | |
| 3288 args = v; | |
| 3289 } else { | |
| 3290 Py_INCREF(orig_args); | |
| 3291 args = orig_args; | |
| 3292 } | |
|
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
|
3293 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
|
3294 /* 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
|
3295 function format the rest of the input. */ |
| 12328 | 3296 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
|
3297 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
|
3298 goto error; |
| 12328 | 3299 fmtcnt = PyString_GET_SIZE(format) - \ |
| 3300 (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
|
3301 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
|
3302 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
|
3303 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
|
3304 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
|
3305 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
|
3306 if (v == NULL) |
| 12328 | 3307 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
|
3308 /* 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
|
3309 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
|
3310 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
|
3311 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
|
3312 Py_DECREF(v); |
| 12328 | 3313 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
|
3314 return w; |
|
17640
9934225fe297
A different approach to the problem reported in
Tim Peters <tim.peters@gmail.com>
parents:
17597
diff
changeset
|
3315 |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3316 error: |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3317 Py_DECREF(result); |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
6286
diff
changeset
|
3318 if (args_owned) { |
|
5396
d1447950fa04
Quickly renamed the last directory.
Guido van Rossum <guido@python.org>
parents:
5288
diff
changeset
|
3319 Py_DECREF(args); |
|
7981
09963a2a24db
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
6286
diff
changeset
|
3320 } |
|
1252
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3321 return NULL; |
|
c4cd0d9d8281
* Changed many files to use mkvalue() instead of newtupleobject().
Guido van Rossum <guido@python.org>
parents:
1023
diff
changeset
|
3322 } |
| 5005 | 3323 |
| 3324 | |
| 3325 #ifdef INTERN_STRINGS | |
| 3326 | |
|
14018
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3327 /* 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
|
3328 * 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
|
3329 * 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
|
3330 * says: |
|
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3331 * |
|
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3332 * 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
|
3333 * 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
|
3334 * 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
|
3335 * 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
|
3336 * 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
|
3337 * in the interned table. |
|
0d805e239d8f
Insure properly identifies the `interned' dictionary as leaking at
Barry Warsaw <barry@python.org>
parents:
13761
diff
changeset
|
3338 */ |
| 5005 | 3339 static PyObject *interned; |
| 3340 | |
| 3341 void | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3342 PyString_InternInPlace(PyObject **p) |
| 5005 | 3343 { |
| 3344 register PyStringObject *s = (PyStringObject *)(*p); | |
| 3345 PyObject *t; | |
| 3346 if (s == NULL || !PyString_Check(s)) | |
| 3347 Py_FatalError("PyString_InternInPlace: strings only please!"); | |
| 3348 if ((t = s->ob_sinterned) != NULL) { | |
| 3349 if (t == (PyObject *)s) | |
| 3350 return; | |
| 3351 Py_INCREF(t); | |
| 3352 *p = t; | |
| 3353 Py_DECREF(s); | |
| 3354 return; | |
| 3355 } | |
| 3356 if (interned == NULL) { | |
| 3357 interned = PyDict_New(); | |
| 3358 if (interned == NULL) | |
| 3359 return; | |
| 3360 } | |
| 3361 if ((t = PyDict_GetItem(interned, (PyObject *)s)) != NULL) { | |
| 3362 Py_INCREF(t); | |
| 3363 *p = s->ob_sinterned = t; | |
| 3364 Py_DECREF(s); | |
| 3365 return; | |
| 3366 } | |
| 3367 t = (PyObject *)s; | |
| 3368 if (PyDict_SetItem(interned, t, t) == 0) { | |
| 3369 s->ob_sinterned = t; | |
| 3370 return; | |
| 3371 } | |
| 3372 PyErr_Clear(); | |
| 3373 } | |
| 3374 | |
| 3375 | |
| 3376 PyObject * | |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3377 PyString_InternFromString(const char *cp) |
| 5005 | 3378 { |
| 3379 PyObject *s = PyString_FromString(cp); | |
| 3380 if (s == NULL) | |
| 3381 return NULL; | |
| 3382 PyString_InternInPlace(&s); | |
| 3383 return s; | |
| 3384 } | |
| 3385 | |
| 3386 #endif | |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3387 |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3388 void |
|
13459
0f95c93f200b
ANSI-fication of the sources.
Fred Drake <fdrake@acm.org>
parents:
13377
diff
changeset
|
3389 PyString_Fini(void) |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3390 { |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3391 int i; |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3392 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
|
3393 Py_XDECREF(characters[i]); |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3394 characters[i] = NULL; |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3395 } |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3396 #ifndef DONT_SHARE_SHORT_STRINGS |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3397 Py_XDECREF(nullstring); |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3398 nullstring = NULL; |
|
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3399 #endif |
|
5935
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3400 #ifdef INTERN_STRINGS |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3401 if (interned) { |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3402 int pos, changed; |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3403 PyObject *key, *value; |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3404 do { |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3405 changed = 0; |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3406 pos = 0; |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3407 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
|
3408 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
|
3409 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
|
3410 changed = 1; |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3411 } |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3412 } |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3413 } while (changed); |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3414 } |
|
d77d3b8e86b2
Change the Fini function to only remove otherwise unreferenced strings
Guido van Rossum <guido@python.org>
parents:
5920
diff
changeset
|
3415 #endif |
|
5920
5fb30413b93b
Added internal routine PyString_Fini() which deletes all interned
Guido van Rossum <guido@python.org>
parents:
5717
diff
changeset
|
3416 } |
|
16751
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3417 |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3418 #ifdef INTERN_STRINGS |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3419 void _Py_ReleaseInternedStrings(void) |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3420 { |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3421 if (interned) { |
|
17597
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17450
diff
changeset
|
3422 fprintf(stderr, "releasing interned strings\n"); |
|
32e7d0898eab
Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents:
17450
diff
changeset
|
3423 PyDict_Clear(interned); |
|
16751
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3424 Py_DECREF(interned); |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3425 interned = NULL; |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3426 } |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3427 } |
|
ca1be7d00413
_Py_ReleaseInternedStrings(): Private API function to decref and
Barry Warsaw <barry@python.org>
parents:
16226
diff
changeset
|
3428 #endif /* INTERN_STRINGS */ |
